Texture problem with OpenGL in QT
-
wrote on 21 May 2012, 11:42 last edited by
HI guys,
I'm trying to make a room in Qt with OpenGL, now I'm stuck with the texture part. It doens't place my texture on the walls.
This is my code:
@
#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;
}
@It shows my square ( that should be my room ), but it doesn't place a .bmp file on the walls.
Can someone help me please?
Kind regards,
-
wrote on 21 May 2012, 21:14 last edited by
You didn't specified texture coordinates for vertices.
See: http://www.opengl.org/sdk/docs/man/xhtml/glTexCoord.xml
1/2