material.go 9.4 KB

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