Results 1 to 8 of 8

Thread: QOpenGL QTimer, how do I animate this?

  1. #1
    Join Date
    Dec 2013
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QOpenGL QTimer, how do I animate this?

    Hi everyone,

    I'm drawing a large amount of triangles on the screen using the intialize-resize-paint method.
    The program works, but without animation.
    I've been trying for a long time to figure out how to use a Timer to animate this, but I haven't been able to find a way.
    (Background info: the animation I want is to make the triangles rotate and scale individually.)
    Can somebody help me?

    Since it is quite a large program, I have brought the code down to structure only.

    Thank you ahead of time!

    In the main:
    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. QApplication a(argc,argv);
    4.  
    5. GlWidget w;
    6. w.show();
    7.  
    8. return a.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    In the glwidget header file:

    Qt Code:
    1. class GlWidget : public QGLWidget
    2. {
    3. Q_OBJECT //this macro is included because we might want to use signals and slots
    4.  
    5. public:
    6. GlWidget(QWidget *parent = 0);
    7. ~GlWidget();
    8. QSize sizeHint() const; //to set reasonable default sizes
    9.  
    10. protected:
    11.  
    12. void initializeGL();
    13. void resizeGL(int width, int height);
    14. void paintGL();
    15.  
    16. private:
    17.  
    18. makeTriangles();
    19. QVector<QVector3D> vertices, colors;
    20.  
    21. };
    To copy to clipboard, switch view to plain text mode 

    In the glwidget source file:

    Qt Code:
    1. #include "glwidget.h"
    2.  
    3. //! [0]
    4. GlWidget::GlWidget(QWidget *parent) : QGLWidget(QGLFormat(), parent)
    5. {
    6. }
    7.  
    8. GlWidget::~GlWidget()
    9. {
    10. }
    11.  
    12.  
    13. QSize GlWidget::sizeHint() const
    14. {
    15. return QSize(520, 640);
    16. }
    17. //! [0]
    18.  
    19.  
    20. //! [1]
    21.  
    22. void GlWidget::initializeGL()
    23. {
    24. glEnable(GL_DEPTH_TEST);
    25.  
    26. glEnable(GL_CULL_FACE);
    27.  
    28. QGLWidget::qglClearColor(QColor(20,20,20,255));
    29.  
    30. shaderProgram.addShaderFromSourceFile(QGLShader::Vertex, ":/vertexShader.vsh");
    31. shaderProgram.addShaderFromSourceFile(QGLShader::Fragment, ":/fragmentShader.fsh");
    32. shaderProgram.link();
    33.  
    34. makeTriangles();
    35. }
    36. //! [1]
    37.  
    38. //! [2]
    39.  
    40. void GlWidget::resizeGL(int width, int height)
    41. {
    42. if (height == 0) {
    43. height = 1;
    44. }
    45.  
    46. pMatrix.setToIdentity();
    47. pMatrix.perspective(60.0, (float) width / (float) height, 0.001, 1000);
    48.  
    49. glViewport(0, 0, width, height);
    50. }
    51. //! [2]
    52.  
    53. //! [3]
    54. void GlWidget::paintGL()
    55. {
    56.  
    57. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    58.  
    59. QMatrix4x4 mMatrix;
    60. QMatrix4x4 vMatrix;
    61.  
    62. shaderProgram.bind();
    63.  
    64. shaderProgram.setUniformValue("mvpMatrix", pMatrix * vMatrix * mMatrix);
    65.  
    66. shaderProgram.setAttributeArray("color", colors.constData());
    67. shaderProgram.enableAttributeArray("color");
    68.  
    69. shaderProgram.setAttributeArray("vertex", vertices.constData());
    70. shaderProgram.enableAttributeArray("vertex");
    71.  
    72. glDrawArrays(GL_TRIANGLES, 0, vertices.size());
    73.  
    74. shaderProgram.disableAttributeArray("vertex");
    75. shaderProgram.disableAttributeArray("color");
    76. shaderProgram.release();
    77. }
    78.  
    79.  
    80. GlWidget::makeTriangles()
    81. {
    82. /*specify vertices and colors of triangles, here, the variables vertices and color are defined*/
    83. }
    84. //! [3]
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: QOpenGL QTimer, how do I animate this?

    How are you using the timer? I think the simplest approach would be to make each triangle a QObject and use QPropertyAnimation to animate its state. Then through the use of signals, notify the view that it needs to be redrawn.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,294
    Thanks
    36
    Thanked 1,521 Times in 1,472 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QOpenGL QTimer, how do I animate this?

    One way would be to add Qt properties for values you want to animate individually and then use QPropertyAnimation to drive the value changes.

    You would then also connect the properties' change signals to the updateGL() slot, so that it is called whenever any of those properties changes.

    Cheers,
    _

  4. #4
    Join Date
    Dec 2013
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QOpenGL QTimer, how do I animate this?

    I tried using a QTimer, (copying the idea from a graphicsscene application I have made)

    adding a declaration in the header:

    Qt Code:
    1. QTimer *timer1;
    2. public slots:
    3. void doSomething();
    To copy to clipboard, switch view to plain text mode 

    adding the following lines to the constructor:

    Qt Code:
    1. timer1 = new QTimer(this);
    2. connect(timer1,SIGNAL(timeout()),this, SLOT(doSomething()));
    3. timer1->start(10);
    To copy to clipboard, switch view to plain text mode 

    and finally adding the slot definition in the source file to test:

    Qt Code:
    1. void GlWidget::doSomething()
    2. {
    3. qDebug() << "I'm doing something!"; //here I would like to call the function makeTriangles() to change the properties 'colors' and 'vertices'
    4. }
    To copy to clipboard, switch view to plain text mode 

    But this apparentlly doesn't work.

    I think the simplest approach would be to make each triangle a QObject and use QPropertyAnimation to animate its state. Then through the use of signals, notify the view that it needs to be redrawn.
    Could you give me an example on how to do this, I have no clue how to implement that?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: QOpenGL QTimer, how do I animate this?

    Quote Originally Posted by JerichoJosh View Post
    Could you give me an example on how to do this, I have no clue how to implement that?
    Have a look at the Animation Framework docs.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Dec 2013
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QOpenGL QTimer, how do I animate this?

    Could you help me to implement a QTimer?
    the documentation you gave me remains too abstract for me, and mostly related to qPainter

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,294
    Thanks
    36
    Thanked 1,521 Times in 1,472 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QOpenGL QTimer, how do I animate this?

    I think what you will have to do is change values in your time slot and then call updateGL()

    That will result in another call to paintGL() in which you can update your GL data.

    Cheers,
    _

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: QOpenGL QTimer, how do I animate this?

    Quote Originally Posted by JerichoJosh View Post
    Could you help me to implement a QTimer?
    the documentation you gave me remains too abstract for me, and mostly related to qPainter
    There is not a single reference to QPainter in the documentation I gave you.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. <qopengl.h> library .lib?
    By giugio in forum Qt Programming
    Replies: 5
    Last Post: 10th September 2013, 10:52
  2. QOpenGL in threads
    By wydesenej in forum Qt Programming
    Replies: 4
    Last Post: 22nd May 2013, 15:11
  3. Example Q, FLTKt and OpenGL without using QOpenGL
    By giorgik in forum Qt Programming
    Replies: 8
    Last Post: 27th December 2012, 18:56
  4. Replies: 15
    Last Post: 4th August 2012, 19:11
  5. how to use QTimer to animate QSlider
    By mike b in forum Newbie
    Replies: 1
    Last Post: 11th May 2010, 05:45

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.