Login  Register

glDrawElements() and glColorPointer()

Posted by gmseed on Dec 06, 2011; 4:35pm
URL: https://forum.jogamp.org/glDrawElements-and-glColorPointer-tp3564836.html

Hi

The code below uses VAs to render a triangle mesh. It supports both vertices only and vertices+vertex indices. Without using vertex indices (mIsUsingVertexIndices is false) it draws the model and colours as expected. However, when using vertex indices (mIsUsingVertexIndices is true) and glDrawElements() instead I get a valid geometry but a half coloured model.

I may have my indices incorrect but I've checked and checked and as the vertex colours use the same indexing as the geometry I'd be surprised if it's tangled indices.

Am I using glDrawElements() and glColorPointer() correctly?

Thanks

Graham

    public void render_fill(GL gl)
    {
        GL2 glv = gl.getGL2();
       
        // set the colour to black
        NormalisedRGBColour defColour = RenderManager.getRenderManager().getDefaultFillColour();
        glv.glColor3f(defColour.getRedFloat(),defColour.getGreenFloat(),defColour.getBlueFloat());
        glv.glPolygonMode(GL2.GL_FRONT_AND_BACK,GL2.GL_FILL);
       
        if (mIsUsingVertexIndices)
        {
            IndexTriMesh3D triMesh = (IndexTriMesh3D)mGeometricObject;
            int numberTriangles = triMesh.numberTriangles();
            int numberIndices = 3 * numberTriangles;
            glv.glEnableClientState(GL2.GL_VERTEX_ARRAY);           // enable vertex arrays
            glv.glEnableClientState(GL2.GL_COLOR_ARRAY);            // enable vertex colours
            glv.glVertexPointer(3, GL.GL_FLOAT, 0, mVertices);      // Set The Vertex Pointer To Our Vertex Data
            glv.glColorPointer( 3, GL.GL_FLOAT, 0, mVertexColours); // set the vertex colour pointer    
            glv.glDrawElements(GL2.GL_TRIANGLES, numberIndices, GL.GL_UNSIGNED_INT, mVertexIndices);    // note: the total number of indices is passed and NOT the number of triangles
            glv.glDisableClientState(GL2.GL_VERTEX_ARRAY);          // disable vertex arrays
            glv.glDisableClientState(GL2.GL_COLOR_ARRAY);           // disable vertex colour array
        }
        else
        {
            glv.glEnableClientState(GL2.GL_VERTEX_ARRAY);           // enable vertex arrays
            glv.glEnableClientState(GL2.GL_COLOR_ARRAY);            // enable vertex colours
            glv.glVertexPointer(3, GL.GL_FLOAT, 0, mVertices);      // set the vertex pointer
            glv.glColorPointer( 3, GL.GL_FLOAT, 0, mVertexColours); // set the vertex colour pointer    
            glv.glDrawArrays(GL.GL_TRIANGLES, 0, mVertexCount);     // draw arrays
            glv.glDisableClientState(GL2.GL_VERTEX_ARRAY);          // disable vertex array
            glv.glDisableClientState(GL2.GL_COLOR_ARRAY);           // disable vertex colour array
        }
    }