Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Texture problem with OpenGL in QT
Forum Update on Monday, May 27th 2025

Texture problem with OpenGL in QT

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    browser90
    wrote on 21 May 2012, 11:42 last edited by
    #1

    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,

    1 Reply Last reply
    0
    • M Offline
      M Offline
      minimoog77
      wrote on 21 May 2012, 21:14 last edited by
      #2

      You didn't specified texture coordinates for vertices.

      See: http://www.opengl.org/sdk/docs/man/xhtml/glTexCoord.xml

      1 Reply Last reply
      0

      1/2

      21 May 2012, 11:42

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved