material.go 9.4 KB

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