material.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. // Side represents the material's visible side(s)
  12. type Side int
  13. // The face side(s) to be rendered. The non-rendered side will be culled to improve performance.
  14. const (
  15. SideFront Side = 0
  16. SideBack Side = 1
  17. SideDouble Side = 2
  18. )
  19. // Blending
  20. type Blending int
  21. // The various blending types
  22. const (
  23. BlendingNone Blending = 0
  24. BlendingNormal Blending = 1
  25. BlendingAdditive Blending = 2
  26. BlendingSubtractive Blending = 3
  27. BlendingMultiply Blending = 4
  28. BlendingCustom Blending = 5
  29. )
  30. // UseLights flags
  31. type UseLights int
  32. const (
  33. UseLightNone UseLights = 0x00
  34. UseLightAmbient UseLights = 0x01
  35. UseLightDirectional UseLights = 0x02
  36. UseLightPoint UseLights = 0x04
  37. UseLightSpot UseLights = 0x08
  38. UseLightAll UseLights = 0xFF
  39. )
  40. // Interface for all materials
  41. type IMaterial interface {
  42. GetMaterial() *Material
  43. RenderSetup(gs *gls.GLS)
  44. Dispose()
  45. }
  46. //
  47. // Base Material
  48. //
  49. type Material struct {
  50. refcount int // Current number of references
  51. shader string // Shader name
  52. shaderUnique bool // shader has only one instance (does not depend on lights or textures)
  53. uselights UseLights // consider lights for shader selection
  54. sidevis Side // sides visible
  55. wireframe bool // show as wirefrme
  56. depthMask bool // Enable writing into the depth buffer
  57. depthTest bool // Enable depth buffer test
  58. depthFunc uint32 // Actvie depth test function
  59. blending Blending // blending mode
  60. blendRGB uint32 // separate blend equation for RGB
  61. blendAlpha uint32 // separate blend equation for Alpha
  62. blendSrcRGB uint32 // separate blend func source RGB
  63. blendDstRGB uint32 // separate blend func dest RGB
  64. blendSrcAlpha uint32 // separate blend func source Alpha
  65. blendDstAlpha uint32 // separate blend func dest Alpha
  66. lineWidth float32 // line width for lines and mesh wireframe
  67. polyOffsetFactor float32 // polygon offset factor
  68. polyOffsetUnits float32 // polygon offset units
  69. textures []*texture.Texture2D // List of textures
  70. }
  71. // NewMaterial returns a pointer to a new material
  72. func NewMaterial() *Material {
  73. mat := new(Material)
  74. return mat.Init()
  75. }
  76. func (mat *Material) Init() *Material {
  77. mat.refcount = 1
  78. mat.uselights = UseLightAll
  79. mat.sidevis = SideFront
  80. mat.wireframe = false
  81. mat.depthMask = true
  82. mat.depthFunc = gls.LEQUAL
  83. mat.depthTest = true
  84. mat.blending = BlendingNormal
  85. mat.lineWidth = 1.0
  86. mat.polyOffsetFactor = 0
  87. mat.polyOffsetUnits = 0
  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. func (mat *Material) RenderSetup(gs *gls.GLS) {
  172. // Sets triangle side view mode
  173. switch mat.sidevis {
  174. case SideFront:
  175. gs.Enable(gls.CULL_FACE)
  176. gs.FrontFace(gls.CCW)
  177. case SideBack:
  178. gs.Enable(gls.CULL_FACE)
  179. gs.FrontFace(gls.CW)
  180. case SideDouble:
  181. gs.Disable(gls.CULL_FACE)
  182. gs.FrontFace(gls.CCW)
  183. }
  184. if mat.depthTest {
  185. gs.Enable(gls.DEPTH_TEST)
  186. } else {
  187. gs.Disable(gls.DEPTH_TEST)
  188. }
  189. gs.DepthMask(mat.depthMask)
  190. gs.DepthFunc(mat.depthFunc)
  191. if mat.wireframe {
  192. gs.PolygonMode(gls.FRONT_AND_BACK, gls.LINE)
  193. } else {
  194. gs.PolygonMode(gls.FRONT_AND_BACK, gls.FILL)
  195. }
  196. // Set polygon offset if requested
  197. gs.PolygonOffset(mat.polyOffsetFactor, mat.polyOffsetUnits)
  198. // Sets line width
  199. gs.LineWidth(mat.lineWidth)
  200. // Sets blending
  201. switch mat.blending {
  202. case BlendingNone:
  203. gs.Disable(gls.BLEND)
  204. case BlendingNormal:
  205. gs.Enable(gls.BLEND)
  206. gs.BlendEquationSeparate(gls.FUNC_ADD, gls.FUNC_ADD)
  207. gs.BlendFunc(gls.SRC_ALPHA, gls.ONE_MINUS_SRC_ALPHA)
  208. case BlendingAdditive:
  209. gs.Enable(gls.BLEND)
  210. gs.BlendEquation(gls.FUNC_ADD)
  211. gs.BlendFunc(gls.SRC_ALPHA, gls.ONE)
  212. case BlendingSubtractive:
  213. gs.Enable(gls.BLEND)
  214. gs.BlendEquation(gls.FUNC_ADD)
  215. gs.BlendFunc(gls.ZERO, gls.ONE_MINUS_SRC_COLOR)
  216. break
  217. case BlendingMultiply:
  218. gs.Enable(gls.BLEND)
  219. gs.BlendEquation(gls.FUNC_ADD)
  220. gs.BlendFunc(gls.ZERO, gls.SRC_COLOR)
  221. break
  222. case BlendingCustom:
  223. gs.BlendEquationSeparate(mat.blendRGB, mat.blendAlpha)
  224. gs.BlendFuncSeparate(mat.blendSrcRGB, mat.blendDstRGB, mat.blendSrcAlpha, mat.blendDstAlpha)
  225. break
  226. default:
  227. panic("Invalid blending")
  228. }
  229. // Render textures
  230. for idx, tex := range mat.textures {
  231. tex.RenderSetup(gs, idx)
  232. }
  233. }
  234. // AddTexture adds the specified Texture2d to the material
  235. func (mat *Material) AddTexture(tex *texture.Texture2D) {
  236. mat.textures = append(mat.textures, tex)
  237. }
  238. // RemoveTexture removes the specified Texture2d from the material
  239. func (mat *Material) RemoveTexture(tex *texture.Texture2D) {
  240. for pos, curr := range mat.textures {
  241. if curr == tex {
  242. copy(mat.textures[pos:], mat.textures[pos+1:])
  243. mat.textures[len(mat.textures)-1] = nil
  244. mat.textures = mat.textures[:len(mat.textures)-1]
  245. break
  246. }
  247. }
  248. }
  249. // HasTexture checks if the material contains the specified texture
  250. func (mat *Material) HasTexture(tex *texture.Texture2D) bool {
  251. for _, curr := range mat.textures {
  252. if curr == tex {
  253. return true
  254. }
  255. }
  256. return false
  257. }
  258. // TextureCount returns the current number of textures
  259. func (mat *Material) TextureCount() int {
  260. return len(mat.textures)
  261. }