material.go 8.9 KB

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