代码之家  ›  专栏  ›  技术社区  ›  JuanPlayz

在PianoMasher.exe中0x0000000000000000处引发OpenGL异常:0xC0000005:执行位置0x000000000000000时发生访问冲突

  •  0
  • JuanPlayz  · 技术社区  · 2 年前

    我在尝试使用OpenGL绘制三角形时遇到以下错误:

    Exception thrown at 0x00000000 in Project3.exe: 0xC0000005: Access violation executing location 0x00000000. occurred
    

    这是我的代码:

    #include <GL/glew.h>
    #include <GLFW/glfw3.h>
    #include <iostream>
    using namespace std;
    
    int main( void )
    {
        GLFWwindow* window;
    
        glewInit();
        //if (glewInit != GLEW_OK)
        //  std::cout << "ew";
        /* 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 poss[ 6 ] =
        {
            -0.5, -0.5,
            0.0,   0.5,
            0.5, -0.5
        };
        unsigned int buffer;
        glGenBuffers( 1, &buffer );
        glBindBuffer( GL_ARRAY_BUFFER, buffer );
        glBufferData( GL_ARRAY_BUFFER, 6 * sizeof( float ), poss, GL_STATIC_DRAW );
        /* 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;
    }
    
    0 回复  |  直到 8 年前
        1
  •  3
  •   genpfault    8 年前

    调用(不计算的地址!) glewInit() 之后 glfwMakeContextCurrent() 。否则,由声明的所有OpenGL函数指针 glew.h 将保持为NULL,因为 glewInit() 需要当前GL上下文才能正确操作。

    已授予您的计划 修复后不会绘制任何内容(由于缺少顶点数组启用和指针设置),但这是一个单独的问题。

    推荐文章