- 最后登录
- 2013-9-29
- 注册时间
- 2012-8-20
- 阅读权限
- 90
- 积分
- 6371
- 纳金币
- 6372
- 精华
- 0
|
理论根据:
假设一个光的方向是(-1,-1,-1) , 投影到XZ平面
一个是直线方程,一个是平面方程,求交
而且平面方程还比较非凡,经过原点,法向量是 0 1 0
简化后就简单了, 假定v是直线的方向
x - vertex.x y - vertex.y z-vertex.z
---------------- = --------------- = -------------- 直线方程
v.x v.y v.z
平面方程 y = 0
带入就得到了
x = vertex.x + v.x / v.y * (-vertex.y)
z = vertex.z + v.x / v.z * (-vertex.z)
源程序:
import Java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.Label;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class SimpleShadow extends Applet{
//三角平面类
public class TriPlane extends Shape3D{
TriPlane(Point3f A,Point3f B,Point3f C){
this.setGeometry(this.createGeometry3(A,B,C));
this.setAppearance(this.createAppearance());
}
//建立三角平面
Geometry createGeometry3(Point3f A,Point3f B,Point3f C){
TriangleArray plane=new TriangleArray(3,GeometryArray.COORDINATESGeometryArray.NORMALS);
//设置平面3个顶点的坐标
plane.setCoordinate(0,A);
plane.setCoordinate(1,B);
plane.setCoordinate(2,C);
//计算平面法向量
Vector3f a=new Vector3f(A.x-B.x,A.y-B.y,A.z-B.z);
Vector3f b=new Vector3f(C.x-B.x,C.y-B.y,C.z-B.z);
Vector3f n=new Vector3f();
n.cross(b,a);
//法向量单位化
n.normalize();
//设置平面3个顶点的法向量
plane.setNormal(0,n);
plane.setNormal(1,n);
plane.setNormal(2,n);
return plane;
}
//创建Material不为空的外观
Appearance createAppearance(){
Appearance appear=new Appearance();
Material material=new Material();
appear.setMaterial(material);
return appear;
}
}
//四边平面类
public class QuadPlane extends Shape3D{
QuadPlane(Point3f A,Point3f B,Point3f C,Point3f D){
this.setGeometry(this.createGeometry4(A,B,C,D));
this.setAppearance(this.createAppearance());
}
//创建四边性平面
Geometry createGeometry4(Point3f A,Point3f B,Point3f C,Point3f D){
QuadArray plane=new QuadArray(4,GeometryArray.COORDINATESGeometryArray.NORMALS);
//设置平面3个顶点的坐标
plane.setCoordinate(0,A);
plane.setCoordinate(1,B);
plane.setCoordinate(2,C);
plane.setCoordinate(3,D);
//计算平面法向量
Vector3f a=new Vector3f(A.x-B.x,A.y-B.y,A.z-B.z);
Vector3f b=new Vector3f(C.x-B.x,C.y-B.y,C.z-B.z);
Vector3f n=new Vector3f();
n.cross(b,a);
//法向量单位化
n.normalize();
//设置平面4个顶点的法向量
plane.setNormal(0,n);
plane.setNormal(1,n);
plane.setNormal(2,n);
plane.setNormal(3,n);
return plane;
}
|
|