我无法将图像纹理渲染到屏幕上。除了空白的黑屏外,没有显示任何内容,并且没有明显的错误。我认为这个问题与glOrtho
电话有关,但我不确定。
所有相关代码均包含在下方。 (在初始化之后和第一次渲染调用之前调用OnResize)
需要OpenGL2.1兼容性,以及稍后添加着色器的视图。纹理内容被硬编码到渲染器只是暂时的,我已经准备好了一个纹理类,一旦我找出了什么是错的。
我想我可能需要一个FrameBuffer对象?我发现的教程经常有很大不同,通常都是针对OGL3
Video::Video() : running(true)
{
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::string err = "Error initialising video: ";
err += SDL_GetError();
throw std::runtime_error(err);
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_MAXIMIZED;
this->window = SDL_CreateWindow("lolpenGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, flags);
if (this->window == nullptr) {
std::string err = "Error creating SDL window: ";
err += SDL_GetError();
throw std::runtime_error(err);
}
this->glcontext = SDL_GL_CreateContext(this->window);
if (this->glcontext == nullptr) {
std::string err = "Error creating GL context: ";
err += SDL_GetError();
throw std::runtime_error(err);
}
GLenum glew_err = glewInit();
if (glew_err != GLEW_OK) {
std::string err = "Error initialising GLEW: ";
err += (char *)glewGetErrorString(glew_err);
throw std::runtime_error(err);
}
if (!GLEW_VERSION_2_1) {
throw std::runtime_error("OpenGL 2.1 not available");
}
int max_tex_size;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_tex_size);
if (max_tex_size < 1024) {
throw std::runtime_error("Maximum supported texture size too small");
}
int max_tex_units;
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_tex_units);
if (max_tex_units < 2) {
throw std::runtime_error("GPU does not have enough texture unnits");
}
SDL_GL_SetSwapInterval(1); // vsync
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST); // what's drawn last is displayed on top.
// Temp texture load
glGenTextures(1, &this->texid);
glBindTexture(GL_TEXTURE_2D, this->texid);
// Set our texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set texture filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Load, create texture and generate mipmaps
int width, height;
unsigned char *image = SOIL_load_image("awesomeface.png", &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0);
glEnable(GL_TEXTURE_2D);
}
void Video::OnResize(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
glOrtho(0, w, 0, h, 9001, -1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Video::Render()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
{
glColor4f(1, 1, 1, 1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->texid);
// do stuff
}
glPopMatrix();
SDL_GL_SwapWindow(this->window);
}
编辑:好的,我是个白痴,我实际上并没有画任何东西。我发现添加以下作品,// do stuff
是。
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(0, 1); glVertex3f(0, 100, 0);
glTexCoord2f(1, 1); glVertex3f(100, 100, 0);
glTexCoord2f(1, 0); glVertex3f(100, 0, 0);
glEnd();
这是最好的方法吗?