|
|
@@ -5,157 +5,158 @@
|
|
|
package light
|
|
|
|
|
|
import (
|
|
|
+ "unsafe"
|
|
|
+
|
|
|
"github.com/g3n/engine/core"
|
|
|
"github.com/g3n/engine/gls"
|
|
|
"github.com/g3n/engine/math32"
|
|
|
)
|
|
|
|
|
|
type Spot struct {
|
|
|
- core.Node // Embedded node
|
|
|
- color math32.Color // Light color
|
|
|
- intensity float32 // Light intensity
|
|
|
- direction math32.Vector3 // Direction in world coordinates
|
|
|
- uni *gls.Uniform3fv // Uniform with spot light properties
|
|
|
-}
|
|
|
-
|
|
|
-const (
|
|
|
- sColor = 0 // index of color vector in uniform (0,1,2)
|
|
|
- sPosition = 1 // index of position vector in uniform (3,4,5)
|
|
|
- sDirection = 2 // index of position vector in uniform (6,7,8)
|
|
|
- sAngularDecay = 9 // position of scalar angular decay in uniform array
|
|
|
- sCutoffAngle = 10 // position of cutoff angle in uniform array
|
|
|
- sLinearDecay = 11 // position of scalar linear decay in uniform array
|
|
|
- sQuadraticDecay = 12 // position of scalar quadratic decay in uniform array
|
|
|
- spotUniSize = 5 // uniform count of 5 float32
|
|
|
-)
|
|
|
+ core.Node // Embedded node
|
|
|
+ color math32.Color // Light color
|
|
|
+ intensity float32 // Light intensity
|
|
|
+ direction math32.Vector3 // Direction in world coordinates
|
|
|
+ uni gls.Uniform2 // Uniform location cache
|
|
|
+ udata struct { // Combined uniform data in 5 vec3:
|
|
|
+ color math32.Color // Light color
|
|
|
+ position math32.Vector3 // Light position
|
|
|
+ direction math32.Vector3 // Light position
|
|
|
+ angularDecay float32 // Angular decay factor
|
|
|
+ cutoffAngle float32 // Cut off angle
|
|
|
+ linearDecay float32 // Distance linear decay
|
|
|
+ quadraticDecay float32 // Distance quadratic decay
|
|
|
+ dummy1 float32 // Completes 5*vec3
|
|
|
+ dummy2 float32 // Completes 5*vec3
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
// NewSpot creates and returns a spot light with the specified color and intensity
|
|
|
func NewSpot(color *math32.Color, intensity float32) *Spot {
|
|
|
|
|
|
- sl := new(Spot)
|
|
|
- sl.Node.Init()
|
|
|
-
|
|
|
- sl.color = *color
|
|
|
- sl.intensity = intensity
|
|
|
-
|
|
|
- // Creates uniforms and set initial values
|
|
|
- sl.uni = gls.NewUniform3fv("SpotLight", spotUniSize)
|
|
|
- sl.SetColor(color)
|
|
|
- sl.SetAngularDecay(15.0)
|
|
|
- sl.SetCutoffAngle(45.0)
|
|
|
- sl.SetLinearDecay(1.0)
|
|
|
- sl.SetQuadraticDecay(1.0)
|
|
|
-
|
|
|
- return sl
|
|
|
+ l := new(Spot)
|
|
|
+ l.Node.Init()
|
|
|
+ l.color = *color
|
|
|
+ l.intensity = intensity
|
|
|
+ l.uni.Init("SpotLight")
|
|
|
+ l.SetColor(color)
|
|
|
+ l.SetAngularDecay(15.0)
|
|
|
+ l.SetCutoffAngle(45.0)
|
|
|
+ l.SetLinearDecay(1.0)
|
|
|
+ l.SetQuadraticDecay(1.0)
|
|
|
+ return l
|
|
|
}
|
|
|
|
|
|
// SetColor sets the color of this light
|
|
|
-func (sl *Spot) SetColor(color *math32.Color) {
|
|
|
+func (l *Spot) SetColor(color *math32.Color) {
|
|
|
|
|
|
- sl.color = *color
|
|
|
- tmpColor := sl.color
|
|
|
- tmpColor.MultiplyScalar(sl.intensity)
|
|
|
- sl.uni.SetColor(sColor, &tmpColor)
|
|
|
+ l.color = *color
|
|
|
+ l.udata.color = l.color
|
|
|
+ l.udata.color.MultiplyScalar(l.intensity)
|
|
|
}
|
|
|
|
|
|
// Color returns the current color of this light
|
|
|
-func (sl *Spot) Color() math32.Color {
|
|
|
+func (l *Spot) Color() math32.Color {
|
|
|
|
|
|
- return sl.color
|
|
|
+ return l.color
|
|
|
}
|
|
|
|
|
|
// SetIntensity sets the intensity of this light
|
|
|
-func (sl *Spot) SetIntensity(intensity float32) {
|
|
|
+func (l *Spot) SetIntensity(intensity float32) {
|
|
|
|
|
|
- sl.intensity = intensity
|
|
|
- tmpColor := sl.color
|
|
|
- tmpColor.MultiplyScalar(sl.intensity)
|
|
|
- sl.uni.SetColor(sColor, &tmpColor)
|
|
|
+ l.intensity = intensity
|
|
|
+ l.udata.color = l.color
|
|
|
+ l.udata.color.MultiplyScalar(l.intensity)
|
|
|
}
|
|
|
|
|
|
// Intensity returns the current intensity of this light
|
|
|
-func (sl *Spot) Intensity() float32 {
|
|
|
+func (l *Spot) Intensity() float32 {
|
|
|
|
|
|
- return sl.intensity
|
|
|
+ return l.intensity
|
|
|
}
|
|
|
|
|
|
// SetDirection sets the direction of the spot light in world coordinates
|
|
|
-func (sp *Spot) SetDirection(direction *math32.Vector3) {
|
|
|
+func (l *Spot) SetDirection(direction *math32.Vector3) {
|
|
|
|
|
|
- sp.direction = *direction
|
|
|
+ l.direction = *direction
|
|
|
}
|
|
|
|
|
|
// Direction returns the current direction of this spot light in world coordinates
|
|
|
-func (sp *Spot) Direction(direction *math32.Vector3) math32.Vector3 {
|
|
|
+func (l *Spot) Direction(direction *math32.Vector3) math32.Vector3 {
|
|
|
|
|
|
- return sp.direction
|
|
|
+ return l.direction
|
|
|
}
|
|
|
|
|
|
// SetCutoffAngle sets the cutoff angle in degrees from 0 to 90
|
|
|
-func (sl *Spot) SetCutoffAngle(angle float32) {
|
|
|
+func (l *Spot) SetCutoffAngle(angle float32) {
|
|
|
|
|
|
- sl.uni.SetPos(sCutoffAngle, angle)
|
|
|
+ l.udata.cutoffAngle = angle
|
|
|
}
|
|
|
|
|
|
// CutoffAngle returns the current cutoff angle in degrees from 0 to 90
|
|
|
-func (sl *Spot) CutoffAngle() float32 {
|
|
|
+func (l *Spot) CutoffAngle() float32 {
|
|
|
|
|
|
- return sl.uni.GetPos(sCutoffAngle)
|
|
|
+ return l.udata.cutoffAngle
|
|
|
}
|
|
|
|
|
|
// SetAngularDecay sets the angular decay exponent
|
|
|
-func (sl *Spot) SetAngularDecay(decay float32) {
|
|
|
+func (l *Spot) SetAngularDecay(decay float32) {
|
|
|
|
|
|
- sl.uni.SetPos(sAngularDecay, decay)
|
|
|
+ l.udata.angularDecay = decay
|
|
|
}
|
|
|
|
|
|
// AngularDecay returns the current angular decay exponent
|
|
|
-func (sl *Spot) AngularDecay() float32 {
|
|
|
+func (l *Spot) AngularDecay() float32 {
|
|
|
|
|
|
- return sl.uni.GetPos(sAngularDecay)
|
|
|
+ return l.udata.angularDecay
|
|
|
}
|
|
|
|
|
|
// SetLinearDecay sets the linear decay factor as a function of the distance
|
|
|
-func (sl *Spot) SetLinearDecay(decay float32) {
|
|
|
+func (l *Spot) SetLinearDecay(decay float32) {
|
|
|
|
|
|
- sl.uni.SetPos(sLinearDecay, decay)
|
|
|
+ l.udata.linearDecay = decay
|
|
|
}
|
|
|
|
|
|
// LinearDecay returns the current linear decay factor
|
|
|
-func (sl *Spot) LinearDecay() float32 {
|
|
|
+func (l *Spot) LinearDecay() float32 {
|
|
|
|
|
|
- return sl.uni.GetPos(sLinearDecay)
|
|
|
+ return l.udata.linearDecay
|
|
|
}
|
|
|
|
|
|
// SetQuadraticDecay sets the quadratic decay factor as a function of the distance
|
|
|
-func (sl *Spot) SetQuadraticDecay(decay float32) {
|
|
|
+func (l *Spot) SetQuadraticDecay(decay float32) {
|
|
|
|
|
|
- sl.uni.SetPos(sQuadraticDecay, decay)
|
|
|
+ l.udata.quadraticDecay = decay
|
|
|
}
|
|
|
|
|
|
// QuadraticDecay returns the current quadratic decay factor
|
|
|
-func (sl *Spot) QuadraticDecay() float32 {
|
|
|
+func (l *Spot) QuadraticDecay() float32 {
|
|
|
|
|
|
- return sl.uni.GetPos(sQuadraticDecay)
|
|
|
+ return l.udata.quadraticDecay
|
|
|
}
|
|
|
|
|
|
// RenderSetup is called by the engine before rendering the scene
|
|
|
-func (sl *Spot) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo, idx int) {
|
|
|
+func (l *Spot) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo, idx int) {
|
|
|
|
|
|
// Calculates and updates light position uniform in camera coordinates
|
|
|
var pos math32.Vector3
|
|
|
- sl.WorldPosition(&pos)
|
|
|
+ l.WorldPosition(&pos)
|
|
|
var pos4 math32.Vector4
|
|
|
pos4.SetVector3(&pos, 1.0)
|
|
|
pos4.ApplyMatrix4(&rinfo.ViewMatrix)
|
|
|
- sl.uni.SetVector3(sPosition, &math32.Vector3{pos4.X, pos4.Y, pos4.Z})
|
|
|
+ l.udata.position.X = pos4.X
|
|
|
+ l.udata.position.Y = pos4.Y
|
|
|
+ l.udata.position.Z = pos4.Z
|
|
|
|
|
|
// Calculates and updates light direction uniform in camera coordinates
|
|
|
- pos4.SetVector3(&sl.direction, 0.0)
|
|
|
+ pos4.SetVector3(&l.direction, 0.0)
|
|
|
pos4.ApplyMatrix4(&rinfo.ViewMatrix)
|
|
|
- // Normalize here ??
|
|
|
- sl.uni.SetVector3(sDirection, &math32.Vector3{pos4.X, pos4.Y, pos4.Z})
|
|
|
-
|
|
|
- // Transfer uniform
|
|
|
- sl.uni.TransferIdx(gs, idx*spotUniSize)
|
|
|
+ l.udata.direction.X = pos4.X
|
|
|
+ l.udata.direction.Y = pos4.Y
|
|
|
+ l.udata.direction.Z = pos4.Z
|
|
|
+
|
|
|
+ // Transfer uniform data
|
|
|
+ const vec3count = 5
|
|
|
+ location := l.uni.LocationIdx(gs, vec3count*int32(idx))
|
|
|
+ gs.Uniform3fvUP(location, vec3count, unsafe.Pointer(&l.udata))
|
|
|
}
|