Hi, i'm trying to animate a moving arm using opengl.

i'm using update() at the end of my paintGL() function but the animation is currently too quick. is there anyway i can control the speed of the update() function?

in addition, can i know what is the difference between update() and updateGL() as my program crashes when i used updateGL() instead of update()?

Any help will be appreciated. Thanks.

Below is my code
Qt Code:
  1. #include "mypanelopengl.h"
  2. #include <cmath>
  3.  
  4.  
  5.  
  6. MyPanelOpenGL::MyPanelOpenGL(QWidget *parent) :
  7. QGLWidget(parent)
  8. {
  9.  
  10. angle = 0.0;
  11. flag = true;
  12.  
  13. }
  14.  
  15.  
  16.  
  17.  
  18. void MyPanelOpenGL::initializeGL()
  19. {
  20. qglClearColor(Qt::white);
  21. glShadeModel(GL_FLAT);
  22. glEnable(GL_DEPTH_TEST);
  23. glEnable(GL_CULL_FACE);
  24. gluLookAt(-5,0,0,0,0,0,0,1,0);
  25. }
  26.  
  27.  
  28. void MyPanelOpenGL::resizeGL(int width, int height)
  29. {
  30. glViewport(0, 0, width, height);
  31. glMatrixMode(GL_PROJECTION);
  32. glLoadIdentity();
  33. glFrustum(-5.0, 5.0, -5.0, 5.0, -15.0, 15.0);
  34. glOrtho(0,10,0,10,-5,5);
  35. glMatrixMode(GL_MODELVIEW);
  36.  
  37. }
  38.  
  39. void MyPanelOpenGL::drawspine()
  40. {
  41. glBegin(GL_LINES);
  42.  
  43. glColor3ub(0,0,0);
  44.  
  45. //Spine
  46. glVertex3f(0, -5, 0);
  47. glVertex3f(0, 5, 0);
  48.  
  49. glEnd();
  50. }
  51.  
  52. void MyPanelOpenGL::drawshoulder()
  53. {
  54. glBegin(GL_LINES);
  55.  
  56. glColor3ub(0,0,0);
  57.  
  58. //Shoulder
  59. glVertex3f(-5, 0, 0);
  60. glVertex3f(5, 0, 0);
  61.  
  62. glEnd();
  63. }
  64.  
  65. void MyPanelOpenGL::drawarm()
  66. {
  67. glBegin(GL_LINES);
  68.  
  69. glColor3ub(0,0,0);
  70.  
  71. //Arm
  72. glVertex3f(-5, 0, 0);
  73. glVertex3f(-5, -5, 0);
  74.  
  75. glEnd();
  76.  
  77. }
  78.  
  79.  
  80. void MyPanelOpenGL::paintGL()
  81. {
  82. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  83. glLoadIdentity();
  84. glTranslated(5.0, 5.0, 0.0);
  85. glLineWidth(1);
  86. glColor3f(0, 0.7f, 0.7f);
  87.  
  88.  
  89. glRotatef(rotationY, 0.0, 1.0, 0.0);
  90.  
  91.  
  92. drawspine();
  93. drawshoulder();
  94. glPushMatrix();
  95. glRotatef(angle,1,0,0);
  96. drawarm();
  97. glPopMatrix();
  98. if(flag)
  99. {
  100. if(angle <90)
  101. angle++;
  102. else
  103. flag = false;
  104. }
  105. else
  106. {
  107. if(angle > 0)
  108. angle--;
  109. else
  110. flag = true;
  111. }
  112.  
  113.  
  114. update();
  115.  
  116. }
  117.  
  118.  
  119.  
  120. void MyPanelOpenGL::mousePressEvent(QMouseEvent *event)
  121. {
  122. lastPos = event->pos();
  123. }
  124.  
  125. void MyPanelOpenGL::mouseMoveEvent(QMouseEvent *event)
  126. {
  127. GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
  128. GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();
  129. if (event->buttons() & Qt::LeftButton) {
  130. rotationX += 180 * dy;
  131. rotationY += 180 * dx;
  132. updateGL();
  133. } else if (event->buttons() & Qt::RightButton) {
  134. rotationX += 180 * dy;
  135. rotationZ += 180 * dx;
  136. updateGL();
  137. }
  138. lastPos = event->pos();
  139. }
To copy to clipboard, switch view to plain text mode