简体   繁体   中英

uniform values and OpenGL shaders

so I have a little animation I guess in openGL in a program I'm coding with shaders in QT.

My animation changed as a function of the time. To calculate time I used a Timer in a Slot that would repeatedly update the time.

I have a Qwidget, my vertex shader is very plain so I wont include the code.

now for my fragment shader:

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

and then my time update method:

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

}

and finally how I use it:

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

however when I run my program it compiles and executes but the timer doesn't update, no matter how long I wait. My hunch is that I am not using a good way to update the time in my shader, what am I doing wrong?

My guess is that you are updating the time correctly but you are not re-drawing your widget. You should call the function that re-draw the widget at the end of updateTime (I think the name of that function is repaint()).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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