Results 1 to 4 of 4

Thread: OpenGL update() function updating too fast

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

    Default OpenGL update() function updating too fast

    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 

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

    Default Re: OpenGL update() function updating too fast

    Quote Originally Posted by kango View Post
    i'm using update() at the end of my paintGL() function
    That doesn't make sense.

    is there anyway i can control the speed of the update() function?
    Use a QTimer, QTimeLine or a QPropertyAnimation.

    in addition, can i know what is the difference between update() and updateGL() as my program crashes when i used updateGL() instead of update()?
    You can look it up in Qt source code: http://qt.gitorious.org/qt/qt/blobs/...opengl/qgl.cpp

    updateGL() calls glDraw(), update() schedules a paint event which ultimately calls both glDraw() and updateOverlayGL().

    Your crash is caused by an infinite loop that happens because from within code called as a result of updateGL() you synchronously call updateGL() again.
    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. The following user says thank you to wysota for this useful post:

    kango (13th February 2013)

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

    Default Re: OpenGL update() function updating too fast

    QTimer solved my problem. Thanks so much.

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

    Default Re: OpenGL update() function updating too fast

    I had a quick look at your code. I really suggest you make "angle" a property and use QPropertyAnimation instead of that timer. This will give you much better control over what is going on in your program assuming you want it to do some more things than to just animate one single arm movement or if you want to make it look more realistic (e.g. by changing linear movement to non-linear one that contains some ease-in/ease-out behaviour).
    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. The following user says thank you to wysota for this useful post:

    kango (14th February 2013)

Similar Threads

  1. repaint/update stops updating GUI
    By gokceng in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 28th November 2011, 14:35
  2. Replies: 6
    Last Post: 6th July 2011, 19:07
  3. QProgreesDialog updating from within threadable function
    By Astrologer in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2010, 15:07
  4. QGraphicsView to fast update images
    By ^NyAw^ in forum Qt Programming
    Replies: 12
    Last Post: 8th May 2009, 22:17
  5. QGraphicsView, OpenGL and repainting/updating
    By Amargedon in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2009, 12:03

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.