简体   繁体   English

Qt QOpenGLWidget glClearColor 无法正常运行

[英]Qt QOpenGLWidget glClearColor not functioning correctly

I have a slight problem when calling glClearColor from various places outside of paintGL().从paintGL() 之外的各个地方调用glClearColor 时,我遇到了一个小问题。 The aim is to enable the user to set the clear colour on the fly but this is not working as planned unless glClearColor is called each frame in paintGL.目的是让用户能够即时设置清晰的颜色,但这不会按计划工作,除非在paintGL 中的每一帧都调用glClearColor。

Aim:目的:

void GLWidget::mousePressEvent(QMouseEvent *event)
{
    m_lastPos = event->pos();
    glClearColor(1.0f, 0.0f, 0.0f, 1.0f); //<-- Doesn't change clear colour
}

Non-optimal workaround:非最佳解决方法:

void GLWidget::mousePressEvent(QMouseEvent *event)
{
    m_lastPos = event->pos();
    r = 1.0f;
    g = 0.0f;
    b = 0.0f;
    a = 1.0f;
}

void GLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glClearColor(r, g, b, a);
...

I assume this has something to do with how Qt swaps buffers and updates the screen but it's not clear what exactly is causing it.我认为这与 Qt 如何交换缓冲区和更新屏幕有关,但尚不清楚究竟是什么原因造成的。 Any ideas would be great, thanks.任何想法都会很棒,谢谢。

I assume this has something to do with how Qt swaps buffers and updates the screen but it's not clear what exactly is causing it. 我认为这与Qt交换缓冲区和更新屏幕的方式有关,但尚不清楚到底是什么原因引起的。 Any ideas would be great, thanks. 任何想法都很好,谢谢。

Wrong, it has to do with doing OpenGL calls with no OpenGL context bound. 错误的,这与在没有OpenGL上下文绑定的情况下进行OpenGL调用有关。 You must call makeCurrent before doing any OpenGL call . 在执行任何OpenGL调用之前,必须先调用makeCurrent

Why does it work in paintGL then? 那为什么它在paintGLpaintGL Because Qt makes the context current automatically before calling paintGL , resizeGL and initializeGL (see their documentation). 因为Qt会在调用paintGLresizeGLinitializeGL之前自动使上下文成为当前上下文(请参阅其文档)。

I don't think @peppe answer correctly because You don't need to call makeCurrent() since Qt have already to我不认为@peppe 回答正确,因为您不需要调用 makeCurrent() 因为 Qt 已经

In my opinion, what you really need is just an update() call in mousePressEvent .在我看来,您真正需要的只是mousePressEventupdate()调用。

If you wan't to call paintGL() to update, what you need is just to call update()如果你不想调用paintGL()来更新,你只需要调用update()

Also, you should know that in glClearColor() , you only set the clearColor attribute on OpenGL state machine.此外,您应该知道在glClearColor() ,您只在 OpenGL 状态机上设置 clearColor 属性。 glClear(GL_COLOR_BUFFER_BIT) is the true function which actually clear the color buffer. glClear(GL_COLOR_BUFFER_BIT)是真正清除颜色缓冲区的函数。 So you should set clearColor by glClearColor() before calling glClear() .所以你应该在调用glClear()之前通过glClearColor()设置 clearColor 。

if I were you, the code would be like:如果我是你,代码会是这样的:

void GLWidget::mousePressEvent(QMouseEvent *event)
{
    m_lastPos = event->pos();
    r = 1.0f;
    g = 0.0f;
    b = 0.0f;
    a = 1.0f;
    update();
}

void GLWidget::paintGL()
{
    glClearColor(r, g, b, a);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM