Sammdahamm
Sammdahamm

Reputation: 11

JOGL GLU usage throwing GLException

I'm trying to get to grips with JOGL (and OGL in general), and have produced the following code from memory (apart from the reshape() callback), so I don't expect the OGL to be perfect. However, my usage of the GLU object and the gluLookAt() and gluPerspective() methods is causing a cryptic error that I can make absolutely no sense of.

Obviously I could just go and copy/paste some working code from a tutorial, but I'd rather learn to be able to work from memory, so if anyone could explain to me what I've done wrong, I'd greatly appreciate it.

import java.awt.Dimension;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.swing.*;

import com.jogamp.opengl.util.gl2.GLUT;

public class Sandbox extends GLCanvas implements GLEventListener
{
    GLU glu;

    public static void main(String[] args) 
    {
        GLCanvas canvas = new Sandbox();
        canvas.setPreferredSize(new Dimension(640, 480));
        JFrame frame = new JFrame("OGL");
        frame.getContentPane().add(canvas);

        frame.pack();
        frame.setVisible(true);
    }

    public Sandbox()
    {
        this.addGLEventListener(this);
    }

    @Override
    public void init(GLAutoDrawable drawable) 
    {
        GL2 gl = drawable.getGL().getGL2();
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
        glu.gluLookAt(0.0f, 2.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
    }

    @Override
    public void dispose(GLAutoDrawable drawable) 
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void display(GLAutoDrawable drawable) 
    {
        GL2 gl = drawable.getGL().getGL2();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);

        gl.glLoadIdentity();

        GLUT glut = new GLUT();
        glut.glutSolidSphere(3.0f, 50, 50);
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) 
    {
         GL2 gl = drawable.getGL().getGL2();  // get the OpenGL 2 graphics context

          if (height == 0) height = 1;   // prevent divide by zero
          float aspect = (float)width / height;

          // Set the view port (display area) to cover the entire window
          gl.glViewport(0, 0, width, height);

          // Setup perspective projection, with aspect ratio matches viewport
          gl.glMatrixMode(GL2.GL_PROJECTION);  // choose projection matrix
          gl.glLoadIdentity();             // reset projection matrix
          glu.gluPerspective(45.0, aspect, 0.1, 100.0); // fovy, aspect, zNear, zFar

          // Enable the model-view transform
          gl.glMatrixMode(GL2.GL_MODELVIEW);
          gl.glLoadIdentity(); // reset

    }
}

and the error: (both gluLookAt() and gluPerspective() produce this)

Exception in thread "AWT-EventQueue-0" javax.media.opengl.GLException: Caught NullPointerException: null on thread AWT-EventQueue-0
    at javax.media.opengl.GLException.newGLException(GLException.java:75)
    at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1311)
    at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:1131)
    at javax.media.opengl.awt.GLCanvas$11.run(GLCanvas.java:1394)
    at javax.media.opengl.Threading.invoke(Threading.java:223)
    at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:525)
    at javax.media.opengl.awt.GLCanvas.paint(GLCanvas.java:579)
    at sun.awt.RepaintArea.paintComponent(Unknown Source)
    at sun.awt.RepaintArea.paint(Unknown Source)
    at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at Sandbox.init(Sandbox.java:39)
    at jogamp.opengl.GLDrawableHelper.init(GLDrawableHelper.java:640)
    at jogamp.opengl.GLDrawableHelper.init(GLDrawableHelper.java:662)
    at javax.media.opengl.awt.GLCanvas$9.run(GLCanvas.java:1366)
    at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1275)
    ... 28 more

Upvotes: 0

Views: 1781

Answers (1)

peter
peter

Reputation: 55

You never create an object and assign it to your global Object glu.

Exception in thread "AWT-EventQueue-0" javax.media.opengl.GLException: Caught NullPointerException: null on thread AWT-EventQueue-0

The GLException says that it has caught an NullPointerException. That means basically that executing any method of an "empty" object throws such an Exception.

Try to add following in the init method:

glu = new GLU();

Upvotes: 1

Related Questions