1

I recevie 0 errors but I get this in the console:

>Exception in thread "main" java.lang.NullPointerException
  >>at org.lwjgl.opengl.GL11.glMatrixMode(GL11.java:2052)
  >>>at game.engine.GameLoop.start(GameLoop.java:22)
  >>>>at game.engine.GameLoop.main(GameLoop.java:15)  

I tried to put the actual value into "glMatrixMode()" instead of "GL11.GL_PROJECTION" but it returns same Exception.

   import org.lwjgl.LWJGLException;
    import org.lwjgl.opengl.DisplayMode;
    import org.lwjgl.opengl.GL11;
    import org.lwjgl.util.Display;


    public class GameLoop 
    {
        //Main
            public static void main(String[] argv)
            {
                GameLoop.start();
            }

            //Metodo che gestisce il loop
            public static void start() 
            {
                //Inizializzazione OpenGL
                GL11.glMatrixMode(GL11.GL_PROJECTION);
                GL11.glLoadIdentity();
                GL11.glOrtho(0, 800, 600, 0, 1, -1);
                GL11.glMatrixMode(GL11.GL_MODELVIEW);

                try 
                {
                    Display.setDisplayMode(new DisplayMode(800, 600));
                    Display.create();
                } catch (LWJGLException e) 
                {
                    e.printStackTrace();
                    System.exit(0);
                }

                while(!Display.isCloseRequested())
                {
                    Entità.pulisci();
                    Entità.colora();
                    Entità.disegna();
                    Display.update();
                }
            }

    }




    import org.lwjgl.opengl.GL11;

    public class Entità 
    {
        //Disegna un poligono
            public static void disegna()
            {
                GL11.glBegin(GL11.GL_QUADS);
                GL11.glVertex2f(100,100);
                GL11.glVertex2f(200,100);
                GL11.glVertex2f(200,200);
                GL11.glVertex2f(100,200);
                GL11.glEnd();
            }

            //Pulisce il buffer
            public static void pulisci()
            {
                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); 
            }

            //Setta il colore al poligono
            public static void colora()
            {
                GL11.glColor3f(0.5f,0.5f,1.0f);
            }

    }
4

1 回答 1

3

我对 Java 或 LWJGL 没有任何经验,但从您的代码看来

Display.setDisplayMode(new DisplayMode(800, 600));
Display.create();

创建实际的显示画布(或者您可以调用它),它还设置了 OpenGL 上下文。因此,在这些函数之前调用任何 OpenGL 函数(如GL11.glMatrixMode)可能会导致一些错误,例如在您的情况下是空指针异常。

所以试着把这四个功能放在显示创建之后。您正在对 OpenGL 上下文进行更改,因此您首先需要一个有效的上下文。

于 2012-06-08T11:03:31.690 回答