【发布时间】:2016-03-31 16:51:02
【问题描述】:
我最近开始学习 OpenGL,在代码块中一起使用 GLEW、SDL 和 C++。我查看了有关如何设置 GLEW 的多个来源,它们都非常相似。我跟着他们了解了所有内容,我需要让 OpenGL 正常工作。
我从this tutorial 复制了确切的代码,然后在发现它没有工作后摆弄它试图让它工作。我添加的唯一内容是:
#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,我遇到了完全相同的问题,所以我肯定做错了什么。
我尝试注释掉一些sn-ps 代码,当我不使用mesh 类时,它运行良好,但当然没有图形。它似乎正在处理没有任何好处的缓冲区。
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_STATIC 定义为编译器参数。检查函数地址是否不为空。您使用哪个驱动程序,您定位哪个配置文件
-
发布您的整个代码。仅仅提供 sn-ps 是不够的。
-
@Jeka 我尝试将 GLEW_STATIC 定义为编译器参数。我确实检查了函数地址,它们确实返回 true。我不认为是司机。我定位的个人资料是什么意思?
-
@Jeka:GLEW_STATIC 只会在出现链接错误时解决任何问题。