2

我正在尝试使用 OpenGL 在 Qt 中创建一个房间,现在我被纹理部分困住了。它不会将我的纹理放在墙上。

这是我的代码:

#include "glwidget.h"

GLWidget::GLWidget(QWidget *parent):QGLWidget(parent)
{

    camPosx = 0.0,  camPosy = 0.0,    camPosz = 1.0;
    camViewx = 0.0, camViewy = 0.0, camViewz = 0.0;
    camUpx = 0.0,   camUpy = 1.0,   camUpz = 0.0;
    timer = new QTimer();
    connect( timer, SIGNAL(timeout()), this, SLOT(updateGL()) );
}

void GLWidget::initializeGL() {

    loadGLTextures();
    glEnable(GL_TEXTURE_2D);
    glClearColor(1.0f,1.0f,1.0f,0.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    timer->start(50);
}

void GLWidget::resizeGL(int width, int height) {

    //set viewport
    glViewport(0,0,width,height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    //set persepective
    //change the next line order to have a different perspective
    GLdouble aspect_ratio=(GLdouble)width/(GLdouble)height;
    gluPerspective(45.0f, aspect_ratio, 0.1 , 100.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void GLWidget::paintGL() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();

    // store current matrix
    glMatrixMode( GL_MODELVIEW );
    glPushMatrix( );

    gluLookAt(camPosx ,camPosy ,camPosz,
              camViewx,camViewy,camViewz,
              camUpx, camUpy, camUpz );

    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glEnable( GL_LIGHTING );
    glEnable( GL_LIGHT0 );
    glScalef(2.0,2.0,2.0);
    glBegin(GL_QUADS);
    /* Floor */
    glColor3f(0,0,0);
    glVertex3f(-1,-1,-1);
    glVertex3f(1,-1,-1);
    glVertex3f(1,-1,1);
    glVertex3f(-1,-1,1);
    /* Ceiling */
    glVertex3f(-1,1,-1);
    glVertex3f(1,1,-1);
    glVertex3f(1,1,1);
    glVertex3f(-1,1,1);
    /* Walls */
    glVertex3f(-1,-1,1);
    glVertex3f(1,-1,1);
    glVertex3f(1,1,1);
    glVertex3f(-1,1,1);

    glVertex3f(-1,-1,-1);
    glVertex3f(1,-1,-1);
    glVertex3f(1,1,-1);
    glVertex3f(-1,1,-1);

    glVertex3f(1,1,1);
    glVertex3f(1,-1,1);
    glVertex3f(1,-1,-1);
    glVertex3f(1,1,-1);

    glVertex3f(-1,1,1);
    glVertex3f(-1,-1,1);
    glVertex3f(-1,-1,-1);
    glVertex3f(-1,1,-1);
    glEnd();
    glEnable( GL_LIGHTING );


    // restore current matrix
    glMatrixMode( GL_MODELVIEW );
    glPopMatrix( );

}
void GLWidget::loadGLTextures()
{
    QImage t;
    QImage b;

    if ( !b.load( "images/Naamloos.bmp" ) )
    {
        qDebug("Didn't found the image.");
        b = QImage( 16, 16, QImage::Format_RGB32 );
        b.fill( 1 );
    }

    t = QGLWidget::convertToGLFormat( b );
    glGenTextures( 1, &texture[0] );
    glBindTexture( GL_TEXTURE_2D, texture[0] );
    glTexImage2D( GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits() );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}

void GLWidget::keyPressEvent( QKeyEvent * e ) {

    if(e->key() == Qt::Key_Up)
        this->camPosy += 0.5;
    if(e->key() == Qt::Key_Down)
        this->camPosy -= 0.5;

}

它显示了我的广场(应该是我的房间),但它没有在墙上放置 .bmp 文件。

4

1 回答 1

2

那么,您必须提供一些纹理坐标。OpenGL 没有内置水晶球,可以猜测您希望如何放置纹理。


附带说明:所有代码initializeGL(纹理加载除外)resizeGL最好放在绘图函数中。实际上,在绘图功能中按需加载纹理也是有意义的,但是只有与适当的几何剔除和按需几何加载相结合才有好处。


同样在错误消息中,请写

“没找到图片。” (过去时“do”暗示现在时动词)

或者

“找不到图像。” (如果没有使用“do”,请用过去时写动词)

然而,这更好地放在https://english.stackexchange.com/

于 2012-05-21T14:50:36.580 回答