I am trying to draw a mesh fast. Without VBO, I think that means triangle strips with indices to avoid repeating vertices.
The immediate mode code works fine, but I’m doing something wrong with the equivalent in vertiex arrays and indices. Can anyone point out what I’m doing wrong? I’m at my wits end.
// genGrid generates the points and the colors
private void genGrid(float[] xyz) {
float dx = (float)(1.0 / numCols), dy = (float)(1.0 / numRows);
float x,y;
final float f = dx;
final int floatSize = 4;
colors = ByteBuffer.allocateDirect(numVertices*3*floatSize).asFloatBuffer();
//colors.order(ByteOrder.nativeOrder());
colors.clear();
y = 0;
float r,g,b;
g = 0;
float dr = -1.0f/numCols, dg = 1.0f/numRows, db = 1.0f/numCols;
for (int i = 0, c = 0; i < numRows; i++, y += dy) {
x = 0;
r = 1; b = 0;
for (int j = 0; j < numCols; j++, x += dx) {
xyz[c++] = x;
xyz[c++] = y;
xyz[c++] = 0;
colors.put(r).put(g).put(b);
r += dr;
b += db;
}
g += dg;
}
}
float r, g = 0, b;
float dr = -1.0f/numCols, dg = 1.0f/numRows, db = 1.0f/numCols;
for (int i = 0, c = 0; i < numRows-1; i++) {
r = 1; b = 0;
gl.glBegin(GL.GL_TRIANGLE_STRIP);
final int nextRow = numCols*3;
for (int j = 0; j < numCols; j++, c += 3) {
gl.glColor3f(r, g, b);
gl.glVertex3f(xyz[c], xyz[c+1],xyz[c+2]);
gl.glColor3f(r, g+dg, b);
gl.glVertex3f(xyz[c+nextRow], xyz[c+nextRow+1],xyz[c+nextRow+2]);
r += dr;
b += db;
}
gl.glEnd();
g += dg;
}
In order to try VertexPointers, I copied the xyz array into a FloatBuffer, and built the indices:
public void init(GL gl) {
xyz = new float[numVertices*3];
genGrid(xyz);
xyzBuf = ByteBuffer.allocateDirect(xyz.length*8).asFloatBuffer();
for (int i = 0; i < xyz.length; i++)
xyzBuf.put(i, xyz[i]);
first = false;
// FILLING ARRAY OF INDICES HERE…
ind = new int[(2 + 2*numCols + 2) * numRows];
for (int j = 0, c = 0; j < numRows; j++) {
ind[c++] = (j * numCols);
ind[c++] = (j+1) * numCols;
for (int i = 1; i < numCols; i++) {
ind[c++] = (i + j * numCols);
ind[c++] = (i + (j+1)*numCols);
}
ind[c++] = (numCols-1 + (j+1)*numCols);
ind[c++] = (0 + (j+1)*numCols);
}
}
With these indices built, this is the drawing code:
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL.GL_COLOR_ARRAY);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, xyzBuf);
gl.glColorPointer(3, GL.GL_FLOAT, 0, colors);
gl.glDrawElements(GL.GL_TRIANGLE_STRIP, ind.length, GL.GL_UNSIGNED_INT, ind);
gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL.GL_COLOR_ARRAY);