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

📄 surface.java

📁 java 3d 系列源码
💻 JAVA
字号:
// 【\source\chapter2\Surface.java】Surface.java
// 生成函数曲面
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class Surface extends Shape3D 
{
		Surface() 
		{
			this.setGeometry(createGeometry());
			this.setAppearance(createAppearance());
		} 
		// 具体函数,更改这一函数即可显示不同函数的图像
		float  calz(float x,float y)
		{
			return (float)(-1f*Math.sqrt(2-x*x-y*y));
		}
		Geometry createGeometry()
		{
			int MAXX=200;
			int MAXY=200;
			QuadArray surf = new QuadArray(
MAXX*MAXY*4, GeometryArray.COORDINATES
				| GeometryArray.NORMALS);
			for(int i=0;i<MAXX;i++)
			{
				for(int j=0;j<MAXY*4;j+=4)
				{
					int jb=j/4;
					float x=(i-100f)/200.0f;
					float y=(jb-100f)/200.0f;
					// 计算一个平面上的4个点
					Point3f A=new Point3f(x,y,calz(x,y));
					Point3f B=
new Point3f(x+0.005f,y,calz(x+0.005f,y));
					Point3f C=new Point3f(x+0.005f,
y+0.005f,calz(x+0.005f,y+0.005f));
					Point3f D=new Point3f(x,y+0.005f,
calz(x,y+0.005f));
					// 计算四个点的法向量
					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();
					// 设置点坐标
					surf.setCoordinate(i*MAXY*4+j, A);
					surf.setCoordinate(i*MAXY*4+j+1, B);
					surf.setCoordinate(i*MAXY*4+j+2, C);
					surf.setCoordinate(i*MAXY*4+j+3, D);
					// 设置点法向量
					surf.setNormal(i*MAXY*4+j, n);
					surf.setNormal(i*MAXY*4+j+1, n);
					surf.setNormal(i*MAXY*4+j+2, n);
					surf.setNormal(i*MAXY*4+j+3, n);
				}
			}
			return surf;
		}
		Appearance createAppearance() 
{
	// 指定外观,这样才有明暗效果
			Appearance appear = new Appearance();
			Material material = new Material();
			Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
			Color3f red   = new Color3f(1.0f, 0.0f, 0.0f);
			material.setDiffuseColor(red);
	        material.setSpecularColor(white);
    	    material.setShininess(2.0f);
			appear.setMaterial(material);
			return appear;
		}
}// end of Surface.java

⌨️ 快捷键说明

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