违反GLFW / GLEW计划的访问权限?

时间:2018-04-17 13:36:06

标签: c++ opengl glfw glew

尝试使用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;
}

1 个答案:

答案 0 :(得分:2)

glewInit()之后调用(不评估!)glfwMakeContextCurrent() 的地址。否则glew.h声明的所有OpenGL函数指针都将保持为NULL,因为glewInit()需要当前的GL上下文才能正常运行。

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