How to flip a texture within beginNativePainting()?

时光怂恿深爱的人放手 提交于 2019-12-23 16:09:13

问题


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:


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).



来源:https://stackoverflow.com/questions/8988485/how-to-flip-a-texture-within-beginnativepainting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!