material.go 7.4 KB

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