opengl 来自glClear的EXC_BAD_ACCESS(GL_COLOR_BUFFER_BIT)?

68de4m5k  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(110)

我环顾四周,显然我已经在这个解决方案之间做出了选择,我的问题与以下相同:文,但我仍然不知道如何解决这个问题。当我使用glClear(GL_COLOR_BUFFER_BIT);在xcode中,它会警告Thread 1: EXC_BAD_ACCESS (code=1, address=0x0),但是当我用“//"注解它时,项目就可以工作了。enter image description here提前感谢!
我尝试“//”这段代码,但它不会清除窗口的呈现。

#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);
    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}
anauzrmj

anauzrmj1#

我可以用你的代码重复这个问题。我也检查了建议的解决方案。你应该在glfwMakeContextCurrent之后调用gladLoadGLLoader。

#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        // error out
    }

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {        
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

这也不是glClear的问题。如果GL函数指针没有正确初始化,任何OpenGL调用也会导致崩溃。

lnvxswe2

lnvxswe22#

在设置活动上下文后,您需要初始化GL入口点(函数指针),如下所示:

if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
    // failed to load...

此外,如果使用GLAD库,请防止包括系统GL标题:

#include <glad/glad.h>
...
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
...

相关问题