统一的值和开放式着色器

uniform values and OpenGL shaders

本文关键字:开放式      更新时间:2023-10-16

,所以我在QT中使用着色器编码的程序中猜我有一个小动画。

我的动画随着时间的变化而变化。为了计算时间,我在插槽中使用的计时器会反复更新时间。

我有一个qwidget,我的顶点着色器很简单,所以我不包括代码。

现在,我的碎片着色器:

 QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
 const char *fsrc =
        "uniform float time;n"
        "in vec4 coordinates;n"
        "out vec4 outcolor;n"
        "void main(void)n"
        "{n"
        "   float l,z=time;"
        "   outcolor=vec4(coordinates * time);" //<- pretend i did something with time here
        "}n";
fshader->compileSourceCode(fsrc);

然后我的时间更新方法:

void MyWidget::updateTime()
{
    float seconds;
    seconds = (float) ((double) clock() - startTime) / CLOCKS_PER_SEC;
    program->setUniformValue("time", (float) seconds / (float) CLOCKS_PER_SEC);
}

最后我如何使用它:

 timeTimer=new QTimer();    
QObject::connect(timeTimer, SIGNAL(timeout()),this, SLOT(updateTime()));
timeTimer->start(20);

但是,当我运行程序时,它会编译和执行,但是计时器不会更新,无论我等待多长时间。我的直觉是我没有使用好方法来更新着色器中的时间,我在做什么错?

我的猜测是您正在正确更新时间,但您没有重新绘制小部件。您应该调用在UpdateTime末尾重新绘制小部件的函数(我认为该函数的名称是Repaver())。