JOGL GLU用法抛出GLException

时间:2015-03-01 15:14:19

标签: java opengl jogl

我正试图掌握JOGL(和一般的OGL),并从内存中生成以下代码(除了reshape()回调),所以我不认为OGL是完美的。但是,我对GLU对象以及gluLookAt()和gluPerspective()方法的使用导致了一个我无法理解的神秘错误。

显然我可以从教程中复制/粘贴一些工作代码,但我宁愿学会从内存中学习,所以如果有人能向我解释我做错了什么,我会非常感谢。

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

    }
}

和错误:( gluLookAt()和gluPerspective()都产生了这个)

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

1 个答案:

答案 0 :(得分:1)

您永远不会创建对象并将其分配给您的全局Object glu。

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

GLException表示它已捕获到NullPointerException。这意味着基本上执行"空的"的任何方法。对象抛出这样的异常。

尝试在init方法中添加以下内容:

glu = new GLU();