0

如果这不准确,我深表歉意。我正在尽我所能将代码从一台计算机手动复制到另一台计算机,而目标计算机没有编译器(不要问)。

头文件

#ifndef MYOPENGLWIDGET_H
#define MYOPENGLWIDGET_H

#include <qopenglwidget.h>

class MyOpenGlWidget : public QOpenGLWidget
{
    Q_OBJECT

public:
    explicit MyOpenGlWidget(QWidget *parent = 0, Qt::WindowFlags f = Qt::WindowFlags());
    virtual ~MyOpenGlWidget();

protected:
    // these are supposed to be overridden, so use the "override" keyword to compiler check yourself
    virtual void initializeGL() override;
    virtual void resizeGL(int w, int h) override;
    virtual void paintGL() override;
private:
    QPixmap *_foregroundPixmap;
}

#endif 

源文件

QOpenGLFunctions_2_1 *f = 0;


MyOpenGlWidget::MyOpenGlWidget(QWidget *parent, Qt::WindowFlags f) :
    QOpenGLWidget(parent, f)
{
    _foregroundPixmap = 0;
    QPixmap *p = new QPixmap("beveled_texture.tiff");
    if (!p->isNull())
    {
        _foregroundPixmap = p;
    }
}

MyOpenGlWidget::~MyOpenGlWidget()
{
    delete _foregroundPixmap;
}

void MyOpenGlWidget::initializeGL()
{
    // getting a deprecated set of functions because such is my work environment 
    // Note: Also, QOpenGLWidget doesn't support these natively.
    f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_2_1>();

    f->glClearColor(0.0f, 1.0f, 0.0f, 1.0f);    // clearing to green
    f->glEnable(GL_DEPTH_TEST);
    f->glEnable(GL_CULL_FACE);  // implicitly culling front face
    f->glEnable(GL_SCISSOR_TEST);

    // it is either copy the matrix and viewport code from resizeGL or just call the method
    this->resizeGL(this->width(), this->height());
}

void MyOpenGlWidget::resizeGL(int w, int h)
{
    // make the viewport square
    int sideLen = qMin(w, h);
    int x = (w - side) / 2;
    int y = (h - side) / 2;

    // the widget is 400x400, so this random demonstration square will show up inside it
    f->glViewport(50, 50, 100, 100);
    f->glMatrixMode(GL_PROJECTION);
    f->glLoadIdentity();
    f->glOrtho(-2.0f, +2.0f, -2.0f, +2.0f, 1.0f, 15.0f);    // magic numbers left over from a demo
    f->glMatrixMode(GL_MODELVIEW);

    // queue up a paint event
    // Note: QGLWidget used updateGL(), but QOpenGLWidget uses update().
    this->update();
}

void MyOpenGlWidget::paintGL()
{
    f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // I want to draw a texture with beveled edges the size of this widget, so I can't 
    // have the background clearing all the way to the edges
    f->glScissor(50, 50, 200, 200);     // more magic numbers just for demonstration

    // clears to green in just scissored area (unless QPainter is created)
    f->glClearColor(0.0f, 1.0f, 0.0f, 1.0f);

    // loading identity matrix, doing f->glTranslatef(...) and f->glRotatef(...)

    // pixmap loaded earlier in another function
    if (_foregroundPixmap != 0)
    {
        // QPixmap apparently draws such that culling the back face will cull the entire 
        // pixmap, so have to switch culling for duration of pixmap drawing
        f->glCullFace(GL_FRONT);

        QPainter(this);
        painter.drawPixmap(0, 0, _foregroundPixmap->scaled(this->size()));

        // done, so switch back to culling the front face
        f->glCullFace(GL_BACK);
    }
QOpenGLFunctions_2_1 *f = 0;

void MyOpenGlWidget::initializeGL()
{
    // getting a deprecated set of functions because such is my work environment 
    // Note: Also, QOpenGLWidget doesn't support these natively.
    f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_2_1>();

    f->glClearColor(0.0f, 1.0f, 0.0f, 1.0f);    // clearing to green
    f->glEnable(GL_DEPTH_TEST);
    f->glEnable(GL_CULL_FACE);  // implicitly culling front face
    f->glEnable(GL_SCISSOR_TEST);

    // it is either copy the matrix and viewport code from resizeGL or just call it directly
    this->resizeGL(this->width(), this->height());
}

void MyOpenGlWidget::resizeGL(int w, int h)
{
    // make the viewport square
    int sideLen = qMin(w, h);
    int x = (w - side) / 2;
    int y = (h - side) / 2;

    // the widget is 400x400, so this random demonstration square will show up inside it
    f->glViewport(50, 50, 100, 100);
    f->glMatrixMode(GL_PROJECTION);
    f->glLoadIdentity();
    f->glOrtho(-2.0f, +2.0f, -2.0f, +2.0f, 1.0f, 15.0f);    // magic numbers left over from a demo
    f->glMatrixMode(GL_MODELVIEW);

    // queue up a paint event
    // Note: QGLWidget used updateGL(), but QOpenGLWidget uses update().
    this->update();
}

void MyOpenGlWidget::paintGL()
{
    f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // I want to draw a texture with beveled edges the size of this widget, so I can't 
    // have the background clearing all the way to the edges
    f->glScissor(50, 50, 200, 200);     // more magic numbers just for demonstration

    // clears to green in just scissored area (unless QPainter is created)
    f->glClearColor(0.0f, 1.0f, 0.0f, 1.0f);

    // loading identity matrix, doing f->glTranslatef(...) and f->glRotatef(...), drawing triangles

    // done drawing, so now draw the beveled foreground
    if (_foregroundPixmap != 0)
    {
        // QPixmap apparently draws such that culling the back face will cull the entire 
        // pixmap, so have to switch culling for duration of pixmap drawing
        f->glCullFace(GL_FRONT);

        QPainter(this);
        painter.drawPixmap(0, 0, _foregroundPixmap->scaled(this->size()));

        // done, so switch back to culling the front face
        f->glCullFace(GL_BACK);
    }
}

问题是这段代码来自paintGL()

QPainter(this);

一旦创建了 QPainter 对象glScissor(...),我之前在函数中所做的调用就会溢出,并且会进行某种glClearColor(...)调用(可能来自 QPainter 的构造函数),将整个视口清除为我刚刚设置的背景颜色glScissor(...)。然后像素图可以很好地绘制我的斜面纹理。

我不希望 QPainter 超出我的剪裁范围。

我得到的最接近解释的是两个 QPainter 方法beginNativePainting()endNativePainting(). 根据文档,这两者之间禁用了剪刀测试,但在他们的示例中,他们重新启用了它。我尝试使用这个“原生绘画”代码,但我无法阻止 QPainter 的存在,因为它忽略了 GL 的剪裁和清除我的整个视口。

为什么会发生这种情况,我该如何阻止这种情况?

注意:这台工作电脑有网络政策,禁止我去imgur之类的娱乐网站上传“我想要的”和“我得到的”图片,所以我必须用文字说明。

4

1 回答 1

0

为什么会这样

OpenGL 上下文是一个共享资源,您必须与其他玩家共享它。

我该如何阻止呢?

你不能。只需做正确的事情并在正确的时刻设置视口、剪刀矩形和所有其他与绘图相关的状态:就在您要绘制依赖于这些设置的东西之前。之前不要在某个“初始化”或重塑处理程序中设置它们(用计算机术语)。并且可以预料,在绘制代码时,您调用的任何使用 OpenGL 的函数都会留下一些垃圾。

于 2016-08-06T11:08:07.590 回答