Explorar o código

consider material UseLights flags when selecting shader program

leonsal %!s(int64=8) %!d(string=hai) anos
pai
achega
95b49ab6f2
Modificáronse 2 ficheiros con 46 adicións e 26 borrados
  1. 11 9
      material/material.go
  2. 35 17
      renderer/shaman.go

+ 11 - 9
material/material.go

@@ -36,11 +36,12 @@ const (
 type UseLights int
 
 const (
-	UseLightAmbient     UseLights = 0
-	UseLightDirectional UseLights = 1
-	UseLightPoint       UseLights = 2
-	UseLightSpot        UseLights = 3
-	UseLightAll         UseLights = 4
+	UseLightNone        UseLights = 0x00
+	UseLightAmbient     UseLights = 0x01
+	UseLightDirectional UseLights = 0x02
+	UseLightPoint       UseLights = 0x04
+	UseLightSpot        UseLights = 0x08
+	UseLightAll         UseLights = 0xFF
 )
 
 // Interface for all materials
@@ -84,7 +85,7 @@ func NewMaterial() *Material {
 
 func (mat *Material) Init() *Material {
 
-    mat.refcount = 1
+	mat.refcount = 1
 	mat.uselights = UseLightAll
 	mat.sidevis = SideFront
 	mat.wireframe = false
@@ -125,9 +126,9 @@ func (mat *Material) Dispose() {
 		mat.refcount--
 		return
 	}
-    for i := 0; i < len(mat.textures); i++ {
-        mat.textures[i].Dispose()
-    }
+	for i := 0; i < len(mat.textures); i++ {
+		mat.textures[i].Dispose()
+	}
 	mat.Init()
 }
 
@@ -145,6 +146,7 @@ func (mat *Material) Shader() string {
 
 // SetUseLights sets the material use lights bit mask specifying which
 // light types will be used when rendering the material
+// By default the material will use all lights
 func (mat *Material) SetUseLights(lights UseLights) {
 
 	mat.uselights = lights

+ 35 - 17
renderer/shaman.go

@@ -15,14 +15,14 @@ import (
 )
 
 type ShaderSpecs struct {
-	Name             string // Shader name
-	Version          string // GLSL version
-	UseLights        material.UseLights
-	AmbientLightsMax int // Current number of ambient lights
-	DirLightsMax     int // Current Number of directional lights
-	PointLightsMax   int // Current Number of point lights
-	SpotLightsMax    int // Current Number of spot lights
-	MatTexturesMax   int // Current Number of material textures
+	Name             string             // Shader name
+	Version          string             // GLSL version
+	UseLights        material.UseLights // Bitmask indicating which lights to consider
+	AmbientLightsMax int                // Current number of ambient lights
+	DirLightsMax     int                // Current Number of directional lights
+	PointLightsMax   int                // Current Number of point lights
+	SpotLightsMax    int                // Current Number of spot lights
+	MatTexturesMax   int                // Current Number of material textures
 }
 
 type ProgSpecs struct {
@@ -124,26 +124,44 @@ func (sm *Shaman) AddProgram(name, vertexName, fragName string) error {
 	return nil
 }
 
-// SetShader set the shader to satify the specified specs
-// Returns an indication if the current shader has changed and an error
-func (sm *Shaman) SetProgram(specs *ShaderSpecs) (bool, error) {
+// SetProgram set the shader program to satisfy the specified specs.
+// Returns an indication if the current shader has changed and a possible error
+// when creating a new shader program.
+// Receives a copy of the specs because it changes the fields which specify the
+// number of lights depending on the UseLights flags.
+func (sm *Shaman) SetProgram(s *ShaderSpecs) (bool, error) {
+
+	// Checks material use lights bit mask
+	specs := *s
+	if (specs.UseLights & material.UseLightAmbient) == 0 {
+		specs.AmbientLightsMax = 0
+	}
+	if (specs.UseLights & material.UseLightDirectional) == 0 {
+		specs.DirLightsMax = 0
+	}
+	if (specs.UseLights & material.UseLightPoint) == 0 {
+		specs.PointLightsMax = 0
+	}
+	if (specs.UseLights & material.UseLightSpot) == 0 {
+		specs.SpotLightsMax = 0
+	}
 
 	// If current shader specs are the same as the specified specs, nothing to do.
-	if sm.specs.Compare(specs) {
+	if sm.specs.Compare(&specs) {
 		return false, nil
 	}
 
 	// Search for compiled program with the specified specs
 	for _, pinfo := range sm.programs {
-		if pinfo.specs.Compare(specs) {
+		if pinfo.specs.Compare(&specs) {
 			sm.gs.UseProgram(pinfo.program)
-			sm.specs = *specs
+			sm.specs = specs
 			return true, nil
 		}
 	}
 
 	// Generates new program with the specified specs
-	prog, err := sm.GenProgram(specs)
+	prog, err := sm.GenProgram(&specs)
 	if err != nil {
 		return false, err
 	}
@@ -151,8 +169,8 @@ func (sm *Shaman) SetProgram(specs *ShaderSpecs) (bool, error) {
 
 	// Save specs as current specs, adds new program to the list
 	// and actives program
-	sm.specs = *specs
-	sm.programs = append(sm.programs, ProgSpecs{prog, *specs})
+	sm.specs = specs
+	sm.programs = append(sm.programs, ProgSpecs{prog, specs})
 	sm.gs.UseProgram(prog)
 	return true, nil
 }