Sign in to follow this  
thepog

glDrawElements not rendering

Recommended Posts

thepog    113
I am currently trying to rewrite my function that renders 3D models from using glBegin(...) / glEnd() sequence (immediate mode, I guess) to using something a bit more efficient. I am trying to use glDrawElements, but it will either render nothing at all, or more usually one quad... that isn't even rendered properly. I am writing this in Java and using JOGL. Here are some pertinent bits of code from what I am doing.

[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[i];
Vec3 n = vertNorm[i];
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[i].d == -1 ? 3 : 4;
indexBuffer = BufferUtil.newIntBuffer( vertIndex.length * 4 );
for( int i = 0; i < vertIndex.length; i++ )
{
Point4i p = vertIndex[i];
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

Share this post


Link to post
Share on other sites
thepog    113
Update:

I just tested to see which quad it was actually rendering and it looks to be the first one in the buffer. As for it not rendering correctly, it appears to have been some kind of an issue with my shader. I disabled it and it looks proper now. Next is to still figure out why the rest of the polygons aren't rendering. :(

Share this post


Link to post
Share on other sites
thepog    113
It looks like I've found the problem. I changed the second argument in glDrawElements from 4 to the entire size of the buffer by using vertIndex.limit(). All polygons seems to be drawing now. Only thing left to figure out is why the shading is all screwed up. I must be doing something wrong with the normals array.

Share this post


Link to post
Share on other sites
Brother Bob    10344
Given the code you posted, you get one quad because you only draw one quad. You probably need to pass vertIndex.length instead of 4 to glDrawElements to draw the entire index array and not just the first four indices.

edit: Shouldn't want too long to post next time I guess :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this