⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 displaytriangletest.java

📁 《JAVA 3D 交互式三维图形编程》书中第三章的全部源代码。
💻 JAVA
字号:
import java.applet.Applet;
import java.awt.BorderLayout;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.universe.*;
public class DisplayTriangleTest extends Applet
{
	public BranchGroup createBranchGroup()
	{
		BranchGroup branch=new BranchGroup();
		BoundingSphere bounds=new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);
		Background bg=new Background(new Color3f(1.0f,1.0f,0.0f));
		bg.setApplicationBounds(bounds);
		branch.addChild(bg);
		DirectionalLight light=new DirectionalLight(new Color3f(1.0f,0.0f,0.0f),new Vector3f(0.0f,0.0f,-1.0f));
		light.setInfluencingBounds(bounds);
		branch.addChild(light);
		TransformGroup trans=new TransformGroup();
		trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		branch.addChild(trans);
		MouseRotate rotate=new MouseRotate(trans);
		rotate.setSchedulingBounds(bounds);
		branch.addChild(rotate);
		MouseZoom zoom=new MouseZoom(trans);
		zoom.setSchedulingBounds(bounds);
		branch.addChild(zoom);
		MouseTranslate translate=new MouseTranslate(trans);
		translate.setSchedulingBounds(bounds);
		branch.addChild(translate);
		trans.addChild(new SurfaceDisplay());
		branch.compile();
		return branch;
	}
	public DisplayTriangleTest()
	{
		this.setLayout(new BorderLayout());
		Canvas3D c=new Canvas3D(SimpleUniverse.getPreferredConfiguration());
		this.add(c,"Center");
		SimpleUniverse u=new SimpleUniverse(c);
		u.getViewingPlatform().setNominalViewingTransform();
		u.addBranchGraph(this.createBranchGroup());
	}
	public static void main(String[] args)
	{
		new MainFrame(new DisplayTriangleTest(),400,400);
	}
}
class SurfaceDisplay extends Shape3D
{
	float[][][] SurfacePointxyz=new float[5][51][3];
	public SurfaceDisplay()
	{
		this.setGeometry(this.createGeometry0());
		this.setAppearance(this.createAppearance0());
	}
	Geometry createGeometry0()
	{
		int i,j,k;
		int nn=50;
		float r=0.8f;
		float theta;
		float[] xp0=new float[nn+1];
		float[] yp0=new float[nn+1];
		float[] zp0=new float[nn+1];
		theta=2.0f*(float)Math.PI/nn;
		for(i=0;i<nn+1;i++)
		{
			xp0[i]=r*(float)Math.cos(theta*i);
			yp0[i]=-0.4f;
			zp0[i]=r*(float)Math.sin(theta*i);
		}
		for(i=0;i<5;i++)
			for(j=0;j<nn+1;j++)
			{
				SurfacePointxyz[i][j][0]=xp0[j];
				SurfacePointxyz[i][j][1]=yp0[j]+i*0.2f;
				SurfacePointxyz[i][j][2]=zp0[j];
			}
		TriangleArray SurfaceTriangleArray=
			new TriangleArray(5*nn*2*3,GeometryArray.COORDINATES|GeometryArray.NORMALS);
		int c=0;
		for(i=0;i<4;i++)
			for(j=0;j<nn;j++)
			{
				Point3f A=new Point3f(SurfacePointxyz[i][j][0],SurfacePointxyz[i][j][1],SurfacePointxyz[i][j][2]);
				Point3f B=new Point3f(SurfacePointxyz[i+1][j][0],SurfacePointxyz[i+1][j][1],SurfacePointxyz[i+1][j][2]);
				Point3f C=new Point3f(SurfacePointxyz[i][j+1][0],SurfacePointxyz[i][j+1][1],SurfacePointxyz[i][j+1][2]);
				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();
				SurfaceTriangleArray.setCoordinate(c,A);
				SurfaceTriangleArray.setCoordinate(c+1,B);
				SurfaceTriangleArray.setCoordinate(c+2,C);
				
				
				SurfaceTriangleArray.setNormal(c,n);
				SurfaceTriangleArray.setNormal(c+1,n);
				SurfaceTriangleArray.setNormal(c+2,n);
				c+=3;
				
				Point3f A0=new Point3f(SurfacePointxyz[i][j+1][0],SurfacePointxyz[i][j+1][1],SurfacePointxyz[i][j+1][2]);
				Point3f B0=new Point3f(SurfacePointxyz[i+1][j][0],SurfacePointxyz[i+1][j][1],SurfacePointxyz[i+1][j][2]);
				Point3f C0=new Point3f(SurfacePointxyz[i+1][j+1][0],SurfacePointxyz[i+1][j+1][1],SurfacePointxyz[i+1][j+1][2]);
				Vector3f a0=new Vector3f(A0.x-B0.x,A0.y-B0.y,A0.z-B0.z);
				Vector3f b0=new Vector3f(C0.x-B0.x,C0.y-B0.y,C.z-B0.z);
				Vector3f n0=new Vector3f();
				n0.cross(b0,a0);
				n0.normalize();
				SurfaceTriangleArray.setCoordinate(c,A0);
				SurfaceTriangleArray.setCoordinate(c+1,B0);
				SurfaceTriangleArray.setCoordinate(c+2,C0);
				
				
				SurfaceTriangleArray.setNormal(c,n0);
				SurfaceTriangleArray.setNormal(c+1,n0);
				SurfaceTriangleArray.setNormal(c+2,n0);
				c+=3;
			}
		return SurfaceTriangleArray;
	}
	Appearance createAppearance0()
	{
		Appearance app=new Appearance();
		PolygonAttributes attri=new PolygonAttributes();
		attri.setCullFace(PolygonAttributes.CULL_NONE);
		attri.setPolygonMode(PolygonAttributes.POLYGON_LINE);
		app.setPolygonAttributes(attri);
		Material material=new Material();
		material.setDiffuseColor(new Color3f(1.0f,0.0f,0.0f));
		material.setSpecularColor(new Color3f(0.0f,1.0f,0.0f));
		material.setShininess(2.0f);
		app.setMaterial(material);
		return app;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -