material.go 9.1 KB

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