material.go 8.0 KB

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