如何使用 LWJGL 3 绘制形状

How to draw a shape using LWJGL 3

原来我用的是:

public void DrawQuad(int x, int y, float width, float height) {
        GL11.glColor3f(0, 255, 255);

        glBegin(GL_QUADS); 

        glVertex2f(-x, y);
        glVertex2f(width, y);
        glVertex2f(width, -height);
        glVertex2f(-x, -height);

       glEnd();
}

但它只是给我这个错误:

A fatal error has been detected by the Java Runtime Environment:

EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffd3538e18a, pid=2584, tid=0x0000000000000e38

JRE version: Java(TM) SE Runtime Environment (8.0_121-b13) (build 1.8.0_121-b13) Java VM: Java HotSpot(TM) 64-Bit Server VM (25.121-b13 mixed mode windows-amd64 compressed oops) Problematic frame: C [lwjgl_opengl.dll+0xe18a]

Failed to write core dump. Minidumps are not enabled by default on client versions of Windows

An error report file with more information is saved as: J:\SkiesDE\hs_err_pid2584.log

If you would like to submit a bug report, please visit: http://bugreport.java.com/bugreport/crash.jsp The crash happened outside the Java Virtual Machine in native code. See problematic frame for where to report the bug.

来自跑步者的代码class:

public static void run() {
        System.out.println("Hello LWJGL " + Version.getVersion() + "!");

        Display window1 = new Display(640, 480);
        window1.init();

        while(window1.isRunning()) {
            /*
            glBegin(GL_QUADS); 
            glVertex2f(-0.5f, 0.5f);
            glVertex2f(0.5f, 0.5f);
            glVertex2f(0.5f, -0.5f);
            glVertex2f(-0.5f, -0.5f);
            glEnd();
            */

            window1.DrawQuad(10, 10, 50, 50);

            window1.update(); 
        }

        window1.terminate();
    }



public static void main(String[] args) {
    run();
}

来自显示的代码 Class:

public long window;
private int length;
private int heigth;

public Display(int length, int heigth) {
    this.length = length;
    this.heigth = heigth;
}

public void init() {
    if(!glfwInit()) {
        throw new IllegalStateException("Failed to initalize GLFW!");
    }

    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    window = glfwCreateWindow(length, heigth, "test", 0, 0); 
    //glfwGetPrimaryMonitor() Replace for full screen^
    if(window == 0) {
        throw new IllegalStateException("Failed to initalize Window!");
    }

    GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (videoMode.width() - length) / 2, (videoMode.height() - heigth) / 2);

    glfwShowWindow(window); 


}

public boolean isRunning() {
    return(!glfwWindowShouldClose(this.window));
}

public void update() {
    glfwSwapBuffers(window); 
    glfwPollEvents();
}

public void terminate() {
    glfwTerminate();
}

public void DrawQuad(int x, int y, float width, float height) {
    GL11.glColor3f(0, 255, 255);

    glBegin(GL_QUADS); 

    glVertex2f(-x, y);
    glVertex2f(width, y);
    glVertex2f(width, -height);
    glVertex2f(-x, -height);

    glEnd();
    //glLoadIdentity(); 
}

您没有将 OpenGL 上下文绑定到 window。您需要在 window 创建后添加 glfwMakeContextCurrent(window)