Results 1 to 15 of 15

Thread: OpenGL Rotation with only left button

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2008
    Location
    Venezuela
    Posts
    34
    Platforms
    MacOS X Unix/X11
    Thanks
    4

    Default Re: OpenGL Rotation with only left button

    Thanks a LOT! I was asking on GLUT because I wanted to try the glut's drawing primitives like solidTeapot and so on... I found that I must supply the LIBS directive to qmake this way:

    Qt Code:
    1. LIBS += -framework GLUT
    To copy to clipboard, switch view to plain text mode 

    Besides that, why does this code need two trackballs instead of one? Actually the Boxes example, uses three, and I'm still wondering why. I have the same question on the conjugate() call on the rotation's quaternions, I'd like to know what sense makes that call here.

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: OpenGL Rotation with only left button

    It uses two trackballs, because with the right mouse you can change the rotation axis for the first. We could setup a fixed Quaternion instead, but maybe you will want to have this flexibility at some point.

    Joh

  3. #3
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    17
    Thanked 90 Times in 88 Posts

    Default Re: OpenGL Rotation with only left button

    Ok! It was an axis problem after all. I got the trackball working by looking at the thing from another position. I also removed the 2nd trackball and replaced it by an identity Quaternion.

    Qt Code:
    1. #include "visualizer.h"
    2.  
    3. //#include <GLUT/glut.h>
    4. #include <QtGlobal>
    5. #include <QtDebug>
    6. #include "constants.h"
    7.  
    8. Visualizer::Visualizer(QWidget *parent) :
    9. QGLWidget(parent)
    10. {
    11. setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer));
    12. this->_trackball = TrackBall(0.05f, QVector3D(0, 1, 0), TrackBall::Sphere);
    13. //this->_trackball2 = TrackBall(0.0f, QVector3D(0, 1, 0), TrackBall::Plane);
    14. }
    15.  
    16.  
    17. void Visualizer::initializeGL()
    18. {
    19. qglColor(Qt::black);
    20. glEnable(GL_DEPTH_TEST);
    21. glEnable(GL_CULL_FACE);
    22. glShadeModel(GL_SMOOTH);
    23.  
    24. GLfloat lightPos[] = { 0.0, 0.0, -10.0, 0.0};
    25. }
    26.  
    27. void Visualizer::paintGL()
    28. {
    29. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    30. glLoadIdentity();
    31. this->draw();
    32. }
    33.  
    34. void Visualizer::resizeGL(int w, int h)
    35. {
    36. glViewport(0, 0, (GLint)w, (GLint)h);
    37. }
    38.  
    39. static void multMatrix(const QMatrix4x4& m)
    40. {
    41. // static to prevent glMultMatrixf to fail on certain drivers
    42. static GLfloat mat[16];
    43. const qreal *data = m.constData();
    44. for (int index = 0; index < 16; ++index)
    45. mat[index] = data[index];
    46. glMultMatrixf(mat);
    47. }
    48.  
    49. static void loadMatrix(const QMatrix4x4& m)
    50. {
    51. // static to prevent glLoadMatrixf to fail on certain drivers
    52. static GLfloat mat[16];
    53. const qreal *data = m.constData();
    54. for (int index = 0; index < 16; ++index)
    55. mat[index] = data[index];
    56. glLoadMatrixf(mat);
    57. }
    58.  
    59. void getProjectionMatrix(QMatrix4x4& mat, float nearZ, float farZ)
    60. {
    61. static const QMatrix4x4 reference(
    62. 1.0f, 0.0f, 0.0f, 0.0f,
    63. 0.0f, 1.0f, 0.0f, 0.0f,
    64. 0.0f, 0.0f, 0.0f, 0.0f,
    65. 0.0f, 0.0f, -1.0f, 0.0f);
    66.  
    67. mat = reference;
    68. mat(2, 2) = (nearZ+farZ)/(nearZ-farZ);
    69. mat(2, 3) = 2.0f*nearZ*farZ/(nearZ-farZ);
    70. }
    71.  
    72. void Visualizer::draw()
    73. {
    74. int i;
    75. GLint v[24][3]= {
    76. { 1, 1, 1},
    77. {-1, 1, 1},
    78. {-1, -1, 1}, // Front
    79. { 1, -1, 1},
    80.  
    81. { 1, 1, 1},
    82. { 1, 1, -1},
    83. {-1, 1, -1}, // Top
    84. {-1, 1, 1},
    85.  
    86. { 1, 1, 1},
    87. { 1, -1, 1},
    88. { 1, -1, -1}, // Right
    89. { 1, 1, -1},
    90.  
    91. {-1, 1, 1},
    92. {-1, 1, -1},
    93. {-1, -1, -1}, // Left
    94. {-1, -1, 1},
    95.  
    96. {-1, -1, 1},
    97. {-1, -1, -1}, // Bottom
    98. { 1, -1, -1},
    99. { 1, -1, 1},
    100.  
    101. { 1, 1, -1},
    102. { 1, -1, -1},
    103. {-1, -1, -1}, // Back
    104. {-1, 1, -1}
    105. };
    106.  
    107.  
    108. glMatrixMode(GL_PROJECTION);
    109.  
    110. glLoadIdentity();
    111. gluPerspective(60.0, width() / height(), 0.01, 15.0);
    112. gluLookAt(0.0,0.0,5.0,0,0,0,0,1,0);
    113.  
    114. glMatrixMode(GL_MODELVIEW);
    115. glLoadIdentity();
    116.  
    117. {
    118. QMatrix4x4 m;
    119. m.rotate(this->_trackball.rotation());
    120. multMatrix(m);
    121. }
    122.  
    123. /*QQuaternion q = this->_trackball.rotation();
    124.   qDebug() << q;
    125.   glRotatef(180.0*q.scalar() / PI, q.x(), q.y(), q.z());*/
    126.  
    127. glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
    128.  
    129. for(i=0; i<24; i+=4) {
    130. glBegin(GL_LINE_LOOP);
    131. glLineWidth(2.0);
    132. qglColor(Qt::white);
    133. glVertex3iv(v[i]);
    134. glEnd();
    135. }
    136.  
    137. glBegin(GL_QUADS);
    138. qglColor(Qt::green);
    139. for(i=0; i<24; i++)
    140. {
    141. glVertex3iv(v[i]);
    142. }
    143. glEnd();
    144. }
    145.  
    146. QPointF Visualizer::pixelPosToViewPos(const QPointF& p)
    147. {
    148. return QPointF(2.0 * float(p.x()) / width() - 1.0,
    149. 1.0 - 2.0 * float(p.y()) / height());
    150. }
    151.  
    152. void Visualizer::mouseMoveEvent(QMouseEvent *e)
    153. {
    154. if(e->buttons() & Qt::LeftButton)
    155. {
    156. this->_trackball.move(pixelPosToViewPos(e->posF()), QQuaternion());
    157. updateGL();
    158. e->accept();
    159. } else {
    160. this->_trackball.release(pixelPosToViewPos(e->posF()), QQuaternion());
    161. }
    162. }
    163.  
    164. void Visualizer::mousePressEvent(QMouseEvent *e)
    165. {
    166. if(e->buttons() & Qt::LeftButton)
    167. {
    168. this->_trackball.push(pixelPosToViewPos(e->posF()), QQuaternion());
    169. //this->_trackball.push(pixelPosToViewPos(e->posF()), this->_trackball2.rotation().conjugate());
    170. e->accept();
    171. }
    172. }
    173.  
    174. void Visualizer::mouseReleaseEvent(QMouseEvent *e)
    175. {
    176. if(e->buttons() & Qt::LeftButton)
    177. {
    178. this->_trackball.release(pixelPosToViewPos(e->posF()),QQuaternion());
    179. e->accept();
    180. }
    181. }
    To copy to clipboard, switch view to plain text mode 
    Thought it would be easier to get those Trackballs working. I only cannibalized some of their functionality some time ago.

    Is your problem solved so far?

    Johannes

  4. The following user says thank you to JohannesMunk for this useful post:

    danielperaza (12th April 2011)

  5. #4
    Join Date
    Mar 2008
    Location
    Venezuela
    Posts
    34
    Platforms
    MacOS X Unix/X11
    Thanks
    4

    Default Re: OpenGL Rotation with only left button

    Man, I really owe you a beer! Thanks a lot!

  6. #5
    Join Date
    Aug 2014
    Posts
    1

    Default Re: OpenGL Rotation with only left button

    I know this is an old thread, but maybe someone can update it with a working example for Qt5? The zip file from danielpereza compiles, but has the arcball problem. Replacing the visualizer.cpp file with the one Johannes made creates a ton of compiler errors, including "cannot convert 'const float*' to 'const qreal*' on line 42, and other gluPerspective not declared in scope, and pixelPosToViewPos not declared in scope, etc.

    This would be great resource as it is a basic implementation of a difficult subject.

Similar Threads

  1. Replies: 4
    Last Post: 29th August 2010, 19:16
  2. Graph rotation
    By jomarin in forum Qwt
    Replies: 2
    Last Post: 16th August 2010, 10:21
  3. combining Alt + Left Mouse Button
    By speedracer in forum Qt Programming
    Replies: 1
    Last Post: 3rd June 2009, 14:29
  4. Rotation on Qframe
    By Pharell in forum Qt Programming
    Replies: 11
    Last Post: 2nd April 2008, 17:31
  5. Rotation problem
    By boss_bhat in forum Qt Programming
    Replies: 5
    Last Post: 17th January 2007, 17:46

Tags for this Thread

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.