用于Opengl的C++中未处理的异常

Gam*_*Man 1 c++ opengl

我已经在一个OpenGL教程上工作了几天.我得到了第一个工作的教程,但是一旦我完成第二个教程就会抛出

First-chance exception at 0x00000000 in playground.exe: 0xC0000005: Access violation.
Unhandled exception at 0x00000000 in playground.exe: 0xC0000005: Access violation.
Run Code Online (Sandbox Code Playgroud)

我的代码是

#include <stdio.h>
#include <stdlib.h>

#include <GL/glew.h>

#include <glfw3.h>
GLFWwindow* window;

#include <glm/glm.hpp>
using namespace glm;

int main( void )
{
    // Initialise GLFW
    if( !glfwInit() )
    {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        return -1;
    }
    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);

    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);


    // Open a window and create its OpenGL context
    window = glfwCreateWindow( 1024, 768, "Playground", NULL, NULL);
    if( window == NULL ){
        fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    // Initialize GLEW
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }

    // Ensure we can capture the escape key being pressed below
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    // Dark blue background
    glClearColor(0.0f, 0.0f, 0.4f, 0.0f);

    // An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = {
    -1.0f, -1.0f, 0.0f,
     1.0f, -1.0f, 0.0f,
     0.0f,  1.0f, 0.0f,
};


// This will identify our vertex buffer
GLuint vertexbuffer;

// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);

// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);


    do{
        // Draw nothing, see you in tutorial 2 !
        // 1rst attribute buffer : vertices
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
        glVertexAttribPointer(
           0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
           3,                  // size
           GL_FLOAT,           // type
           GL_FALSE,           // normalized?
           0,                  // stride
           (void*)0            // array buffer offset
        );

        // Draw the triangle !
        glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle

        glDisableVertexAttribArray(0);

        // Swap buffers
        glfwSwapBuffers(window);
        glfwPollEvents();

    } // Check if the ESC key was pressed or the window was closed
    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
           glfwWindowShouldClose(window) == 0 );

    // Close OpenGL window and terminate GLFW
    glfwTerminate();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

gen*_*ult 5

命令排序错误.你有这个:

int main( void )
{
    ...
    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);
    ...
    window = glfwCreateWindow( 1024, 768, "Playground", NULL, NULL);
    ...
    glfwMakeContextCurrent(window);
    ...
    if (glewInit() != GLEW_OK) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

由于您使用GLEW函数指针,glGenVertexArrays()并且glBindVertexArray()在成功之前无效glewInit().

移动你的VAO创作于 glewInit():

int main( void )
{
    ...
    window = glfwCreateWindow( 1024, 768, "Playground", NULL, NULL);
    ...
    glfwMakeContextCurrent(window);
    ...
    if (glewInit() != GLEW_OK) {
    ...
    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);
    ...
}
Run Code Online (Sandbox Code Playgroud)

由于您正在创建GL 2.1上下文,因此ARB_vertex_array_object在使用VAO之前应明确检查支持.在GL 3.0之前,VAO不是核心.

您使用没有相应着色器的通用顶点属性是最好的.供应一些着色器或退回glVertexPointer()和朋友.