使用着色器时在QGLWidget中通过OpenGL使用QPainter [英] Using QPainter over OpenGL in QGLWidget when using shaders

查看:509
本文介绍了使用着色器时在QGLWidget中通过OpenGL使用QPainter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多Qt(特别是4.6)用户将熟悉OpenGL教程中提供的Overpainting示例,我正在尝试做非常相似的事情,但将着色器用于纯OpenGL数据,而不是旧的固定功能管道.

Many of you Qt (4.6 specifically) users will be familiar with the Overpainting example supplied in the OpenGL tutorials, I'm trying to do something very similar but using shaders for the pure OpenGL data, instead of the old fixed-function pipeline.

//  Set background and state.
makeCurrent();
qglClearColor( bgColour_ );

glEnable( GL_DEPTH_TEST );
glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );

if ( smoothLines_ ) {
    glEnable( GL_BLEND );
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
    glEnable( GL_LINE_SMOOTH );
    glHint( GL_LINE_SMOOTH_HINT, GL_NICEST );
}

//  Clear the buffers.
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glUseProgram( shader_ );

//  Draw the grid.
try {
    glLineWidth( 2.0f );
    manager_->setColour( centreColour_ );
    grid_->draw( oE_GLGrid::Centre );

    glLineWidth( 1.5f );
    manager_->setColour( majorColour_ );
    grid_->draw( oE_GLGrid::Major );

    glLineWidth( 1.0f );
    manager_->setColour( minorColour_ );
    grid_->draw( oE_GLGrid::Minor );
} catch( oE_GLException& e ) {
    cout << "OpenGL Error: " << e.what() << endl;
    return;
}

//  Reset OpenGL state for overlays.
glDisable( GL_DEPTH_TEST );
if ( smoothLines_ ) {
    glDisable( GL_BLEND );
    glDisable( GL_LINE_SMOOTH );
}

//  Draw the overlays.
QPainter p( this );
p.beginNativePainting();
p.fillRect( 10, 10, 100, 100,
        QColor( 255, 0, 0 ) );
p.endNativePainting();

我正在构建3D资产应用程序,因此正在构建网格东西.为了使其在工作时确实很明显,应该在小部件的左上角显示一个大的红色矩形-但这不是.

I'm building a 3D asset application hence the grid stuff. To make it really obvious when it works, a big red rectangle should appear in the top left corner of the widget - but it doesn't.

3D可以正常工作,但是QPainter在屏幕上什么也没有.我和我的文档之间唯一真正的区别是,所有投影和变换矩阵计算都被其他函数隐藏,然后通过glUniform上传到着色器.大概是因为QPainter将2D资产拆分为三角形,这意味着QPainter的着色器无法访问我的投影/变换矩阵,所以我的红色三角形开始绘制-但可能不在屏幕上.

The 3D works fine, but QPainter gets nothing to the screen. The only real difference I can see between mine and the docs is that the all the projection and transformation matrix calculations are hidden away in other functions and then uploaded to the shader via glUniform. Presumably as QPainter splits the 2D assets into triangles, this means that QPainter's shaders don't have access to my projection/transformation matrices, so my red triangle is begin drawn - but perhaps somewhere offscreen.

推荐答案

在进行OpenGL调用之前,调用beginNativePainting(). OpenGL状态的glPush/Pop也可能是必需的.尝试如下操作:

Call beginNativePainting() before making OpenGL calls. A glPush/Pop of the OpenGL state may also be necessary. Try something like the following:

QPainter p( this );
p.beginNativePainting();

// Maybe necessary
glPushAttrib(GL_ALL_ATTRIB_BITS);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();

// Put OpenGL code here

// Necessary if used glPush-es above
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopAttrib();

p.endNativePainting();

p.fillRect( 10, 10, 100, 100,
        QColor( 255, 0, 0 ) );

这篇关于使用着色器时在QGLWidget中通过OpenGL使用QPainter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆