c++ - glGenBuffers 访问冲突异常

标签 c++ glew glfw

我正在使用 GLFWGLEW。 但是在我的对象初始化期间 glGenBuffers 抛出异常

void Character::init2D(glm::vec3 top, glm::vec3 bottom_left, glm::vec3 bottom_right)
{
    glm::vec3 Vertices[3];
    Vertices[0] = bottom_left;
    Vertices[1] = top;
    Vertices[2] = bottom_right;

    this->top = top;
    this->left_front = bottom_left;
    this->right_front = bottom_right;

    glGenBuffers(1, &VBO); //throws an exception 0xC0000005: Access violation
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

    CompileShaders(shaderProgram, "vertex.shader", "fragment.shader");
}

我这样声明我的类 Character

#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <glm\glm.hpp>
#include <glm\gtc\type_ptr.hpp>
#pragma comment(lib, "glfw3.lib")
#pragma comment(lib, "glew32.lib")

class Character
{
private:
    glm::vec3   top, 
                left_front, 
                right_front, 
                left_back, 
                right_back;
    GLuint VBO;
    GLuint shaderProgram;
public:
    Character();
    void init2D(glm::vec3 top, 
                glm::vec3 bottom_left, 
                glm::vec3 bottom_right);
    void draw();
    void move();
    void action();
    ~Character() {};
};

我的 main.cpp 看起来像这样

#include <iostream>
#include "character.h"
#define WIDTH 600
#define HEIGHT 600

using namespace std;

Character simple;

void render()
{
    simple.draw();
}

int main(int argc, char** argv)
{
    GLFWwindow *window;

    if (!glewInit())
        exit(EXIT_FAILURE);

    if (!glfwInit())
        exit(EXIT_FAILURE);

    window = glfwCreateWindow(WIDTH, HEIGHT, "Imensia", NULL, NULL);
    if (!window) { glfwTerminate(); exit(EXIT_FAILURE); }

    glm::vec3 left(-0.5, 0, 0);
    glm::vec3 top(0, 0.5, 0);
    glm::vec3 right(0.5, 0, 0);

    simple.init2D(top, left, right);

    glfwMakeContextCurrent(window);
    while(!glfwWindowShouldClose(window))
    {
        glViewport(0, 0, WIDTH, HEIGHT); 
        glClear(GL_COLOR_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-1, 1, -1, 1, 1, -1);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        render();

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
};

是初始化有问题还是什么? 在项目的 properties 中,我设置了 includelibrary 目录...

最佳答案

Glew 初始化应该在创建 windows/opengl 上下文之后完成。

对于给定的代码,glewInit 可以移动到 glfwCreateWindow 的正下方:

int main(int argc, char** argv)
{
    GLFWwindow *window;

    if (!glfwInit())
        exit(EXIT_FAILURE);

    window = glfwCreateWindow(WIDTH, HEIGHT, "Imensia", NULL, NULL);
    if (!window) { glfwTerminate(); exit(EXIT_FAILURE); }

    if (!glewInit())
        exit(EXIT_FAILURE);
    :
    :
}

关于c++ - glGenBuffers 访问冲突异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20767812/

相关文章:

c++ - 在windows xp上用VS 2012编译MFC程序

c++ - 这段代码中如何调用函数传值? C++

c++ - 我需要为 OpenGL 3.3 Core 使用着色器吗?

c++ - glewInit() 时 OpenGL 错误 GL_INVALID_ENUM (0x0500)

c++ - 使用 mingw 在 window 上 build 闪光

c++ - 我怎样才能有可选的默认构造函数?

c++ - 静态结构 - 定义、对象、成员

c++ - GLEW 无法与 CMAKE 一起使用

c++ - 为什么我不能使用一些 GLFW 方法

c++ - CMake:GLFW 作为外部项目