【问题标题】:OpenGL C++ (Error thrown while making simple window)OpenGL C++(制作简单窗口时抛出错误)
【发布时间】:2020-05-17 18:38:10
【问题描述】:

在 C++ 中创建一个基本的 openGL 窗口时,我得到一个错误。我在 Visual Studio 19 上使用 GLFW 和 GLAD,我很确定我的计算机规格足够好。我想我正确链接了我所有的 glfw 文件,并且 Glad 的所有内容都正确附加。

The error: Exception thrown at 0x00000000 in OpenGL.exe: 0xC0000005: Access 
           violation executing location 0x00000000.

我编写的完整代码(一些来自教程)。是的,包含很奇怪,但所有包含都正确添加,我确定。 :`

#include <GLFW/glad.h>                                       
#include <GLFW/khrplatform.h>
#include <GLFW/glfw3.h>

int main()
{
    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);

    float vertices[] = {
    -0.5f, -0.5f, 0.0f,
     0.5f, -0.5f, 0.0f,
     0.0f,  0.5f, 0.0f
    };

    unsigned int buffer;
    glGenBuffers(1, &buffer);

    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    /* 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;
}

【问题讨论】:

  • 你没有初始化 GLAD。
  • 你试过调试吗?你在某处有一个空指针
  • @HolyBlackCat 哦,有什么我会怎么做的例子吗?因为,我认为没有函数可以初始化高兴,只有 glfw。
  • 在调试器中运行你的代码,它会准确地告诉你哪个指针是空的
  • 可能 NULL 指针是 glGenBuffer (或主要是其他 gl-call),它在初始化后不为空。

标签: c++ opengl


【解决方案1】:

你必须高兴:)

#include <glad/glad.h>
int main(int argc, char **argv) { 
  // .. setup the context 
  if(!gladLoadGL()) 
  { 
   printf("Something went wrong!\n");    
   exit(-1); 
  } 
printf("OpenGL %d.%d\n",  GLVersion.major, GLVersion.minor);
 // .. render here .. 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多