0

I'm trying to create a three-dimensional cube with OpenGL. Dimensional cube is obtained. Trying to add another side of the cube. And here I see this error:

LogCat tell's me that

triangle1Buffer = NULL

    FATAL EXCEPTION: GLThread 270
    java.lang.NullPointerException
            at com.google.android.gles_jni.GLImpl.glVertexPointer(GLImpl.java:1122)
            at com.podlinov.elseapp.app.Renderer.onDrawFrame(Renderer.java:94)

Code:

public class Renderer implements GLSurfaceView.Renderer {

    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {

    }

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

        gl.glViewport(0, 0, width, height);
        float ratio = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
    }

    float [] triangle = {
            0, 0, 0,   // A
            1, 0, 0,   // B
            0, 1, 0 ,  // C

            1, 0, 0,   // B
            1, 1, 0 ,  // D
            0, 1, 0 ,  // C
    };

    float [] triangle2 = {
            0, 0, 0,   // A
            1, 0, 0,   // B
            0, 1, 0,  // C
    };


    FloatBuffer triangleBuffer;
    FloatBuffer triangle1Buffer;

    public Renderer() {

        ByteBuffer bb = ByteBuffer.allocateDirect(6*3*4);
        bb.order(ByteOrder.nativeOrder());
        triangleBuffer = bb.asFloatBuffer();
        triangleBuffer.put(triangle);
        triangleBuffer.position(0);

        ByteBuffer bb2 = ByteBuffer.allocateDirect(3*3*4);
        bb2.order(ByteOrder.nativeOrder());
        triangleBuffer = bb2.asFloatBuffer();
        triangleBuffer.put(triangle2);
        triangleBuffer.position(0);

    }

    @Override
    public void onDrawFrame(GL10 gl) {

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

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

        gl.glColor4f(1,0,1,1);
        gl.glTranslatef(-1.5f, 0.5f, -3f);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleBuffer);
        gl.glDrawArrays(GL10.GL_TRIANGLES, 0,6);


        gl.glColor4f(0,0,1,1);
        gl.glTranslatef(1.5f, 0, -3f);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangle1Buffer); // NULL POINTER EXCEPTION triangle1Buffer =NULL
        gl.glDrawArrays(GL10.GL_TRIANGLES, 0,3);
    }

}
genpfault
  • 47,669
  • 9
  • 68
  • 119
Garf1eld
  • 568
  • 2
  • 7
  • 21

1 Answers1

0

I think your problem is here:

    ByteBuffer bb2 = ByteBuffer.allocateDirect(3*3*4);
    bb2.order(ByteOrder.nativeOrder());
    triangleBuffer = bb2.asFloatBuffer();
    triangleBuffer.put(triangle2);
    triangleBuffer.position(0);

You should have changed it to triangle1Buffer Im guessing?

Monkey
  • 34
  • 6
  • No problem. You could actually use OpenGL's GL_QUADS instead of GL_TRIANGLES to draw a cube, it would require less sides to draw (since a cube has 6 square sides instead of 12 triangle ones.) Just be sure to add vertices to the float arrays and change the buffer sizes - which by the way, 3*3*4 is 36, but your triangle2 array only has 9 values in. Doesn't it just need to be 3*3, or am I being dumb? – Monkey Jun 11 '14 at 14:09
  • Value of triangle2 is just for exaple. – Garf1eld Jun 11 '14 at 14:37
  • @Monkey: The question has `opengl-es` and `android` tags, so it deals with OpenGL ES. There is no `GL_QUADS` in ES, and it's also deprecated from the core profile in regular OpenGL. – Reto Koradi Jun 11 '14 at 18:17
  • Okay, I didn't know that sorry. I'm new to stack exchange, and don't know a lot about OpenGL. It's probably only deprecated in newer versions though, GL11 still contains it which is what I have used before, which is why I assumed it would be here too. – Monkey Jun 11 '14 at 18:27