How to flip a texture within beginNativePainting()?

598 views Asked by At

I'm writing a class that inherits from QGLWidget:

class GLWidget : public QGLWidget
{
    Q_OBJECT

public:
    GLWidget(QWidget *parent = 0);
    ~GLWidget();

    void paintEvent(QPaintEvent *event);    

private:
    QImage* _frame;
};

The constructor of GLWidget loads an image from the disk, and paintEvent() is responsible to send the data to the GPU using native OpenGL calls that are executed between beginNativePainting() and endNativePainting(), and this works OK.

The problem is that the image is being displayed flipped (vertically), and I need to fix this. I haven't found a way to this so I'm hoping somebody can help me on this issue.

In a native Glut application I could simply call glFrustum() like this to do the flip:

static void Reshape( int width, int height )
{
   glViewport( 0, 0, width, height );

   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();

   glFrustum( -1.0, 1.0, 1.0, -1.0, 10.0, 100.0 );  // flip vertically

   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity();
   glTranslatef( 0.0, 0.0, -15.0 );
}

But I've tried loading the projection matrix after the beginNativePainting() and calling glFrustum() with the same values as the call above and all I get is a black screen (which means it didn't worked as expected).

Any tips?

Also, using the mirrowed(false, true) method of QImage to do the flipping didn't had any effect in the displaying of the image.

1

There are 1 answers

2
datenwolf On BEST ANSWER

Don't flip the projection, mirror the texture coordinates.

BTW: OpenGL assumes the origin of texture image data in the lower left corner (contrary to most windowing systems, which have it in the upper left).