首页 > 解决方案 > 没有通过公认的 API 方法 (glfwMakeContextCurrent) 使 OpenGL 上下文成为当前的

问题描述

我正在学习 LWJGL,但出现以下错误:线程“主”java.lang.IllegalStateException 中的异常:没有通过 GL.createCapabilities() 上公认的 API 方法 (glfwMakeContextCurrent) 使 OpenGL 上下文成为最新;

这是我的窗口课

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

}

我正在尝试将背景颜色更改为红色。

请有人帮忙。

标签: javaopengllwjgl

解决方案


推荐阅读