Results 1 to 9 of 9

Thread: OpenGL Runtime Error

  1. #1
    Join Date
    Jan 2013
    Posts
    17
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default OpenGL Runtime Error

    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 

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

    Default Re: OpenGL Runtime Error

    What is the size of "points"?
    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 2013
    Posts
    17
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: OpenGL Runtime Error

    size of "points" is the number of sets of data i've recorded into a QList. it exits the loop and stop redrawing after reading the last set of coordinates.

  4. #4
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: OpenGL Runtime Error

    You are updating counter in two places, one time in paintGL, and second time in mouseMoveEvent.
    Although you do check it in paintGL, you do it after calling drawarm (counter) with (possibly) incorrect counter value.

  5. The following user says thank you to lanz for this useful post:

    kango (18th February 2013)

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

    Default Re: OpenGL Runtime Error

    Furthermore you are creating new timers there each time you draw the scene. Which means a memory consumption and possibly wacky results. paintGL() is meant to paint the scene and only paint the scene, nothing more. No counting, no creating objects -- just painting. I think I already suggested in some other thread that you use a QPropertyAnimation instead of a timer. Please consider that, you'll get code that is predictable. In contrast your current code is not -- try to think what happens when you start resizing the window very quickly.
    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.


  7. The following user says thank you to wysota for this useful post:

    kango (18th February 2013)

  8. #6
    Join Date
    Jan 2013
    Posts
    17
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: OpenGL Runtime Error

    Quote Originally Posted by wysota View Post
    Furthermore you are creating new timers there each time you draw the scene. Which means a memory consumption and possibly wacky results. paintGL() is meant to paint the scene and only paint the scene, nothing more. No counting, no creating objects -- just painting. I think I already suggested in some other thread that you use a QPropertyAnimation instead of a timer. Please consider that, you'll get code that is predictable. In contrast your current code is not -- try to think what happens when you start resizing the window very quickly.
    Thanks for the suggestion but is it possible to gave me a quick example on how i can change to a QPropertyAnimation? i've read the documentation but still could not really understand as i'm very new to programming. Or is there anyway i could just add some constraints to my counter in my mouseEvent to make it work?

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: OpenGL Runtime Error

    You need to add a property to your class representing the robot that keeps track of the current angle of the arm. Then you can animate this property having a start and end value for the angle. Qt will then interpolate the movement properly. I have no idea what your mouse move event is supposed to do so I can't give you a precise example for your usecase.
    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.


  10. The following user says thank you to wysota for this useful post:

    kango (18th February 2013)

  11. #8
    Join Date
    Jan 2013
    Posts
    17
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: OpenGL Runtime Error

    unlike my previous program in another thread, my current program is using the coordinates of the elbow to plot out arm position instead of using angles. so my start value and end value will be the start and end value of my counter?

    do you know of any good tutorials or examples on using qproperty from scratch?

    the mouseevent is just to allow rotation of the view in the y-direction.

  12. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: OpenGL Runtime Error

    Quote Originally Posted by kango View Post
    my current program is using the coordinates of the elbow to plot out arm position instead of using angles.
    It doesn't change anything. A value is a value.

    do you know of any good tutorials or examples on using qproperty from scratch?
    Qt Reference Manual.
    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. Replies: 1
    Last Post: 25th September 2010, 08:20
  2. Runtime Error
    By sophister in forum Qt Programming
    Replies: 9
    Last Post: 15th June 2009, 06:28
  3. Runtime error
    By nina1983 in forum Qt Programming
    Replies: 2
    Last Post: 11th September 2008, 15:37
  4. QT 4.4 runtime error
    By Ricochet in forum Newbie
    Replies: 1
    Last Post: 5th May 2008, 18:50
  5. runtime error
    By mickey in forum General Programming
    Replies: 10
    Last Post: 12th December 2006, 20:10

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
  •  
Qt is a trademark of The Qt Company.