material.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. // Only dispose if last
  114. if mat.refcount > 1 {
  115. mat.refcount--
  116. return
  117. }
  118. // Delete textures
  119. for i := 0; i < len(mat.textures); i++ {
  120. mat.textures[i].Dispose()
  121. }
  122. mat.Init()
  123. }
  124. // SetShader sets the name of the shader program for this material
  125. func (mat *Material) SetShader(sname string) {
  126. mat.shader = sname
  127. }
  128. // Shader returns the current name of the shader program for this material
  129. func (mat *Material) Shader() string {
  130. return mat.shader
  131. }
  132. // SetShaderUnique sets indication that this material shader is unique and
  133. // does not depend on the number of lights in the scene and/or the
  134. // number of textures in the material.
  135. func (mat *Material) SetShaderUnique(unique bool) {
  136. mat.shaderUnique = unique
  137. }
  138. // ShaderUnique returns this material shader is unique.
  139. func (mat *Material) ShaderUnique() bool {
  140. return mat.shaderUnique
  141. }
  142. // SetUseLights sets the material use lights bit mask specifying which
  143. // light types will be used when rendering the material
  144. // By default the material will use all lights
  145. func (mat *Material) SetUseLights(lights UseLights) {
  146. mat.uselights = lights
  147. }
  148. // UseLights returns the current use lights bitmask
  149. func (mat *Material) UseLights() UseLights {
  150. return mat.uselights
  151. }
  152. // SetSide sets the visible side(s) (SideFront | SideBack | SideDouble)
  153. func (mat *Material) SetSide(side Side) {
  154. mat.sidevis = side
  155. }
  156. // Side returns the current side visibility for this material
  157. func (mat *Material) Side() Side {
  158. return mat.sidevis
  159. }
  160. // SetTransparent sets whether this material is transparent.
  161. func (mat *Material) SetTransparent(state bool) {
  162. mat.transparent = state
  163. }
  164. // Transparent returns whether this material is transparent.
  165. func (mat *Material) Transparent() bool {
  166. return mat.transparent
  167. }
  168. // SetWireframe sets whether only the wireframe is rendered.
  169. func (mat *Material) SetWireframe(state bool) {
  170. mat.wireframe = state
  171. }
  172. // Wireframe returns whether only the wireframe is rendered.
  173. func (mat *Material) Wireframe() bool {
  174. return mat.wireframe
  175. }
  176. func (mat *Material) SetDepthMask(state bool) {
  177. mat.depthMask = state
  178. }
  179. func (mat *Material) SetDepthTest(state bool) {
  180. mat.depthTest = state
  181. }
  182. func (mat *Material) SetDepthFunc(state uint32) {
  183. mat.depthFunc = state
  184. }
  185. func (mat *Material) SetBlending(blending Blending) {
  186. mat.blending = blending
  187. }
  188. func (mat *Material) SetLineWidth(width float32) {
  189. mat.lineWidth = width
  190. }
  191. func (mat *Material) SetPolygonOffset(factor, units float32) {
  192. mat.polyOffsetFactor = factor
  193. mat.polyOffsetUnits = units
  194. }
  195. // RenderSetup is called by the renderer before drawing objects with this material.
  196. func (mat *Material) RenderSetup(gs *gls.GLS) {
  197. // Sets triangle side view mode
  198. switch mat.sidevis {
  199. case SideFront:
  200. gs.Enable(gls.CULL_FACE)
  201. gs.FrontFace(gls.CCW)
  202. case SideBack:
  203. gs.Enable(gls.CULL_FACE)
  204. gs.FrontFace(gls.CW)
  205. case SideDouble:
  206. gs.Disable(gls.CULL_FACE)
  207. gs.FrontFace(gls.CCW)
  208. }
  209. if mat.depthTest {
  210. gs.Enable(gls.DEPTH_TEST)
  211. } else {
  212. gs.Disable(gls.DEPTH_TEST)
  213. }
  214. gs.DepthMask(mat.depthMask)
  215. gs.DepthFunc(mat.depthFunc)
  216. if mat.wireframe {
  217. gs.PolygonMode(gls.FRONT_AND_BACK, gls.LINE)
  218. } else {
  219. gs.PolygonMode(gls.FRONT_AND_BACK, gls.FILL)
  220. }
  221. // Set polygon offset if requested
  222. gs.PolygonOffset(mat.polyOffsetFactor, mat.polyOffsetUnits)
  223. // Sets line width
  224. gs.LineWidth(mat.lineWidth)
  225. // Sets blending
  226. switch mat.blending {
  227. case BlendingNone:
  228. gs.Disable(gls.BLEND)
  229. case BlendingNormal:
  230. gs.Enable(gls.BLEND)
  231. gs.BlendEquation(gls.FUNC_ADD)
  232. gs.BlendFunc(gls.SRC_ALPHA, gls.ONE_MINUS_SRC_ALPHA)
  233. case BlendingAdditive:
  234. gs.Enable(gls.BLEND)
  235. gs.BlendEquation(gls.FUNC_ADD)
  236. gs.BlendFunc(gls.SRC_ALPHA, gls.ONE)
  237. case BlendingSubtractive:
  238. gs.Enable(gls.BLEND)
  239. gs.BlendEquation(gls.FUNC_ADD)
  240. gs.BlendFunc(gls.ZERO, gls.ONE_MINUS_SRC_COLOR)
  241. break
  242. case BlendingMultiply:
  243. gs.Enable(gls.BLEND)
  244. gs.BlendEquation(gls.FUNC_ADD)
  245. gs.BlendFunc(gls.ZERO, gls.SRC_COLOR)
  246. break
  247. case BlendingCustom:
  248. gs.BlendEquationSeparate(mat.blendRGB, mat.blendAlpha)
  249. gs.BlendFuncSeparate(mat.blendSrcRGB, mat.blendDstRGB, mat.blendSrcAlpha, mat.blendDstAlpha)
  250. break
  251. default:
  252. panic("Invalid blending")
  253. }
  254. // Render textures
  255. // Keep track of counts of unique sampler names to correctly index sampler arrays
  256. samplerCounts := make(map[string]int)
  257. for slotIdx, tex := range mat.textures {
  258. samplerName, _ := tex.GetUniformNames()
  259. uniIdx, _ := samplerCounts[samplerName]
  260. tex.RenderSetup(gs, slotIdx, uniIdx)
  261. samplerCounts[samplerName] = uniIdx + 1
  262. }
  263. }
  264. // AddTexture adds the specified Texture2d to the material
  265. func (mat *Material) AddTexture(tex *texture.Texture2D) {
  266. mat.textures = append(mat.textures, tex)
  267. }
  268. // RemoveTexture removes the specified Texture2d from the material
  269. func (mat *Material) RemoveTexture(tex *texture.Texture2D) {
  270. for pos, curr := range mat.textures {
  271. if curr == tex {
  272. copy(mat.textures[pos:], mat.textures[pos+1:])
  273. mat.textures[len(mat.textures)-1] = nil
  274. mat.textures = mat.textures[:len(mat.textures)-1]
  275. break
  276. }
  277. }
  278. }
  279. // HasTexture checks if the material contains the specified texture
  280. func (mat *Material) HasTexture(tex *texture.Texture2D) bool {
  281. for _, curr := range mat.textures {
  282. if curr == tex {
  283. return true
  284. }
  285. }
  286. return false
  287. }
  288. // TextureCount returns the current number of textures
  289. func (mat *Material) TextureCount() int {
  290. return len(mat.textures)
  291. }