GLEW编译,但在运行时崩溃

GLEW compiles, but crashes on runtime

本文关键字:运行时 崩溃 编译 GLEW      更新时间:2023-10-16

我最近开始学习OpenGL,在代码块中一起使用GLEW、SDL和C++。我已经查看了关于如何设置GLEW的多个来源,它们都非常相似。我遵循了他们的理解,我需要让OpenGL工作。

我从本教程中复制了确切的代码,然后在发现它没有工作后,我对它进行了篡改。我唯一添加的内容是:

#define GLEW_STATIC
glewInit(); // This returns true
glewExperimental = GL_TRUE;

每当我编译这个程序时,我都不会出错。但每当我运行该程序时,都会出现一条windows错误消息,上面写着"GLEW_Program has stopped working"。如果我链接静态库,就会发生这种情况。如果我动态链接,我只会得到一堆未定义的引用。

以下是我的链接器选项:

-lglew32s -lopengl32
-lglu32 -lfreeglut
-lmingw32 -lSDL2main -lSDL2

我把DLL放在可执行文件夹中,我已经尝试了32位和64位。我正在使用Windows 10。我也在另一台Windows 7电脑上尝试过同样的程序,但我遇到了完全相同的问题,所以我肯定做错了什么。

我试着注释掉了一些代码片段,当我不使用网格类时,它运行得很好,但当然没有图形。它似乎在处理那些没有任何好处的缓冲区。

glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(vertices[0]), vertices, GL_STATIC_DRAW);

我知道其他关于这个问题的帖子,但答案不适用于我的。

我弄不清楚如何解决这个问题,所以我现在请求帮助。我希望这是我明显的错误。这是我的第一篇帖子,所以请随时要求澄清。

// main.cpp    
#include "display.h"
#include "mesh.h"
int main(int argc, char** argv)
{
    Display display(800, 600, "OpenGL Window");
    Vertex vertices[] =
    {
        Vertex(glm::vec3(-0.5, -0.5, 0)),
        Vertex(glm::vec3(0, 0.5, 0)),
        Vertex(glm::vec3(0.5, -0.5, 0)),
    };
    Mesh mesh(vertices, sizeof(vertices)/sizeof(vertices[0]));
    while(!display.IsClosed())
    {
        display.Clear(.2f,.8f,1.f,1.f); //r g b a
       // mesh.Draw();
        display.Update();
    }
    return 0;
}
// display.h
#ifndef DISPLAY_H
#define DISPLAY_H
#define GLEW_STATIC
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <iostream>
#include <string>
class Display
{
public:
    Display(int width, int height, const std::string& title);
    void Clear(float r, float g, float b, float a);
    void Update();
    bool IsClosed();
    virtual ~Display();
protected:
private:
    Display(const Display& other) {}
    void operator=(const Display& other) {}
    SDL_Window* m_window;
    SDL_GLContext m_glContext;
    bool m_isClosed;
};
#endif // DISPLAY_H
// display.cpp
#include "display.h"
Display::Display(int width, int height, const std::string& title)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    glewInit();
    glewExperimental = GL_TRUE;
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    m_window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
    m_glContext = SDL_GL_CreateContext(m_window);
}
Display::~Display()
{
    SDL_GL_DeleteContext(m_glContext);
    SDL_DestroyWindow(m_window);
    SDL_Quit();
}
void Display::Clear(float r, float g, float b, float a)
{
    glClearColor(r, g, b, a);
    glClear(GL_COLOR_BUFFER_BIT);
}
bool Display::IsClosed()
{
    return m_isClosed;
}
void Display::Update()
{
    SDL_GL_SwapWindow(m_window);
    SDL_Event e;
    while(SDL_PollEvent(&e))
    {
        if(e.type == SDL_QUIT) m_isClosed = true;
    }
}
// mesh.h
#ifndef MESH_H
#define MESH_H
#define GLEW_STATIC
#include <glm/glm/glm.hpp>
#include <iostream>
#include <GL/glew.h>
class Vertex
{
public:
    Vertex(const glm::vec3& pos)
    {
        this->pos = pos;
    }
protected:
private:
    glm::vec3 pos;
};
class Mesh
{
public:
    Mesh(Vertex* vertices, unsigned int numVertices);
    void Draw();
    virtual ~Mesh();
protected:
private:
    Mesh(const Mesh& other);
    void operator=(const Mesh& other);
    enum
    {
        POSITION_VB,
        NUM_BUFFERS
    };
    GLuint m_vertexArrayObject;
    GLuint m_vertexArrayBuffers[NUM_BUFFERS];
    unsigned int m_drawCount;
};
#endif // MESH_H
// mesh.cpp
#include "mesh.h"
Mesh::Mesh(Vertex* vertices, unsigned int numVertices)
{
    m_drawCount = numVertices;
    glGenVertexArrays(1, &m_vertexArrayObject);
    glBindVertexArray(m_vertexArrayObject);
    glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
    glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
    glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(vertices[0]), vertices, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glBindVertexArray(0);
}
Mesh::~Mesh()
{
    glDeleteVertexArrays(1, &m_vertexArrayObject);
}
void Mesh::Draw()
{
    glBindVertexArray(m_vertexArrayObject);
    glDrawArrays(GL_TRIANGLES, 0, m_drawCount);
    glBindVertexArray(0);
}

代码未正确初始化GLEW。

glewInit(); // Wrong
...
m_window = SDL_CreateWindow(...);
m_glContext = SDL_GL_CreateContext(...);

在这里,glewInit()是在没有OpenGL上下文的情况下调用的。在初始化GLEW之前,必须先初始化OpenGL上下文。换句话说,你必须这样做:

m_window = SDL_CreateWindow(...);
m_glContext = SDL_GL_CreateContext(...);
...
glewInit(); // Correct

必须以这种方式进行操作的原因是,OpenGL函数的入口点在您真正拥有OpenGL上下文之前根本不可用。换句话说,只有少数入口点位于opengl32.dll/libgl.so中,大多数入口点位于设备特定的OpenGL库中,该库在您首次创建上下文时加载。一旦创建了上下文,wglGetProcAddress()/etc就会工作,这就是GLEW在初始化时用来加载函数指针的。

还要注意,还有更好的GL加载库。例如,在目前的状态下,GLEW在OSX上几乎完全无用。