我试图创建一个2d纹理以显示在我的Qt / OpenGL应用程序中。然后,我创建了一个填满屏幕的四边形来放置它,但是我得到的只是一个没有任何错误的白色屏幕。有人看到我的代码有什么问题吗?
#include "AppGLWidget.h"
#include <iostream>
AppGLWidget::AppGLWidget(QWidget *parent) : QGLWidget(parent),
mnRange(100),
mcDisplayedImage(512,512,QImage::Format_ARGB32)
{
mcDisplayedImage.fill(qRgb(100,0,0));
mcDisplayedImage = QGLWidget::convertToGLFormat(mcDisplayedImage);
int value;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
}
AppGLWidget::~AppGLWidget(void)
{
}
void AppGLWidget::initializeGL() {
glClearColor(0, 1, 0, 1);
glPushMatrix();
glOrtho(0, size().width(), size().height(), 0, -1, 1);
glViewport(0, 0, (GLint)size().width(), (GLint)size().height());
glDisable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
glEnable(GL_TEXTURE_2D);
glGenTextures(1,&mcTextureObj);
glBindTexture(GL_TEXTURE_2D,mcTextureObj);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mcDisplayedImage.width(), mcDisplayedImage.height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glDisable(GL_TEXTURE_2D);
// check OpenGL error
GLenum err;
while ((err = glGetError()) != GL_NO_ERROR) {
std::cerr << "OpenGL error: " << err << std::endl;
}
}
void AppGLWidget::resizeGL(int w, int h)
{
glPopMatrix();
glPushMatrix();
glOrtho(0, w, h, 0, -1, 1);
glViewport(0, 0, w, h);
}
void AppGLWidget::paintGL() {
glBindTexture(GL_TEXTURE_2D, mcTextureObj);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, mcDisplayedImage.width(), mcDisplayedImage.height(),
GL_RGBA, GL_UNSIGNED_BYTE, mcDisplayedImage.bits());
glBegin(GL_QUADS);
glTexCoord2i(0,0); glVertex2i(0,lnHeight);
glTexCoord2i(0,1); glVertex2i(0,0);
glTexCoord2i(1,1); glVertex2i(lnWidth,0);
glTexCoord2i(1,0); glVertex2i(lnWidth,lnHeight);
glEnd();
// check OpenGL error
GLenum err;
while ((err = glGetError()) != GL_NO_ERROR) {
std::cerr << "OpenGL error: " << err << std::endl;
}
}
最佳答案
GL_TEXTURE_2D
需要使用glEnable()
进行渲染。
现在,您用glDisable()
对其进行initializeGL()
编码,然后将其保留。