|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
微软什么都提供了。你可以试想一下,如果你是新手,你是希望你点一下按钮程序就能运行那,还是想自己一点一点的组织结构,然后打包发部,调错再打包......
实际依据:
假定一个光的偏向是(-1,-1,-1),投影到XZ立体
一个是直线方程,一个是立体方程,求交
并且立体方程还对照特别,经由原点,法向量是010
简化后就复杂了,假定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)
源程序:
importjava.applet.Applet;
importjava.awt.BorderLayout;
importjava.awt.GraphicsConfiguration;
importjava.awt.Label;
importcom.sun.j3d.utils.applet.MainFrame;
importcom.sun.j3d.utils.universe.*;
importcom.sun.j3d.utils.geometry.*;
importjavax.media.j3d.*;
importjavax.vecmath.*;
publicclassSimpleShadowextendsApplet{
//三角立体类
publicclassTriPlaneextendsShape3D{
TriPlane(Point3fA,Point3fB,Point3fC){
this.setGeometry(this.createGeometry3(A,B,C));
this.setAppearance(this.createAppearance());
}
//创建三角立体
GeometrycreateGeometry3(Point3fA,Point3fB,Point3fC){
TriangleArrayplane=newTriangleArray(3,
GeometryArray.COORDINATES|GeometryArray.NORMALS);
//设置立体3个极点的坐标
plane.setCoordinate(0,A);
plane.setCoordinate(1,B);
plane.setCoordinate(2,C);
//盘算立体法向量
Vector3fa=newVector3f(A.x-B.x,A.y-B.y,A.z-B.z);
Vector3fb=newVector3f(C.x-B.x,C.y-B.y,C.z-B.z);
Vector3fn=newVector3f();
n.cross(b,a);
//法向量单元化
n.normalize();
//设置立体3个极点的法向量
plane.setNormal(0,n);
plane.setNormal(1,n);
plane.setNormal(2,n);
returnplane;
}
//创立Material不为空的表面
AppearancecreateAppearance(){
Appearanceappear=newAppearance();
Materialmaterial=newMaterial();
appear.setMaterial(material);
returnappear;
}
}
//四边立体类
publicclassQuadPlaneextendsShape3D{
QuadPlane(Point3fA,Point3fB,Point3fC,Point3fD){
this.setGeometry(this.createGeometry4(A,B,C,D));
this.setAppearance(this.createAppearance());
}
//创立四边性立体
GeometrycreateGeometry4(Point3fA,Point3fB,Point3fC,Point3fD){
QuadArrayplane=newQuadArray(4,GeometryArray.COORDINATES|GeometryArray.NORMALS);
//设置立体3个极点的坐标
plane.setCoordinate(0,A);
plane.setCoordinate(1,B);
plane.setCoordinate(2,C);
plane.setCoordinate(3,D);
//盘算立体法向量
Vector3fa=newVector3f(A.x-B.x,A.y-B.y,A.z-B.z);
Vector3fb=newVector3f(C.x-B.x,C.y-B.y,C.z-B.z);
Vector3fn=newVector3f();
n.cross(b,a);
//法向量单元化
n.normalize();
//设置立体4个极点的法向量
plane.setNormal(0,n);
plane.setNormal(1,n);
plane.setNormal(2,n);
plane.setNormal(3,n);
returnplane;
}
//创立Material不为空的表面
AppearancecreateAppearance(){
Appearanceappear=newAppearance();
Materialmaterial=newMaterial();
appear.setMaterial(material);
returnappear;
}
}
//暗影类
publicclassShadow3DextendsShape3D{
Shadow3D(GeometryArraygeom,Vector3fdirection,Color3fcol,floatheight){
intvCount=geom.getVertexCount();
TriangleArraypoly=newTriangleArray(vCount,
GeometryArray.COORDINATES|GeometryArray.COLOR_3);
intv;
Point3fvertex=newPoint3f();
Point3fshadow=newPoint3f();
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);
}
}
publicSimpleShadow(){
this.setLayout(newBorderLayout());
//GraphicsConfigurationconfig=SimpleUniverse.getPreferredConfiguration();
Canvas3Dc=newCanvas3D(null);
this.add("Center",c);
//创立分支节点
BranchGroupscene=newBranchGroup();
//创立三角形可见立体
Shape3Dplane=newTriPlane(newPoint3f(0.0f,0.6f,-0.2f),
newPoint3f(-0.3f,-0.3f,0.2f),
newPoint3f(0.6f,-0.3f,0.2f));
//增加到根分支节点
scene.addChild(plane);
//创立四边形投影立体
Shape3Dfloor=newQuadPlane(newPoint3f(-1.0f,-0.5f,-1.0f),
newPoint3f(-1.0f,-0.5f,1.0f),newPoint3f(1.0f,-0.5f,1.0f),
newPoint3f(1.0f,-0.5f,-1.0f));
//增加到根分支节点
scene.addChild(floor);
//增加到情况光
AmbientLightlightA=newAmbientLight();
lightA.setInfluencingBounds(newBoundingSphere());
scene.addChild(lightA);
//增加平行光
DirectionalLightlightD1=newDirectionalLight();
lightD1.setInfluencingBounds(newBoundingSphere());
Vector3fdirection=newVector3f(-1.0f,-1.0f,-1.0f);
//偏向矢量单元化
direction.normalize();
lightD1.setDirection(direction);
scene.addChild(lightD1);
//创立暗影物体
Shape3Dshadow=newShadow3D((GeometryArray)plane.getGeometry(),direction,
newColor3f(0.2f,0.2f,0.2f),-0.5f);
scene.addChild(shadow);
SimpleUniverseu=newSimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
publicstaticvoidmain(Stringargv[]){
newMainFrame(newSimpleShadow(),256,256);
}
}
还有就是总有人问我到底该学习什么语言,什么语言有前途,那么我的回答是不论是C,C++,java,.net,ruby,asp或是其他语言都可以学,编程的关键不是语言,而是思想。 |
|