c++ 执行“glBindVertexArray( VAO )”后出现异常“

4uqofj5v  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(287)

这是我的代码:

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

const int IMAGE_SIZE = 32;

GLFWwindow* window;
GLuint vao;
GLuint vbo;

int initWindow() {
    // Initialize GLFW
    if (!glfwInit()) {
        cerr << "Error: Failed to initialize GLFW." << endl;
        return 1;
    }

    // Set up GLFW window hints for OpenGL 3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    // Create window
    window = glfwCreateWindow(IMAGE_SIZE * 20, IMAGE_SIZE * 20 + 60, "PBM Image", NULL, NULL);
    if (!window) {
        cerr << "Error: Failed to create GLFW window." << endl;
        glfwTerminate();
        return 1;
    }
    // Create context
    glfwMakeContextCurrent(window);
    // Set color and blendmode
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    // Create and bind VAO and VBO
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao); // Here is the spot where I get the exception
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    return 0;
}

void renderImage(vector<unsigned char> vertices) {
    // Update VBO data
    glBufferData(GL_ARRAY_BUFFER, vertices.size(), vertices.data(), GL_STATIC_DRAW);
    // Set vertex attribute pointers
    glVertexAttribPointer(0, 3, GL_UNSIGNED_BYTE, GL_FALSE, 3 * sizeof(unsigned char), (void*)0);
    glEnableVertexAttribArray(0);
    // Unbind VBO and VAO
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
    // Set up projection matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, IMAGE_SIZE, 0.0, IMAGE_SIZE, 0.0, 1.0);
    // Clear screen
    glClear(GL_COLOR_BUFFER_BIT);
    // Bind VAO
    glBindVertexArray(vao);
    // Draw points
    glDrawArrays(GL_POINTS, 0, vertices.size() / 3);
    // Unbind VAO
    glBindVertexArray(0);
    // Swap buffers
    glfwSwapBuffers(window);
}

int main() {
    if (initWindow()) return 1;
    // Here comes the code that generates vector<unsigned char> vertices
    renderImage(vertices);
    getchar();
    // Delete VAO and VBO
    glDeleteVertexArrays(1, &vao);
    glDeleteBuffers(1, &vbo);
    // Terminate GLFW
    glfwTerminate();
    return 0;
}

它使用glew来设置opengl和glfw进行窗口处理。初始化窗口后,它生成renderImage接收的vector<unsigned char> vertices。之后,它删除VAO、VBO并终止GLFW。但它甚至不会到达这一点,因为它在glBindVertexArray(vao);处抛出了以下异常:

Exception thrown at 0x0000000000000000 in generator.exe: 0xC0000005: Access violation executing location 0x0000000000000000.

这里是我的库,包括设置和文件夹:
VC++ Directories
Input
Lib Folder
Lib x64 Folder
Include GL
Include GLFW

k75qkfdt

k75qkfdt1#

  • glew* 需要初始化(请参见Initializing GLEW)。如果glew未初始化,则不会设置OpenGL API函数指针。在glfwMakeContextCurrent(window)之后立即调用glewInit()
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK)
    return 1;

相关问题