0
votes

I've done a lot of DirecX work with meshes but I can't wrap my head around what's going on with my triangles in OpenGL ES. I've tried using some example boxes from the web as well as the data for a box I had in an old project but they always come out wrong, and often invisible.

Here's an example.

with the verts:

-25.0 -25.0 25.0 
25.0 -25.0 25.0 
-25.0 25.0 25.0 
25.0 25.0 25.0 

and indices (starting at 0):

2 1 3 

I get a triangle who's right angle points to a completely different corner of the screen than the one it gives me if I use the indices:

3 1 2

Based on my understanding, it shouldn't even be showing a triangle if the vertices are listed in CW order with my current config. Any clues on what's going on with these verts???

Btw, here's my drawing code:

gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
//gl.glColor4f(1.0f,0.0f,1.0f,1.0f);
gl.glDrawElements(GL10.GL_TRIANGLES, indexCount, GL10.GL_UNSIGNED_SHORT, indexBuffer);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);

edit:

3 1 2 draws a corner pointing to the top left and 2 1 3 draws one pointing to the top right, but only whichever one is listed first is drawn. This is getting so weird...

{
public void initAndStuff(...)
{
    try{
        //
        ...
        They get initialized up here...
        This is also where the external data is fed in (but is erased below)
        //

        ////////////////////////////////
        vertexBuffer.clear();
        indexBuffer.clear();

        vertexBuffer.put(-25.0f);
        vertexBuffer.put(-25.0f);
        vertexBuffer.put(25.0f);

        vertexBuffer.put(25.0f);
        vertexBuffer.put(-25.0f);
        vertexBuffer.put(25.0f);

        vertexBuffer.put(-25.0f);
        vertexBuffer.put(25.0f);
        vertexBuffer.put(25.0f);

        vertexBuffer.put(25.0f);
        vertexBuffer.put(25.0f);
        vertexBuffer.put(25.0f);


        indexBuffer.put(3);
        indexBuffer.put(1); // points toward top left
        indexBuffer.put(2);
        indexBuffer.put(2);
        indexBuffer.put(1); // points toward top right
        indexBuffer.put(3);


        indexIndex = 6;
        //////////////////////////////////


        // wrap things up
        vertexBuffer.position(0);
        indexBuffer.position(0);

    }
    catch(Exception e)
    {
        e.printStackTrace();
        Toast.makeText(context,"exception in loadfile",Toast.LENGTH_LONG).show();
        return false;
    }
    return true;
}

public final void draw(GL10 gl)
{
    gl.glFrontFace(GL10.GL_CCW);
    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glCullFace(GL10.GL_BACK);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    //gl.glColor4f(1.0f,0.0f,1.0f,1.0f);
    gl.glDrawElements(GL10.GL_TRIANGLES, indexIndex, GL10.GL_UNSIGNED_SHORT, indexBuffer);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisable(GL10.GL_CULL_FACE);

}
}
1
What data do vertexBuffer and indexBuffer contain? Of course we can guess it, but seeing it would make sure they contain correct data and are of correct type. And the rest of the code might also be interresting. - Christian Rau
I have actually checked the buffers and they do contain the correct data in the order listed. I would put all my code up here but it would be too easy to get lost in the massive procedure I wrote for reading the data from a file (which I've tested extensively). I've also tried manually loading the data into the buffers (not from a separate file) and the results are equally confusing. - user980058
But in this case you might have to be satisfied with an unanswered question, as the code I can see looks reasonable. A minimal example that reproduces the problem would be enough. - Christian Rau
I've discovered that for some reason, the order in which the triangles are listed seems to be effecting the render... Keeps experimenting... - user980058
updated with isolated example and more info - user980058

1 Answers

0
votes

It was because I initialized my short buffer as an int buffer.