standard.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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
  5. import (
  6. "github.com/g3n/engine/gls"
  7. "github.com/g3n/engine/math32"
  8. "unsafe"
  9. )
  10. // Standard material supports the classic lighting model with
  11. // ambient, diffuse, specular and emissive lights.
  12. // The lighting calculation is implemented in the vertex shader.
  13. type Standard struct {
  14. Material // Embedded material
  15. uni gls.Uniform2 // Uniform location cache
  16. udata struct { // Combined uniform data in 6 vec3:
  17. ambient math32.Color // Ambient color reflectivity
  18. diffuse math32.Color // Diffuse color reflectivity
  19. specular math32.Color // Specular color reflectivity
  20. emissive math32.Color // Emissive color
  21. shininess float32 // Specular shininess factor
  22. opacity float32 // Opacity
  23. psize float32 // Point size
  24. protationZ float32 // Point rotation around Z axis
  25. }
  26. }
  27. // Number of glsl shader vec3 elements used by uniform data
  28. const standardVec3Count = 6
  29. // NewStandard creates and returns a pointer to a new standard material
  30. func NewStandard(color *math32.Color) *Standard {
  31. ms := new(Standard)
  32. ms.Init("standard", color)
  33. return ms
  34. }
  35. // Init initializes the material setting the specified shader and color
  36. // It is used mainly when the material is embedded in another type
  37. func (ms *Standard) Init(shader string, color *math32.Color) {
  38. ms.Material.Init()
  39. ms.SetShader(shader)
  40. // Creates uniforms and set initial values
  41. ms.uni.Init("Material")
  42. ms.SetColor(color)
  43. ms.SetSpecularColor(&math32.Color{0.5, 0.5, 0.5})
  44. ms.SetEmissiveColor(&math32.Color{0, 0, 0})
  45. ms.SetShininess(30.0)
  46. ms.SetOpacity(1.0)
  47. }
  48. // AmbientColor returns the material ambient color reflectivity.
  49. func (ms *Standard) AmbientColor() math32.Color {
  50. return ms.udata.ambient
  51. }
  52. // SetAmbientColor sets the material ambient color reflectivity.
  53. // The default is the same as the diffuse color
  54. func (ms *Standard) SetAmbientColor(color *math32.Color) {
  55. ms.udata.ambient = *color
  56. }
  57. // SetColor sets the material diffuse color and also the
  58. // material ambient color reflectivity
  59. func (ms *Standard) SetColor(color *math32.Color) {
  60. ms.udata.diffuse = *color
  61. ms.udata.ambient = *color
  62. }
  63. // SetEmissiveColor sets the material emissive color
  64. // The default is {0,0,0}
  65. func (ms *Standard) SetEmissiveColor(color *math32.Color) {
  66. ms.udata.emissive = *color
  67. }
  68. // EmissiveColor returns the material current emissive color
  69. func (ms *Standard) EmissiveColor() math32.Color {
  70. return ms.udata.emissive
  71. }
  72. // SetSpecularColor sets the material specular color reflectivity.
  73. // The default is {0.5, 0.5, 0.5}
  74. func (ms *Standard) SetSpecularColor(color *math32.Color) {
  75. ms.udata.specular = *color
  76. }
  77. // SetShininess sets the specular highlight factor. Default is 30.
  78. func (ms *Standard) SetShininess(shininess float32) {
  79. ms.udata.shininess = shininess
  80. }
  81. // SetOpacity sets the material opacity (alpha). Default is 1.0.
  82. func (ms *Standard) SetOpacity(opacity float32) {
  83. ms.udata.opacity = opacity
  84. }
  85. // RenderSetup is called by the engine before drawing the object
  86. // which uses this material
  87. func (ms *Standard) RenderSetup(gs *gls.GLS) {
  88. ms.Material.RenderSetup(gs)
  89. location := ms.uni.Location(gs)
  90. gs.Uniform3fvUP(location, standardVec3Count, unsafe.Pointer(&ms.udata))
  91. }