material.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. UseLightAmbient UseLights = 0
  32. UseLightDirectional UseLights = 1
  33. UseLightPoint UseLights = 2
  34. UseLightSpot UseLights = 3
  35. UseLightAll UseLights = 4
  36. )
  37. // Interface for all materials
  38. type IMaterial interface {
  39. GetMaterial() *Material
  40. RenderSetup(gs *gls.GLS)
  41. Dispose()
  42. }
  43. //
  44. // Base Material
  45. //
  46. type Material struct {
  47. refcount int // Current number of references
  48. shader string // Shader name
  49. uselights UseLights // Use lights bit mask
  50. sidevis Side // sides visible
  51. wireframe bool // show as wirefrme
  52. depthMask bool // Enable writing into the depth buffer
  53. depthTest bool // Enable depth buffer test
  54. depthFunc uint32 // Actvie depth test function
  55. blending Blending // blending mode
  56. blendRGB uint32 // separate blend equation for RGB
  57. blendAlpha uint32 // separate blend equation for Alpha
  58. blendSrcRGB uint32 // separate blend func source RGB
  59. blendDstRGB uint32 // separate blend func dest RGB
  60. blendSrcAlpha uint32 // separate blend func source Alpha
  61. blendDstAlpha uint32 // separate blend func dest Alpha
  62. lineWidth float32 // line width for lines and mesh wireframe
  63. polyOffsetFactor float32 // polygon offset factor
  64. polyOffsetUnits float32 // polygon offset units
  65. textures []*texture.Texture2D // List of textures
  66. }
  67. // NewMaterial returns a pointer to a new material
  68. func NewMaterial() *Material {
  69. mat := new(Material)
  70. return mat.Init()
  71. }
  72. func (mat *Material) Init() *Material {
  73. mat.refcount = 1
  74. mat.uselights = UseLightAll
  75. mat.sidevis = SideFront
  76. mat.wireframe = false
  77. mat.depthMask = true
  78. mat.depthFunc = gls.LEQUAL
  79. mat.depthTest = true
  80. mat.blending = BlendingNormal
  81. mat.lineWidth = 1.0
  82. mat.polyOffsetFactor = 0
  83. mat.polyOffsetUnits = 0
  84. mat.textures = make([]*texture.Texture2D, 0)
  85. return mat
  86. }
  87. // GetMaterial satisfies the IMaterial interface
  88. func (mat *Material) GetMaterial() *Material {
  89. return mat
  90. }
  91. // Incref increments the reference count for this material
  92. // and returns a pointer to the material.
  93. // It should be used when this material is shared by another
  94. // Graphic object.
  95. func (mat *Material) Incref() *Material {
  96. mat.refcount++
  97. return mat
  98. }
  99. // Dispose decrements this material reference count and
  100. // if necessary releases OpenGL resources, C memory
  101. // and textures associated with this material.
  102. func (mat *Material) Dispose() {
  103. if mat.refcount > 1 {
  104. mat.refcount--
  105. return
  106. }
  107. for i := 0; i < len(mat.textures); i++ {
  108. mat.textures[i].Dispose()
  109. }
  110. mat.Init()
  111. }
  112. // SetShader sets the name of the shader program for this material
  113. func (mat *Material) SetShader(sname string) {
  114. mat.shader = sname
  115. }
  116. // Shader returns the current name of the shader program for this material
  117. func (mat *Material) Shader() string {
  118. return mat.shader
  119. }
  120. // SetUseLights sets the material use lights bit mask specifying which
  121. // light types will be used when rendering the material
  122. func (mat *Material) SetUseLights(lights UseLights) {
  123. mat.uselights = lights
  124. }
  125. func (mat *Material) UseLights() UseLights {
  126. return mat.uselights
  127. }
  128. // Sets the visible side(s) (SideFront | SideBack | SideDouble)
  129. func (mat *Material) SetSide(side Side) {
  130. mat.sidevis = side
  131. }
  132. // Side returns the current side visibility for this material
  133. func (mat *Material) Side() Side {
  134. return mat.sidevis
  135. }
  136. func (mat *Material) SetWireframe(state bool) {
  137. mat.wireframe = state
  138. }
  139. func (mat *Material) SetDepthMask(state bool) {
  140. mat.depthMask = state
  141. }
  142. func (mat *Material) SetDepthTest(state bool) {
  143. mat.depthTest = state
  144. }
  145. func (mat *Material) SetBlending(blending Blending) {
  146. mat.blending = blending
  147. }
  148. func (mat *Material) SetLineWidth(width float32) {
  149. mat.lineWidth = width
  150. }
  151. func (mat *Material) SetPolygonOffset(factor, units float32) {
  152. mat.polyOffsetFactor = factor
  153. mat.polyOffsetUnits = units
  154. }
  155. func (mat *Material) RenderSetup(gs *gls.GLS) {
  156. // Sets triangle side view mode
  157. switch mat.sidevis {
  158. case SideFront:
  159. gs.Enable(gls.CULL_FACE)
  160. gs.FrontFace(gls.CCW)
  161. case SideBack:
  162. gs.Enable(gls.CULL_FACE)
  163. gs.FrontFace(gls.CW)
  164. case SideDouble:
  165. gs.Disable(gls.CULL_FACE)
  166. gs.FrontFace(gls.CCW)
  167. }
  168. if mat.depthTest {
  169. gs.Enable(gls.DEPTH_TEST)
  170. } else {
  171. gs.Disable(gls.DEPTH_TEST)
  172. }
  173. gs.DepthMask(mat.depthMask)
  174. gs.DepthFunc(mat.depthFunc)
  175. if mat.wireframe {
  176. gs.PolygonMode(gls.FRONT_AND_BACK, gls.LINE)
  177. } else {
  178. gs.PolygonMode(gls.FRONT_AND_BACK, gls.FILL)
  179. }
  180. // Set polygon offset if requested
  181. gs.PolygonOffset(mat.polyOffsetFactor, mat.polyOffsetUnits)
  182. // Sets line width
  183. gs.LineWidth(mat.lineWidth)
  184. // Sets blending
  185. switch mat.blending {
  186. case BlendingNone:
  187. gs.Disable(gls.BLEND)
  188. case BlendingNormal:
  189. gs.Enable(gls.BLEND)
  190. gs.BlendEquationSeparate(gls.FUNC_ADD, gls.FUNC_ADD)
  191. gs.BlendFunc(gls.SRC_ALPHA, gls.ONE_MINUS_SRC_ALPHA)
  192. case BlendingAdditive:
  193. gs.Enable(gls.BLEND)
  194. gs.BlendEquation(gls.FUNC_ADD)
  195. gs.BlendFunc(gls.SRC_ALPHA, gls.ONE)
  196. case BlendingSubtractive:
  197. gs.Enable(gls.BLEND)
  198. gs.BlendEquation(gls.FUNC_ADD)
  199. gs.BlendFunc(gls.ZERO, gls.ONE_MINUS_SRC_COLOR)
  200. break
  201. case BlendingMultiply:
  202. gs.Enable(gls.BLEND)
  203. gs.BlendEquation(gls.FUNC_ADD)
  204. gs.BlendFunc(gls.ZERO, gls.SRC_COLOR)
  205. break
  206. case BlendingCustom:
  207. gs.BlendEquationSeparate(mat.blendRGB, mat.blendAlpha)
  208. gs.BlendFuncSeparate(mat.blendSrcRGB, mat.blendDstRGB, mat.blendSrcAlpha, mat.blendDstAlpha)
  209. break
  210. default:
  211. panic("Invalid blending")
  212. }
  213. // Render textures
  214. for idx, tex := range mat.textures {
  215. tex.RenderSetup(gs, idx)
  216. }
  217. }
  218. // AddTexture adds the specified Texture2d to the material
  219. func (mat *Material) AddTexture(tex *texture.Texture2D) {
  220. mat.textures = append(mat.textures, tex)
  221. }
  222. // RemoveTexture removes the specified Texture2d from the material
  223. func (mat *Material) RemoveTexture(tex *texture.Texture2D) {
  224. for pos, curr := range mat.textures {
  225. if curr == tex {
  226. copy(mat.textures[pos:], mat.textures[pos+1:])
  227. mat.textures[len(mat.textures)-1] = nil
  228. mat.textures = mat.textures[:len(mat.textures)-1]
  229. break
  230. }
  231. }
  232. }
  233. // HasTexture checks if the material contains the specified texture
  234. func (mat *Material) HasTexture(tex *texture.Texture2D) bool {
  235. for _, curr := range mat.textures {
  236. if curr == tex {
  237. return true
  238. }
  239. }
  240. return false
  241. }
  242. // TextureCount returns the current number of textures
  243. func (mat *Material) TextureCount() int {
  244. return len(mat.textures)
  245. }