gpt4 book ai didi

java - JOGL GLU 使用抛出 GLException

转载 作者:行者123 更新时间:2023-12-01 11:51:27 25 4
gpt4 key购买 nike

我正在尝试掌握 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

最佳答案

您永远不会创建一个对象并将其分配给全局对象 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();

关于java - JOGL GLU 使用抛出 GLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28795174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com