Parcourir la source

uniforms grouping...

leonsal il y a 8 ans
Parent
commit
e8fc97eab5
6 fichiers modifiés avec 38 ajouts et 89 suppressions
  1. 1 1
      gls/gls.go
  2. 3 0
      material/phong.go
  3. 15 42
      material/point.go
  4. 12 37
      material/standard.go
  5. 3 1
      renderer/shader/chunkMaterial.go
  6. 4 8
      renderer/shader/shaderPoint.go

+ 1 - 1
gls/gls.go

@@ -113,7 +113,7 @@ func (gs *GLS) SetCheckErrors(enable bool) {
 	if enable {
 		C.glapiCheckError(1)
 	} else {
-		C.glapiCheckError(1)
+		C.glapiCheckError(0)
 	}
 	gs.checkErrors = enable
 }

+ 3 - 0
material/phong.go

@@ -8,6 +8,9 @@ import (
 	"github.com/g3n/engine/math32"
 )
 
+// Phong material is identical to the Standard material but
+// the calculation of the lighting model is done in the
+// fragment shader.
 type Phong struct {
 	Standard // Embedded standard material
 }

+ 15 - 42
material/point.go

@@ -9,37 +9,21 @@ import (
 	"github.com/g3n/engine/math32"
 )
 
+// Point material is normally used for single point sprites
 type Point struct {
-	Material                // Embedded base material
-	emissive  gls.Uniform3f // point emissive uniform
-	size      gls.Uniform1f // point size uniform
-	opacity   gls.Uniform1f // point opacity uniform
-	rotationZ gls.Uniform1f // point z rotation
+	Standard // Embedded standard material
 }
 
 // NewPoint creates and returns a pointer to a new point material
 func NewPoint(color *math32.Color) *Point {
 
 	pm := new(Point)
-	pm.Material.Init()
-	pm.SetShader("shaderPoint")
-
-	// Creates color uniform
-	pm.emissive.Init("MatEmissiveColor")
-	pm.emissive.SetColor(color)
-
-	// Creates point size uniform
-	pm.size.Init("PointSize")
-	pm.size.Set(1.0)
-
-	// Creates point opacity uniform
-	pm.opacity.Init("MatOpacity")
-	pm.opacity.Set(1.0)
-
-	// Creates point rotation Z uniform
-	pm.rotationZ.Init("RotationZ")
-	pm.rotationZ.Set(0)
+	pm.Standard.Init("shaderPoint", color)
 
+	// Sets uniform's initial values
+	pm.uni.SetColor(vEmissive, color)
+	pm.uni.SetPos(pSize, 1.0)
+	pm.uni.SetPos(pRotationZ, 0)
 	return pm
 }
 
@@ -47,36 +31,25 @@ func NewPoint(color *math32.Color) *Point {
 // The default is {0,0,0}
 func (pm *Point) SetEmissiveColor(color *math32.Color) {
 
-	pm.emissive.SetColor(color)
-}
-
-// EmissiveColor returns the material current emissive color
-func (pm *Point) EmissiveColor() math32.Color {
-
-	return pm.emissive.GetColor()
+	pm.uni.SetColor(vEmissive, color)
 }
 
+// SetSize sets the point size
 func (pm *Point) SetSize(size float32) {
 
-	pm.size.Set(size)
-}
-
-func (pm *Point) SetOpacity(opacity float32) {
-
-	pm.opacity.Set(opacity)
+	pm.uni.SetPos(pSize, size)
 }
 
+// SetRotationZ sets the point rotation around the Z axis.
 func (pm *Point) SetRotationZ(rot float32) {
 
-	pm.rotationZ.Set(rot)
+	pm.uni.SetPos(pRotationZ, rot)
 }
 
+// RenderSetup is called by the engine before drawing the object
+// which uses this material
 func (pm *Point) RenderSetup(gs *gls.GLS) {
 
 	pm.Material.RenderSetup(gs)
-
-	pm.emissive.Transfer(gs)
-	pm.size.Transfer(gs)
-	pm.opacity.Transfer(gs)
-	pm.rotationZ.Transfer(gs)
+	pm.uni.Transfer(gs)
 }

+ 12 - 37
material/standard.go

@@ -9,15 +9,12 @@ import (
 	"github.com/g3n/engine/math32"
 )
 
+// Standard material supports the classic lighting model with
+// ambient, diffuse, specular and emissive lights.
+// The lighting calculation is implemented in the vertex shader.
 type Standard struct {
 	Material                 // Embedded material
 	uni      *gls.Uniform3fv // Uniform array of 3 floats with material properties
-	//ambient   *gls.Uniform3f // Ambient color uniform
-	//diffuse   *gls.Uniform3f // Diffuse color uniform
-	//specular  *gls.Uniform3f // Specular color uniform
-	//emissive  *gls.Uniform3f // Emissive color uniform
-	//shininess *gls.Uniform1f // Shininess exponent uniform
-	//opacity   *gls.Uniform1f // Opacity (alpha)uniform
 }
 
 const (
@@ -26,8 +23,10 @@ const (
 	vSpecular  = 2              // index for Specular color in uniform array
 	vEmissive  = 3              // index for Emissive color in uniform array
 	pShininess = vEmissive * 4  // position for material shininess in uniform array
-	pOpacity   = pShininess + 1 // position for material opacitiy in uniform array
-	uniSize    = 5              // total count of groups 3 floats in uniform
+	pOpacity   = pShininess + 1 // position for material opacity in uniform array
+	pSize      = pOpacity + 1   // position for material point size
+	pRotationZ = pSize + 1      // position for material point rotation
+	uniSize    = 6              // total count of groups 3 floats in uniform
 )
 
 // NewStandard creates and returns a pointer to a new standard material
@@ -38,34 +37,21 @@ func NewStandard(color *math32.Color) *Standard {
 	return ms
 }
 
+// Init initializes the material setting the specified shader and color
+// It is used mainly when the material is embedded in another type
 func (ms *Standard) Init(shader string, color *math32.Color) {
 
 	ms.Material.Init()
 	ms.SetShader(shader)
 
-	// Creates uniforms and adds to material
+	// Creates uniforms and set initial values
 	ms.uni = gls.NewUniform3fv("Material", uniSize)
-	//ms.emissive = gls.NewUniform3f("MatEmissiveColor")
-	//ms.ambient = gls.NewUniform3f("MatAmbientColor")
-	//ms.diffuse = gls.NewUniform3f("MatDiffuseColor")
-	//ms.specular = gls.NewUniform3f("MatSpecularColor")
-	//ms.shininess = gls.NewUniform1f("MatShininess")
-	//ms.opacity = gls.NewUniform1f("MatOpacity")
-
-	// Set initial values
 	ms.uni.SetColor(vAmbient, color)
 	ms.uni.SetColor(vDiffuse, color)
 	ms.uni.Set(vSpecular, 0.5, 0.5, 0.5)
 	ms.uni.Set(vEmissive, 0, 0, 0)
 	ms.uni.SetPos(pShininess, 30.0)
 	ms.uni.SetPos(pOpacity, 1.0)
-
-	//ms.ambient.SetColor(color)
-	//ms.diffuse.SetColor(color)
-	//ms.emissive.Set(0, 0, 0)
-	//ms.specular.Set(0.5, 0.5, 0.5)
-	//ms.shininess.Set(30.0)
-	//ms.opacity.Set(1.0)
 }
 
 // AmbientColor returns the material ambient color reflectivity.
@@ -87,8 +73,6 @@ func (ms *Standard) SetColor(color *math32.Color) {
 
 	ms.uni.SetColor(vDiffuse, color)
 	ms.uni.SetColor(vAmbient, color)
-	//	ms.diffuse.SetColor(color)
-	//	ms.ambient.SetColor(color)
 }
 
 // SetEmissiveColor sets the material emissive color
@@ -96,14 +80,12 @@ func (ms *Standard) SetColor(color *math32.Color) {
 func (ms *Standard) SetEmissiveColor(color *math32.Color) {
 
 	ms.uni.SetColor(vEmissive, color)
-	//	ms.emissive.SetColor(color)
 }
 
 // EmissiveColor returns the material current emissive color
 func (ms *Standard) EmissiveColor() math32.Color {
 
 	return ms.uni.GetColor(vEmissive)
-	//return ms.emissive.GetColor()
 }
 
 // SetSpecularColor sets the material specular color reflectivity.
@@ -111,13 +93,11 @@ func (ms *Standard) EmissiveColor() math32.Color {
 func (ms *Standard) SetSpecularColor(color *math32.Color) {
 
 	ms.uni.SetColor(vSpecular, color)
-	//ms.specular.SetColor(color)
 }
 
 // SetShininess sets the specular highlight factor. Default is 30.
 func (ms *Standard) SetShininess(shininess float32) {
 
-	//ms.shininess.Set(shininess)
 	ms.uni.SetPos(pShininess, shininess)
 }
 
@@ -125,17 +105,12 @@ func (ms *Standard) SetShininess(shininess float32) {
 func (ms *Standard) SetOpacity(opacity float32) {
 
 	ms.uni.SetPos(pOpacity, opacity)
-	//ms.opacity.Set(opacity)
 }
 
+// RenderSetup is called by the engine before drawing the object
+// which uses this material
 func (ms *Standard) RenderSetup(gs *gls.GLS) {
 
 	ms.Material.RenderSetup(gs)
 	ms.uni.Transfer(gs)
-	//ms.emissive.Transfer(gs)
-	//ms.ambient.Transfer(gs)
-	//ms.diffuse.Transfer(gs)
-	//ms.specular.Transfer(gs)
-	//ms.shininess.Transfer(gs)
-	//ms.opacity.Transfer(gs)
 }

+ 3 - 1
renderer/shader/chunkMaterial.go

@@ -10,7 +10,7 @@ func init() {
 
 const chunkMaterial = `
 // Material uniforms
-uniform vec3	Material[5];
+uniform vec3	Material[6];
 
 // Macros to access elements inside the Material uniform array
 #define MatAmbientColor		Material[0]
@@ -19,6 +19,8 @@ uniform vec3	Material[5];
 #define MatEmissiveColor	Material[3]
 #define MatShininess		Material[4].x
 #define MatOpacity			Material[4].y
+#define MatPointSize		Material[4].z
+#define MatPointRotationZ	Material[5].x
 
 {{if .MatTexturesMax}}
 // Textures uniforms

+ 4 - 8
renderer/shader/shaderPoint.go

@@ -24,10 +24,6 @@ uniform mat4 MVP;
 // Material uniforms
 {{template "material" .}}
 
-// Specific uniforms
-uniform float PointSize;
-uniform float RotationZ;
-
 // Outputs for fragment shader
 out vec3 Color;
 flat out mat2 Rotation;
@@ -35,8 +31,8 @@ flat out mat2 Rotation;
 void main() {
 
     // Rotation matrix for fragment shader
-    float rotSin = sin(RotationZ);
-    float rotCos = cos(RotationZ);
+    float rotSin = sin(MatPointRotationZ);
+    float rotCos = cos(MatPointRotationZ);
     Rotation = mat2(rotCos, rotSin, - rotSin, rotCos);
 
     // Sets the vertex position
@@ -44,7 +40,7 @@ void main() {
     gl_Position = pos;
 
     // Sets the size of the rasterized point decreasing with distance
-    gl_PointSize = (1.0 - pos.z / pos.w) * PointSize;
+    gl_PointSize = (1.0 - pos.z / pos.w) * MatPointSize;
 
     // Outputs color
     Color = MatEmissiveColor;
@@ -73,7 +69,7 @@ void main() {
     {{ range loop .MatTexturesMax }}
     {
         vec2 pt = gl_PointCoord - vec2(0.5);
-        vec4 texcolor = texture(MatTexture[{{.}}], (Rotation * pt + vec2(0.5)) * MatTexinfo[{{.}}]TexRepeat + MatTexinfo[{{.}}]TexOffset);
+        vec4 texcolor = texture(MatTexture[{{.}}], (Rotation * pt + vec2(0.5)) * MatTexRepeat({{.}}) + MatTexOffset({{.}}));
         if ({{.}} == 0) {
             texCombined = texcolor;
         } else {