[Qt-interest] [OpenGL] How implement GLWidget::resizeGL for 2D bitmap texture?

John McClurkin jwm at nei.nih.gov
Fri Jun 19 22:00:41 CEST 2009


Ed Sutton wrote:
> I am a Linux/OpenGL newbie trying to convert my GLUT application that draws video frames as 2-D bitmap textures to use QT4 QGLWidget.
>       
> How can I implement the OpenGL equivalent of glutDisplayFunc(&paintGL) for the QT4 GLWidget::resizeGL() method?
>       
> I tried simply calling paintGL() from GLWidget::resizeGL(). What am I missing?
>       
> Thanks in advance for any tips or direction,
>       
> -Ed
You don't call paintGL from the resizeGL function. Also, never call the 
paintGL function directly. If you need to call it, call the widget's 
update function.
Here is my resizeGL function:
void MatrixDisplay::resizeGL(int width, int height)
{
  	int side = qMin(width, height);
	glViewport((width - side) / 2, (height - side) / 2, side, side);

  	glMatrixMode(GL_PROJECTION);

  	glLoadIdentity();
	glOrtho(-8.0, 16.0, -8.0, 16.0, -1000.0, 1000.00);

  	glMatrixMode(GL_MODELVIEW);
}
This is called whenever the widget is resized. The widget is then 
updated, which results in the paintGL function being called. You dont' 
do this by hand.

>       
> // Using QT 4
> // GLWidget is derived from QGLWidget
> class GLWidget : public QGLWidget{};
> 
> void GLWidget::resizeGL(int width, int height)
> {
>    // ToDo: Under glut, glutDisplayFunc(&paintGL) worked
>    // Under QT what is the equivalent?
> }
> 
> void GLWidget::initializeGL()
> {
>    glEnable(GL_DOUBLE);
>    glEnable(GL_DEPTH);
>    glEnable(GL_RGB);
>    
>    m_pBitmap = BitmapHelper::LoadBitMap("splash.bmp");
>    glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); // Black background
>    m_width = m_pBitmap->ptrHeaderBitMapInfo.width;
>    m_height = m_pBitmap->ptrHeaderBitMapInfo.height;
>    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
>    glPixelStorei(GL_PACK_ALIGNMENT, 1);
>    glBindTexture(GL_TEXTURE_2D, m_texture[0]);
>    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, m_width, m_height, 0, GL_BGR, GL_UNSIGNED_BYTE, (GLvoid*)m_pBitmap->pData);
>    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
> }
> 
> void GLWidget::paintGL()
> {
>    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the window with current clearing color
>    glShadeModel(GL_SMOOTH);
>    glEnable(GL_NORMALIZE);
>    glPushMatrix();
>    {
>       glDisable(GL_LIGHTING); // Draw plane that the cube rests on
>       glEnable(GL_TEXTURE_2D); // should use shader, but for an example fixed pipeline is ok ;\)
>       glBindTexture(GL_TEXTURE_2D, m_texture[0]);
>       glBegin(GL_TRIANGLE_STRIP); // draw something with the texture on
>       {
>          glTexCoord2f(0.0, 0.0);
>          glVertex2f(-1.0, -1.0);
>          
>          glTexCoord2f(1.0, 0.0);
>          glVertex2f(1.0, -1.0);
>          
>          glTexCoord2f(0.0, 1.0);
>          glVertex2f(-1.0, 1.0);
>          
>          glTexCoord2f(1.0, 1.0);
>          glVertex2f(1.0, 1.0);
>       }
>       glEnd();
>    }
>    glPopMatrix();
>    swapBuffers(); // Flush drawing commands
> }
> 
> 



More information about the Qt-interest-old mailing list