我正在尝试渲染已映射到图形视频内存的纹理。我已经使用 qDebug 来测试我的图像的输入,实际上它拥有一个 512x512 的图像,它是 2 的幂。但是当我尝试渲染时,结果只是一个白色的四边形,而不是纹理。
注意:GLuint m_textureIdents[1]; 在 GLWidget.h 中正确定义
#include "GLWidget.h"
GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent)
{
setMouseTracking(true);
m_sceneIndex = SinQuest::SplashScreen;
}
void GLWidget::setApplicationPath(QString strAppPath)
{
m_strAppPath = strAppPath;
}
#include <QDebug>
void GLWidget::loadResources(void)
{
m_imageHandler.addImage(QString(m_strAppPath).append(QDir().toNativeSeparators("\\resources\\images\\")).append("splash_logo.png"), "splash-logo");
QImage glImageData = QGLWidget::convertToGLFormat(*m_imageHandler.getImage("splash-logo"));
qDebug() << glImageData.width() << ":" << glImageData.height();
m_textureIdents[0] = 0;
glGenTextures(1, &m_textureIdents[0]);
glBindTexture(GL_TEXTURE_2D, m_textureIdents[0]);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, glImageData.width(), glImageData.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, glImageData.bits());
}
void GLWidget::initializeGL()
{
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glEnable(GL_POLYGON_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0, 0, 0, 0);
}
void GLWidget::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, 0, h, -1.0l, 1.0l); // set up origin
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void GLWidget::paintGL()
{
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f);
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3f(1.0f, 1.0f, 1.0f);
glShadeModel( GL_FLAT );
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, m_textureIdents[0]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2i(0, 0);
glTexCoord2f(1.0f, 0.0f);
glVertex2i(512, 0);
glTexCoord2f(1.0f, 1.0f);
glVertex2i(512, 512);
glTexCoord2f(0.0f, 1.0f);
glVertex2i(0, 512);
glEnd();
}
void GLWidget::mousePressEvent(QMouseEvent *event) {
}
void GLWidget::mouseMoveEvent(QMouseEvent *event) {
printf("%d, %d\n", event->x(), event->y());
}
void GLWidget::keyPressEvent(QKeyEvent* event) {
switch(event->key()) {
case Qt::Key_Escape:
close();
break;
default:
event->ignore();
break;
}
}
...
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDesktopWidget *pDeskWidget = app.desktop();
// available geometry to obtain resolution for primary screen
QRect avGeometry = pDeskWidget->availableGeometry(pDeskWidget->primaryScreen());
// create window, set resolution and invoke fullscreen window state
GLWidget window;
window.setApplicationPath(QCoreApplication::applicationDirPath());
window.loadResources();
window.resize(avGeometry.width(), avGeometry.height());
window.setWindowState(Qt::WindowFullScreen);
window.show();
return app.exec();
}