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);
}
}
vertexBufferandindexBuffercontain? 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