Commits

Michael Ludwig committed 3e87975

Implement light influence, thanks to jME for the inspiration.

Comments (0)

Files changed (2)

src/com/ferox/core/scene/states/Light.java

 	}
 	
 	public float getInfluence(SpatialLeaf leaf) {
-		//TODO: implement
-		return 0;
+		return Math.max(.5f * getColorValue(this.amb), Math.max(.8f * getColorValue(this.diff), .85f * getColorValue(this.spec)));
+	}
+	
+	private static float getColorValue(float[] color) {
+		return color[0] + color[1] + color[2] + color[3];
 	}
 	
 	@Override

src/com/ferox/core/scene/states/SpotLight.java

 
 import org.openmali.vecmath.Vector3f;
 
+import com.ferox.core.scene.SpatialLeaf;
 import com.ferox.core.util.io.InputChunk;
 import com.ferox.core.util.io.OutputChunk;
 
  *
  */
 public class SpotLight extends Light {
+	private static final float RAD_TO_DEGREES = 180f / (float)Math.PI;
+	private static final Vector3f dir = new Vector3f();
+	private static final Vector3f light = new Vector3f();
+	
 	private float constantAttenuation;
 	private float linearAttenuation;
 	private float quadAttenuation;
 		this.spotCutoff = cutoff;
 	}
 	
+	public float getInfluence(SpatialLeaf leaf) {
+		dir.sub(leaf.getWorldTransform().getTranslation(), this.getWorldTransform().getTranslation());
+		float dist = dir.length();
+		if (this.spotCutoff != 180f) {
+			this.getWorldTransform().getRotation().transform(this.getLightDirection(), light);
+			
+			float ang = (float)Math.acos(dir.dot(light) / (dist * light.length())) * RAD_TO_DEGREES;
+			if (ang > this.spotCutoff)
+				return 0f;
+		}
+		
+		float color = super.getInfluence(leaf);
+		return color / (this.constantAttenuation + dist * this.linearAttenuation + dist * dist * this.quadAttenuation);
+	}
+	
 	@Override
 	public void readChunk(InputChunk in) {
 		super.readChunk(in);