0

我正在学习GL2,有一个函数(来自学校)来加载纹理:

bookTex = loadTexture(gl, "wattBook.jpg");

private Texture loadTexture(GL2 gl, String filename) {
    Texture tex = null;
    try {
      File f = new File(filename);
      BufferedImage img = ImageIO.read(f); // read file into BufferedImage
      ImageUtil.flipImageVertically(img);
      tex = AWTTextureIO.newTexture(GLProfile.getDefault(), img, false);
      // Different filter settings can be used to give different effects when the texture
      tex.setTexParameteri(gl, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
      tex.setTexParameteri(gl, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);
    }
    catch(Exception e) {
      System.out.println("Error loading texture " + filename); 
    }
    return tex;
}

但是,它总是在这里抛出异常:(它的错误行太多,所以我只是复制前行)

*Error loading texture wattBook.jpg
Exception in thread "main-FPSAWTAnimator#00-Timer0" com.jogamp.opengl.util.AnimatorBase$UncaughtAnimatorException: java.lang.RuntimeException: com.jogamp.opengl.GLException: Caught NullPointerException: null on thread AWT-EventQueue-0
    at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:92)
    at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:452)
    at com.jogamp.opengl.util.FPSAnimator$MainTask.run(FPSAnimator.java:178)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)
Caused by: java.lang.RuntimeException: com.jogamp.opengl.GLException: Caught NullPointerException: null on thread AWT-EventQueue-0
    at com.jogamp.common.util.awt.AWTEDTExecutor.invoke(AWTEDTExecutor.java:58)
    at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:103)
    at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:201)
    at com.jogamp.opengl.Threading.invokeOnOpenGLThread(Threading.java:202)
    at com.jogamp.opengl.Threading.invoke(Threading.java:221)
    at com.jogamp.opengl.awt.GLCanvas.display(GLCanvas.java:505)
    at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:81)
    ... 4 more*

我在mac上的Eclipse上运行这个,GL环境已经设置好了。有人可以解释这个错误以及如何解决它吗?谢谢!

4

0 回答 0