|
|
@@ -7,27 +7,29 @@ package material
|
|
|
import (
|
|
|
"github.com/g3n/engine/gls"
|
|
|
"github.com/g3n/engine/math32"
|
|
|
+ "unsafe"
|
|
|
)
|
|
|
|
|
|
// 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
|
|
|
+ Material // Embedded material
|
|
|
+ uni gls.Uniform2 // Uniform location cache
|
|
|
+ udata struct { // Combined uniform data in 6 vec3:
|
|
|
+ ambient math32.Color // Ambient color reflectivity
|
|
|
+ diffuse math32.Color // Diffuse color reflectivity
|
|
|
+ specular math32.Color // Specular color reflectivity
|
|
|
+ emissive math32.Color // Emissive color
|
|
|
+ shininess float32 // Specular shininess factor
|
|
|
+ opacity float32 // Opacity
|
|
|
+ psize float32 // Point size
|
|
|
+ protationZ float32 // Point rotation around Z axis
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-const (
|
|
|
- vAmbient = 0 // index for Ambient color in uniform array
|
|
|
- vDiffuse = 1 // index for Diffuse color in uniform array
|
|
|
- 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 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
|
|
|
-)
|
|
|
+// Number of glsl shader vec3 elements used by uniform data
|
|
|
+const udataVec3 = 6
|
|
|
|
|
|
// NewStandard creates and returns a pointer to a new standard material
|
|
|
func NewStandard(color *math32.Color) *Standard {
|
|
|
@@ -45,66 +47,65 @@ func (ms *Standard) Init(shader string, color *math32.Color) {
|
|
|
ms.SetShader(shader)
|
|
|
|
|
|
// Creates uniforms and set initial values
|
|
|
- ms.uni = gls.NewUniform3fv("Material", uniSize)
|
|
|
- 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.uni.Init("Material")
|
|
|
+ ms.SetColor(color)
|
|
|
+ ms.SetSpecularColor(&math32.Color{0.5, 0.5, 0.5})
|
|
|
+ ms.SetEmissiveColor(&math32.Color{0, 0, 0})
|
|
|
+ ms.SetShininess(30.0)
|
|
|
+ ms.SetOpacity(1.0)
|
|
|
}
|
|
|
|
|
|
// AmbientColor returns the material ambient color reflectivity.
|
|
|
func (ms *Standard) AmbientColor() math32.Color {
|
|
|
|
|
|
- return ms.uni.GetColor(vAmbient)
|
|
|
+ return ms.udata.ambient
|
|
|
}
|
|
|
|
|
|
// SetAmbientColor sets the material ambient color reflectivity.
|
|
|
// The default is the same as the diffuse color
|
|
|
func (ms *Standard) SetAmbientColor(color *math32.Color) {
|
|
|
|
|
|
- ms.uni.SetColor(vAmbient, color)
|
|
|
+ ms.udata.ambient = *color
|
|
|
}
|
|
|
|
|
|
// SetColor sets the material diffuse color and also the
|
|
|
// material ambient color reflectivity
|
|
|
func (ms *Standard) SetColor(color *math32.Color) {
|
|
|
|
|
|
- ms.uni.SetColor(vDiffuse, color)
|
|
|
- ms.uni.SetColor(vAmbient, color)
|
|
|
+ ms.udata.diffuse = *color
|
|
|
+ ms.udata.ambient = *color
|
|
|
}
|
|
|
|
|
|
// SetEmissiveColor sets the material emissive color
|
|
|
// The default is {0,0,0}
|
|
|
func (ms *Standard) SetEmissiveColor(color *math32.Color) {
|
|
|
|
|
|
- ms.uni.SetColor(vEmissive, color)
|
|
|
+ ms.udata.emissive = *color
|
|
|
}
|
|
|
|
|
|
// EmissiveColor returns the material current emissive color
|
|
|
func (ms *Standard) EmissiveColor() math32.Color {
|
|
|
|
|
|
- return ms.uni.GetColor(vEmissive)
|
|
|
+ return ms.udata.emissive
|
|
|
}
|
|
|
|
|
|
// SetSpecularColor sets the material specular color reflectivity.
|
|
|
// The default is {0.5, 0.5, 0.5}
|
|
|
func (ms *Standard) SetSpecularColor(color *math32.Color) {
|
|
|
|
|
|
- ms.uni.SetColor(vSpecular, color)
|
|
|
+ ms.udata.specular = *color
|
|
|
}
|
|
|
|
|
|
// SetShininess sets the specular highlight factor. Default is 30.
|
|
|
func (ms *Standard) SetShininess(shininess float32) {
|
|
|
|
|
|
- ms.uni.SetPos(pShininess, shininess)
|
|
|
+ ms.udata.shininess = shininess
|
|
|
}
|
|
|
|
|
|
// SetOpacity sets the material opacity (alpha). Default is 1.0.
|
|
|
func (ms *Standard) SetOpacity(opacity float32) {
|
|
|
|
|
|
- ms.uni.SetPos(pOpacity, opacity)
|
|
|
+ ms.udata.opacity = opacity
|
|
|
}
|
|
|
|
|
|
// RenderSetup is called by the engine before drawing the object
|
|
|
@@ -112,5 +113,6 @@ func (ms *Standard) SetOpacity(opacity float32) {
|
|
|
func (ms *Standard) RenderSetup(gs *gls.GLS) {
|
|
|
|
|
|
ms.Material.RenderSetup(gs)
|
|
|
- ms.uni.Transfer(gs)
|
|
|
+ location := ms.uni.Location(gs)
|
|
|
+ gs.Uniform3fvUP(location, udataVec3, unsafe.Pointer(&ms.udata))
|
|
|
}
|