Lights (see #362) : added light v. sphere intersection, and implementations of intersectsSphere(), second attempt
This commit is contained in:
parent
3412c0cf2b
commit
cfdb9a8759
@ -32,6 +32,7 @@
|
|||||||
package com.jme3.light;
|
package com.jme3.light;
|
||||||
|
|
||||||
import com.jme3.bounding.BoundingBox;
|
import com.jme3.bounding.BoundingBox;
|
||||||
|
import com.jme3.bounding.BoundingSphere;
|
||||||
import com.jme3.math.ColorRGBA;
|
import com.jme3.math.ColorRGBA;
|
||||||
import com.jme3.math.Vector3f;
|
import com.jme3.math.Vector3f;
|
||||||
import com.jme3.renderer.Camera;
|
import com.jme3.renderer.Camera;
|
||||||
@ -62,6 +63,11 @@ public class AmbientLight extends Light {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean intersectsSphere(BoundingSphere sphere, TempVars vars) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean intersectsFrustum(Camera camera, TempVars vars) {
|
public boolean intersectsFrustum(Camera camera, TempVars vars) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2009-2014 jMonkeyEngine
|
* Copyright (c) 2009-2015 jMonkeyEngine
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
@ -78,8 +78,9 @@ public final class DefaultLightFilter implements LightFilter {
|
|||||||
}
|
}
|
||||||
} else if (bv instanceof BoundingSphere) {
|
} else if (bv instanceof BoundingSphere) {
|
||||||
if (!Float.isInfinite( ((BoundingSphere)bv).getRadius() )) {
|
if (!Float.isInfinite( ((BoundingSphere)bv).getRadius() )) {
|
||||||
// Non-infinite bounding sphere... Not supported yet.
|
if (!light.intersectsSphere((BoundingSphere)bv, vars)) {
|
||||||
throw new UnsupportedOperationException("Only AABB supported for now");
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
package com.jme3.light;
|
package com.jme3.light;
|
||||||
|
|
||||||
import com.jme3.bounding.BoundingBox;
|
import com.jme3.bounding.BoundingBox;
|
||||||
|
import com.jme3.bounding.BoundingSphere;
|
||||||
import com.jme3.export.InputCapsule;
|
import com.jme3.export.InputCapsule;
|
||||||
import com.jme3.export.JmeExporter;
|
import com.jme3.export.JmeExporter;
|
||||||
import com.jme3.export.JmeImporter;
|
import com.jme3.export.JmeImporter;
|
||||||
@ -116,6 +117,11 @@ public class DirectionalLight extends Light {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean intersectsSphere(BoundingSphere sphere, TempVars vars) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean intersectsFrustum(Camera camera, TempVars vars) {
|
public boolean intersectsFrustum(Camera camera, TempVars vars) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
package com.jme3.light;
|
package com.jme3.light;
|
||||||
|
|
||||||
import com.jme3.bounding.BoundingBox;
|
import com.jme3.bounding.BoundingBox;
|
||||||
|
import com.jme3.bounding.BoundingSphere;
|
||||||
import com.jme3.export.*;
|
import com.jme3.export.*;
|
||||||
import com.jme3.math.ColorRGBA;
|
import com.jme3.math.ColorRGBA;
|
||||||
import com.jme3.renderer.Camera;
|
import com.jme3.renderer.Camera;
|
||||||
@ -196,6 +197,20 @@ public abstract class Light implements Savable, Cloneable {
|
|||||||
*/
|
*/
|
||||||
public abstract boolean intersectsBox(BoundingBox box, TempVars vars);
|
public abstract boolean intersectsBox(BoundingBox box, TempVars vars);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines if the light intersects with the given bounding sphere.
|
||||||
|
* <p>
|
||||||
|
* For non-local lights, such as {@link DirectionalLight directional lights},
|
||||||
|
* {@link AmbientLight ambient lights}, or {@link PointLight point lights}
|
||||||
|
* without influence radius, this method should always return true.
|
||||||
|
*
|
||||||
|
* @param sphere The sphere to check intersection against.
|
||||||
|
* @param vars TempVars in case it is needed.
|
||||||
|
*
|
||||||
|
* @return True if the light intersects the sphere, false otherwise.
|
||||||
|
*/
|
||||||
|
public abstract boolean intersectsSphere(BoundingSphere sphere, TempVars vars);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines if the light intersects with the given camera frustum.
|
* Determines if the light intersects with the given camera frustum.
|
||||||
*
|
*
|
||||||
|
@ -32,12 +32,14 @@
|
|||||||
package com.jme3.light;
|
package com.jme3.light;
|
||||||
|
|
||||||
import com.jme3.bounding.BoundingBox;
|
import com.jme3.bounding.BoundingBox;
|
||||||
|
import com.jme3.bounding.BoundingSphere;
|
||||||
import com.jme3.bounding.BoundingVolume;
|
import com.jme3.bounding.BoundingVolume;
|
||||||
import com.jme3.bounding.Intersection;
|
import com.jme3.bounding.Intersection;
|
||||||
import com.jme3.export.InputCapsule;
|
import com.jme3.export.InputCapsule;
|
||||||
import com.jme3.export.JmeExporter;
|
import com.jme3.export.JmeExporter;
|
||||||
import com.jme3.export.JmeImporter;
|
import com.jme3.export.JmeImporter;
|
||||||
import com.jme3.export.OutputCapsule;
|
import com.jme3.export.OutputCapsule;
|
||||||
|
import com.jme3.math.FastMath;
|
||||||
import com.jme3.math.ColorRGBA;
|
import com.jme3.math.ColorRGBA;
|
||||||
import com.jme3.math.Vector3f;
|
import com.jme3.math.Vector3f;
|
||||||
import com.jme3.renderer.Camera;
|
import com.jme3.renderer.Camera;
|
||||||
@ -195,6 +197,16 @@ public class PointLight extends Light {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean intersectsSphere(BoundingSphere sphere, TempVars vars) {
|
||||||
|
if (this.radius == 0) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
// Sphere v. sphere collision
|
||||||
|
return sphere.getCenter().subtract(position).lengthSquared() < FastMath.sqr(radius + sphere.getRadius());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean intersectsFrustum(Camera camera, TempVars vars) {
|
public boolean intersectsFrustum(Camera camera, TempVars vars) {
|
||||||
if (this.radius == 0) {
|
if (this.radius == 0) {
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
package com.jme3.light;
|
package com.jme3.light;
|
||||||
|
|
||||||
import com.jme3.bounding.BoundingBox;
|
import com.jme3.bounding.BoundingBox;
|
||||||
|
import com.jme3.bounding.BoundingSphere;
|
||||||
import com.jme3.bounding.BoundingVolume;
|
import com.jme3.bounding.BoundingVolume;
|
||||||
import com.jme3.export.*;
|
import com.jme3.export.*;
|
||||||
import com.jme3.math.ColorRGBA;
|
import com.jme3.math.ColorRGBA;
|
||||||
@ -225,6 +226,43 @@ public class SpotLight extends Light {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean intersectsSphere(BoundingSphere sphere, TempVars vars) {
|
||||||
|
if (this.spotRange > 0f) {
|
||||||
|
// Check spot range first.
|
||||||
|
// Sphere v. sphere collision
|
||||||
|
if (sphere.getCenter().subtract(position).lengthSquared() >= FastMath.sqr(spotRange + sphere.getRadius())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float otherRadiusSquared = FastMath.sqr(sphere.getRadius());
|
||||||
|
float otherRadius = sphere.getRadius();
|
||||||
|
|
||||||
|
// Check if sphere is within spot angle.
|
||||||
|
// Cone v. sphere collision.
|
||||||
|
Vector3f E = direction.mult(otherRadius * outerAngleSinRcp, vars.vect1);
|
||||||
|
Vector3f U = position.subtract(E, vars.vect2);
|
||||||
|
Vector3f D = sphere.getCenter().subtract(U, vars.vect3);
|
||||||
|
|
||||||
|
float dsqr = D.dot(D);
|
||||||
|
float e = direction.dot(D);
|
||||||
|
|
||||||
|
if (e > 0f && e * e >= dsqr * outerAngleCosSqr) {
|
||||||
|
D = sphere.getCenter().subtract(position, vars.vect3);
|
||||||
|
dsqr = D.dot(D);
|
||||||
|
e = -direction.dot(D);
|
||||||
|
|
||||||
|
if (e > 0f && e * e >= dsqr * outerAngleSinSqr) {
|
||||||
|
return dsqr <= otherRadiusSquared;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean intersectsFrustum(Camera cam, TempVars vars) {
|
public boolean intersectsFrustum(Camera cam, TempVars vars) {
|
||||||
if (spotRange == 0) {
|
if (spotRange == 0) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user