理論根據(jù):
假設(shè)一個(gè)光的方向是(-1,-1,-1) , 投影到XZ平面
一個(gè)是直線方程,一個(gè)是平面方程,求交
而且平面方程還比較非凡,經(jīng)過(guò)原點(diǎn),法向量是 0 1 0
簡(jiǎn)化后就簡(jiǎn)單了, 假定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);
//設(shè)置平面3個(gè)頂點(diǎn)的坐標(biāo)
plane.setCoordinate(0,A);
plane.setCoordinate(1,B);
plane.setCoordinate(2,C);
//計(jì)算平面法向量
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();
//設(shè)置平面3個(gè)頂點(diǎn)的法向量
plane.setNormal(0,n);
plane.setNormal(1,n);
plane.setNormal(2,n);
return plane;
}
//創(chuàng)建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());
}
//創(chuàng)建四邊性平面
Geometry createGeometry4(Point3f A,Point3f B,Point3f C,Point3f D){
QuadArray plane=new QuadArray(4,GeometryArray.COORDINATESGeometryArray.NORMALS);
//設(shè)置平面3個(gè)頂點(diǎn)的坐標(biāo)
plane.setCoordinate(0,A);
plane.setCoordinate(1,B);
plane.setCoordinate(2,C);
plane.setCoordinate(3,D);
//計(jì)算平面法向量
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();
//設(shè)置平面4個(gè)頂點(diǎn)的法向量
plane.setNormal(0,n);
plane.setNormal(1,n);
plane.setNormal(2,n);
plane.setNormal(3,n);
return plane;
}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注