PDA

View Full Version : Texturing in a QGLWidget



so Qt
16th July 2015, 13:17
Hey,

I'm just trying to setup a minimal example trying to draw a textured quad and already failing on this one. Code looks like this:


class Foo : public QGLWidget
{
public:
Foo( QWidget* parent ) : QGLWidget( parent )
{
}

void initializeGL( )
{
glDisable( GL_LIGHTING );
glEnable( GL_TEXTURE_2D );
glViewport( 0, 0, width( ), height( ) );

glMatrixMode( GL_PROJECTION );
glLoadIdentity( );

gluPerspective( 45.0f, (GLfloat)width( ) / (GLfloat)height( ), 0.1f, 100.0f );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );

texId = bindTexture( "test.png" );
}

void paintGL( ) override
{
glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT );

glLoadIdentity();
glTranslatef( 0.0f, 0.0f, -6.0f );
//drawTexture( rect( ), texId);
glBegin( GL_QUADS );
glTexCoord2f( 0.0f, 0.0f );
glVertex3f( -1.0f, 1.0f, 0.0f );
glTexCoord2f( 1.0f, 0.0f );
glVertex3f( 1.0f, 1.0f, 0.0f );
glTexCoord2f( 1.0f, 1.0f );
glVertex3f( 1.0f, -1.0f, 0.0f );
//glColor3f( 0.0f, 1.0f, 0.0f );
glTexCoord2f( 0.0f, 1.0f );
glVertex3f( -1.0f, -1.0f, 0.0f );
glEnd( );
}

private:
GLuint texId;
};

/*
* Application entry point.
*/
int main( int argc, char **argv )
{
QApplication app(argc, argv);

QMainWindow *window = new QMainWindow( );
window->setFixedSize( 800, 600 );
window->setWindowTitle( QString::fromUtf8( "test" ) );

QWidget *centralWidget = new QWidget( window );
QVBoxLayout* layout = new QVBoxLayout( centralWidget );
Foo* foo = new Foo( centralWidget );
layout->addWidget( foo );

window->setCentralWidget( centralWidget );
window->show( );

return app.exec( );
}

I've also tried to manually bind the texture with glGenTexture, glBindTexture and converting the QImage to a OpenGL format and I've tried to experiment with drawTexture but nothing works. If I comment in the glColor line, my quad is rendered green, so it's definitely in my viewport. Am I maybe missing some other state variable or might this be a GL context problem?

Can I somehow double check that bindTexture worked as expected? Because texId will be 0 no matter if I input a valid or invalid filename. Are there any constraits about the texture's size? Because mine is 609x731.