JAVA3D学习系列(10)----- 面的生成(下)
汕头大学机电系 张杰(jzhang@mailserv.stu.edu.cn)
*****************************************
JAVA3D学习系列中的例题将有非常多的VRML程序与之相比较,因而
欢迎购买<>一书。
特殊购书方式:
1。作者售书
1。网上订购(email address: jzhang@mailserv.stu.edu.cn)
2。可以先获书,后汇款(不满意可退书),
只需将通信地址及邮编告知作者,即可在最短的时间内得到书。
3。书价为25元/本,免收邮购费用。
4。书为16开本,正文161页。
5. 购书可获盖有出版社财务章的收据。
6. 如果需要书中所有的源程序,我可以email一个打包程序
*****************************************
一. IndexedTriangleArray生成的面
IndexedTriangleArray对象的定义为:
IndexedTriangleArray (int vertexCount , int vertexFormat,
int indexCount)
利用这个对象,我们可以从一个顶点数组中挑选一些顶点生成自己
所需要的三角片面。程序triShape5.java中,有一个10个点的数组,我们
从这个数组中选择了8个点,生成了三个面,其中有一个点用了两次,如果
所挑选的点都只用一次,则选择的点的数目必须为3的倍数。
另外,在编程时,我们要注意点的旋转顺序,逆时针旋转和顺时针
旋转有时会产生不同的效果。
程序中,vertexCount表示顶点数组顶点的总数,indexCount表示生成
的面的顶点个数,数值为面的个数的三倍,本例中,生成的面为三个,故
vertexCount为9。
//triShape5.java
import javax.media.j3d.*;
public class triShape5 extends Shape3D {
private float vert[] = {
-.6f , .6f ,.0f ,
-.6f , -.6f, .0f ,
-.3f , .6f ,.0f ,
-.3f , -.6f, .0f ,
-.0f , .6f ,.0f ,
-.0f , -.6f, .0f ,
.3f , .6f ,.0f ,
.3f , -.6f, .0f ,
.6f , .6f ,.0f ,
.6f , -.6f, .0f ,
};
private float color[] = {
1.0f,0.5f,0.0f,
1.0f,0.0f,0.5f,
1.0f,0.8f,0.0f,
5.0f,1.0f,0.0f,
0.0f,1.0f,0.5f,
0.9f,1.0f,0.0f,
0.5f,0.0f,1.0f,
0.0f,0.5f,1.0f,
1.0f,0.5f,0.0f,
1.0f,0.0f,0.5f,
};
public triShape5() {
int[] index={ 0 , 1 , 2 , 4 , 5 , 6 , 6 , 7 , 8 };
int VertexCount=9;
IndexedTriangleArray tri = new IndexedTriangleArray(10,
IndexedTriangleArray.COORDINATES|
IndexedTriangleArray.COLOR_3 , VertexCount);
tri.setCoordinates(0,vert);
tri.setColors(0,color);
tri.setCoordinateIndices(0,index);
tri.setColorIndices(0,index);
PolygonAttributes pa = new PolygonAttributes();
pa.setCullFace(PolygonAttributes.CULL_NONE);
Appearance ap = new Appearance();
ap.setPolygonAttributes(pa);
this.setGeometry(tri);
this.setAppearance(ap);
this.setGeometry(tri);
}
}
//end of triShape5.java
二. IndexedQuadArray生成的面
IndexedQuadArray对象的定义为:
IndexedQuadArray (int vertexCount , int vertexFormat,
int indexCount )
利用这个对象,我们可以从一个顶点数组中挑选一些顶点生成自己
所需要的四个点所构成的平面。程序quadShape2.java中,有一个10个点
的数组,我们从这个数组中选择了7个点,生成了两个面,其中有一个点
用了两次,如果所挑选的点都只用一次,则选择的点的数目必须为4的倍数。
这些选中的点构成了数组index。在输入每一个顶点的坐标及颜色时,我们
用到了下面两种方法:
setCoordinateIndices(0,index);
setColorIndices(0,index);
程序中,vertexCount表示顶点数组顶点的总数,indexCount表示生成
的面的顶点个数,数值为面的个数的4倍,本例中,生成的面为两个,故
vertexCount为8。
编写IndexedQuadArray应用程序时,我们仍然要注意下面几个问题。
首先是四点应当共面,如果不共面,程序仍然可以编译
运行,但显示的内容为两个三角面。
其次是四个点组成的面不应有凹点,这有点象VRML程序中
的Extrusion、IndexedFaceSet里的情况,当然,在VRML程序中
四个点构成的面可以有凹点,这时只需要在相应的节点内加上
一个参数:
convex TRUE
而在JAVA3D程序中,如果QuadArray生成的面有凹点时,
程序的显示结果会不正确。
//quadShape2.java
import javax.media.j3d.*;
public class quadShape2 extends Shape3D {
private float vert[] = {
-.6f , .6f , .0f ,
-.6f , -.6f, .0f ,
-.3f , .6f , .0f ,
-.3f , -.6f, .0f ,
-.0f , .6f , .0f ,
-.0f , -.6f, .0f ,
.3f , .6f , .0f ,
.3f , -.6f, .0f ,
.6f , .6f , .0f ,
.6f , -.6f, .0f ,
};
private float color[] = {
1.0f,0.5f,0.0f,
1.0f,0.0f,0.5f,
1.0f,0.8f,0.0f,
5.0f,1.0f,0.0f,
0.0f,1.0f,0.5f,
0.9f,1.0f,0.0f,
0.5f,0.0f,1.0f,
0.0f,0.5f,1.0f,
1.0f,0.5f,0.0f,
1.0f,0.0f,0.5f,
};
public quadShape2() {
int[] index={ 2 , 1 , 3 , 4 , 4 , 7 , 9 , 6};
int VertexCount=8;
IndexedQuadArray quad = new IndexedQuadArray(10,
IndexedQuadArray.COORDINATES|
IndexedQuadArray.COLOR_3 , VertexCount);
quad.setCoordinates(0,vert);
quad.setColors(0,color);
quad.setCoordinateIndices(0,index);
quad.setColorIndices(0,index);
PolygonAttributes pa = new PolygonAttributes();
pa.setCullFace(PolygonAttributes.CULL_NONE);
Appearance ap = new Appearance();
ap.setPolygonAttributes(pa);
this.setGeometry(quad);
this.setAppearance(ap);
this.setGeometry(quad);
}
}
//end of quadShape2.java
三. IndexedTriangleStripArray生成的面
IndexedTriangleStripArray对象的定义为:
IndexedTriangleStripArray( int vertexCount, int vertexFormat,
int indexCount, int stripIndexCounts[])
利用IndexedTriangleStripArray对象,我们可以生成多组三角片面,
对于每一组三角片面来说,它的头三个点生成一个面,从第四个点
开始,每一个点都和前两个点生成一个新的面。这些点可以通过一个数组index
从一个顶点数组中任意选取,它和TriangleStripArray的差别在于
TriangleStripArray对顶点没有选择权,而IndexedTriangleStripArray
对顶点具有选择权,其它的都一样。
程序triShape6.java给出了一个16个数构成的顶点数组,从中挑选了
两组数,每一组都有6个顶点构成并生成了相连的四个三角片面。
//triShape6.java
import javax.media.j3d.*;
public class triShape6 extends Shape3D {
int StripCount[] = new int[2];
private float vert[] = {
-.6f , .6f , .1f ,
-.6f , .2f , .2f ,
-.6f , -.2f , .1f ,
-.6f , -.6f , .2f ,
-.0f , .6f , -.1f ,
-.0f , .2f , -.2f ,
-.0f , -.2f , -.1f ,
-.0f , -.6f , -.2f ,
.6f , .6f , .1f ,
.6f , .2f , .2f ,
.6f , -.2f , .1f ,
.6f , -.6f , .2f ,
};
private float color[] = {
1.0f,0.5f,0.0f,
1.0f,0.0f,0.5f,
1.0f,0.8f,0.0f,
5.0f,1.0f,0.0f,
0.0f,1.0f,0.5f,
0.9f,1.0f,0.0f,
0.5f,0.0f,1.0f,
0.0f,0.5f,1.0f,
1.0f,0.5f,0.0f,
1.0f,0.0f,0.5f,
0.9f,1.0f,0.0f,
0.5f,0.0f,1.0f,
};
public triShape6() {
int[] index={ 0 , 1 , 4 , 5 , 8 , 9 , 2 , 3 , 6 , 7 , 10 }
;
StripCount[0] = 6;
StripCount[1] = 5;
int indexCount=11;
IndexedTriangleStripArray tri = new IndexedTriangleStripArray(
12,
IndexedTriangleStripArray.COORDINATES|
IndexedTriangleStripArray.COLOR_3 , indexCount ,
StripCount);
tri.setCoordinates(0,vert);
tri.setColors(0,color);
tri.setCoordinateIndices(0,index);
tri.setColorIndices(0,index);
PolygonAttributes pa = new PolygonAttributes();
pa.setCullFace(PolygonAttributes.CULL_NONE);
Appearance ap = new Appearance();
ap.setPolygonAttributes(pa);
this.setGeometry(tri);
this.setAppearance(ap);
this.setGeometry(tri);
}
}
//end of Shape6.java
四. IndexedTriangleFanArray生成的面
IndexedTriangleFanArray对象的定义为:
IndexedTriangleFanArray ( int vertexCount, int vertexFormat,
int indexCount, int stripIndexCounts[])
利用这一对象,我们可以从一组顶点数组中挑选我们所需要的
顶点,生成多组三角片面,每组三角片面占用一定数量的顶点,每个组
在生成三角片面时,头三个顶点构成一个三角片面,其余的顶点和前面
的顶点及每组第一个顶点生成一个三角片面。下面的triShape7.java程
序中,我们生成了两组三角片面,头5个点生成了三个相连的三角片面,
后6个点生成了四个相连的三角片面。形状就像两把扇子,一大一小。
程序中所用的数组为20个点的顶点数组。
IndexedTriangleFanArray对象和TriangleFanArray的应用方法很相似,
它们之间的不同在于IndexedTriangleFanArray对象可以从顶点数组中挑选
自己所需要的顶点,而TriangleFanArray对象没有挑选权,只能被动地使用
顶点数组中的数据。
//triShape7.java
import javax.media.j3d.*;
public class triShape7 extends Shape3D {
int StripCount[] = new int[2];
private float vert[] = {
-.6f , .8f , -.1f ,
-.6f , .4f , -.0f ,
-.6f , .0f , .1f ,
-.6f , -.8f , -.1f ,
-.4f , .8f , .1f ,
-.4f , .4f , .1f ,
-.4f , .0f , -.1f ,
-.4f , -.8f , .1f ,
-.0f , .8f , -.1f ,
-.0f , .4f , -.0f ,
-.0f , .0f , .1f ,
-.0f , -.8f , -.1f ,
.4f , .8f , .1f ,
.4f , .4f , -.05f ,
.4f , .0f , -.1f ,
.4f , -.8f , .1f ,
.6f , .8f , -.1f ,
.6f , .4f , -.0f ,
.6f , .0f , .1f ,
.6f , -.8f , -.1f ,
};
private float color[] = {
1.0f,0.5f,0.0f,
1.0f,0.0f,0.5f,
1.0f,0.8f,0.0f,
0.5f,1.0f,0.0f,
0.0f,1.0f,0.5f,
0.9f,1.0f,0.0f,
0.5f,0.0f,1.0f,
0.0f,0.5f,1.0f,
1.0f,0.5f,0.0f,
1.0f,0.0f,0.5f,
0.9f,1.0f,0.0f,
0.5f,0.0f,1.0f,
0.0f,1.0f,0.5f,
0.9f,1.0f,0.0f,
0.5f,0.0f,1.0f,
0.0f,0.5f,1.0f,
1.0f,0.5f,0.0f,
1.0f,0.0f,0.5f,
0.9f,1.0f,0.0f,
0.5f,0.0f,1.0f,
};
public triShape7() {
int[] index={ 9 , 4 , 8 , 12 , 16 , 11 , 2 , 6 , 10 , 14 ,
18};
StripCount[0] = 5;
StripCount[1] = 6;
int indexCount=11;
IndexedTriangleFanArray tri = new IndexedTriangleFanArray(20,
IndexedTriangleFanArray.COORDINATES|
IndexedTriangleFanArray.COLOR_3 , indexCount , S
tripCount);
tri.setCoordinates(0,vert);
tri.setColors(0,color);
tri.setCoordinateIndices(0,index);
tri.setColorIndices(0,index);
PolygonAttributes pa = new PolygonAttributes();
pa.setCullFace(PolygonAttributes.CULL_NONE);
Appearance ap = new Appearance();
ap.setPolygonAttributes(pa);
this.setGeometry(tri);
this.setAppearance(ap);
this.setGeometry(tri);
}
}
/end of triShape7.java
--
※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.192.159.19]