没有通过公认的api方法(glfwmakecontextcurrent)使opengl上下文成为当前上下文

4szc88ey  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(325)

我正在学习lwjgl,我得到了以下错误:线程“main”java.lang.illegalstateexception中的异常:没有opengl上下文通过gl.createcapabilities()上可识别的api方法(glfwmakecontextcurrent)成为当前的;
这是我的视窗课

public class window {
    private int width, height;
    private String title;
    private long window;
    public int frames;
    public static long time;
    public input input;
    private float bcgR, bcgG, bcgB;

    public window(int width, int height, String title) {
        this.width = width;
        this.height = height;
        this.title = title;
    }

    public void create() {
        if (! GLFW.glfwInit()) {
            System.err.println("ERROR: GLFW isn't initializied");
            return;
        }

        input = new input();

        window = GLFW.glfwCreateWindow(width, height, title, 0, 0);
        if (window == 0) {
            System.err.println("ERROR: window wasn't created");
            return;
        }
        GLFWVidMode videoMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
        GLFW.glfwSetWindowPos(window, (videoMode.width() - width)/2, (videoMode.height() - height)/2);
        GLFW.glfwMakeContextCurrent(window);
        GL.createCapabilities();

        GLFW.glfwShowWindow(window);
        GLFW.glfwSetKeyCallback(window, input.getKeybaord());
        GLFW.glfwSetCursorPosCallback(window,input.getMouseMove());     
        GLFW.glfwSetMouseButtonCallback(window, input.getMouseButtons());
        GLFW.glfwSwapInterval(1);
        time = System.currentTimeMillis();
    }

    public void update() {
        GL11.glClearColor(bcgR, bcgG, bcgB, 1.0f);
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
        GLFW.glfwPollEvents();
        frames ++;
        if (System.currentTimeMillis() > time + 1000) {
            GLFW.glfwSetWindowTitle(window, title + "|FPS: " + frames);
            time = System.currentTimeMillis();
            frames = 0;
        }
    }

    public void swapBuffers() {
        GLFW.glfwSwapBuffers(window);
    }

    public boolean shouldClose() {
        return GLFW.glfwWindowShouldClose(window);
    }

    public void destroy() {
        input.destory();
        GLFW.glfwWindowShouldClose(window);
        GLFW.glfwDestroyWindow(window);
        GLFW.glfwTerminate();
    }

    public void setBcgColor(float r, float g, float b) {
        bcgR = r;
        bcgG = g;
        bcgB = b;
    }
}

这是我的主课

public class main implements Runnable {
    public  window window;
    public final int WIDTH = 1200, HEIGHT = 800;

    public void init() {
        System.out.println("Initializing Game!");
        window = new window(WIDTH, HEIGHT, "Game");
        window.setBcgColor(1.0f, 0, 0);
        window .create();
    }

    public void run() {
        init();
        while (!window.shouldClose()) {
            GLFW.glfwWaitEvents();
            update();
            render();
            if (input.isKeyDown(GLFW.GLFW_KEY_ESCAPE)) return;
        }
        window.destroy();
    }

    private void update() {
        window.update();
        if (input.isButtonDown(GLFW.GLFW_MOUSE_BUTTON_LEFT)) {
            System.out.println("X: " + window.input.getMouseX() + "Y: " + window.input.getMouseY());
        }
    }

    private void render() {
        window.swapBuffers();
    }

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

}

我正在尝试将背景颜色更改为红色。
有人帮忙吗。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题