Why does my android openGL ES test renderer crash

1.8k views Asked by At

Once again I'm trying to get into openGL, but as usual I choke when passing around vertices/vertixes/whatever and every little detail can lead to disaster (wrong format, initialization not properly set up, where memory are saved, etc.).

My main goal is to use openGL for 2D graphics to speed up performance compared to regular cpu drawing.

Anyways, my openGL Renderer looks like this:

package com.derp.testopengl;


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

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;

public class OpenGLRenderer implements Renderer {

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // Set the background color to black ( rgba ).
        gl.glClearColor(1.0f, 0.0f, 0.0f, 0.5f);  // OpenGL docs.
        // Enable Smooth Shading, default not really needed.
        gl.glShadeModel(GL10.GL_SMOOTH);// OpenGL docs.
        // Depth buffer setup.
        gl.glClearDepthf(1.0f);// OpenGL docs.
        // Enables depth testing.
        gl.glEnable(GL10.GL_DEPTH_TEST);// OpenGL docs.
        // The type of depth testing to do.
        gl.glDepthFunc(GL10.GL_LEQUAL);// OpenGL docs.
        // Really nice perspective calculations.
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, // OpenGL docs.
                          GL10.GL_NICEST);
    }


    public void onDrawFrame(GL10 gl) {
        // Clears the screen and depth buffer.
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | // OpenGL docs.
                           GL10.GL_DEPTH_BUFFER_BIT);

        // Define the points of my triangle
        float floatbuff[] = {
                1.0f,0.0f,0.0f,
                0.0f,1.0f,0.0f,
                -1.0f,0.0f,0.0f
                };
        // Create memory on the heap
        ByteBuffer vbb = ByteBuffer.allocateDirect(3 * 3 * 4);
        vbb.order(ByteOrder.nativeOrder());
        FloatBuffer vertices = vbb.asFloatBuffer();

        // Insert points into floatbuffer
        vertices.put(floatbuff);
        // Reset position
        vertices.position(0);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

        // Change color to green
        gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);

        // Pass vertices to openGL
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertices);

        // Draw 'em
        gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);

        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }


    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // Sets the current view port to the new size.
        gl.glViewport(0, 0, width, height);// OpenGL docs.
        // Select the projection matrix
        gl.glMatrixMode(GL10.GL_PROJECTION);// OpenGL docs.
        // Reset the projection matrix
        gl.glLoadIdentity();// OpenGL docs.

        // Should give a 2D coordinate system that responds to the screen
        gl.glOrthof(0.0f, width, 0.0f, height, 0, 200.0f);

        gl.glMatrixMode(GL10.GL_MODELVIEW); 
        gl.glLoadIdentity();// OpenGL docs.
    }
}

It currently crash on the line:

gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);

With an IndexOutOfBoundsException, but I'm sure there are more problems with the code.

Thankful for any help!

3

There are 3 answers

0
Brian On

You probably already solved it but i thought i put in the answer for somebody else. When you called this line:

gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);

You are saying that there are 3 triangles in the vertices buffer. But there is actually 1 triangle. You should replace 3 with:

vertices.length / 3

That way, if you put in any more points for more polygons, it will render it correctly. Hope this helps.

-Brian

PS: I am currently learning how to use opengl with android. So I am figuring out all these little problems too. Good luck :)

0
RicardoSBA On

Try changing this:

// Create memory on the heap
ByteBuffer vbb = ByteBuffer.allocateDirect(3 * 3 * 4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer vertices = vbb.asFloatBuffer();

// Insert points into floatbuffer
vertices.put(floatbuff);
// Reset position
vertices.position(0);

to this:

FloatBuffer vertices = FloatBuffer.wrap(floatbuff);
0
sravan On

it may not be answere but a advice... don't put this below code in OnDrawFrame, since your triangle co-ordinates don't change... do this stuff in onSurfaceCreated. make FloatBuffer a member variable of the class

 // Define the points of my triangle
    float floatbuff[] = {
            1.0f,0.0f,0.0f,
            0.0f,1.0f,0.0f,
            -1.0f,0.0f,0.0f
            };
    // Create memory on the heap
    ByteBuffer vbb = ByteBuffer.allocateDirect(3 * 3 * 4);
    vbb.order(ByteOrder.nativeOrder());
    FloatBuffer vertices = vbb.asFloatBuffer();

    // Insert points into floatbuffer
    vertices.put(floatbuff);

take a look at this class TriangleRenderer.java it will give you basic