|
|
@@ -11,45 +11,66 @@ import (
|
|
|
)
|
|
|
|
|
|
type Spot struct {
|
|
|
- core.Node // Embedded node
|
|
|
- color math32.Color // Light color
|
|
|
- intensity float32 // Light intensity
|
|
|
- direction math32.Vector3 // Direction in world coordinates
|
|
|
- uColor gls.Uniform3f // Uniform for light color
|
|
|
- uPosition gls.Uniform3f // Uniform for position in camera coordinates
|
|
|
- uDirection gls.Uniform3f // Uniform for direction in camera coordinates
|
|
|
- uAngularDecay gls.Uniform1f // Uniform for angular attenuation exponent
|
|
|
- uCutoffAngle gls.Uniform1f // Uniform for cutoff angle from 0 to 90 degrees
|
|
|
- uLinearDecay gls.Uniform1f // Uniform for linear distance decay
|
|
|
- uQuadraticDecay gls.Uniform1f // Uniform for quadratic distance decay
|
|
|
-}
|
|
|
+ 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
|
|
|
+
|
|
|
+ //uColor gls.Uniform3f // Uniform for light color
|
|
|
+ //uPosition gls.Uniform3f // Uniform for position in camera coordinates
|
|
|
+ //uDirection gls.Uniform3f // Uniform for direction in camera coordinates
|
|
|
+ //uAngularDecay gls.Uniform1f // Uniform for angular attenuation exponent
|
|
|
+ //uCutoffAngle gls.Uniform1f // Uniform for cutoff angle from 0 to 90 degrees
|
|
|
+ //uLinearDecay gls.Uniform1f // Uniform for linear distance decay
|
|
|
+ //uQuadraticDecay gls.Uniform1f // Uniform for quadratic distance decay
|
|
|
+}
|
|
|
+
|
|
|
+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
|
|
|
+)
|
|
|
|
|
|
// NewSpot creates and returns a spot light with the specified color and intensity
|
|
|
func NewSpot(color *math32.Color, intensity float32) *Spot {
|
|
|
|
|
|
- sp := new(Spot)
|
|
|
- sp.Node.Init()
|
|
|
-
|
|
|
- sp.color = *color
|
|
|
- sp.intensity = intensity
|
|
|
+ sl := new(Spot)
|
|
|
+ sl.Node.Init()
|
|
|
|
|
|
- // Creates uniforms
|
|
|
- sp.uColor.Init("SpotLightColor")
|
|
|
- sp.uPosition.Init("SpotLightPosition")
|
|
|
- sp.uDirection.Init("SpotLightDirection")
|
|
|
- sp.uAngularDecay.Init("SpotLightAngularDecay")
|
|
|
- sp.uCutoffAngle.Init("SpotLightCutoffAngle")
|
|
|
- sp.uLinearDecay.Init("SpotLightLinearDecay")
|
|
|
- sp.uQuadraticDecay.Init("SpotQuadraticDecay")
|
|
|
+ sl.color = *color
|
|
|
+ sl.intensity = intensity
|
|
|
|
|
|
- // Set initial values
|
|
|
- sp.intensity = intensity
|
|
|
- sp.SetColor(color)
|
|
|
- sp.uAngularDecay.Set(15.0)
|
|
|
- sp.uCutoffAngle.Set(45.0)
|
|
|
- sp.uLinearDecay.Set(1.0)
|
|
|
- sp.uQuadraticDecay.Set(1.0)
|
|
|
- return sp
|
|
|
+ // 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)
|
|
|
+
|
|
|
+ // // Creates uniforms
|
|
|
+ // sp.uColor.Init("SpotLightColor")
|
|
|
+ // sp.uPosition.Init("SpotLightPosition")
|
|
|
+ // sp.uDirection.Init("SpotLightDirection")
|
|
|
+ // sp.uAngularDecay.Init("SpotLightAngularDecay")
|
|
|
+ // sp.uCutoffAngle.Init("SpotLightCutoffAngle")
|
|
|
+ // sp.uLinearDecay.Init("SpotLightLinearDecay")
|
|
|
+ // sp.uQuadraticDecay.Init("SpotQuadraticDecay")
|
|
|
+ //
|
|
|
+ // // Set initial values
|
|
|
+ // sp.intensity = intensity
|
|
|
+ // sp.SetColor(color)
|
|
|
+ // sp.uAngularDecay.Set(15.0)
|
|
|
+ // sp.uCutoffAngle.Set(45.0)
|
|
|
+ // sp.uLinearDecay.Set(1.0)
|
|
|
+ // sp.uQuadraticDecay.Set(1.0)
|
|
|
+ return sl
|
|
|
}
|
|
|
|
|
|
// SetColor sets the color of this light
|
|
|
@@ -58,7 +79,7 @@ func (sl *Spot) SetColor(color *math32.Color) {
|
|
|
sl.color = *color
|
|
|
tmpColor := sl.color
|
|
|
tmpColor.MultiplyScalar(sl.intensity)
|
|
|
- sl.uColor.SetColor(&tmpColor)
|
|
|
+ sl.uni.SetColor(sColor, &tmpColor)
|
|
|
}
|
|
|
|
|
|
// Color returns the current color of this light
|
|
|
@@ -73,7 +94,7 @@ func (sl *Spot) SetIntensity(intensity float32) {
|
|
|
sl.intensity = intensity
|
|
|
tmpColor := sl.color
|
|
|
tmpColor.MultiplyScalar(sl.intensity)
|
|
|
- sl.uColor.SetColor(&tmpColor)
|
|
|
+ sl.uni.SetColor(sColor, &tmpColor)
|
|
|
}
|
|
|
|
|
|
// Intensity returns the current intensity of this light
|
|
|
@@ -97,59 +118,59 @@ func (sp *Spot) Direction(direction *math32.Vector3) math32.Vector3 {
|
|
|
// SetCutoffAngle sets the cutoff angle in degrees from 0 to 90
|
|
|
func (sl *Spot) SetCutoffAngle(angle float32) {
|
|
|
|
|
|
- sl.uCutoffAngle.Set(angle)
|
|
|
+ sl.uni.SetPos(sCutoffAngle, angle)
|
|
|
}
|
|
|
|
|
|
// CutoffAngle returns the current cutoff angle in degrees from 0 to 90
|
|
|
func (sl *Spot) CutoffAngle() float32 {
|
|
|
|
|
|
- return sl.uCutoffAngle.Get()
|
|
|
+ return sl.uni.GetPos(sCutoffAngle)
|
|
|
}
|
|
|
|
|
|
// SetAngularDecay sets the angular decay exponent
|
|
|
func (sl *Spot) SetAngularDecay(decay float32) {
|
|
|
|
|
|
- sl.uAngularDecay.Set(decay)
|
|
|
+ sl.uni.SetPos(sAngularDecay, decay)
|
|
|
}
|
|
|
|
|
|
// AngularDecay returns the current angular decay exponent
|
|
|
func (sl *Spot) AngularDecay() float32 {
|
|
|
|
|
|
- return sl.uAngularDecay.Get()
|
|
|
+ return sl.uni.GetPos(sAngularDecay)
|
|
|
}
|
|
|
|
|
|
// SetLinearDecay sets the linear decay factor as a function of the distance
|
|
|
func (sl *Spot) SetLinearDecay(decay float32) {
|
|
|
|
|
|
- sl.uLinearDecay.Set(decay)
|
|
|
+ sl.uni.SetPos(sLinearDecay, decay)
|
|
|
}
|
|
|
|
|
|
// LinearDecay returns the current linear decay factor
|
|
|
func (sl *Spot) LinearDecay() float32 {
|
|
|
|
|
|
- return sl.uLinearDecay.Get()
|
|
|
+ return sl.uni.GetPos(sLinearDecay)
|
|
|
}
|
|
|
|
|
|
// SetQuadraticDecay sets the quadratic decay factor as a function of the distance
|
|
|
func (sl *Spot) SetQuadraticDecay(decay float32) {
|
|
|
|
|
|
- sl.uQuadraticDecay.Set(decay)
|
|
|
+ sl.uni.SetPos(sQuadraticDecay, decay)
|
|
|
}
|
|
|
|
|
|
// QuadraticDecay returns the current quadratic decay factor
|
|
|
func (sl *Spot) QuadraticDecay() float32 {
|
|
|
|
|
|
- return sl.uQuadraticDecay.Get()
|
|
|
+ return sl.uni.GetPos(sQuadraticDecay)
|
|
|
}
|
|
|
|
|
|
// RenderSetup is called by the engine before rendering the scene
|
|
|
func (sl *Spot) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo, idx int) {
|
|
|
|
|
|
- sl.uColor.TransferIdx(gs, idx)
|
|
|
- sl.uAngularDecay.TransferIdx(gs, idx)
|
|
|
- sl.uCutoffAngle.TransferIdx(gs, idx)
|
|
|
- sl.uLinearDecay.TransferIdx(gs, idx)
|
|
|
- sl.uQuadraticDecay.TransferIdx(gs, idx)
|
|
|
+ //sl.uColor.TransferIdx(gs, idx)
|
|
|
+ //sl.uAngularDecay.TransferIdx(gs, idx)
|
|
|
+ //sl.uCutoffAngle.TransferIdx(gs, idx)
|
|
|
+ //sl.uLinearDecay.TransferIdx(gs, idx)
|
|
|
+ //sl.uQuadraticDecay.TransferIdx(gs, idx)
|
|
|
|
|
|
// Calculates and updates light position uniform in camera coordinates
|
|
|
var pos math32.Vector3
|
|
|
@@ -157,13 +178,18 @@ func (sl *Spot) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo, idx int) {
|
|
|
var pos4 math32.Vector4
|
|
|
pos4.SetVector3(&pos, 1.0)
|
|
|
pos4.ApplyMatrix4(&rinfo.ViewMatrix)
|
|
|
- sl.uPosition.SetVector3(&math32.Vector3{pos4.X, pos4.Y, pos4.Z})
|
|
|
- sl.uPosition.TransferIdx(gs, idx)
|
|
|
+ //sl.uPosition.SetVector3(&math32.Vector3{pos4.X, pos4.Y, pos4.Z})
|
|
|
+ //sl.uPosition.TransferIdx(gs, idx)
|
|
|
+ sl.uni.SetVector3(sPosition, &math32.Vector3{pos4.X, pos4.Y, pos4.Z})
|
|
|
|
|
|
// Calculates and updates light direction uniform in camera coordinates
|
|
|
pos4.SetVector3(&sl.direction, 0.0)
|
|
|
pos4.ApplyMatrix4(&rinfo.ViewMatrix)
|
|
|
// Normalize here ??
|
|
|
- sl.uDirection.SetVector3(&math32.Vector3{pos4.X, pos4.Y, pos4.Z})
|
|
|
- sl.uDirection.TransferIdx(gs, idx)
|
|
|
+ //sl.uDirection.SetVector3(&math32.Vector3{pos4.X, pos4.Y, pos4.Z})
|
|
|
+ //sl.uDirection.TransferIdx(gs, idx)
|
|
|
+ sl.uni.SetVector3(sDirection, &math32.Vector3{pos4.X, pos4.Y, pos4.Z})
|
|
|
+
|
|
|
+ // Transfer uniform
|
|
|
+ sl.uni.TransferIdx(gs, idx*spotUniSize)
|
|
|
}
|