Opengl的c++中未处理的异常

Unhandled exception in C++ for Opengl

本文关键字:异常 未处理 c++ Opengl      更新时间:2023-10-16

这几天我一直在做一个OpenGL教程。我让第一个教程工作,但当我到第二个教程时,它抛出

First-chance exception at 0x00000000 in playground.exe: 0xC0000005: Access violation.
Unhandled exception at 0x00000000 in playground.exe: 0xC0000005: Access violation.
我的代码是
#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 GLFWn" );
        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 GLEWn");
        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;
}

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

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

由于您使用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);
    ...
}

由于您正在创建GL 2.1上下文,您应该在使用vao之前显式检查ARB_vertex_array_object支持。直到GL 3.0, vao才成为核心。

你使用的通用顶点属性没有相应的着色器是不确定的。

glVertexPointer()和朋友提供一些着色器或下降。