PDA

View Full Version : glVertex3fv() and two dimensinal array parameter?



marce
07-13-2009, 06:34 AM
I am reading the Redbook and trying to convert this C code to Java:

#define X .525731112119133606
#define Z .850650808352039932

static GLfloat vdata[12][3] = {
{-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},
{0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},
{Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
};
static GLuint tindices[20][3] = {
{0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
{8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
{7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
{6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} };
int i;

glBegin(GL_TRIANGLES);
for (i = 0; i < 20; i++) {
/* color information here */
glVertex3fv(&amp;vdata[tindices[i][0]][0]);
glVertex3fv(&amp;vdata[tindices[i][1]][0]);
glVertex3fv(&amp;vdata[tindices[i][2]][0]);
}
glEnd();

The Java code would be something like:

final float X = .525731112119133606f;
final float Z = .850650808352039932f;
float vdata[][] = new float[][]{
{-X, 0.0f, Z}, {X, 0.0f, Z}, {-X, 0.0f, -Z}, {X, 0.0f, -Z},
{0.0f, Z, X}, {0.0f, Z, -X}, {0.0f, -Z, X}, {0.0f, -Z, -X},
{Z, X, 0.0f}, {-Z, X, 0.0f}, {Z, -X, 0.0f}, {-Z, -X, 0.0f}
};
int tindices[][] = new int[][]{
{0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
{8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
{7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
{6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} };

gl.glBegin(GL.GL_TRIANGLES);
for (int i = 0; i < 20; i++) {
/* color information here */

// WHAT TO CHANGE HERE???
gl.glVertex3fv(vdata[tindices[i][0]][0],0);
gl.glVertex3fv(vdata[tindices[i][1]][0]);
gl.glVertex3fv(vdata[tindices[i][2]][0]);
}
gl.glEnd();

The problem is that glVertex3fv() does not take a two dimensional array as parameter. C language solves that by using a reference to such an array.

But how do we do in Java?

ZbuffeR
07-13-2009, 06:44 AM
Like this :
[snip]

EDIT : sorry, my post was totally wrong !

marce
07-13-2009, 06:54 AM
Thanks!

What about these lines of code:

glVertex3fv(&amp;vdata[tindices[i][0]][0]);
glVertex3fv(&amp;vdata[tindices[i][1]][0]);
glVertex3fv(&amp;vdata[tindices[i][2]][0]);


How do I rewrite them in Java?
I mean, glVertex3fv(vdata) is not the same as the C arrays.

ZbuffeR
07-13-2009, 08:12 AM
In fact &amp;vdata[ti][0] in C is equivalent to vdata[ti] in both C and Java.
I wonder why it was written that way in the first place ...

marce
07-14-2009, 06:37 AM
I donīt know why, I found the example here: http://www.glprogramming.com/red/chapter02.html#name6

The problem is solved:

final float X = 3.525731112119133606f;
final float Z = 3.850650808352039932f;

float vdata[][] = new float[][]{
{-X, 0.0f, Z}, {X, 0.0f, Z}, {-X, 0.0f, -Z}, {X, 0.0f, -Z},
{0.0f, Z, X}, {0.0f, Z, -X}, {0.0f, -Z, X}, {0.0f, -Z, -X},
{Z, X, 0.0f}, {-Z, X, 0.0f}, {Z, -X, 0.0f}, {-Z, -X, 0.0f}
};
int tindices[][] = new int[][]{
{0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
{8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
{7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
{6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} };


gl.glBegin(GL.GL_TRIANGLES);
for (int i = 0; i < 20; i++)
{
gl.glNormal3fv(vdata[tindices[i][0]], 0);
gl.glVertex3fv(vdata[tindices[i][0]], 0);
gl.glNormal3fv(vdata[tindices[i][1]], 0);
gl.glVertex3fv(vdata[tindices[i][1]], 0);
gl.glNormal3fv(vdata[tindices[i][2]], 0);
gl.glVertex3fv(vdata[tindices[i][2]], 0);

System.out.println(vdata[tindices[i][0]][0]);
}
gl.glEnd();