glDrawElements() and glColorPointer()

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

glDrawElements() and glColorPointer()

gmseed
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
        }
    }

Reply | Threaded
Open this post in threaded view
|

Re: glDrawElements() and glColorPointer()

gmseed
Hi

Found the problem but don't know why.

I have the following code to turn on/off glColorMaterial() to preserve assigned colours to mesh triangles.

        // material colour tracking
        if (mMaterialColourTrackCurrentColour)
        {
            // needed so material for triangles will be set from colour map
            glv.glColorMaterial( GL.GL_FRONT_AND_BACK, GL2.GL_AMBIENT_AND_DIFFUSE ); // call glColorMaterial before enabling GL_COLOR_MATERIAL
            glv.glEnable( GL2.GL_COLOR_MATERIAL );
        }

It would appear that this is conflicting when I call glDrawElements() but is fine when I use glDrawArrays()??

Graham
Reply | Threaded
Open this post in threaded view
|

Re: glDrawElements() and glColorPointer()

gouessej
Administrator
I confirm it is strange. Do you reproduce this problem on other machines?
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: glDrawElements() and glColorPointer()

gmseed
Hi gouessej

Yes, it's the same on both my desktop and laptop.

The geometry and colouring works fines with glInterleavedArrays() and glDrawArrays() as indicated.

However, glDrawElements() causes problems when using glColorMaterial() and lighting enabled.

I'm setting up 4 classes for rendering a triangle mesh using VAs, VAs with interleaving, VBOs and VBOs+shaders and encountered this problem when I started using indices with glDrawElements().

Graham
Reply | Threaded
Open this post in threaded view
|

Re: glDrawElements() and glColorPointer()

gouessej
Administrator
Maybe there is a problem with your indices.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: glDrawElements() and glColorPointer()

gmseed
Hi

I read at:

http://www.khronos.org/opengles/sdk/1.1/docs/man/glColorPointer.xml

regarding glColorPointer():

"Specifies the number of components per color. Must be 4. The initial value is 4."

so I redid both of my vertex and colour arrays to handle size*4 and changed the pointer functions to specify the new size:

            glv.glVertexPointer(4, GL.GL_FLOAT, 0, mVertices);      // Set The Vertex Pointer To Our Vertex Data
            glv.glColorPointer( 4, GL.GL_FLOAT, 0, mVertexColours);

and I get exactly the same problem of a half-rendered object?

Graham
Reply | Threaded
Open this post in threaded view
|

Re: glDrawElements() and glColorPointer()

gouessej
Administrator
Where is your index buffer?
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: glDrawElements() and glColorPointer()

gmseed
Hi

My buffers are in a parent class:


public abstract class VertexObjectRenderer extends ObjectRenderer
{

    ////////////
    // fields //
    ////////////
   
    protected int           mVertexCount            = 0;
    protected FloatBuffer   mVertices               = null;
   
    protected IntBuffer     mVertexIndices          = null;
    protected boolean       mIsUsingVertexIndices   = false;
   
    protected FloatBuffer   mVertexColours          = null;
//...


and built by the following method:


    private boolean buildRenderer_IndexTriMesh3D_Indices(GL gl)
    {
        ObjectAttributeManager objAttrManager = Model.getModel().getObjectAttributeManager();

        IndexTriMesh3D  triMesh         = (IndexTriMesh3D)mGeometricObject;
        int             numberTriangles = triMesh.numberTriangles();
       
        // vertices //
       
        // number of vertices now set to actual size of points vector
        IndexPoints3D   triMeshVertices = triMesh.getPoints();
        int             numberVertices  = triMeshVertices.size();
        mVertexCount = numberVertices;
        // allocate vertex buffer [3 for (x,y,z) for each vertex]
        int vertexBufferSize = 4 * mVertexCount;
        mVertices      = Buffers.newDirectFloatBuffer(vertexBufferSize);
        int vertexColoursBufferSize = 4 * mVertexCount;
        mVertexColours = Buffers.newDirectFloatBuffer(vertexColoursBufferSize);
        for (int i=0; i<numberVertices; i++)
        {
            Point3D v = triMeshVertices.getPoint(i);
            mVertices.put(v.getXFloat()); mVertices.put(v.getYFloat()); mVertices.put(v.getZFloat()); mVertices.put(1.0f);
        }
        mVertices.flip();
       
        // indices //
       
        // number of indices equal to 3xnumberTris since ordered: {tri0[viIndex,vjIndex,vkIndex],...}
        int indexBufferSize = 3 * numberTriangles;
        mVertexIndices = Buffers.newDirectIntBuffer(indexBufferSize);
        HashMap<ID,IndexAdjacentTriangle3D>         tris = triMesh.getTriangles();
        Iterator<Entry<ID,IndexAdjacentTriangle3D>> it   = tris.entrySet().iterator();
        while (it.hasNext())
        {
            Entry<ID,IndexAdjacentTriangle3D>   entry  = it.next();
            ID                                  itriID = entry.getKey();
            IndexAdjacentTriangle3D             itri   = entry.getValue();
            // vertex indices
            int v0Index = itri.vertex0Index();
            int v1Index = itri.vertex1Index();
            int v2Index = itri.vertex2Index();
            mVertexIndices.put(v0Index); mVertexIndices.put(v1Index); mVertexIndices.put(v2Index);
            // vertex colours
            ColourAttribute colourAttribute = (ColourAttribute)objAttrManager.getObjectAttribute(itriID,AttributeType.ColourAttributeType);
            NormalisedRGBAlphaColour objectColour = null;
            if (colourAttribute == null)
            {
                objectColour = RenderManager.getRenderManager().getDefaultFillColour();
            }
            else
            {
                objectColour = colourAttribute.getAttribute();
            }
            //objectColour = RenderManager.getRenderManager().getDefaultFillColour();
            NormalisedRGBAlphaColour v0Colour = objectColour;
            NormalisedRGBAlphaColour v1Colour = objectColour;
            NormalisedRGBAlphaColour v2Colour = objectColour;
            mVertexColours.put(v0Index+0,v0Colour.getRedFloat()); mVertexColours.put(v0Index+1,v0Colour.getGreenFloat()); mVertexColours.put(v0Index+2,v0Colour.getBlueFloat()); mVertexColours.put(v0Index+3,v0Colour.getAlphaFloat());
            mVertexColours.put(v1Index+0,v1Colour.getRedFloat()); mVertexColours.put(v1Index+1,v1Colour.getGreenFloat()); mVertexColours.put(v1Index+2,v1Colour.getBlueFloat()); mVertexColours.put(v1Index+3,v1Colour.getAlphaFloat());
            mVertexColours.put(v2Index+0,v2Colour.getRedFloat()); mVertexColours.put(v2Index+1,v2Colour.getGreenFloat()); mVertexColours.put(v2Index+2,v2Colour.getBlueFloat()); mVertexColours.put(v2Index+3,v2Colour.getAlphaFloat());
        }
        mVertexIndices.flip();
        mVertexColours.rewind(); // note use of rewind() after using put(index,value)
       
        return true;
    }


PS. IS THERE A WAY TO INSERT TAGS FOR A CODE BLOCK TO MAKE IT MORE READABLE?
Reply | Threaded
Open this post in threaded view
|

Re: glDrawElements() and glColorPointer()

gmseed
Hi

I eventually found the problem. My own fault of a stupid indexing problem!!

Graham
Reply | Threaded
Open this post in threaded view
|

Re: glDrawElements() and glColorPointer()

gouessej
Administrator
That is what I thought but I had found nothing really wrong in the piece of code you gave us.
Julien Gouesse | Personal blog | Website