translate iphone opengl es code into libgdx code

admin

Administrator
Staff member
in this posting <a href="http://www.badlogicgames.com/wordpress/?p=504" rel="nofollow">http://www.badlogicgames.com/wordpress/?p=504</a> Mr. libgdx Mario writes:

<blockquote>
OpenGL from the ground up: an extremely well written tutorial series on OpenGL ES 1.x. Covers all the basics you need to get started with OpenGL. Note that the tutorial is written for the IPhone and uses Objective C/C++. This shouldn’t be a big problem though as the API is the same.
</blockquote>

To my shame, I wasn't able to get a libgdx aequivalent of the very first example in that tutorial running, which is this:

Code:
- (void)drawView:(GLView*)view;
{
    Vertex3D    vertex1 = Vertex3DMake(0.0, 1.0, -3.0);
    Vertex3D    vertex2 = Vertex3DMake(1.0, 0.0, -3.0);
    Vertex3D    vertex3 = Vertex3DMake(-1.0, 0.0, -3.0);
    Triangle3D  triangle = Triangle3DMake(vertex1, vertex2, vertex3);

    glLoadIdentity();
    glClearColor(0.7, 0.7, 0.7, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColor4f(1.0, 0.0, 0.0, 1.0);
    glVertexPointer(3, GL_FLOAT, 0, &amp;triangle);
    glDrawArrays(GL_TRIANGLES, 0, 9);
    glDisableClientState(GL_VERTEX_ARRAY);
}

my code...

Code:
public void render () {
    Gdx.gl11.glLoadIdentity();
    Gdx.gl11.glRotatef(rotation, 0.0f, 0.0f, 1.0f);
    Gdx.gl11.glClearColor((float)0.7, (float)0.7, (float)0.7, (float)1.0);
    Gdx.gl11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    Gdx.gl11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
    Gdx.gl11.glColor4f((float)1.0, (float)0.0, (float)0.0, (float)1.0);
    Gdx.gl11.glVertexPointer(3, GL11.GL_FLOAT, BYTES_PER_VERTEX, vertices);
    Gdx.gl11.glDrawArrays(GL11.GL_TRIANGLES, 0, 9);
    Gdx.gl11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
}

The problem here is 'vertices'. I have no idea what that should be. After lots of googling I came up with:

Code:
final int BYTES_PER_VERTEX = (3 + 4) * 4;

public void create () {
    ByteBuffer buffer = ByteBuffer.allocateDirect(BYTES_PER_VERTEX * 3);
    buffer.order(ByteOrder.nativeOrder());
    vertices = buffer.asFloatBuffer();

    float[] verts = {
            0.0f, 1.0f, 0.0f, 1, 0, 0, 0,
            1.0f, 0.0f, 0.0f, 0, 1, 0, 0,
            -1.0f, 0.0f, 0.0f, 0, 0, 1, 0};

    vertices.put(verts);
    vertices.flip();
}

. and that seems to be displaying a triangle, but the values for the vertices are not the same as in the original example (z value is 0 instead of -3, in which case I wouldn't see anything).

Can anyone shed any light on vertices?