Culling and drawing problems [SOLVED]

Oh look, here I am again. This time it’s something that shouldn’t even be giving me a problem, but for some reason I can’t get my cubes to have their back faces culled properly. I first tried getting the winding order from NeHe, then from a few other sites, then I drew myself a picture and tried labeling it myself, but every time some faces are culled improperly. Am I drawing the faces in the wrong order, or the triangles that make up each face, or both?

Here’s my vertex declaration:

public Cube()
 {
     lowLeftFront = new Point3D(0, 0, 0);
     lowLeftBack =  new Point3D(0, 0, 1);
     upLeftFront = new Point3D(0, 1, 0);
     upLeftBack = new Point3D(0, 1, 1);
     lowRightFront = new Point3D(1, 0, 0);
     lowRightBack = new Point3D(1, 0, 1);
     upRightFront =  new Point3D(1, 1, 0);
     upRightBack =  new Point3D(1, 1, 1);
     
     color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
 }

Here’s my vertex buffer setup:

public FloatBuffer makeVertexBuffer()
 {
     //2 triangles per face * 6 faces * 3 verts per triangle * 3 coords per vert
     vertexBuffer = BufferUtil.newFloatBuffer(108);
     
     float[] verts = {//front
                      upLeftFront.getX(), upLeftFront.getY(), upLeftFront.getZ(),
                      lowLeftFront.getX(), lowLeftFront.getY(), lowLeftFront.getZ(),
                      upRightFront.getX(), upRightFront.getY(), upRightFront.getZ(),
                      
                      lowLeftFront.getX(), lowLeftFront.getY(), lowLeftFront.getZ(),
                      lowRightFront.getX(), lowRightFront.getY(), lowRightFront.getZ(),
                      upRightFront.getX(), upRightFront.getY(), upRightFront.getZ(),
                      
                      //top
                      upLeftBack.getX(), upLeftBack.getY(), upLeftBack.getZ(),
                      upLeftFront.getX(), upLeftFront.getY(), upLeftFront.getZ(),
                      upRightBack.getX(), upRightBack.getY(), upRightBack.getZ(),
                      
                      upRightBack.getX(), upRightBack.getY(), upRightBack.getZ(),
                      upLeftFront.getX(), upLeftFront.getY(),upLeftFront.getZ(),
                      upRightFront.getX(),upRightFront.getY(),upRightFront.getZ(),
                      
                      //left
                      upLeftFront.getX(), upLeftFront.getY(), upLeftFront.getZ(),
                      upLeftBack.getX(), upLeftBack.getY(), upLeftBack.getZ(),
                      lowLeftFront.getX(), lowLeftFront.getY(), lowLeftFront.getZ(),
                      
                      lowLeftFront.getX(), lowLeftFront.getY(), lowLeftFront.getZ(),
                      upLeftBack.getX(), upLeftBack.getY(), upLeftBack.getZ(),
                      lowLeftBack.getX(), lowLeftBack.getY(), lowLeftBack.getZ(),
                      
                      //bottom
                      lowLeftFront.getX(), lowLeftFront.getY(), lowLeftFront.getZ(),
                      lowLeftBack.getX(), lowLeftBack.getY(), lowLeftBack.getZ(),
                      lowRightFront.getX(), lowRightFront.getY(), lowRightFront.getZ(),
                      
                      lowRightFront.getX(), lowRightFront.getY(), lowRightFront.getZ(),
                      lowLeftBack.getX(), lowLeftBack.getY(), lowLeftBack.getZ(),
                      lowRightBack.getX(), lowRightBack.getY(), lowRightBack.getZ(),
                      
                      //right
                      upRightFront.getX(), upRightFront.getY(), upRightFront.getZ(),
                      lowRightFront.getX(), lowRightFront.getY(), lowRightFront.getZ(),
                      upRightBack.getX(), upRightBack.getY(), upRightBack.getZ(),
                      
                      upRightBack.getX(), upRightBack.getY(), upRightBack.getZ(),
                      lowRightFront.getX(), lowRightFront.getY(), lowRightFront.getZ(),
                      lowRightBack.getX(), lowRightBack.getY(), lowRightBack.getZ(),
                      
                      //back
                      upLeftBack.getX(), upLeftBack.getY(), upLeftBack.getZ(),
                      upRightBack.getX(), upRightBack.getY(), upRightBack.getZ(),
                      lowLeftBack.getX(), lowLeftBack.getY(), lowLeftBack.getZ(),
                      
                      lowLeftBack.getX(), lowLeftBack.getY(), lowLeftBack.getZ(),
                      upRightBack.getX(), upRightBack.getY(), upRightBack.getZ(),
                      lowRightBack.getX(), lowRightBack.getY(), lowRightBack.getZ()
     };
     
     vertexBuffer.put(verts, 0, verts.length);
     vertexBuffer.rewind();
     return vertexBuffer;
 }

Here’s my draw function (in case you need it?):

public void draw(GL gl)
 {
     gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
     gl.glEnableClientState(gl.GL_COLOR_ARRAY);
     gl.glVertexPointer(3, gl.GL_FLOAT, 0, makeVertexBuffer());
     gl.glColorPointer(4, gl.GL_FLOAT, 0, makeColorBuffer());
     
     gl.glDrawArrays(gl.GL_TRIANGLES, 0, vertexBuffer.capacity() / BufferUtil.SIZEOF_FLOAT);
     gl.glDrawArrays(gl.GL_COLOR, 0, colorBuffer.capacity()/ BufferUtil.SIZEOF_FLOAT);
     
     gl.glDisableClientState(gl.GL_COLOR_ARRAY);
     gl.glDisableClientState(gl.GL_VERTEX_ARRAY);

 }

I have a really wacky colour function so I can colour each triangle separately. I’ll post it if you think you’ll need to see it.

EDIT: I did a lot of editing and now using glDrawElements instead of glDrawArrays. It works now.

A couple of things to check for: make sure that you enable culling, you have it set to cull the back face, and have the winding set to CCW (I examined your verts and it seemed that’s what it was).

I also noticed that there is a problem with your glDrawArrays() command, for each call, the last argument is the number of elements to use in the currently enabled vertex arrays (color, normal, vertex, etc). This doesn’t have anything to do with the type or byte size of the arrays (since they could be different), instead replace it with vertexbuffer.capacity() / 3 since you have 3 vertices for each ‘element’. Similarly, you’d use colorbuffer.capacity() / 4 since you have 4 color components per each color ‘element’. I use glDrawElements myself, so let me know if that works out.

Oh I think I fixed it. I needed to reverse all the z coordinates. Now the box is being culled and drawn properly, though not coloured properly.

It’s supposed to be a white cube, but all but 2 faces are black.