简体   繁体   English

在 Project0_opengl.exe 中的 0x00000000 处抛出异常:0xC0000005:访问冲突执行位置 0x00000000

[英]Exception thrown at 0x00000000 in Project0_opengl.exe: 0xC0000005: Access violation executing location 0x00000000

I'm writing right code but the compiler throws an error.我正在编写正确的代码,但编译器抛出错误。 The error says that the mistake is on glGenBuffers but I've copied it from the official website.该错误表示错误出在glGenBuffers但我已从官方网站复制了它。 Where is my mistake?我的错误在哪里?

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <stdio.h>

int main(void)
{
    GLFWwindow* window;
    glewExperimental = GL_TRUE;

    /* 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 pos[6] = {
        -0.5f, -0.5f,
         0.0f,  0.5f,
         0.5f, -0.5f
    };

    GLuint buf;
    glGenBuffers(1, &buf);
    glBindBuffer(GL_ARRAY_BUFFER, buf);
    glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), pos, GL_STATIC_DRAW);

    if (glewInit() != GLEW_OK)
        printf("Error\n");

    printf("%s", glGetString(GL_VERSION));

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

        glDrawArrays(GL_TRIANGLES, 0, 3);

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

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

    glfwTerminate();
    return 0;
}

glewInit() has to be called after the OpenGL context is made current, after glfwMakeContextCurrent . glewInit()必须在 OpenGL 上下文glfwMakeContextCurrent当前之后,在glfwMakeContextCurrent之后glfwMakeContextCurrent
But it has to be called before any OpenGL instruction.但它必须在任何 OpenGL 指令之前调用。 See also Initializing GLEW :另请参阅初始化 GLEW

// [...]

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

glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
    printf("Error\n");

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

GLuint buf;
glGenBuffers(1, &buf);
glBindBuffer(GL_ARRAY_BUFFER, buf);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), pos, GL_STATIC_DRAW);

// [...]

Note, the instructions like glGenBuffers are function pointers.注意,像glGenBuffers这样的指令是函数指针。 These pointers are initialized to NULL .这些指针被初始化为NULL glewInit() assigns the address of the function to those pointers. glewInit()将函数的地址分配给这些指针。
When you try to call the function, before the initialization, this causes:当您尝试调用该函数时,在初始化之前,这会导致:

Access violation executing location 0x00000000访问冲突执行位置 0x00000000

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 CobwebDiagram.exe 中的 0x00000000 处引发异常:0xC0000005:访问冲突执行位置 0x00000000 - Exception thrown at 0x00000000 in CobwebDiagram.exe: 0xC0000005: Access violation executing location 0x00000000 为什么我在 ConsoleApplication1.exe 中的 0x00000000 处出现异常异常:0xC0000005:访问冲突执行位置 0x00000000 - Why do I get an Exception Exception thrown at 0x00000000 in ConsoleApplication1.exe: 0xC0000005: Access violation executing location 0x00000000 0xC0000005:访问冲突执行位置0x00000000 - 0xC0000005: Access violation executing location 0x00000000 0xC0000005:访问冲突读取位置0x00000000 - 0xC0000005: Access violation reading location 0x00000000 Project4.exe中0x00998876处的首次机会异常:0xC0000005:访问冲突写入位置0x00000000 - First-chance exception at 0x00998876 in Project4.exe: 0xC0000005: Access violation writing location 0x00000000 0xC0000005:访问冲突读取位置0x00000000 - 0xC0000005: Access violation reading location 0x00000000 在0xC0000005中的0x6ececafa出现未处理的异常:访问冲突写入位置0x00000000 - Unhandled Exception as at 0x6ececafa in 0xC0000005 : Access violation writing location 0x00000000 NVIDIA未处理的异常,位于0x002a2da2中 <work.exe> 0xC0000005:访问冲突读取位置0x00000000 - NVIDIA Unhandled exception at 0x002a2da2 in <work.exe>0xC0000005: Access violation reading location 0x00000000 Engine.exe中0x00000000的未处理异常:0xC0000005:访问冲突 - Unhandled exception at 0x00000000 in Engine.exe: 0xC0000005: Access violation tester.exe中0x00b21840处未处理的异常:0xC0000005:访问冲突读取位置0x00000000 - Unhandled exception at 0x00b21840 in tester.exe: 0xC0000005: Access violation reading location 0x00000000
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM