Tauhidul Alam
Tauhidul Alam

Reputation: 103

drawing rectangle in opengl android

I am trying to draw a rectangle using android opengl. The rectangle will be formed within a colorful background. After running the code i can see a background but no rectangle inside it.

public void onDrawFrame(GL10 gl) 
{
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);        
    gl.glClearColor(0.4f, 0.5f, 0.6f, 0.5f);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);       
    float[] vertices=
    {
            -1.0f, 1.0f, 0.0f, 
            -1.0f, -1.0f, 0.0f,
            1.0f, -1.0f, 0.0f, 
            1.0f, 1.0f, 0.0f,               
    };
    short[] indices = { 0, 1, 2, 0, 2, 3 };     
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    FloatBuffer vertexBuffer = vbb.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);       
    ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    ShortBuffer indexBuffer = ibb.asShortBuffer();
    indexBuffer.put(indices);
    indexBuffer.position(0);
    gl.glFrontFace(GL10.GL_CCW);
    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glCullFace(GL10.GL_BACK);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);       
    gl.glColor4f(0.5f, 0.3f, 0.3f, 0.7f);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,
    GL10.GL_UNSIGNED_SHORT, indexBuffer);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisable(GL10.GL_CULL_FACE);
}
public void onSurfaceChanged(GL10 gl, int width, int height) 
{
    gl.glViewport(0, 0, width, height);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    GLU.gluPerspective(gl, 45.0f,
    (float) width / (float) height,
    0.1f, 100.0f);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();

}
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) 
{
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    gl.glShadeModel(GL10.GL_SMOOTH);
    gl.glClearDepthf(1.0f);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glDepthFunc(GL10.GL_LEQUAL);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);     

}

what's wrong in my code? Any suggestions please....

Upvotes: 0

Views: 13663

Answers (2)

Gunnar Karlsson
Gunnar Karlsson

Reputation: 28470

Initializing the Buffers in your onDrawFrame method doesn't look right.

I suggest you create a basic Rectangle class with vertices and indices as fields:

public class Rectangle {

    private float vertices[]={
        -1.0f, 1.0f, 0.0f,
        -1.0f,-1.0f,0.0f,
        1.0f,-1.0f,0.0f,
        1.0f,1.0f,0.0f
    };


    private short[] indices = {0,1,2,0,2,3};

    private FloatBuffer vertexBuffer;
    private ShortBuffer indexBuffer;

    public Rectangle(){
        ByteBuffer vbb  = ByteBuffer.allocateDirect(vertices.length * 4);
        vbb.order(ByteOrder.nativeOrder());
        vertexBuffer = vbb.asFloatBuffer();
        vertexBuffer.put(vertices);
        vertexBuffer.position(0);

        ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
        ibb.order(ByteOrder.nativeOrder());
        indexBuffer = ibb.asShortBuffer();
        indexBuffer.put(indices);
        indexBuffer.position(0);
    }

    public void draw(GL10 gl){
        gl.glFrontFace(GL10.GL_CCW);
        gl.glEnable(GL10.GL_CULL_FACE);
        gl.glCullFace(GL10.GL_BACK);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisable(GL10.GL_CULL_FACE);
    }

}

Add a Rectangle field to your Renderer and initialize it in the Renderer's constructor:

public MyOpenGLRenderer() {
    mRectangle = new Rectangle();
}

and call the Rectangle's draw method in your Renderer's onDrawFrame method:

    public void onDrawFrame(GL10 gl) {
    //...
    mRectangle.draw(gl);
}

Follow the first 3 parts of this tutorial for a complete solution to how it can be done.

Upvotes: 11

Tim
Tim

Reputation: 35923

Your triangles are behind the near plane. The camera is at z=0, the near plane is 0.1f, and your triangles are at z=0.

Either draw the triangles further along the -z axis, or set up a camera with z > 0.1f.

Also to draw a rectangle with GL_TRIANGLES, you need 6 vertices, not 4. You should still see something though.

Upvotes: 1

Related Questions