[The rendering function]
FloatBuffer verts = shape.getVertexBuffer();FloatBuffer norms = shape.getNormalBuffer();IntBuffer vertIndex = shape.getIndexBuffer();gl.glEnableClientState( GL.GL_VERTEX_ARRAY );gl.glEnableClientState( GL.GL_NORMAL_ARRAY );gl.glPolygonMode( GL.GL_FRONT, GL.GL_FILL );gl.glNormalPointer( GL.GL_FLOAT, 0, norms );gl.glVertexPointer( 3, GL.GL_FLOAT, 0, verts );gl.glDrawElements( GL.GL_QUADS, 4, GL.GL_UNSIGNED_INT, vertIndex );gl.glDisableClientState( GL.GL_VERTEX_ARRAY );gl.glDisableClientState( GL.GL_NORMAL_ARRAY );
I realize that this code is only expecting quads and that I will need to modify it to also handle triangles where necessary, but I have been testing with models that I know are composed only of quads. I have tried the above code several different ways: removing the lines dealing with normals; removing the glDisableClientState lines, etcetera - all to the same effect.
The source for the buffer creation is here;
[Buffer creation]
vertBuffer = BufferUtil.newFloatBuffer( vertices.length * 3 ); normBuffer = BufferUtil.newFloatBuffer( vertNorm.length * 3 ); for( int i = 0; i < vertices.length; i++ ) { Vec3 v = vertices; Vec3 n = vertNorm; vertBuffer.put( v.x ); vertBuffer.put( v.y ); vertBuffer.put( v.z ); normBuffer.put( n.x ); normBuffer.put( n.y ); normBuffer.put( n.z ); } vertBuffer.rewind(); normBuffer.rewind(); int size = 0; for( int i = 0; i < vertIndex.length; i++ ) size += vertIndex.d == -1 ? 3 : 4; indexBuffer = BufferUtil.newIntBuffer( vertIndex.length * 4 ); for( int i = 0; i < vertIndex.length; i++ ) { Point4i p = vertIndex; indexBuffer.put( p.a ); indexBuffer.put( p.b ); indexBuffer.put( p.c ); indexBuffer.put( p.d ); } indexBuffer.rewind();
indexBuffer is a Point4f where a, b, c, and d are the four components. Each index in the list represents either three or four vertices of a polygon. The four components each represent the index of the vertex used by the polygon. For a triangle, d would be -1.
Every tutorial I have looked at does these same steps and nothing more (so it seems), but I only get one incorrectly rendered quad when I use this. Just thought I'd throw this out there to see if there's something obvious or at the very least, something simple wrong with this.
Thanks,
Nathan