Kalisme
Kalisme

Reputation: 518

Need help understanding glDrawElements for GLES 2.0

I'm trying to learn OpenGL ES 2.0 for Android, but I am finding it hard to locate good introduction tutorials... I found This, but it only explains "glDrawArrays" and not "glDrawElements"... I'm trying to convert code from my model loading class ES 1.1, but I feel that drawing arrays might be too slow...

So what I'm asking is how would I convert the following to work in ES 2.0?

How an object is stored (seems to be fine):

    private ShortBuffer SindexBuffer;
private FloatBuffer SvertexBuffer;
private FloatBuffer StexBuffer;

private void initSprite()
{
    float[] fcol = {1,1,1,1};
    float[] coords = {
            0.5f, 0.5f, 0f,
            -0.5f, 0.5f, 0f,
            0.5f, -0.5f, 0f,
            -0.5f, -0.5f, 0f
            };
    short[] index = {
            0,1,2,
            1,3,2
            };
    float[] texCoords ={
            0,1,
            1,1,
            0,0,
            1,0
        };

    //float has 4 bytes
    ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    SvertexBuffer = vbb.asFloatBuffer();

    ByteBuffer tC = ByteBuffer.allocateDirect(texCoords.length * 4);
    tC.order(ByteOrder.nativeOrder());
    StexBuffer = tC.asFloatBuffer();
    StexBuffer.put(texCoords);
    StexBuffer.position(0);

    //short has 2 bytes
    ByteBuffer ibb = ByteBuffer.allocateDirect(index.length*2);
    ibb.order(ByteOrder.nativeOrder());
    SindexBuffer = ibb.asShortBuffer();

    SvertexBuffer.put(coords);
    SindexBuffer.put(index);

    SvertexBuffer.position(0);
    SindexBuffer.position(0);

    ByteBuffer fbb = ByteBuffer.allocateDirect(fcol.length * 4);
    fbb.order(ByteOrder.nativeOrder());
}

And how it is drawn (this is where I need help):

        public void drawSprite(GL10 gl, int tex)
    {
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, tex);      
        //defines the vertices we want to draw
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, SvertexBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, StexBuffer);

        //Draw the vertices
        gl.glDrawElements(GL10.GL_TRIANGLES,  6, GL10.GL_UNSIGNED_SHORT,  SindexBuffer);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    }

(though the texture part isn't that important yet... not up to that yet).

Thanks to anyone who can help... oh, and does anyone know any simple introductions to ES 2.0 for Android aimed at people who used 1.1 first?

Upvotes: 1

Views: 4339

Answers (1)

Abel Miranda
Abel Miranda

Reputation: 11

Translate as this

 float[] coords = {
        0.5f, 0.5f, 0f,        //index 0
        -0.5f, 0.5f, 0f,       //index 1 
        0.5f, -0.5f, 0f,       //index 2
        -0.5f, -0.5f, 0f       //index 3
}     

And you are telling to opengl that plot using this sequence of points:

0, 1, 2, 1, 3 , 2.

Here you save memory becouse indexes are short type and each vertex needs 3 floats

Regards

Upvotes: 1

Related Questions