views:

381

answers:

1

I'm putting together a simple test made up of two tutorials available online for OpenGL ES on Android. This is really just so that I can learn about the basics of OpenGL ES to better understand how I have to design my program.

Right now, when it tries to render, the mouse movement effect works, but I get no square drawn on the screen.

Here are the two source files I'm dealing with:

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;

//Simple clear-screen renderer.
class ClearRenderer implements GLSurfaceView.Renderer {

    private float mRed;
    private float mGreen;
    private float mBlue;

    private TileUI testTile;

    public ClearRenderer() {

        this.testTile = new TileUI();

    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        gl.glShadeModel(GL10.GL_SMOOTH);
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
        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); 

    }

    public void onSurfaceChanged(GL10 gl, int w, int h) {

        gl.glViewport(0, 0, w, h);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();

        // Calculate The Aspect Ratio Of The Window
        GLU.gluPerspective(gl, 45.0f, (float)w / (float)h, 0.1f, 100.0f);

        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();

    }

    public void onDrawFrame(GL10 gl) {

        gl.glClearColor(mRed, mGreen, mBlue, 1.0f);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glTranslatef(0.0f, 0.0f, 0.0f);
        testTile.draw(gl);

    }

    public void setColor(float r, float g, float b) {
        mRed = r;
        mGreen = g;
        mBlue = b;
    }

}

The second one is the tile object itself:

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class TileUI {

    private float[] vertices = {
        -1.0f, -1.0f,  0.0f,        // Bottom left
         1.0f, -1.0f,  0.0f,        // Bottom right
        -1.0f,  1.0f,  0.0f,        // Top left
         1.0f,  1.0f,  0.0f         // Top right
    };

    private FloatBuffer vertexBuffer;

    public TileUI() {

        ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
        byteBuf.order(ByteOrder.nativeOrder());
        vertexBuffer = byteBuf.asFloatBuffer();
        vertexBuffer.put(vertices);
        vertexBuffer.position(0);

    }

    // Performs the actual drawing of a Tile.
    public void draw(GL10 gl) {

        // Set the face rotation.
        gl.glFrontFace(GL10.GL_CW);
        // Point to our vertex buffer.
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

        // Enable vertex buffer.
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        // Draw the vertices as a triangle strip.
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
        // Disable the client state before leaving.
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);      

    }

}

I will definitely apologize in advance, this is a bit of a Frankenstein, any help and explanation about what's going on here would be greatly appreciated.

+1  A: 

I think that the line that's causing you problems is gl.glTranslatef(0.0f, 0.0f, 0.0f);. That means that you are drawing your quad at the origin and that's also where the camera is so you don't see it. Try something like gl.glTranslatef(0.0f, 0.0f, -10.0f);. That should move the quad into the screen and put it in front of your camera. Alternatively, if you're doing 2D drawing you could use glOrtho() instead of gluPerspective() and that will give you an orthographic, or flat, projection.

CaseyB
Do you know of a good tutorial on really grasping the differences of projections? Especially in an OpenGL (ES) context?I'm getting a pretty decent handle on all of this, but I'd like to have some things to look deeper into the meaning of all the various function calls. I only truly understand about 20% of the gl.* calls made here.
Omega
The NeHe Tutorials (http://nehe.gamedev.net/) are the defacto standard when it comes to learning OpenGL. You can get a really good grasp on the basics as far as setup and what the majority of the calls do and 90% of that should transfer to OpenGL ES. It seems that some one is working on porting the tutorials to Android, but I don't know how complete they are. (http://insanitydesign.com/wp/projects/nehe-android-ports/)
CaseyB
I'm aware of those tutorials, though not much is explained there and I'm finding a lot of details get lost in the translation.Hopefully something crops up soon, just to make it easier to absorb.Thanks!
Omega
gl.glClearDepthf also looks like it might help out if I initialize with it, no?
Omega
`glClearDepth()` is not really going to help in this case. It's only used if you don't want to clear the whole depth buffer, which is kind of a rare case. Did the change to the `glTranslate()` show the quad? As far as having something that will help you learn OpenGL those tutorials, Google and playing around are probably the best you're going to find. There are books available, but they are mostly just reference books.
CaseyB
I agree, so far I'm making decent progress with Stack Overflow, the OpenGL ES API and another port of NeHe for Android: http://code.google.com/p/nehe-android/. Your suggestion was the solution, thanks so much! I have more questions coming up. :)
Omega