material.go 8.5 KB

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