Java 版 (精华区)

发信人: bali (阿奔), 信区: Java
标  题: JAVA3D学习系列(9)--面的生成(上) 
发信站: 紫 丁 香 (Fri Mar 24 11:00:55 2000), 转信

发信人: vrml (3d), 信区: Java
标  题: JAVA3D学习系列(9)--面的生成(上)
发信站: BBS 水木清华站 (Fri Apr  9 15:27:22 1999)

        JAVA3D学习系列(9)----- 面的生成(上)


    汕头大学机电系    张杰(jzhang@mailserv.stu.edu.cn)

一. 生成平面的对象及其定义
    JAVA3D可通过编程显示出面来,面有两种:三角形和四边形,
相应的对象为Triangle和Quad。
    JAVA3D用于生成平面的对象有:
1.  TriangleArray
    TriangleArray (int vertexCount, int vertexFormat )

2.  QuadArray
    QuadArray (int vertexCount, int vertexFormat )

3.  TriangleStripArray
    TriangleStripArray ( int vertexCount , int vertexFormat,
                         int[] stripVertexCounts )

4.  TriangleFanArray
    TriangleFanArray ( int vertexCount ,int vetexFormat,
                       int[] stripVertexCounts )

5.  IndexedTriangleArray
    IndexedTriangleArray (int vertexCount , int vertexFormat,
                          int indexCount)

6.  IndexedQuadArray
    IndexedQuadArray (int vertexCount , int vertexFormat,
                      int indexCount )

7.  IndexedTriangleStripArray
    IndexedTriangleStripArray( int vertexCount, int vertexFormat,
                               int indexCount, int stripIndexCounts[])

8.  IndexedTriangleFanArray
    IndexedTriangleFanArray ( int vertexCount, int vertexFormat,
                              int indexCount, int stripIndexCounts[])

二. TriangleArray生成的面
    和前面介绍的PointArray、LineArray一样,面也可以用
TriangleArray来生成,利用它可以生成三角片面我们先看一下TriangleArray的定义:
        TriangleArray (int vertexCount, int vertexFormat )
    这里:
        vertexCount表示顶点的个数(必须为三的倍数)
        vertexFormat表示顶点的格式(第七讲有介绍)
    下面我们看一个利用TriangleArray的例子,例子里有九个点。

      --1--         --4--      --7--

 --0--        --3--        --6--

      --2--         --5--      --8--

//triShape1.java
import javax.media.j3d.*;

public class triShape1 extends Shape3D {

    private float vert[] = {
        -.8f , .0f ,0.0f,
        -.4f , .8f ,0.0f,
        -.4f , -.8f,0.0f,
        -.2f , .0f ,0.0f,
        0.2f , .8f ,0.0f,
        0.2f , -.8f,0.0f,
        0.4f , .0f ,0.0f,
        0.8f , .8f ,0.0f,
        0.8f , -.8f,0.0f,
       };

    private float color[] = {
        0.0f,0.5f,1.0f,
        0.5f,0.0f,1.0f,
        0.0f,0.8f,2.0f,
        1.0f,0.0f,0.3f,
        0.0f,1.0f,0.3f,
        0.3f,0.8f,0.0f,
        0.0f,0.5f,1.0f,
        0.5f,0.0f,1.0f,
        0.0f,0.8f,2.0f,e3D {
    int StripCount[] = new int[2];
     private float vert[] = {
         -.6f ,  .8f , 0.0f,
         -.6f , -.8f,  0.2f,
        -0.2f ,  .5f,  -.2f,
        -0.2f , -.5f , 0.2f,
         0.0f , -.5f,  -.2f,
         0.0f ,  .5f ,  .2f,
         0.2f ,  .0f,   .0f,
         0.2f ,  .8f , 0.3f,
         0.2f , -.8f, -0.3f,
         0.6f ,  .8f,  0.0f,
         0.6f , -.8f,  0.5f,
         0.8f , 0.0f ,  .3f
         };

    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,
        1.0f,0.8f,0.0f,
      };

    public triShape3() {
        StripCount[0] = 7;
        StripCount[1] = 5;
        TriangleStripArray tri = new TriangleStripArray(12,
             TriangleStripArray.COORDINATES|
                      TriangleStripArray.COLOR_3 , StripCount);
          tri.setCoordinates(0,vert);
          tri.setColors(0,color);
        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 triShape3.java


五. TriangleFanArray生成的面
    TriangleFanArray对象的定义为:
    TriangleFanArray ( int vertexCount ,int vetexFormat,
                       int[] stripVertexCounts )
    利用TriangleFanArray对象,我们可以生成多组三角片面,每组
三角片面占用一定数量的顶点,每个组在生成三角片面时,头三个顶
点构成一个三角片面,其余的顶点和前面的顶点及每组第一个顶点生成
一个三角片面。下面的triShape4.java程序中,我们生成了两组三角
片面,头5个点生成了三个相连的三角片面,后6个点生成了四个相连的
三角片面。形状就像两把扇子,一大一小。
//triShape4.java

import javax.media.j3d.*;

public class triShape4 extends Shape3D {
    int FanCount[] = new int[2];
     private float vert[] = {
              0.0f , 0.0f , 0.0f,
              -0.3f , 0.3f , 0.02f,
              -0.1f , 0.4f , -0.02f,
               0.1f , 0.4f ,  0.02f,
               0.3f,  0.3f ,  -0.02f,
               0.0f, -0.8f ,  0.0f,
              -0.6f, -0.2f,  0.02f,
              -0.3f, -0.1f , -0.02f,
                 .0f, -0.05f, 0.02f,
                 .3f, -0.1f, -0.02f,
                 .6f, -0.2f,  0.02f
          };

    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 triShape4() {
        FanCount[0] = 5;
        FanCount[1] = 6;
        TriangleFanArray tri = new TriangleFanArray(11,
             TriangleFanArray.COORDINATES|
                      TriangleFanArray.COLOR_3 , FanCount);
          tri.setCoordinates(0,vert);
          tri.setColors(0,color);
        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 triShape4.java


--
※ 来源:.紫 丁 香 bbs.hit.edu.cn.[FROM: 202.118.243.89]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:4.039毫秒