point.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 light
  5. import (
  6. "unsafe"
  7. "github.com/g3n/engine/core"
  8. "github.com/g3n/engine/gls"
  9. "github.com/g3n/engine/math32"
  10. )
  11. // Point is an omnidirectional light source
  12. type Point struct {
  13. core.Node // Embedded node
  14. color math32.Color // Light color
  15. intensity float32 // Light intensity
  16. uni gls.Uniform // Uniform location cache
  17. udata struct { // Combined uniform data in 3 vec3:
  18. color math32.Color // Light color
  19. position math32.Vector3 // Light position
  20. linearDecay float32 // Distance linear decay factor
  21. quadraticDecay float32 // Distance quadratic decay factor
  22. dummy float32 // Completes 3*vec3
  23. }
  24. }
  25. // NewPoint creates and returns a point light with the specified color and intensity
  26. func NewPoint(color *math32.Color, intensity float32) *Point {
  27. lp := new(Point)
  28. lp.Node.Init()
  29. lp.color = *color
  30. lp.intensity = intensity
  31. // Creates uniform and sets initial values
  32. lp.uni.Init("PointLight")
  33. lp.SetColor(color)
  34. lp.SetIntensity(intensity)
  35. lp.SetLinearDecay(1.0)
  36. lp.SetQuadraticDecay(1.0)
  37. return lp
  38. }
  39. // SetColor sets the color of this light
  40. func (lp *Point) SetColor(color *math32.Color) {
  41. lp.color = *color
  42. lp.udata.color = lp.color
  43. lp.udata.color.MultiplyScalar(lp.intensity)
  44. }
  45. // Color returns the current color of this light
  46. func (lp *Point) Color() math32.Color {
  47. return lp.color
  48. }
  49. // SetIntensity sets the intensity of this light
  50. func (lp *Point) SetIntensity(intensity float32) {
  51. lp.intensity = intensity
  52. lp.udata.color = lp.color
  53. lp.udata.color.MultiplyScalar(lp.intensity)
  54. }
  55. // Intensity returns the current intensity of this light
  56. func (lp *Point) Intensity() float32 {
  57. return lp.intensity
  58. }
  59. // SetLinearDecay sets the linear decay factor as a function of the distance
  60. func (lp *Point) SetLinearDecay(decay float32) {
  61. lp.udata.linearDecay = decay
  62. }
  63. // LinearDecay returns the current linear decay factor
  64. func (lp *Point) LinearDecay() float32 {
  65. return lp.udata.linearDecay
  66. }
  67. // SetQuadraticDecay sets the quadratic decay factor as a function of the distance
  68. func (lp *Point) SetQuadraticDecay(decay float32) {
  69. lp.udata.quadraticDecay = decay
  70. }
  71. // QuadraticDecay returns the current quadratic decay factor
  72. func (lp *Point) QuadraticDecay() float32 {
  73. return lp.udata.quadraticDecay
  74. }
  75. // RenderSetup is called by the engine before rendering the scene
  76. func (lp *Point) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo, idx int) {
  77. // Calculates light position in camera coordinates and updates uniform
  78. var pos math32.Vector3
  79. lp.WorldPosition(&pos)
  80. pos4 := math32.Vector4{pos.X, pos.Y, pos.Z, 1.0}
  81. pos4.ApplyMatrix4(&rinfo.ViewMatrix)
  82. lp.udata.position.X = pos4.X
  83. lp.udata.position.Y = pos4.Y
  84. lp.udata.position.Z = pos4.Z
  85. // Transfer uniform data
  86. const vec3count = 3
  87. location := lp.uni.LocationIdx(gs, vec3count*int32(idx))
  88. gs.Uniform3fvUP(location, vec3count, unsafe.Pointer(&lp.udata))
  89. }