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