material.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package material contains several types of materials which
  5. // can be used to set the appearance of graphic object
  6. package material
  7. import (
  8. "github.com/g3n/engine/gls"
  9. "github.com/g3n/engine/texture"
  10. )
  11. // Material visible side(s)
  12. type Side int
  13. const (
  14. SideFront Side = 0
  15. SideBack Side = 1
  16. SideDouble Side = 2
  17. )
  18. // Blending
  19. type Blending int
  20. const (
  21. BlendingNone Blending = 0
  22. BlendingNormal Blending = 1
  23. BlendingAdditive Blending = 2
  24. BlendingSubtractive Blending = 3
  25. BlendingMultiply Blending = 4
  26. BlendingCustom Blending = 5
  27. )
  28. // Use lights flags
  29. type UseLights int
  30. const (
  31. UseLightNone UseLights = 0x00
  32. UseLightAmbient UseLights = 0x01
  33. UseLightDirectional UseLights = 0x02
  34. UseLightPoint UseLights = 0x04
  35. UseLightSpot UseLights = 0x08
  36. UseLightAll UseLights = 0xFF
  37. )
  38. // Interface for all materials
  39. type IMaterial interface {
  40. GetMaterial() *Material
  41. RenderSetup(gs *gls.GLS)
  42. Dispose()
  43. }
  44. //
  45. // Base Material
  46. //
  47. type Material struct {
  48. refcount int // Current number of references
  49. shader string // Shader name
  50. shaderUnique bool // shader has only one instance (does not depend on lights or textures)
  51. uselights UseLights // consider lights for shader selection
  52. sidevis Side // sides visible
  53. wireframe bool // show as wirefrme
  54. depthMask bool // Enable writing into the depth buffer
  55. depthTest bool // Enable depth buffer test
  56. depthFunc uint32 // Actvie depth test function
  57. blending Blending // blending mode
  58. blendRGB uint32 // separate blend equation for RGB
  59. blendAlpha uint32 // separate blend equation for Alpha
  60. blendSrcRGB uint32 // separate blend func source RGB
  61. blendDstRGB uint32 // separate blend func dest RGB
  62. blendSrcAlpha uint32 // separate blend func source Alpha
  63. blendDstAlpha uint32 // separate blend func dest Alpha
  64. lineWidth float32 // line width for lines and mesh wireframe
  65. polyOffsetFactor float32 // polygon offset factor
  66. polyOffsetUnits float32 // polygon offset units
  67. defines map[string]string // shader defines
  68. textures []*texture.Texture2D // List of textures
  69. }
  70. // NewMaterial returns a pointer to a new material
  71. func NewMaterial() *Material {
  72. mat := new(Material)
  73. return mat.Init()
  74. }
  75. func (mat *Material) Init() *Material {
  76. mat.refcount = 1
  77. mat.uselights = UseLightAll
  78. mat.sidevis = SideFront
  79. mat.wireframe = false
  80. mat.depthMask = true
  81. mat.depthFunc = gls.LEQUAL
  82. mat.depthTest = true
  83. mat.blending = BlendingNormal
  84. mat.lineWidth = 1.0
  85. mat.polyOffsetFactor = 0
  86. mat.polyOffsetUnits = 0
  87. mat.defines = make(map[string]string)
  88. mat.textures = make([]*texture.Texture2D, 0)
  89. return mat
  90. }
  91. // GetMaterial satisfies the IMaterial interface
  92. func (mat *Material) GetMaterial() *Material {
  93. return mat
  94. }
  95. // Incref increments the reference count for this material
  96. // and returns a pointer to the material.
  97. // It should be used when this material is shared by another
  98. // Graphic object.
  99. func (mat *Material) Incref() *Material {
  100. mat.refcount++
  101. return mat
  102. }
  103. // Dispose decrements this material reference count and
  104. // if necessary releases OpenGL resources, C memory
  105. // and textures associated with this material.
  106. func (mat *Material) Dispose() {
  107. if mat.refcount > 1 {
  108. mat.refcount--
  109. return
  110. }
  111. for i := 0; i < len(mat.textures); i++ {
  112. mat.textures[i].Dispose()
  113. }
  114. mat.Init()
  115. }
  116. // SetShader sets the name of the shader program for this material
  117. func (mat *Material) SetShader(sname string) {
  118. mat.shader = sname
  119. }
  120. // Shader returns the current name of the shader program for this material
  121. func (mat *Material) Shader() string {
  122. return mat.shader
  123. }
  124. // SetShaderUnique sets indication that this material shader is unique and
  125. // does not depend on the number of lights in the scene and/or the
  126. // number of textures in the material.
  127. func (mat *Material) SetShaderUnique(unique bool) {
  128. mat.shaderUnique = unique
  129. }
  130. // ShaderUnique returns this material shader is unique.
  131. func (mat *Material) ShaderUnique() bool {
  132. return mat.shaderUnique
  133. }
  134. // SetUseLights sets the material use lights bit mask specifying which
  135. // light types will be used when rendering the material
  136. // By default the material will use all lights
  137. func (mat *Material) SetUseLights(lights UseLights) {
  138. mat.uselights = lights
  139. }
  140. // UseLights returns the current use lights bitmask
  141. func (mat *Material) UseLights() UseLights {
  142. return mat.uselights
  143. }
  144. // Sets the visible side(s) (SideFront | SideBack | SideDouble)
  145. func (mat *Material) SetSide(side Side) {
  146. mat.sidevis = side
  147. }
  148. // Side returns the current side visibility for this material
  149. func (mat *Material) Side() Side {
  150. return mat.sidevis
  151. }
  152. func (mat *Material) SetWireframe(state bool) {
  153. mat.wireframe = state
  154. }
  155. func (mat *Material) SetDepthMask(state bool) {
  156. mat.depthMask = state
  157. }
  158. func (mat *Material) SetDepthTest(state bool) {
  159. mat.depthTest = state
  160. }
  161. func (mat *Material) SetBlending(blending Blending) {
  162. mat.blending = blending
  163. }
  164. func (mat *Material) SetLineWidth(width float32) {
  165. mat.lineWidth = width
  166. }
  167. func (mat *Material) SetPolygonOffset(factor, units float32) {
  168. mat.polyOffsetFactor = factor
  169. mat.polyOffsetUnits = units
  170. }
  171. // SetShaderDefine defines a name with the specified value which are
  172. // passed to this material shader.
  173. func (mat *Material) SetShaderDefine(name, value string) {
  174. mat.defines[name] = value
  175. }
  176. // UnsetShaderDefines removes the specified name from the defines which
  177. // are passed to this material shader.
  178. func (mat *Material) UnsetShaderDefine(name string) {
  179. delete(mat.defines, name)
  180. }
  181. // ShaderDefines returns a map with the shader defines.
  182. func (mat *Material) ShaderDefines() map[string]string {
  183. return mat.defines
  184. }
  185. func (mat *Material) RenderSetup(gs *gls.GLS) {
  186. // Sets triangle side view mode
  187. switch mat.sidevis {
  188. case SideFront:
  189. gs.Enable(gls.CULL_FACE)
  190. gs.FrontFace(gls.CCW)
  191. case SideBack:
  192. gs.Enable(gls.CULL_FACE)
  193. gs.FrontFace(gls.CW)
  194. case SideDouble:
  195. gs.Disable(gls.CULL_FACE)
  196. gs.FrontFace(gls.CCW)
  197. }
  198. if mat.depthTest {
  199. gs.Enable(gls.DEPTH_TEST)
  200. } else {
  201. gs.Disable(gls.DEPTH_TEST)
  202. }
  203. gs.DepthMask(mat.depthMask)
  204. gs.DepthFunc(mat.depthFunc)
  205. if mat.wireframe {
  206. gs.PolygonMode(gls.FRONT_AND_BACK, gls.LINE)
  207. } else {
  208. gs.PolygonMode(gls.FRONT_AND_BACK, gls.FILL)
  209. }
  210. // Set polygon offset if requested
  211. gs.PolygonOffset(mat.polyOffsetFactor, mat.polyOffsetUnits)
  212. // Sets line width
  213. gs.LineWidth(mat.lineWidth)
  214. // Sets blending
  215. switch mat.blending {
  216. case BlendingNone:
  217. gs.Disable(gls.BLEND)
  218. case BlendingNormal:
  219. gs.Enable(gls.BLEND)
  220. gs.BlendEquationSeparate(gls.FUNC_ADD, gls.FUNC_ADD)
  221. gs.BlendFunc(gls.SRC_ALPHA, gls.ONE_MINUS_SRC_ALPHA)
  222. case BlendingAdditive:
  223. gs.Enable(gls.BLEND)
  224. gs.BlendEquation(gls.FUNC_ADD)
  225. gs.BlendFunc(gls.SRC_ALPHA, gls.ONE)
  226. case BlendingSubtractive:
  227. gs.Enable(gls.BLEND)
  228. gs.BlendEquation(gls.FUNC_ADD)
  229. gs.BlendFunc(gls.ZERO, gls.ONE_MINUS_SRC_COLOR)
  230. break
  231. case BlendingMultiply:
  232. gs.Enable(gls.BLEND)
  233. gs.BlendEquation(gls.FUNC_ADD)
  234. gs.BlendFunc(gls.ZERO, gls.SRC_COLOR)
  235. break
  236. case BlendingCustom:
  237. gs.BlendEquationSeparate(mat.blendRGB, mat.blendAlpha)
  238. gs.BlendFuncSeparate(mat.blendSrcRGB, mat.blendDstRGB, mat.blendSrcAlpha, mat.blendDstAlpha)
  239. break
  240. default:
  241. panic("Invalid blending")
  242. }
  243. // Render textures
  244. for idx, tex := range mat.textures {
  245. tex.RenderSetup(gs, idx)
  246. }
  247. }
  248. // AddTexture adds the specified Texture2d to the material
  249. func (mat *Material) AddTexture(tex *texture.Texture2D) {
  250. mat.textures = append(mat.textures, tex)
  251. }
  252. // RemoveTexture removes the specified Texture2d from the material
  253. func (mat *Material) RemoveTexture(tex *texture.Texture2D) {
  254. for pos, curr := range mat.textures {
  255. if curr == tex {
  256. copy(mat.textures[pos:], mat.textures[pos+1:])
  257. mat.textures[len(mat.textures)-1] = nil
  258. mat.textures = mat.textures[:len(mat.textures)-1]
  259. break
  260. }
  261. }
  262. }
  263. // HasTexture checks if the material contains the specified texture
  264. func (mat *Material) HasTexture(tex *texture.Texture2D) bool {
  265. for _, curr := range mat.textures {
  266. if curr == tex {
  267. return true
  268. }
  269. }
  270. return false
  271. }
  272. // TextureCount returns the current number of textures
  273. func (mat *Material) TextureCount() int {
  274. return len(mat.textures)
  275. }