English 中文(简体)
在 Qt 中以 OpenGL 以 OpenGL 装入纹理
原标题:Texture loading with OpenGL in Qt
  • 时间:2012-05-21 11:49:48
  •  标签:
  • qt
  • opengl

我试图用 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文件。

问题回答

您必须提供一些纹理坐标。 OpenGL 没有一个加密的圆球, 并且可以猜到您想要如何设置纹理 。


旁注: 初始化GL (纹理加载除外) 和 重新缩放GL 中的所有代码更好地置于绘图功能中。在绘图函数中按需装入纹理也是有道理的,但是,如果与适当的几何缩放和按需的几何加载相结合,它只会有好处。


在错误消息中,请同时写入

"没有找到图像。 (过去时态是指当前时态动词)

"未找到图像" (如果没有用过的话) (如果没有用过去时态写入动词)

然而,在>https://english.stackschange.com/上,这个平台更合适。





相关问题
Qt: Do events get processed in order?

If I had a class A, where one of its functions does: void A::func() { emit first_signal(); emit second_signal(); } Assuming that a class B has 2 slots, one connected to first_signal, and the ...

How to determine how much free space on a drive in Qt?

I m using Qt and want a platform-independent way of getting the available free disk space. I know in Linux I can use statfs and in Windows I can use GetDiskFreeSpaceEx(). I know boost has a way, ...

Drag & drop with .ui files

I m having big troubles with drag & drop. I ve created a new Qt Designer Form Class in which I have one QListWidget and one QWidget. Now I want to enable dragging & dropping between these two ...

Link errors on Snow Leopard

I creating a small desktop application using Qt and Poco on Mac OS X Snow Leopard. Qt works fine, but once I started linking with Poco I get the following warning: ld: warning: in /Developer/SDKs/...

Showing two windows in Qt4

My friend and I have each created parts of a GUI using Qt 4. They both work independently and I am trying to integrate his form with the my main window. As of now this is the code I am using to try ...

Qt equivalent of .NET data binding?

Is there an equivalent of .NET s data binding in Qt? I want to populate some combo boxes and other widgets with QStrings that refer to specific entities in my database. However, it would be cleaner ...

热门标签