Problem with glDrawElements()

Started by julien.1486, February 23, 2009, 11:30:34

Previous topic - Next topic

julien.1486

Hi there !

I'm reading the Red Book and I try to code the examples and I have a problem with the Vertex Arrays. I want to display a cube.
I have this variables :
private int[] vertices =  {-1,-1,0,								
    						  -1,1,0,							
    						  1,1,0,							
    						  1,-1,0,						
    						  -1,-1,-1,
    						  -1,1,-1,
    						  1,1,-1,
    						  1,-1,-1};
    
    private float[] colors = {1.0f, 0.2f, 0.2f,
    						  0.2f, 0.2f, 1.0f,
    						  0.8f, 1.0f, 0.2f,
    						  0.75f, 0.75f, 0.75f,
    						  0.35f, 0.35f, 0.35f,
    						  0.5f, 0.5f, 0.5f};
    private IntBuffer verticesBuf;
    private FloatBuffer colorsBuf;


And here is my Render() function :
private void render() {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);          // Clear The Screen And The Depth Buffer
        GL11.glLoadIdentity();                          // Reset The Current Modelview Matrix
        GL11.glColor3f(0.5f, 0.5f, 0.5f);
        GL11.glTranslatef(0.0f ,0.0f ,-6.0f);
        int[] indices = {0,1,2,3,4,5,6,7,0,1,5,4,3,2,6,7,1,5,6,2,0,4,7,3};
        
        GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
        GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
        
        GL11.glColorPointer(3, GL11.GL_FLOAT, colorsBuf);
        GL11.glVertexPointer(3, GL11.GL_INT, verticesBuf);
        
        
        GL11.glDrawElements(GL11.GL_QUADS, IntBuffer.wrap(indices));
        
        GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
        GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
    }


This code is displaying either an ugly red thing or crashes (most of the time). What's the problem ? Should I declare all the vertices of every faces even if a lot of vertices are shared by faces ?
Or is it something else ?

Thanks a lot for your help !