Here is some code that I use in a graph plotting app. The class Table is a
simple abstraction of a matrix of figures. This code also colors in the
surface and ensures that it can be seen from both sides.

Table.val(row,col) returns the value stoored at row,col.
Table.rows(), Table.cols() return the dimensions of a table.

  public ColorSurface(Table table, Appearance _a) {
    Appearance a = new Appearance();
    a.duplicateNodeComponent(_a);
    PolygonAttributes pa = a.getPolygonAttributes();
    if (pa == null)
      pa = new PolygonAttributes();
    pa.setCullFace(PolygonAttributes.CULL_NONE);
    pa.setBackFaceNormalFlip(true);
    if (a.getPolygonAttributes() == null)
      a.setPolygonAttributes(pa);

    // some counters
    int r;
    int rr;
    int c;
    int cc;

    // usefull table numbers
    float max = table.max();    // max value in the table
    float min = table.min();    // min value in the table
    float range = max-min;
    float inRange;
    float outRange;
    float alpha = 1f;

    // bookkeeping vectors
    Vector3f centre = new Vector3f();
    Vector3f i = new Vector3f();
    Vector3f j = new Vector3f();
    Vector3f k = new Vector3f();

    // the corners
    Vector3f RC =   new Vector3f();
    Vector3f R1C =  new Vector3f();
    Vector3f RC1 =  new Vector3f();
    Vector3f R1C1 = new Vector3f();

    // the extreem colors
    Vector3f minCol = new Vector3f(0.0f, 0.0f, 1.0f);
    Vector3f maxCol = new Vector3f(0.0f, 1.0f, 0.0f);

    // the arrays and their size
    int things = (table.rows()-1)*(table.cols()-1)*3*3*4;
    float [] verts  = new float[things];
    float [] norms  = new float[things];
    float [] colors = new float[things];

    for(r=0; r < table.rows()-1; r++) {
      rr = r*4*3*3*(table.cols()-1); // index into this row
      for(c=0; c<table.cols()-1; c++) {
        RC.x   = (float)r;     RC.y   = (float)c;     RC.z   = table.val(r,c);

        R1C.x  = (float)(r+1); R1C.y  = (float)c;     R1C.z  =
table.val(r+1,c);
        RC1.x  = (float)r;     RC1.y  = (float)(c+1); RC1.z  =
table.val(r,c+1);
        R1C1.x = (float)(r+1); R1C1.y = (float)(c+1); R1C1.z =
table.val(r+1,c+1);

        centre.add(RC,R1C); centre.add(RC1); centre.add(R1C1);
        centre.scale(1.0f/4.0f);

        // 1st triangle
        cc = rr+c*4*3*3;
        i.sub(centre,RC);
        j.sub(centre,R1C);
        k.cross(i,j); k.normalize();

        inRange = (centre.z-min)/range; outRange = 1.0f-inRange;
        colors[cc]   = minCol.x*inRange + maxCol.x*outRange;
        colors[cc+1] = minCol.y*inRange + maxCol.y*outRange;
        colors[cc+2] = minCol.z*inRange + maxCol.z*outRange;
        verts[cc]    = centre.x;
        verts[cc+1]  = centre.y;
        verts[cc+2]  = centre.z;
        norms[cc]    = k.x;
        norms[cc+1]  = k.y;
        norms[cc+2]  = k.z;

        inRange = (RC.z-min)/range; outRange = 1.0f-inRange;
        colors[cc+3] = minCol.x*inRange+maxCol.x*outRange;
        colors[cc+4] = minCol.y*inRange+maxCol.y*outRange;
        colors[cc+5] = minCol.z*inRange+maxCol.z*outRange;
        verts[cc+3]  = RC.x;
        verts[cc+4]  = RC.y;
        verts[cc+5]  = RC.z;
        norms[cc+3]  = k.x;
        norms[cc+4]  = k.y;
        norms[cc+5]  = k.z;

        inRange = (R1C.z-min)/range; outRange = 1.0f-inRange;
        colors[cc+6] = minCol.x*inRange+maxCol.x*outRange;
        colors[cc+7] = minCol.y*inRange+maxCol.y*outRange;
        colors[cc+8] = minCol.z*inRange+maxCol.z*outRange;
        verts[cc+6]  = R1C.x;
        verts[cc+7]  = R1C.y;
        verts[cc+8]  = R1C.z;
        norms[cc+6]  = k.x;
        norms[cc+7]  = k.y;
        norms[cc+8]  = k.z;

        // 2nd triangle
        cc+=9;
        i.sub(centre,R1C);
        j.sub(centre,R1C1);
        k.cross(i,j); k.normalize();

        inRange = (centre.z-min)/range; outRange = 1.0f-inRange;
        colors[cc]   = minCol.x*inRange + maxCol.x*outRange;
        colors[cc+1] = minCol.y*inRange + maxCol.y*outRange;
        colors[cc+2] = minCol.z*inRange + maxCol.z*outRange;
        verts[cc]    = centre.x;
        verts[cc+1]  = centre.y;
        verts[cc+2]  = centre.z;
        norms[cc]    = k.x;
        norms[cc+1]  = k.y;
        norms[cc+2]  = k.z;

        inRange = (R1C.z-min)/range; outRange = 1.0f-inRange;
        colors[cc+3] = minCol.x*inRange+maxCol.x*outRange;
        colors[cc+4] = minCol.y*inRange+maxCol.y*outRange;
        colors[cc+5] = minCol.z*inRange+maxCol.z*outRange;
        verts[cc+3]  = R1C.x;
        verts[cc+4]  = R1C.y;
        verts[cc+5]  = R1C.z;
        norms[cc+3]  = k.x;
        norms[cc+4]  = k.y;
        norms[cc+5]  = k.z;

        inRange = (R1C1.z-min)/range; outRange = 1.0f-inRange;
        colors[cc+6] = minCol.x*inRange+maxCol.x*outRange;
        colors[cc+7] = minCol.y*inRange+maxCol.y*outRange;
        colors[cc+8] = minCol.z*inRange+maxCol.z*outRange;
        verts[cc+6]  = R1C1.x;
        verts[cc+7]  = R1C1.y;
        verts[cc+8]  = R1C1.z;
        norms[cc+6]  = k.x;
        norms[cc+7]  = k.y;
        norms[cc+8]  = k.z;

        // 3rd triangle
        cc+=9;
        i.sub(centre,R1C1);
        j.sub(centre,RC1);
        k.cross(i,j); k.normalize();

        inRange = (centre.z-min)/range; outRange = 1.0f-inRange;
        colors[cc]   = minCol.x*inRange + maxCol.x*outRange;
        colors[cc+1] = minCol.y*inRange + maxCol.y*outRange;
        colors[cc+2] = minCol.z*inRange + maxCol.z*outRange;
        verts[cc]    = centre.x;
        verts[cc+1]  = centre.y;
        verts[cc+2]  = centre.z;
        norms[cc]    = k.x;
        norms[cc+1]  = k.y;
        norms[cc+2]  = k.z;

        inRange = (R1C1.z-min)/range; outRange = 1.0f-inRange;
        colors[cc+3] = minCol.x*inRange+maxCol.x*outRange;
        colors[cc+4] = minCol.y*inRange+maxCol.y*outRange;
        colors[cc+5] = minCol.z*inRange+maxCol.z*outRange;
        verts[cc+3]  = R1C1.x;
        verts[cc+4]  = R1C1.y;
        verts[cc+5]  = R1C1.z;
        norms[cc+3]  = k.x;
        norms[cc+4]  = k.y;
        norms[cc+5]  = k.z;

        inRange = (RC1.z-min)/range; outRange = 1.0f-inRange;
        colors[cc+6] = minCol.x*inRange+maxCol.x*outRange;
        colors[cc+7] = minCol.y*inRange+maxCol.y*outRange;
        colors[cc+8] = minCol.z*inRange+maxCol.z*outRange;
        verts[cc+6]  = RC1.x;
        verts[cc+7]  = RC1.y;
        verts[cc+8]  = RC1.z;
        norms[cc+6]  = k.x;
        norms[cc+7]  = k.y;
        norms[cc+8]  = k.z;

        // 4th triangle
        cc+=9;
        i.sub(centre,RC1);
        j.sub(centre,RC);
        k.cross(i,j); k.normalize();

        inRange = (centre.z-min)/range; outRange = 1.0f-inRange;
        colors[cc]   = minCol.x*inRange + maxCol.x*outRange;
        colors[cc+1] = minCol.y*inRange + maxCol.y*outRange;
        colors[cc+2] = minCol.z*inRange + maxCol.z*outRange;
        verts[cc]    = centre.x;
        verts[cc+1]  = centre.y;
        verts[cc+2]  = centre.z;
        norms[cc]    = k.x;
        norms[cc+1]  = k.y;
        norms[cc+2]  = k.z;

        inRange = (RC1.z-min)/range; outRange = 1.0f-inRange;
        colors[cc+3] = minCol.x*inRange+maxCol.x*outRange;
        colors[cc+4] = minCol.y*inRange+maxCol.y*outRange;
        colors[cc+5] = minCol.z*inRange+maxCol.z*outRange;
        verts[cc+3]  = RC1.x;
        verts[cc+4]  = RC1.y;
        verts[cc+5]  = RC1.z;
        norms[cc+3]  = k.x;
        norms[cc+4]  = k.y;
        norms[cc+5]  = k.z;

        inRange = (RC.z-min)/range; outRange = 1.0f-inRange;
        colors[cc+6] = minCol.x*inRange+maxCol.x*outRange;
        colors[cc+7] = minCol.y*inRange+maxCol.y*outRange;
        colors[cc+8] = minCol.z*inRange+maxCol.z*outRange;
        verts[cc+6]  = RC.x;
        verts[cc+7]  = RC.y;
        verts[cc+8]  = RC.z;
        norms[cc+6]  = k.x;
        norms[cc+7]  = k.y;
        norms[cc+8]  = k.z;
      }
    }

    TriangleArray triangles = new TriangleArray(things,
                                                TriangleArray.COLOR_3 |
                                                TriangleArray.COORDINATES |
                                                TriangleArray.NORMALS);
    triangles.setColors(0,colors);
    triangles.setCoordinates(0, verts);
    triangles.setNormals(0, norms);
    TransformGroup tg = new TransformGroup();
    Transform3D tf = new Transform3D();
    tf.set(new Vector3f(0.5f,0.5f,0.0f));
    tg.setTransform(tf);
    tg.addChild(new Shape3D(triangles,a));
    chart = tg;
  }


Gautam Jindal wrote:

> I have a 2d array of corordinates [x,y]=z where z is the height at
> point[x,y].How can I view this image in 3d using Java3dapi.Any suggestions
> are welcome.Please answer.
>
> Gautam Jindal
> CSE Department
> University of Nebraska,Lincoln.
>
> =====================================================================
> To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
> Java 3D Home Page: http://java.sun.com/products/java-media/3D/

=====================================================================
To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
Java 3D Home Page: http://java.sun.com/products/java-media/3D/

Reply via email to