glVertex3fv() and two dimensinal array parameter?

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(&vdata[tindices[i][0]][0]); 
   glVertex3fv(&vdata[tindices[i][1]][0]); 
   glVertex3fv(&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?

GL.glVertex3fv expects two arguments:

gl.glVertex3fv(myFloats, p);

  1. A one dimentional array of float [nobbc]myFloats[/nobbc] and
  2. An index p to specify that the xyz coordinates you want to use are [nobbc]myFloats[p][/nobbc], [nobbc]myFloats[p+1][/nobbc] and [nobbc]myFloats[p+2][/nobbc]

In your case myFloats is one element of vdata, and p is always 0.

Imagine that the one element of vdata is vdata[tindices[0][0]], then the xyz coordinates we want are vdata[tindices[0][0]][0], vdata[tindices[0][0]][0+1] and vdata[tindices[0][0]][0+2].

So


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

should work

(Note: Multidimentional arrays work differently in Java and C. Basically Java doesn’t have multidimentional arrays but it does have “arrays of arrays”)

Thanks for answering!

But I don´t think this make sense, look:

gl.glVertex3fv(vdata[ tindices[i][0] ], 0); 

is like

gl.glVertex3fv(float[int], 0); 

…which means that we try to use an element of the vdata array as argument, but glVertex3fv does not take just one element of a float array, it has to take the whole array.

So I can not even compile since this generates an error. In someway, I have to use the whole float array and that is what I don´t know how to do.

Yes, the first argument of GL.glVertex3fv is an array of floats. One element of vdata is an array of floats because vdata is an array of (array of floats).

boolean test = Arrays.equals(vdata[ tindices[0][0] ], new float[]{-X, 0.0f, Z}) is true

Thanks! Got it working now.

The C function:

glVertex3fv(&vdata[tindices[i][0]][0]);

Why doesn´t it take a second parameter as the Java method since Java and C use the same OpenGL API?

I don’t exactly know but I think it’s because of differences between how arrays/pointers/memory management work in C and Java.

[nobbc]&myFloats[4][/nobbc] essentially “points” (or “refrences”) to the fifth element of the array myFloats

To do the pointing in Java you need to specify the second argument p=4 (gl.glVertex3fv(myFloats, p))