程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java3D中平行光投影的實現

Java3D中平行光投影的實現

編輯:關於JAVA

理論根據:

假設一個光的方向是(-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.COORDINATES|GeometryArray.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.COORDINATES|GeometryArray.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;
}
//創建Material不為空的外觀 
Appearance createAppearance(){
Appearance appear=new Appearance();
Material material=new Material();
appear.setMaterial(material);
return appear;
}
}
//陰影類
public class Shadow3D extends Shape3D{
Shadow3D(GeometryArray geom,Vector3f direction,Color3f col,float height){
int vCount=geom.getVertexCount();
TriangleArray poly=new TriangleArray(vCount,
GeometryArray.COORDINATES|GeometryArray.COLOR_3);
int v;
Point3f vertex=new Point3f();
Point3f shadow=new Point3f(); 
for(v=0;v<vCount;v++){
//計算可見面頂點在投影面上的投影坐標
geom.getCoordinate(v,vertex);
System.out.println(vertex.y-height);
shadow.set(vertex.x-(vertex.y-height),height+0.0001f,
vertex.z-(vertex.y-height));
poly.setCoordinate(v,shadow);
poly.setColor(v,col);
}
this.setGeometry(poly);
}
}
public SimpleShadow(){
this.setLayout(new BorderLayout());
//GraphicsConfiguration config =SimpleUniverse.getPreferredConfiguration();
Canvas3D c=new Canvas3D(null);
this.add("Center",c);
//創建分支節點
BranchGroup scene=new BranchGroup();
//創建三角形可見平面
Shape3D plane=new TriPlane(new Point3f(0.0f,0.6f,-0.2f),
new Point3f(-0.3f,-0.3f,0.2f),
new Point3f(0.6f,-0.3f,0.2f));
//添加到根分支節點
scene.addChild(plane);
//創建四邊形投影平面
Shape3D floor=new QuadPlane(new Point3f(-1.0f,-0.5f,-1.0f),
new Point3f(-1.0f,-0.5f,1.0f),new Point3f(1.0f,-0.5f,1.0f),
new Point3f(1.0f,-0.5f,-1.0f));
//添加到根分支節點
scene.addChild(floor);
//添加到環境光
AmbientLight lightA=new AmbientLight();
lightA.setInfluencingBounds(new BoundingSphere());
scene.addChild(lightA);
//添加平行光
DirectionalLight lightD1=new DirectionalLight();
lightD1.setInfluencingBounds(new BoundingSphere());
Vector3f direction=new Vector3f(-1.0f,-1.0f,-1.0f);
//方向矢量單位化
direction.normalize();
lightD1.setDirection(direction);
scene.addChild(lightD1);
//創建陰影物體
Shape3D shadow=new Shadow3D((GeometryArray)plane.getGeometry(),direction,
new Color3f(0.2f,0.2f,0.2f),-0.5f);
scene.addChild(shadow);
SimpleUniverse u=new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public static void main(String argv[]){
new MainFrame(new SimpleShadow(),256,256);
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved