0

I am learning PyOpenGL and my first animation test is trying to make a blinking rectangle, but I could not get it to animate, It only blinks when I move the cursor on a window. I guess I am missing a while loop but I could not figure out where to put it. Here is a short of my code.

In fragment shader:

#version 410 core

in Data
{
    vec2 uv;
    vec4 rgb;
} fs_in;

layout ( location = 0 ) out vec4 fragColor;
uniform float alpha;

void main(){
    fragColor = vec4(1.0, 1.0, 1.0, alpha);
}

In main code: I am only using QOpenGLWindow of PyQt5 to draw the scene.

class Rect(QOpenGLWindow):
    def __init__(self):
        super(Rect, self).__init__()
        ...
        self.queued = 0.0
        self.angle = 0.0

    def initializeGL(self):
        ...

    def blinking(self):
        x = mapX(sin(self.angle), -1, 1, 0, 1 ) 
        self.rect.shader.uniformf('alpha', x)    
        self.angle += 0.16

    def paintGL(self):
        glClearColor( 0.0, 0.0, 0.0, 1.0 )
        glClear( GL_COLOR_BUFFER_BIT )

        ...
        #Draw a rectangle
        ...
        #Animate the rectangle
        self.blinking()

....

And I try this in paintGL but still not working

start = time.time()
time.sleep(0.01) 
done = time.time()
timeDifference = done - start

self.queued += timeDifference

while self.queued >= 0.01:
    self.blinking()
    self.queued -= 0.01

Any suggestions?

ryanvolap
  • 77
  • 1
  • 8
  • @Rabbid76 I'm new to Qt as well. I have to look into it. Thanks. – ryanvolap Aug 16 '17 at 11:56
  • @ryanvolap: Every QObject has timer capability. Overwrite the `timerEvent` virtual method and use `startTimer` on the instance for which you want recurrent events. For continuous redraw start a *idle* timer (interval=0) and call `update` in the idle timer event. – datenwolf Aug 16 '17 at 14:06
  • @datenwolf You mean using `QTimer`? And calling `update` you mean `self.update()` in paintGL function? Thanks – ryanvolap Aug 17 '17 at 06:55
  • @ryanvolap: No, I mean **[QObject::startTimer](https://doc.qt.io/qt-5/qobject.html#startTimer)** which you'd call with `self.startTimer`. And yes, I mean you to call **[QWidget::update](https://doc.qt.io/qt-5/qwidget.html#update-1)** with `self.update()` to initiate a redraw. – datenwolf Aug 17 '17 at 09:14

0 Answers0