Java3d学习笔记(3)

时间:2022-11-07 20:36:22

1.使用java3d提供的图形api绘制图形

由于例子很多,所以这里只演示一个例子

package com.java3d.study;

import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.DirectionalLight;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class Java3DShape {
public Java3DShape(){

//构建空间 和物体

// 创建一个虚拟空间
SimpleUniverse universe = new SimpleUniverse();
// 创建一个用来包含对象的数据结构
BranchGroup group = new BranchGroup();
// 创建一个圆柱形状并把它加入到group中
Cylinder cylinder=new Cylinder(.5f,1.0f); //圆柱型
group.addChild(cylinder);

//灯光构造

Color3f light1Color = new Color3f(1.8f, 0.1f, 0.1f);
// 设置光线的颜色
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
// 设置光线的作用范围
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
// 设置光线的方向
DirectionalLight light1= new DirectionalLight(light1Color, light1Direction);
// 指定颜色和方向,产生单向光源
light1.setInfluencingBounds(bounds);
// 把光线的作用范围加入光源中
group.addChild(light1);
// 将光源加入group组
// 安放观察点
universe.getViewingPlatform().setNominalViewingTransform();
// 把group加入到虚拟空间中
universe.addBranchGraph(group);
}

public static void main(String[] args) {
new Java3DShape();
}
}

Java3d学习笔记(3)

2.使用三角网绘制三维图形

用IndexedTriangleStripArray创建连续的三角形面,在顶点坐标数组中,前三个点将创建一个三角形面,从第4个顶点开始,每个顶点和前一个三角形面的后两个顶点组成一个三角形面。这里我们用IndexedTriangleStripArray创建一个皇冠,先用50个顶点创建皇冠下部的圆环,然后在圆环的上边沿挑选24个顶点,与顶点数组中后12个顶点组成12个三角片面,形成皇冠上的三角形装饰物。因此顶点数组中一共有62个元素,期中有24个顶点用了2次,共生成13组三角形面。

Java3d学习笔记(3)

package com.java3d.study;

import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.IndexedTriangleArray;
import javax.media.j3d.IndexedTriangleStripArray;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TriangleArray;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.geometry.GeometryInfo;
import com.sun.j3d.utils.geometry.NormalGenerator;
import com.sun.j3d.utils.scenegraph.io.state.javax.media.j3d.IndexedTriangleStripArrayState;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class Demo2 {
BranchGroup createSceneGraph()
{
BranchGroup BranchGroup1=new BranchGroup();
BoundingSphere BoundingSphere1=new BoundingSphere(new Point3d(0,0,0),100);
Color3f color3f=new Color3f(1f,1f,1f);
Background Background1=new Background(color3f);
Background1.setApplicationBounds(BoundingSphere1);
BranchGroup1.addChild(Background1);
color3f=new Color3f(1f,1f,1f);
Vector3f vector3f=new Vector3f(0f,0f,-1f);
DirectionalLight DirectionalLight1=new DirectionalLight(color3f,vector3f);
DirectionalLight1.setInfluencingBounds(BoundingSphere1);
BranchGroup1.addChild(DirectionalLight1);
TransformGroup TransformGroup1=new TransformGroup();
TransformGroup1.setCapability(18);
TransformGroup1.setCapability(17);
BranchGroup1.addChild(TransformGroup1);
MouseRotate MouseRotate1=new MouseRotate(TransformGroup1);
MouseRotate1.setTransformGroup(TransformGroup1);
MouseRotate1.setSchedulingBounds(BoundingSphere1);
BranchGroup1.addChild(MouseRotate1);
MouseZoom MouseZoom1=new MouseZoom();
MouseZoom1.setTransformGroup(TransformGroup1);
MouseZoom1.setSchedulingBounds(BoundingSphere1);
BranchGroup1.addChild(MouseZoom1);
MouseTranslate MouseTranslate1=new MouseTranslate();
MouseTranslate1.setTransformGroup(TransformGroup1);
MouseTranslate1.setSchedulingBounds(BoundingSphere1);
Transform3D transform3D = new Transform3D ();
transform3D.setScale(0.5);
TransformGroup TransformGroup2=new TransformGroup(transform3D);
BranchGroup1.addChild(MouseTranslate1);
Appearance Appearance1=new Appearance();
Material Material1=new Material();
Material1.setDiffuseColor(new Color3f(1f,1f,0f));
Appearance1.setMaterial(Material1);
int n=24;
int i;
float x1,x2,y1,y2,z1,z2;
//创建Point3f类型的顶点数组和Color3f类型的颜色数组,用于存放点的顶点坐标和颜色值,数组的大小为62
//前50个为半径大小相同,高度不同的点;后12个点为半径略大,位置较高的顶点。
Point3f[] vert=new Point3f[62];
Color3f[] colors=new Color3f[62];
for(i=0;i<n;i++)
{
x1=(float)(0.6f*Math.cos(i*2*Math.PI/24));
y1=0.1f;;
z1=(float)(0.6f*Math.sin(i*2*Math.PI/24));
vert[i*2]=new Point3f(x1,y1,z1);
x2=(float)(0.6f*Math.cos(i*2*Math.PI/24));
y2=0.1f;;
z2=(float)(0.6f*Math.sin(i*2*Math.PI/24));
vert[i*2+1]=new Point3f(x2,y2,z2);
}
vert[48]=new Point3f(0.6f,0.1f,0.0f);
vert[49]=new Point3f(0.6f,-0.1f,0.0f);
for(i=50;i<62;i++)
{
x1=(float)(0.65f*Math.cos((i-50)*2*Math.PI/12));
y1=0.35f;
z1=(float)(0.65f*Math.sin((i-50)*2*Math.PI/12));
vert[i]=new Point3f(x1,y1,z1);
}
for(i=0;i<62;i++)colors[i]=new Color3f(1f,0f,1f);
//创建索引数组index,因为有24个点要使用2次,因此index数组的大小为62+24=86
//数组中每3个顶点组成一个三角形面
int[] index=new int[62+24];
for(i=0;i<50;i++)index[i]=i;
for(i=0;i<12;i++)
{
index[i*3+50]=i*4;
index[i*3+1+50]=i*4+4;
index[i*3+2+50]=i+50;

}
//定义创建13组三角形面,前50个顶点为一组三角形面,后36个顶点每3个顶点为一组三角形面
int[] StripCount=new int[13];
StripCount[0]=50;
for(i=1;i<13;i++)
{
StripCount[i]=3;

}
/*
*IndexedTriangleStripArray(int vertexCount, int vertexFormat, int indexCount, int[] stripIndexCounts)
*Constructs an empty IndexedTriangleStripArray object using the specified parameters.
*/

IndexedTriangleStripArray indexedTriangleStripArray = new IndexedTriangleStripArray(vert.length,IndexedTriangleStripArray.COORDINATES|IndexedTriangleStripArray.COLOR_3,index.length,StripCount);
indexedTriangleStripArray.setCoordinates(0,vert);
indexedTriangleStripArray.setCoordinateIndices(0,index);
indexedTriangleStripArray.setColors(0,colors);
Shape3D shape3D=new Shape3D();
System.out.println(IndexedTriangleArray.COORDINATES);//|IndexedTriangleArray.COLOR_3);
GeometryInfo GeometryInfo1=new GeometryInfo(indexedTriangleStripArray);
NormalGenerator NormalGenerator1=new NormalGenerator();
NormalGenerator1.generateNormals(GeometryInfo1);
shape3D.setGeometry(GeometryInfo1.getGeometryArray());
shape3D.setAppearance(Appearance1);
TransformGroup2.addChild(shape3D);
TransformGroup1.addChild(TransformGroup2);
BranchGroup1.compile();
return BranchGroup1;
}

public static void main( String[] args ) {
SimpleUniverse universe = new SimpleUniverse();
universe.getViewingPlatform().setNominalViewingTransform();
BranchGroup group = new Demo2().createSceneGraph();
universe.addBranchGraph(group);
}
}