理论根据:
假设一个光的方向是() 投影到XZ平面
一个是直线方程一个是平面方程求交
而且平面方程还比较特殊经过原点法向量是
简化后就简单了 假定v是直线的方向
x vertexx y vertexy zvertexz
= = 直线方程
vx vy vz
平面方程 y =
带入就得到了
x = vertexx + vx / vy * (vertexy)
z = vertexz + vx / vz * (vertexz)
源程序:
import javaappletApplet;
import javaawtBorderLayout;
import javaawtGraphicsConfiguration;
import javaawtLabel;
import comsunjdutilsappletMainFrame;
import comsunjdutilsuniverse*;
import comsunjdutilsgeometry*;
import diajd*;
import javaxvecmath*;
public class SimpleShadow extends Applet{
//三角平面类
public class TriPlane extends ShapeD{
TriPlane(Pointf APointf BPointf C){
thissetGeometry(thiscreateGeometry(ABC));
thissetAppearance(thiscreateAppearance());
}
//建立三角平面
Geometry createGeometry(Pointf APointf BPointf C){
TriangleArray plane=new TriangleArray(GeometryArrayCOORDINATES|GeometryArrayNORMALS);
//设置平面个顶点的坐标
planesetCoordinate(A);
planesetCoordinate(B);
planesetCoordinate(C);
//计算平面法向量
Vectorf a=new Vectorf(AxBxAyByAzBz);
Vectorf b=new Vectorf(CxBxCyByCzBz);
Vectorf n=new Vectorf();
ncross(ba);
//法向量单位化
nnormalize();
//设置平面个顶点的法向量
planesetNormal(n);
planesetNormal(n);
planesetNormal(n);
return plane;
}
//创建Material不为空的外观
Appearance createAppearance(){
Appearance appear=new Appearance();
Material material=new Material();
appearsetMaterial(material);
return appear;
}
}
//四边平面类
public class QuadPlane extends ShapeD{
QuadPlane(Pointf APointf BPointf CPointf D){
thissetGeometry(thiscreateGeometry(ABCD));
thissetAppearance(thiscreateAppearance());
}
//创建四边性平面
Geometry createGeometry(Pointf APointf BPointf CPointf D){
QuadArray plane=new QuadArray(GeometryArrayCOORDINATES|GeometryArrayNORMALS);
//设置平面个顶点的坐标
planesetCoordinate(A);
planesetCoordinate(B);
planesetCoordinate(C);
planesetCoordinate(D);
//计算平面法向量
Vectorf a=new Vectorf(AxBxAyByAzBz);
Vectorf b=new Vectorf(CxBxCyByCzBz);
Vectorf n=new Vectorf();
ncross(ba);
//法向量单位化
nnormalize();
//设置平面个顶点的法向量
planesetNormal(n);
planesetNormal(n);
planesetNormal(n);
planesetNormal(n);
return plane;
}
//创建Material不为空的外观
Appearance createAppearance(){
Appearance appear=new Appearance();
Material material=new Material();
appearsetMaterial(material);
return appear;
}
}
//阴影类
public class ShadowD extends ShapeD{
ShadowD(GeometryArray geomVectorf directionColorf colfloat height){
int vCount=geomgetVertexCount();
TriangleArray poly=new TriangleArray(vCountGeometryArrayCOORDINATES|GeometryArrayCOLOR_);
int v;
Pointf vertex=new Pointf();
Pointf shadow=new Pointf();
for(v=;v<vCount;v++){
//计算可见面顶点在投影面上的投影坐标
geomgetCoordinate(vvertex);
Systemoutprintln(vertexyheight);
shadowset(vertexx(vertexyheight)height+fvertexz(vertexyheight));
polysetCoordinate(vshadow);
polysetColor(vcol);
}
thissetGeometry(poly);
}
}
public SimpleShadow(){
thissetLayout(new BorderLayout());
//GraphicsConfiguration config =SimpleUniversegetPreferredConfiguration();
CanvasD c=new CanvasD(null);
thisadd(Centerc);
//创建分支节点
BranchGroup scene=new BranchGroup();
//创建三角形可见平面
ShapeD plane=new TriPlane(new Pointf(fff)new Pointf(fff)new Pointf(fff));
//添加到根分支节点
sceneaddChild(plane);
//创建四边形投影平面
ShapeD floor=new QuadPlane(new Pointf(fff)new Pointf(fff)new Pointf(fff)new Pointf(fff));
//添加到根分支节点
sceneaddChild(floor);
//添加到环境光
AmbientLight lightA=new AmbientLight();
lightAsetInfluencingBounds(new BoundingSphere());
sceneaddChild(lightA);
//添加平行光
DirectionalLight lightD=new DirectionalLight();
lightDsetInfluencingBounds(new BoundingSphere());
Vectorf direction=new Vectorf(fff);
//方向矢量单位化
directionnormalize();
lightDsetDirection(direction);
sceneaddChild(lightD);
//创建阴影物体
ShapeD shadow=new ShadowD((GeometryArray)planegetGeometry()directionnew Colorf(fff)f);
sceneaddChild(shadow);
SimpleUniverse u=new SimpleUniverse(c);
ugetViewingPlatform()setNominalViewingTransform();
uaddBranchGraph(scene);
}
public static void main(String argv[]){
new MainFrame(new SimpleShadow());
}
}