科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件Java3D中平行光投影的实现

Java3D中平行光投影的实现

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文给出利用Java3D来实现平行光投影的理论根据和源程序。

作者:中国IT实验室 来源:中国IT实验室 2007年9月3日

关键字: Java3D 平行光

  • 评论
  • 分享微博
  • 分享邮件

理论根据:

假设一个光的方向是(-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);

}

}

 

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章