2

I spend almost whole day trying to render simple polygon using opengl 1.1 and vertex buffers, but no luck. I searched and searched, but I haven't found much.

This is what i have so far:

public class Polygon {

    int bufferId = 0;

    private FloatBuffer vertexBuffer;  // Buffer for vertex-array

    private float[] vertices = {  // Vertices for the square
            -1.0f, -1.0f, 0.0f,  // 0. left-bottom
            1.0f, -1.0f, 0.0f,  // 1. right-bottom
            -1.0f, 1.0f, 0.0f,  // 2. left-top
            1.0f, 1.0f, 0.0f   // 3. right-top
    };

    private ByteBuffer indexBuffer;

    private byte[] indices = {0, 1, 2, 3}; // Indices to above vertices (in CCW)


    // Constructor - Setup the vertex buffer
    public Polygon() {
        // Setup vertex array buffer. Vertices in float. A float has 4 bytes
        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
        vbb.order(ByteOrder.nativeOrder()); // Use native byte order
        vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float
        vertexBuffer.put(vertices);         // Copy data into buffer
        vertexBuffer.position(0);           // Rewind


        indexBuffer = ByteBuffer.allocateDirect(indices.length);
        indexBuffer.put(indices);
        indexBuffer.position(0);


        int[] buffers = new int[1];
        GLES11.glGenBuffers(1, buffers, 0);
        bufferId = buffers[0];

        GLES11.glBindBuffer(GLES11.GL_ARRAY_BUFFER, bufferId);
        GLES11.glBufferData(GLES11.GL_ARRAY_BUFFER, vertices.length, vertexBuffer, GLES11.GL_STATIC_DRAW);
        GLES11.glBindBuffer(GLES11.GL_ARRAY_BUFFER, 0);
    }


    // Render the shape
    public void draw(GL10 gl) {

        GLES11.glBindBuffer(GLES11.GL_ARRAY_BUFFER, bufferId);

        GLES11.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        GLES11.glVertexPointer(3, GLES11.GL_FLOAT, 0, 0);
        GLES11.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length);
        GLES11.glDisableClientState(GL10.GL_VERTEX_ARRAY);

        GLES11.glBindBuffer(GLES11.GL_ARRAY_BUFFER, 0);
    }
}

It doesn't render anything and there is no relevant error in android logcat. I ommited rest of the code. The problem is obviously in this class, since it works fine when I change draw method to this:

public void draw(GL10 gl) {

            GLES11.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            GLES11.glVertexPointer(3, GLES11.GL_FLOAT, 0, vertexBuffer);
            GLES11.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length);
            GLES11.glDisableClientState(GL10.GL_VERTEX_ARRAY);

        }

So, what am I doing wrong?

beegor
  • 251
  • 1
  • 3
  • 13

1 Answers1

0
  1. Don't look in the logCat for errors, you want to check for OpenGL errors with glGetError(), and check that the return value is zero (no error), or nonzero (error).

  2. vertices.length is the wrong argument to glDrawArrays. You want to supply the number of vertices, not the number of floats. It should be vertices.length/3 (3 floats per vertex). You're currently drawing way past your array into some garbage data, so I'm not sure what kind of consequences that could have.

Tim
  • 35,413
  • 11
  • 95
  • 121
  • Tim, thanks for your answer. I wasn't sure what is right value for that, however I tried to put vertices.length/3 before, and again now to be sure, unfortunately it doesn't work when using vertex buffer. – beegor Jul 23 '12 at 15:15
  • I found it. The problem was in glBufferData call. Buffer size argument was wrong. I multiplied vertices number by 4 (since float has 4 bytes) and it works now! Accepting Tim's answer because it led me to the solution. – beegor Jul 23 '12 at 15:50