GLEW 编译,但在运行时崩溃
GLEW compiles, but crashes on runtime
我最近开始学习 OpenGL,在代码块中一起使用 GLEW、SDL 和 C++。我查看了有关如何设置 GLEW 的多个来源,它们都非常相似。我按照他们的理解理解了所有内容,我需要让 OpenGL 正常工作。
我从 this tutorial 复制了准确的代码,然后摆弄它试图在发现它不能工作后让它工作。我唯一添加的地方:
#define GLEW_STATIC
glewInit(); // This returns true
glewExperimental = GL_TRUE;
每当我编译程序时,我都没有出错。但是每当我 运行 程序时,都会出现一条 windows 错误消息 "GLEW_Program has stopped working"
。如果我 link 静态库,就会发生这种情况。如果我 link 是动态的,我只会得到一堆未定义的引用。
这些是我的 linker 选项:
-lglew32s -lopengl32
-lglu32 -lfreeglut
-lmingw32 -lSDL2main -lSDL2
我把DLL放在可执行文件夹里,我试过32位和64位。我正在使用 Windows 10。
我还在另一台计算机上尝试了确切的程序,即 Windows 7 我遇到了完全相同的问题,所以我肯定做错了什么。
我尝试注释掉一些代码片段,当我不使用网格 class 时,它 运行 没问题,但当然没有图形。它似乎正在处理没有任何好处的缓冲区。
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);
我知道其他 post 就此问题提出的问题,但答案不适用于我。
我已经筋疲力尽地想出如何解决这个问题,所以我现在寻求帮助。我有点希望这是我的明显错误。这是我的第一个 post,如有任何疑问,请随时提出。
// 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 在 OS X 上几乎完全无用。
我最近开始学习 OpenGL,在代码块中一起使用 GLEW、SDL 和 C++。我查看了有关如何设置 GLEW 的多个来源,它们都非常相似。我按照他们的理解理解了所有内容,我需要让 OpenGL 正常工作。
我从 this tutorial 复制了准确的代码,然后摆弄它试图在发现它不能工作后让它工作。我唯一添加的地方:
#define GLEW_STATIC
glewInit(); // This returns true
glewExperimental = GL_TRUE;
每当我编译程序时,我都没有出错。但是每当我 运行 程序时,都会出现一条 windows 错误消息 "GLEW_Program has stopped working"
。如果我 link 静态库,就会发生这种情况。如果我 link 是动态的,我只会得到一堆未定义的引用。
这些是我的 linker 选项:
-lglew32s -lopengl32
-lglu32 -lfreeglut
-lmingw32 -lSDL2main -lSDL2
我把DLL放在可执行文件夹里,我试过32位和64位。我正在使用 Windows 10。 我还在另一台计算机上尝试了确切的程序,即 Windows 7 我遇到了完全相同的问题,所以我肯定做错了什么。
我尝试注释掉一些代码片段,当我不使用网格 class 时,它 运行 没问题,但当然没有图形。它似乎正在处理没有任何好处的缓冲区。
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);
我知道其他 post 就此问题提出的问题,但答案不适用于我。
我已经筋疲力尽地想出如何解决这个问题,所以我现在寻求帮助。我有点希望这是我的明显错误。这是我的第一个 post,如有任何疑问,请随时提出。
// 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 在 OS X 上几乎完全无用。