Sign in to follow this  
Matthewj234

VBO is Rendering an extra triangle

Recommended Posts

Matthewj234    1069

Hey guys, I have been working on a 3D mesh system based on adding polygons to the mesh. However, I have run into an issue!

 

2013-05-27_0218.png

Whats happening is the extra triangle returning to the origin is being added, and I am clueless as to why.

 

 public void createMesh(){
        Tessellator t = new Tessellator();
        t.setColour(0.7f, 0.2f, 0.2f, 1f);
        t.addPoly(new Vector3f[]{
                new Vector3f(120, 120, 1f), new Vector3f(80, 120, 1f), new Vector3f(100, 200f, 1f)
        }, null);
        t.setColour(1f, 0f, 0f, 1f);
        t.addPoly(new Vector3f[]{
                new Vector3f(120, 120, 1f), new Vector3f(200, 120, 1f), new Vector3f(160, 200f, 1f),
        }, null);
        t.setColour(0f, 1f, 0f, 1f);
        t.addPoly(new Vector3f[]{
                new Vector3f(120, 120, 1f), new Vector3f(200, 120, 1f), new Vector3f(160, 80f, 1f)
        }, null);

        mesh = t.generateMesh();
    }

That is the code for setting the vertices and colours.

 

    public void render() {
        preRender();
        doRender();
        postRender();
    }

    public void preRender() {
        glEnableClientState(GL_VERTEX_ARRAY);
        if (hasTexCoords || hasTexLighting) glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        if (hasColour) glEnableClientState(GL_COLOR_ARRAY);
        if (hasNormals) glEnableClientState(GL_NORMAL_ARRAY);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboVertexBuffer);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboIndexBuffer);

        glVertexPointer(VERTEX_SIZE, GL11.GL_FLOAT, stride, vertexOffset);

        if (hasTexCoords) {
            GL13.glClientActiveTexture(GL13.GL_TEXTURE0);
            glTexCoordPointer(TEX_COORD_SIZE, GL11.GL_FLOAT, stride, texCoordOffset);
        }

        if (hasTexLighting) {
            GL13.glClientActiveTexture(GL13.GL_TEXTURE1);
            glTexCoordPointer(TEX_LIGHTING_SIZE, GL11.GL_FLOAT, stride, texLightingOffset);
        }

        if (hasColour) glColorPointer(COLOUR_SIZE, GL11.GL_FLOAT, stride, colourOffset);
        if (hasNormals) glNormalPointer(GL11.GL_FLOAT, stride, normalOffset);
    }

    public void postRender() {
        if (hasNormals) glDisableClientState(GL_NORMAL_ARRAY);
        if (hasColour) glDisableClientState(GL_COLOR_ARRAY);
        if (hasTexCoords || hasTexLighting) glDisableClientState(GL_TEXTURE_COORD_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
    }

    public void doRender() {
        GL11.glDrawElements(GL11.GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
    }

And thats the render code for the mesh.

 

 

If anyone can point me towards what I am doing wrong, I would be greatly appreciative!

Share this post


Link to post
Share on other sites
shocobenn    273

Ok, so if your vertex count in your buffer is smaller than the "indexCount" you're passing you won't have an error but have an additions of points who are located at the corner left of you screen (or it's more random ?) 

Share this post


Link to post
Share on other sites
marcClintDion    435

You end up overwriting or accessing memory from other areas of the program.  Sometimes nothings shows up at all until you start loading lots of other models, then the weird triangles can show up on models that are set up correctly.  Sometimes the program will crash.  What happens when you do this is definitely random.

Share this post


Link to post
Share on other sites
marcClintDion    435

The only program that I've come across that can give you easy to read info on VBO errors like this is a program for MacOS called 'Instruments' in the section iOS->graphics->openGL ES Analyzer.  Knowing this won't help you immediately but maybe someone passing through here will know of a similar tool for the platform you are using.

Share this post


Link to post
Share on other sites
marcClintDion    435

I've been staring at your code and the attached image for quite some time now and I'm convinced that you should not be using glDrawElements for this type of rendering.  Many of your vertices are standing out on their own.  Using an index buffer only offers optimization if calculations on redundant points can be eliminated, this only happens if your model has many triangles that share vertices with other triangles.  Also, you will have to process all the newly created geometry to determine whether or not points are being shared and then recalculate the index buffer every time more geometry is added, this is going to offset any performance benefit that glDrawElements offers since your doing more math just to use this function.  I'd suggest you use glDrawArrays instead.   Here's a quote from an Apple document: " ..an indexed triangle list requires additional memory for the indices and adds overhead to look up vertices." http://developer.apple.com/library/ios/#documentation/3ddrawing/conceptual/opengles_programmingguide/TechniquesforWorkingwithVertexData/TechniquesforWorkingwithVertexData.html After reading about someone else's problems with indices I decided to try the following. I entered the number 1000 to -> glBindBuffer(GL_ARRAY_BUFFER, 1000); in a program that only has a few dozen models and it not only crashed the program, it took down VirtualBox OS emulator running Windows7 instantly. Very nasty.

Share this post


Link to post
Share on other sites
Matthewj234    1069

Thanks for the responses guys!

 

I will check the indexCount and test it with more triangles.  

 

As for the usage of glDrawElements, I am using this system to render 3D mesh's, I was just using the 3 triangles to test the setup.

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