Kaynağa Gözat

changing uniform transfers...

leonsal 8 yıl önce
ebeveyn
işleme
5b407864db
3 değiştirilmiş dosya ile 107 ekleme ve 105 silme
  1. 0 1
      light/directional.go
  2. 31 29
      light/point.go
  3. 76 75
      light/spot.go

+ 0 - 1
light/directional.go

@@ -73,7 +73,6 @@ func (ld *Directional) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo, idx int)
 	ld.WorldPosition(&pos)
 	pos4 := math32.Vector4{pos.X, pos.Y, pos.Z, 0.0}
 	pos4.ApplyMatrix4(&rinfo.ViewMatrix)
-	//ld.uni.SetVector3(dirPosition, &math32.Vector3{pos4.X, pos4.Y, pos4.Z})
 	ld.udata.position.X = pos4.X
 	ld.udata.position.Y = pos4.Y
 	ld.udata.position.Z = pos4.Z

+ 31 - 29
light/point.go

@@ -5,26 +5,27 @@
 package light
 
 import (
+	"unsafe"
+
 	"github.com/g3n/engine/core"
 	"github.com/g3n/engine/gls"
 	"github.com/g3n/engine/math32"
 )
 
 type Point struct {
-	core.Node                 // Embedded node
-	color     math32.Color    // Light color
-	intensity float32         // Light intensity
-	uni       *gls.Uniform3fv // Uniform with light properties
+	core.Node              // Embedded node
+	color     math32.Color // Light color
+	intensity float32      // Light intensity
+	uni       gls.Uniform2 // Uniform location cache
+	udata     struct {     // Combined uniform data in 3 vec3:
+		color          math32.Color   // Light color
+		position       math32.Vector3 // Light position
+		linearDecay    float32        // Distance linear decay factor
+		quadraticDecay float32        // Distance quadratic decay factor
+		dummy          float32        // Completes 3*vec3
+	}
 }
 
-const (
-	pColor          = 0                // index of color vector in uniform (0,1,2)
-	pPosition       = 1                // index of position vector in uniform (3,4,5)
-	pLinearDecay    = 6                // position of scalar linear decay in uniform array
-	pQuadraticDecay = pLinearDecay + 1 // position of scalar linear decay in uniform array
-	pointUniSize    = 3                // uniform count of 3 float32
-)
-
 // NewPoint creates and returns a point light with the specified color and intensity
 func NewPoint(color *math32.Color, intensity float32) *Point {
 
@@ -34,12 +35,11 @@ func NewPoint(color *math32.Color, intensity float32) *Point {
 	lp.intensity = intensity
 
 	// Creates uniform and sets initial values
-	lp.uni = gls.NewUniform3fv("PointLight", pointUniSize)
+	lp.uni.Init("PointLight")
 	lp.SetColor(color)
 	lp.SetIntensity(intensity)
 	lp.SetLinearDecay(1.0)
 	lp.SetQuadraticDecay(1.0)
-
 	return lp
 }
 
@@ -47,9 +47,8 @@ func NewPoint(color *math32.Color, intensity float32) *Point {
 func (lp *Point) SetColor(color *math32.Color) {
 
 	lp.color = *color
-	tmpColor := lp.color
-	tmpColor.MultiplyScalar(lp.intensity)
-	lp.uni.SetColor(pColor, &tmpColor)
+	lp.udata.color = lp.color
+	lp.udata.color.MultiplyScalar(lp.intensity)
 }
 
 // Color returns the current color of this light
@@ -62,9 +61,8 @@ func (lp *Point) Color() math32.Color {
 func (lp *Point) SetIntensity(intensity float32) {
 
 	lp.intensity = intensity
-	tmpColor := lp.color
-	tmpColor.MultiplyScalar(lp.intensity)
-	lp.uni.SetColor(pColor, &tmpColor)
+	lp.udata.color = lp.color
+	lp.udata.color.MultiplyScalar(lp.intensity)
 }
 
 // Intensity returns the current intensity of this light
@@ -76,37 +74,41 @@ func (lp *Point) Intensity() float32 {
 // SetLinearDecay sets the linear decay factor as a function of the distance
 func (lp *Point) SetLinearDecay(decay float32) {
 
-	lp.uni.SetPos(pLinearDecay, decay)
+	lp.udata.linearDecay = decay
 }
 
 // LinearDecay returns the current linear decay factor
 func (lp *Point) LinearDecay() float32 {
 
-	return lp.uni.GetPos(pLinearDecay)
+	return lp.udata.linearDecay
 }
 
 // SetQuadraticDecay sets the quadratic decay factor as a function of the distance
 func (lp *Point) SetQuadraticDecay(decay float32) {
 
-	lp.uni.SetPos(pQuadraticDecay, decay)
+	lp.udata.quadraticDecay = decay
 }
 
 // QuadraticDecay returns the current quadratic decay factor
 func (lp *Point) QuadraticDecay() float32 {
 
-	return lp.uni.GetPos(pQuadraticDecay)
+	return lp.udata.quadraticDecay
 }
 
 // RenderSetup is called by the engine before rendering the scene
 func (lp *Point) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo, idx int) {
 
-	// Calculates and updates light position uniform in camera coordinates
+	// Calculates light position in camera coordinates and updates uniform
 	var pos math32.Vector3
 	lp.WorldPosition(&pos)
 	pos4 := math32.Vector4{pos.X, pos.Y, pos.Z, 1.0}
 	pos4.ApplyMatrix4(&rinfo.ViewMatrix)
-	lp.uni.SetVector3(pPosition, &math32.Vector3{pos4.X, pos4.Y, pos4.Z})
-
-	// Transfer uniform
-	lp.uni.TransferIdx(gs, idx*pointUniSize)
+	lp.udata.position.X = pos4.X
+	lp.udata.position.Y = pos4.Y
+	lp.udata.position.Z = pos4.Z
+
+	// Transfer uniform data
+	const vec3count = 3
+	location := lp.uni.LocationIdx(gs, vec3count*int32(idx))
+	gs.Uniform3fvUP(location, vec3count, unsafe.Pointer(&lp.udata))
 }

+ 76 - 75
light/spot.go

@@ -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))
 }