0

This is the code I wrote in order to get 2D drawing to work. The activity and the view and renderer are setup the correct way. I get a completely black screen, but with no polygons on it. Nothing is drawn. This is about to drive me crazy...

public class OpenGLActivity extends Activity {

    private GLSurfaceView surfaceView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN
                );

        this.surfaceView = new OpenGLSurfaceView(this);
        setContentView(this.surfaceView);
    }

    @Override
    protected void onPause() {
        super.onPause();
        surfaceView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        surfaceView.onResume();
    }
}

public class OpenGLSurfaceView extends GLSurfaceView {

    private OpenGLSurfaceRenderer renderer;

    public OpenGLSurfaceView(Context context) {
        super(context);
        this.renderer = new OpenGLSurfaceRenderer();
        setRenderer(renderer);
    }

}

public class OpenGLSurfaceRenderer implements Renderer {

    private FloatBuffer vertexBuffer;
    private float[] vertices = {
            0.0f, 1.0f, 0.0f,
            0.0f, 0.0f, 0.0f,
            1.0f, 1.0f, 0.0f,
            1.0f, 0.0f, 0.0f,
            0.0f, 10.0f, 0.0f,
            0.0f, 0.0f, 0.0f,
            10.0f, 10.0f, 0.0f,
            10.0f, 0.0f, 0.0f
    };

    public OpenGLSurfaceRenderer() {
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * (Float.SIZE >> 3));
        vertexBuffer = byteBuffer.asFloatBuffer();
        vertexBuffer.put(vertices);
        vertexBuffer.position(0);
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {

        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

        gl.glClearColor(0.0f, 0.0f, 0.0f, 1);
        gl.glShadeModel(GL10.GL_FLAT);
        gl.glDisable(GL10.GL_DEPTH_TEST);
        gl.glEnable(GL10.GL_TEXTURE_2D);

        gl.glDisable(GL10.GL_DITHER);
        gl.glDisable(GL10.GL_LIGHTING);

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    }

    public void onDrawFrame(GL10 gl) {

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

        gl.glTranslatef(0.0f, 0.0f, 0.5f);
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {

        gl.glViewport(0, 0, width, height);

        // Clear the projection matrix
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();

        // Set up orthographic projection mode (2D drawing)
        gl.glOrthof(0, width, height, 0, 0, 1);

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

}
2
  • Where is your activity code? ...Instantiate a GLSurfaceView and set its renderer to OpenGLSurfaceRenderer , Feb 11, 2012 at 16:05
  • Added my activity and view code. Feb 11, 2012 at 16:10

1 Answer 1

1

I see four problems:

  1. The last parameter to glDrawArrays() should be 8, not 4. It's the number of vertices you are providing, not the number of triangles in your triangle strip.

  2. Your orthographic projection makes your world units equivalent to pixels. Your first 4 vertexes describe triangles that are 1-pixel wide. Maybe you are getting a single white pixel in the bottom left corner of the screen?

  3. You advance the model projection along the Z-axis every frame. After 2 frames it will be out of the view frustrum.

  4. Most importantly, you do not set the right ordering on your ByteBuffer. Insert this line into your OpenGlRenderer() constructor:

    public OpenGLSurfaceRenderer() { ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * 4); byteBuffer.order(ByteOrder.nativeOrder()); <------ vertexBuffer = byteBuffer.asFloatBuffer(); vertexBuffer.put(vertices); vertexBuffer.position(0); }

9
  • The reason I have 8 vertices, is that I have tried to create a rectangle that is 1x1 and another rectangle that is 10x10 pixels wide. I added them to the same vertexBuffer in order to be able to switch between then easily. I have also tried to draw a 100x100 and I have used negative coordinate values, but still no results.. Feb 11, 2012 at 16:26
  • If I change gl.glClearColor I do get another color on my screen, so it seems that OpenGL does something. Feb 11, 2012 at 16:35
  • I see you have texturing enabled but don't do any texturing. Suggest removing gl.glEnable(GL10.GL_TEXTURE_2D); or changing it to a glDisable. Feb 11, 2012 at 16:49
  • Oh look, you're also advancing 0.5 units along Z every single frame. So you won't see anything at all after 2 frames. Feb 12, 2012 at 10:19
  • Tried to comment out gl.glTranslatef(0.0f, 0.0f, 0.5f), but with no luck, I also thought that the translation matrix was not applied to the vertex buffer memory, but only applied by openGL "under the hood". Feb 12, 2012 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.