Search code examples
opengl-escubeindices

android/ openGL cube with GL_TRIANGLE_FAN


I am some what new to openGL ES and I want to build a simple cube, but seem to be having some problems letting the indice byte buffer that I want to have 4 different TRIANGLE_FANs, how would I go about doing this/ rewriting my code:

public class GLCube{

private float vertices[] = {
    1, 1, -1, //p0 - topFrontRight
    1, -1, -1, //p1 bottomFront Right
    -1, -1, -1, //p2 bottom front left
    -1, 1, -1, //p3 front top left
    1, 1, 1, //p4 - topBackRight
    1, -1, 1, //p5 bottomBack Right
    -1, -1, 1, //p6 bottom back left
    -1, 1, 1, //p7 front back left
};

private FloatBuffer vertBuff;

private short[] pIndex = { 
        0, 4, 1, 3, //i0 fan of top front right
        5, 4, 1, 6, //i1 fan from bottom right back
        2, 1, 3, 6, //i2 fan from bottom left front
        7, 3, 4, 6 //i3 fan from top left back
};

private ShortBuffer pBuff;

public GLCube(){

    ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
    bBuff.order(ByteOrder.nativeOrder());
    vertBuff = bBuff.asFloatBuffer();
    vertBuff.put(vertices);
    vertBuff.position(0);


    ByteBuffer pbBuff = ByteBuffer.allocateDirect(pIndex.length * 2);
    pbBuff.order(ByteOrder.nativeOrder());
    pBuff = pbBuff.asShortBuffer();
    pBuff.put(pIndex);
    pBuff.position(0);
}

public void draw(GL10 gl){
    gl.glFrontFace(GL10.GL_CW);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
    gl.glDrawElements(GL10.GL_TRIANGLE_FAN, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

}
}

I assume you have to use GL10.GL_PRIMITIVE_RESTART, but with android, I have don't think this exists...


Solution

  • You are correct in your assumption, without primitive_restart extension you cannot render multiple triangle fans in one call to glDrawElements. What you can do is use glMultiDrawElements, where you can draw multiple primitives in one call, but it's use is a bit cumbersome and I'm not sure if it really gives you a peformance benefit over a single triangle list (especially with this simple cube).

    The general tip I would give you is, stay away from complicated primitives like triangle strips and fans, because they are only of use for some special small geometries (those where you won't get any performance benefit, anyway). Larger general meshes require much more effort to really gain an advantage from those primitive types, if any. Just use a simple indexed triangle list and you're fine.

    By the way, if you really want a "sophisticated" tessellation of a cube, you can build a cube from a single triangle strip, but the solution to this is up to you or google.

    EDIT: Just looked up the GLES spec and it seems the glMultiDraw... functions were removed in ES. So there's no way around a single indexed triangle list (or the triangle strip solution), at least if you want to draw the cube in one call, which is advisable.