Android - OPENGL cube is NOT in the display

Posted by Marc Ortiz on Stack Overflow See other posts from Stack Overflow or by Marc Ortiz
Published on 2012-09-28T21:35:09Z Indexed on 2012/09/28 21:37 UTC
Read the original article Hit count: 315

Filed under:
|

I'm trying to display a square on my display and i can't. Whats my problem? How can I display it on the screen (center of the screen)? I let my code below!

Here's my render class:

public class GLRenderEx implements Renderer {

    private GLCube cube;
    Context c;

    GLCube quad; // ( NEW )

    // Constructor
    public GLRenderEx(Context context) {
        // Set up the data-array buffers for these shapes ( NEW )
        quad = new GLCube(); // ( NEW )
    }

    // Call back when the surface is first created or re-created.
    @Override
       public void onSurfaceCreated(GL10 gl, EGLConfig config) {
          // NO CHANGE - SKIP
       }

    // Call back after onSurfaceCreated() or whenever the window's size changes.
    @Override
       public void onSurfaceChanged(GL10 gl, int width, int height) {
          // NO CHANGE - SKIP
       }

    // Call back to draw the current frame.
    @Override
    public void onDrawFrame(GL10 gl) {
        // Clear color and depth buffers using clear-values set earlier
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        gl.glLoadIdentity(); // Reset model-view matrix ( NEW )
        gl.glTranslatef(-1.5f, 0.0f, -6.0f); // Translate left and into the
                                                // screen ( NEW )

        // Translate right, relative to the previous translation ( NEW )
        gl.glTranslatef(3.0f, 0.0f, 0.0f);
        quad.draw(gl); // Draw quad ( NEW )
    }
}

And here is my square class:

public class GLCube {

    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
    };

    // Constructor - Setup the vertex buffer
    public GLCube() {
        // 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
    }

    // Render the shape
    public void draw(GL10 gl) {
        // Enable vertex-array and define its buffer
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        // Draw the primitives from the vertex-array directly
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }

}

Thanks!!

© Stack Overflow or respective owner

Related posts about android

Related posts about opengl