Hi, I'm trying to animate the motion of the upper arm by using a series of x,y and z coordinates of the elbow. the runtime error occurs after i rotated for a while by using the mouse. i believe the error is due to the "counter++" in "void MyPanelOpenGL::mouseMoveEvent(QMouseEvent *event)" as the animation works if i don't try to do any rotation.

Can anyone help me solve this issue? Any help will be greatly appreciated. Thanks.


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