如何使用LWJGL绘制形状3

时间:2017-07-14 01:32:52

标签: java eclipse lwjgl

我最初使用过:

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();
}

但它只是给了我这个错误:

  

Java Runtime Environment检测到致命错误:

     

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

     

JRE版本:Java(TM)SE运行时环境(8.0_121-b13)(版本1.8.0_121-b13)   Java VM:Java HotSpot(TM)64位服务器VM(25.121-b13混合模式windows-amd64压缩oops)   有问题的框架:   C [lwjgl_opengl.dll + 0xe18a]

     

无法编写核心转储。默认情况下,在Windows的客户端版本

上未启用小型转储      

包含更多信息的错误报告文件保存为:   j:\ SkiesDE \ hs_err_pid2584.log

     

如果您想提交错误报告,请访问:     http://bugreport.java.com/bugreport/crash.jsp   崩溃发生在Java虚拟机外部的本机代码中。   查看有问题的框架,了解报告错误的位置。

来自跑步者类的代码:

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();
}

展示位代码:

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(); 
}

1 个答案:

答案 0 :(得分:1)

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