Hi,
loosely based upon NeHe’s milkshape loader, I am drawing some spinning turtles and ferraris.
It works perfectly fine with immediate drawing.
For each mesh do:
gl.glBegin(GL.GL_TRIANGLES);
{
for(int j = 0; j < meshes[i].getNumTriangles(); j++)
{
int triangleIndex = meshes[i].getTriangleIndices()[j];
Triangle pTri = triangles[triangleIndex];
for(iint k = 0; k < 3; k++ )
{
gl.glNormal3f(pTri.getVertexNormals()[k][0],
pTri.getVertexNormals()[k][1],
pTri.getVertexNormals()[k][2]);
gl.glTexCoord2f(pTri.getS()[k], pTri.getT()[k]);
int index = pTri.getVertexIndices()[k];
gl.glVertex3f(vertices[index].getLocation()[0],
vertices[index].getLocation()[1],
vertices[index].getLocation()[2]);
}
}
}
gl.glEnd();
However, to increase performance, I decided to write all the data into VBOs as follows.
Create a VBO for each mesh:
int numTriangles = meshes[i].getNumTriangles();
FloatBuffer vFloatBuffer = BufferUtil.newFloatBuffer(numTriangles * 8 * 3);
// Write the coordinates into the FloatBuffer
for(int j = 0; j < numTriangles; j++)
{
int triangleIndex = meshes[i].getTriangleIndices()[j];
Triangle pTri = triangles[triangleIndex];
for(int k = 0; k < 3; k++)
{
// 2x Texture
vFloatBuffer.put(pTri.getS()[k]);
vFloatBuffer.put(pTri.getT()[k]);
// 3x Normals
vFloatBuffer.put(pTri.getVertexNormals()[k][0]);
vFloatBuffer.put(pTri.getVertexNormals()[k][1]);
vFloatBuffer.put(pTri.getVertexNormals()[k][2]);
// 3x Vertex
int index = pTri.getVertexIndices()[k];
vFloatBuffer.put(vertices[index].getLocation()[0]);
vFloatBuffer.put(vertices[index].getLocation()[1]);
vFloatBuffer.put(vertices[index].getLocation()[2]);
}
}
vFloatBuffer.rewind();
// Generate and bind the buffer
int[] tmpBuffer = new int[1];
gl.glGenBuffersARB(1, tmpBuffer, 0);
gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, tmpBuffer[0]);
gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB, numTriangles*8*BufferUtil.SIZEOF_FLOAT, vFloatBuffer, GL.GL_STATIC_DRAW_ARB);
vboBuffer[i] = tmpBuffer[0];
Drawing (for each mesh):
gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, vboBuffer[i]);
gl.glInterleavedArrays(GL.GL_T2F_N3F_V3F, 0, 0);
gl.glDrawArrays(GL.GL_TRIANGLES, 0, meshes[i].getNumTriangles());
The performance gain is incredible, but now about half my vertices are apparently getting swallowed:
http://h0st.mine.nu/~dhomas/turtles.jpg
Has anybody got an idea why? With immediate rendering, it worked perfectly fine.
I also tried creating three separate buffers for vertices, texCoords and normals (also tried glEnableClientState(…)), but to no avail.
What am I doing wrong? The transmitted data should be the same in both cases.
P.S.: The texture coordinates are not flipped or something, it’s just that I don’t use backface culling.