你可以在 opengl 中混合着色器和非着色器代码吗?

时间:2021-06-22 21:40:53

标签: shader lwjgl opengl-1.x

我目前正在尝试使用 LWJGL opengl 库实现一个小地图(基本上只是彼此相邻的彩色像素)。

所以我在尝试使用 glBegin(GL_POINTS); 时遇到了问题 对于以下命令

glColor3i(stack.stack.get(z).minimapColor.getRed(),stack.stack.get(z).minimapColor.getRed(), stack.stack.get(z).minimapColor.getBlue());
glVertex2i(xStart-Board.map(x-x0), yStart-Board.map(y-y0));

它告诉我我没有上下文或当前上下文中不允许使用该函数:

FATAL ERROR in native method: Thread[main,5,main]: No context is current or a function that is not available in the current context was called. The JVM will abort execution.
    at org.lwjgl.opengl.GL11.glBegin(Native Method)
    at UI.Minimap.draw(Minimap.java:34)
    at newUI.game.Renderer.render(Renderer.java:81)
    at newUI.game.Game.render(Game.java:52)
    at newUI.engine.GameEngine.gameLoop(GameEngine.java:63)
    at newUI.engine.GameEngine.run(GameEngine.java:33)
    at newUI.game.MainEcoGameLWJGL2_2021.main(MainEcoGameLWJGL2_2021.java:13)

问题是我显然有一个上下文,我在我的代码中检查了它。我也在主线程上绘制我的其他东西。

我尝试移动代码(将它放在我的着色器解除绑定之后或在我的着色器绑定之前)

public void render(final Window window, final BoardMode boardMode, final Player player, final Board board) {
    clear();
    
    if (window.isResized()) {
        glViewport(0, 0, window.getWidth(), window.getHeight());
        window.setResized(false);
    }

    shaderProgram.bind();

    shaderProgram.setUniform("texture_sampler", 0);
    // Render each gameItem
    switch(boardMode) {
        case STARTING_MENU:
            renderStartingMenu(window);
            break;
        case GAME:
            renderGame(window, player, board);
            break;
    }
    drawFPS(window);
    drawMode(window);

    shaderProgram.unbind();
}

我是否只是缺少使用非着色器样式 opengl 的一些基本知识,还是无法将着色器与非着色器 opengl 混合使用?

过去 7 小时我在网上找到的大部分内容大多是基于着色器的代码,或者至少它们将坐标绑定到一个数组中,然后使用该数组进行绘制。

我知道调试我的代码需要更多代码,但我只是问一个一般性问题,“是否可以将非常基本的非基于着色器的 opengl 与基于着色器的代码混合使用?”

PS:这里是调用 glfwWindowHint() 和 GL.createCapabilities() 以及 glfwMakeContextCurrent() 的请求代码部分:

public void init() {
    // Setup an error callback. The default implementation
    // will print the error message in System.err.
    GLFWErrorCallback.createPrint(System.err).set();

    // Initialize GLFW. Most GLFW functions will not work before doing this.
    if (!glfwInit()) {
        throw new IllegalStateException("Unable to initialize GLFW");
    }

    glfwDefaultWindowHints(); // optional, the current window hints are already the default
    glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // the window will be resizable
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_MAXIMIZED, GL_TRUE);

    // Create the window
    windowHandle = glfwCreateWindow(width, height, title, NULL, NULL);
    if (windowHandle == NULL) {
        throw new RuntimeException("Failed to create the GLFW window");
    }

    // Setup resize callback
    glfwSetFramebufferSizeCallback(windowHandle, (window, width, height) -> {
        this.width = width;
        this.height = height;
        this.setResized(true);
    });
    
    glfwSetWindowCloseCallback(windowHandle, (window)->{
        saveAndQuit();
    });

    // Setup a key callback. It will be called every time a key is pressed, repeated or released.
    glfwSetKeyCallback(windowHandle, (window, key, scancode, action, mods) -> {
        KeyProcessor.generateKeyEvents(window, key, scancode, action, mods);
    });

    // Get the resolution of the primary monitor
    final GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());


    // Make the OpenGL context current
    glfwMakeContextCurrent(windowHandle);

    if (isvSync()) {
        // Enable v-sync
        glfwSwapInterval(1);
    }

    // Make the window visible
    glfwShowWindow(windowHandle);

    GL.createCapabilities();

    // Set the clear color
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    //glEnable(GL_DEPTH_TEST);
}

1 个答案:

答案 0 :(得分:1)

如果您请求兼容性上下文并使用程序 0 进行固定功能绘制,应该可以正常工作。

相关问题