Results 1 to 15 of 15

Thread: OpenGL Rotation with only left button

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

    Question OpenGL Rotation with only left button

    Hi!, I'm new to OpenGL programming, I'm working in my thesis right now, and I followed the Tetrahedron example. I was able to do some stuff from that point on.

    The problem is that the Tetrahedron source code handles rotation by two different ways: it rotates around the XY axis if the left mouse button is pressed, or the XZ axis otherwise. What I'd like to do is to handle rotation only with the left mouse button, and leave the right mouse button to trigger translations.

    Also, I'd like to perform zooming when an user makes double click with the left button.

    Isn't any OpenGL-based widget library that I might use to implement these controls? I'd like to do these operations the way GoogleEarth does.

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

    Default Re: OpenGL Rotation with only left button

    GoogleEarth features a trackball mechanism. Have a look at Qt's boxes demo.

    The spherical trackball should be what you are looking for.

    http://doc.trolltech.com/latest/demo...ackball-h.html
    http://doc.trolltech.com/latest/demo...kball-cpp.html

    For the rest (zooming, translating) you will have to get your hands dirty and implement your wishes into the appropriate mouseEvents.

    Let us know if you need help with a more specific problem.

    HIH

    Johannes

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

    danielperaza (4th April 2011)

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

    Default Re: OpenGL Rotation with only left button

    Ok, I have reviewed the Boxes example. However, since it has a lot of stuff that I don't even need to know at this time, I tried to keep focused at the TrackBall class. I took a look to http://www.opengl.org/wiki/Trackball too, and I have a basic understanding of how the theory works. But I'm still needing a hand on implementation, becasue I'm very confused about it.

    Based on the Scene.cpp file, I tried to find out how to use the TrackBall in my code, so I tried to build a test project, handling the rotation by these functions:

    Qt Code:
    1. void Visualizer::mouseMoveEvent(QMouseEvent *e)
    2. {
    3. if(e->buttons() & Qt::LeftButton)
    4. {
    5. this->_trackball.move(e->posF(), this->_trackball.rotation().conjugate());
    6. updateGL();
    7. }
    8. }
    9.  
    10. void Visualizer::mousePressEvent(QMouseEvent *e)
    11. {
    12. if(e->buttons() & Qt::LeftButton)
    13. {
    14. this->_trackball.push(e->posF(), this->_trackball.rotation().conjugate());
    15. }
    16. }
    17.  
    18. void Visualizer::mouseReleaseEvent(QMouseEvent *e)
    19. {
    20. if(e->buttons() & Qt::LeftButton)
    21. {
    22. this->_trackball.release(e->posF(), this->_trackball.rotation().conjugate());
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    and in my draw() method (which is called from paintGL()) I tried this:

    Qt Code:
    1. glMatrixMode(GL_MODELVIEW);
    2. glLoadIdentity();
    3. QQuaternion q = this->_trackball.rotation();
    4. glRotatef(180.0*q.scalar() / PI, q.x(), q.y(), q.z());
    To copy to clipboard, switch view to plain text mode 

    And it didn't work. Sorry if this question seems to be too stupid to ask :S, but remember that I'm new to Computer Graphics in general.

    Any suggestions?

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

    Default Re: OpenGL Rotation with only left button

    Could you post your complete test code?

    Joh

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

    Default Re: OpenGL Rotation with only left button

    Here is my test project.

    tracktest.zip

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

    Default Re: OpenGL Rotation with only left button

    Thx. Saved me to recreate all that!

    I didn't solve the problem yet, but found some things:

    1) Try to set the axis of the trackballs

    2) The trackball methods get passed anothers trackball quaternion!
    this->_trackball.move(e->posF(), this->_trackball2.rotation().conjugate());

    3) Ortho works. But try to get a perspective projection running.

    My changes so far - still not working though:

    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. void Visualizer::initializeGL()
    17. {
    18. qglColor(Qt::black);
    19. glEnable(GL_DEPTH_TEST);
    20. glEnable(GL_CULL_FACE);
    21. glShadeModel(GL_SMOOTH);
    22.  
    23. GLfloat lightPos[] = { 0.0, 0.0, -10.0, 0.0};
    24. }
    25.  
    26. void Visualizer::paintGL()
    27. {
    28. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    29. glLoadIdentity();
    30. this->draw();
    31. }
    32.  
    33. void Visualizer::resizeGL(int w, int h)
    34. {
    35. //int ratio = w/h;
    36. int side = qMin(w, h);
    37. glViewport((w - side) / 2, (h - side) / 2, side, side);
    38. //glViewport(0, 0, w, h);
    39. }
    40.  
    41. static void multMatrix(const QMatrix4x4& m)
    42. {
    43. // static to prevent glMultMatrixf to fail on certain drivers
    44. static GLfloat mat[16];
    45. const qreal *data = m.constData();
    46. for (int index = 0; index < 16; ++index)
    47. mat[index] = data[index];
    48. glMultMatrixf(mat);
    49. }
    50.  
    51. static void loadMatrix(const QMatrix4x4& m)
    52. {
    53. // static to prevent glLoadMatrixf to fail on certain drivers
    54. static GLfloat mat[16];
    55. const qreal *data = m.constData();
    56. for (int index = 0; index < 16; ++index)
    57. mat[index] = data[index];
    58. glLoadMatrixf(mat);
    59. }
    60.  
    61. void Visualizer::draw()
    62. {
    63. int i;
    64. GLint v[24][3]= {
    65. { 1, 1, 1},
    66. {-1, 1, 1},
    67. {-1, -1, 1}, // Front
    68. { 1, -1, 1},
    69.  
    70. { 1, 1, 1},
    71. { 1, 1, -1},
    72. {-1, 1, -1}, // Top
    73. {-1, 1, 1},
    74.  
    75. { 1, 1, 1},
    76. { 1, -1, 1},
    77. { 1, -1, -1}, // Right
    78. { 1, 1, -1},
    79.  
    80. {-1, 1, 1},
    81. {-1, 1, -1},
    82. {-1, -1, -1}, // Left
    83. {-1, -1, 1},
    84.  
    85. {-1, -1, 1},
    86. {-1, -1, -1}, // Bottom
    87. { 1, -1, -1},
    88. { 1, -1, 1},
    89.  
    90. { 1, 1, -1},
    91. { 1, -1, -1},
    92. {-1, -1, -1}, // Back
    93. {-1, 1, -1}
    94. };
    95.  
    96.  
    97. glMatrixMode(GL_PROJECTION);
    98. glLoadIdentity();
    99. gluPerspective(60.0, width() / height(), 0.01, 15.0);
    100. //glOrtho(-2, 2, -2, 2, -2, 2);
    101.  
    102. glMatrixMode(GL_MODELVIEW);
    103. glLoadIdentity();
    104.  
    105. //glTranslatef(0.0,-50.0,0.0);
    106.  
    107. {
    108. QMatrix4x4 m;
    109. m.rotate(this->_trackball2.rotation());
    110. multMatrix(m);
    111. }
    112.  
    113. {
    114. QMatrix4x4 m;
    115. m.rotate(this->_trackball.rotation());
    116. multMatrix(m);
    117. }
    118.  
    119. /*QQuaternion q = this->_trackball.rotation();
    120.   qDebug() << q;
    121.   glRotatef(180.0*q.scalar() / PI, q.x(), q.y(), q.z());*/
    122.  
    123. glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
    124.  
    125. for(i=0; i<24; i+=4) {
    126. glBegin(GL_LINE_LOOP);
    127. glLineWidth(2.0);
    128. qglColor(Qt::white);
    129. glVertex3iv(v[i]);
    130. glEnd();
    131. }
    132.  
    133. glBegin(GL_QUADS);
    134. qglColor(Qt::green);
    135. for(i=0; i<24; i++)
    136. {
    137. glVertex3iv(v[i]);
    138. }
    139. glEnd();
    140. }
    141.  
    142. void Visualizer::mouseMoveEvent(QMouseEvent *e)
    143. {
    144. if(e->buttons() & Qt::LeftButton)
    145. {
    146. this->_trackball.move(e->posF(), this->_trackball2.rotation().conjugate());
    147. updateGL();
    148. }
    149. }
    150.  
    151. void Visualizer::mousePressEvent(QMouseEvent *e)
    152. {
    153. if(e->buttons() & Qt::LeftButton)
    154. {
    155. this->_trackball.push(e->posF(), this->_trackball2.rotation().conjugate());
    156. }
    157. }
    158.  
    159. void Visualizer::mouseReleaseEvent(QMouseEvent *e)
    160. {
    161. if(e->buttons() & Qt::LeftButton)
    162. {
    163. this->_trackball.release(e->posF(), this->_trackball2.rotation().conjugate());
    164. }
    165. }
    To copy to clipboard, switch view to plain text mode 
    Can't look at this further until tomorrow afternoon!

    Good luck

    Johannes

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

    Default Re: OpenGL Rotation with only left button

    How do I link to GLUT in Mac OS X 10.6 using qmake's .pro file?
    Last edited by danielperaza; 10th April 2011 at 00:11. Reason: spelling corrections

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

    Default Re: OpenGL Rotation with only left button

    For this code you don't need to!

    But concerning this, I usually use GLee to get access to advanced OpenGL features:
    http://www.opengl.org/sdk/libs/GLee/

    I will have a look at your trackball later.

    HIH

    Johannes

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

    danielperaza (10th April 2011)

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

    Default Re: OpenGL Rotation with only left button

    Well I hope that if I use GLee my code doesn't start to sing as in the American TV Show, that would be horrible!

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

    Default Re: OpenGL Rotation with only left button

    I can only deliver another intermediate:

    1) accepted range of trackball parameters is [-1,1]!

    2) perspective projection works! Remembered how it works :->

    3) trackball works in x-direction. y still screwed up.

    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. //int ratio = w/h;
    37. glViewport(0, 0, (GLint)w, (GLint)h);
    38. /*int side = qMin(w, h);
    39.   glViewport((w - side) / 2, (h - side) / 2, side, side);*/
    40. //glViewport(0, 0, w, h);
    41. }
    42.  
    43. static void multMatrix(const QMatrix4x4& m)
    44. {
    45. // static to prevent glMultMatrixf to fail on certain drivers
    46. static GLfloat mat[16];
    47. const qreal *data = m.constData();
    48. for (int index = 0; index < 16; ++index)
    49. mat[index] = data[index];
    50. glMultMatrixf(mat);
    51. }
    52.  
    53. static void loadMatrix(const QMatrix4x4& m)
    54. {
    55. // static to prevent glLoadMatrixf to fail on certain drivers
    56. static GLfloat mat[16];
    57. const qreal *data = m.constData();
    58. for (int index = 0; index < 16; ++index)
    59. mat[index] = data[index];
    60. glLoadMatrixf(mat);
    61. }
    62.  
    63. void getProjectionMatrix(QMatrix4x4& mat, float nearZ, float farZ)
    64. {
    65. static const QMatrix4x4 reference(
    66. 1.0f, 0.0f, 0.0f, 0.0f,
    67. 0.0f, 1.0f, 0.0f, 0.0f,
    68. 0.0f, 0.0f, 0.0f, 0.0f,
    69. 0.0f, 0.0f, -1.0f, 0.0f);
    70.  
    71. mat = reference;
    72. mat(2, 2) = (nearZ+farZ)/(nearZ-farZ);
    73. mat(2, 3) = 2.0f*nearZ*farZ/(nearZ-farZ);
    74. }
    75.  
    76. void Visualizer::draw()
    77. {
    78. int i;
    79. GLint v[24][3]= {
    80. { 1, 1, 1},
    81. {-1, 1, 1},
    82. {-1, -1, 1}, // Front
    83. { 1, -1, 1},
    84.  
    85. { 1, 1, 1},
    86. { 1, 1, -1},
    87. {-1, 1, -1}, // Top
    88. {-1, 1, 1},
    89.  
    90. { 1, 1, 1},
    91. { 1, -1, 1},
    92. { 1, -1, -1}, // Right
    93. { 1, 1, -1},
    94.  
    95. {-1, 1, 1},
    96. {-1, 1, -1},
    97. {-1, -1, -1}, // Left
    98. {-1, -1, 1},
    99.  
    100. {-1, -1, 1},
    101. {-1, -1, -1}, // Bottom
    102. { 1, -1, -1},
    103. { 1, -1, 1},
    104.  
    105. { 1, 1, -1},
    106. { 1, -1, -1},
    107. {-1, -1, -1}, // Back
    108. {-1, 1, -1}
    109. };
    110.  
    111.  
    112. glMatrixMode(GL_PROJECTION);
    113.  
    114. /* QMatrix4x4 mat;
    115.   getProjectionMatrix(mat, 0.1f, 100.0f);
    116.  
    117.   glMatrixMode(GL_PROJECTION);
    118.   glPushMatrix();
    119.   loadMatrix(mat);*/
    120.  
    121. glLoadIdentity();
    122. gluPerspective(60.0, width() / height(), 0.01, 15.0);
    123. gluLookAt(5.0,0,0,0,0,0,0,1,0);
    124. //glOrtho(-2, 2, -2, 2, -2, 2);
    125.  
    126. glMatrixMode(GL_MODELVIEW);
    127. glLoadIdentity();
    128.  
    129. //glTranslatef(0.0,0.0,-5.0);
    130.  
    131. {
    132. QMatrix4x4 m;
    133. m.rotate(this->_trackball2.rotation());
    134. multMatrix(m);
    135. }
    136.  
    137. {
    138. QMatrix4x4 m;
    139. m.rotate(this->_trackball.rotation());
    140. multMatrix(m);
    141. }
    142.  
    143. /*QQuaternion q = this->_trackball.rotation();
    144.   qDebug() << q;
    145.   glRotatef(180.0*q.scalar() / PI, q.x(), q.y(), q.z());*/
    146.  
    147. glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
    148.  
    149. for(i=0; i<24; i+=4) {
    150. glBegin(GL_LINE_LOOP);
    151. glLineWidth(2.0);
    152. qglColor(Qt::white);
    153. glVertex3iv(v[i]);
    154. glEnd();
    155. }
    156.  
    157. glBegin(GL_QUADS);
    158. qglColor(Qt::green);
    159. for(i=0; i<24; i++)
    160. {
    161. glVertex3iv(v[i]);
    162. }
    163. glEnd();
    164. }
    165.  
    166. QPointF Visualizer::pixelPosToViewPos(const QPointF& p)
    167. {
    168. return QPointF(2.0 * float(p.x()) / width() - 1.0,
    169. 1.0 - 2.0 * float(p.y()) / height());
    170. }
    171.  
    172. void Visualizer::mouseMoveEvent(QMouseEvent *e)
    173. {
    174. if(e->buttons() & Qt::LeftButton)
    175. {
    176. this->_trackball.move(pixelPosToViewPos(e->posF()), this->_trackball2.rotation().conjugate());
    177. updateGL();
    178. e->accept();
    179. } else {
    180. this->_trackball.release(pixelPosToViewPos(e->posF()), this->_trackball2.rotation().conjugate());
    181. }
    182. }
    183.  
    184. void Visualizer::mousePressEvent(QMouseEvent *e)
    185. {
    186. if(e->buttons() & Qt::LeftButton)
    187. {
    188. this->_trackball.push(pixelPosToViewPos(e->posF()), this->_trackball2.rotation().conjugate());
    189. e->accept();
    190. }
    191. }
    192.  
    193. void Visualizer::mouseReleaseEvent(QMouseEvent *e)
    194. {
    195. if(e->buttons() & Qt::LeftButton)
    196. {
    197. this->_trackball.release(pixelPosToViewPos(e->posF()), this->_trackball2.rotation().conjugate());
    198. e->accept();
    199. }
    200. }
    To copy to clipboard, switch view to plain text mode 

    More tomorrow.

    Joh

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

    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.

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

    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

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

    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

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

    danielperaza (12th April 2011)

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

    Default Re: OpenGL Rotation with only left button

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

  18. #15
    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, 18:16
  2. Graph rotation
    By jomarin in forum Qwt
    Replies: 2
    Last Post: 16th August 2010, 09:21
  3. combining Alt + Left Mouse Button
    By speedracer in forum Qt Programming
    Replies: 1
    Last Post: 3rd June 2009, 13:29
  4. Rotation on Qframe
    By Pharell in forum Qt Programming
    Replies: 11
    Last Post: 2nd April 2008, 16:31
  5. Rotation problem
    By boss_bhat in forum Qt Programming
    Replies: 5
    Last Post: 17th January 2007, 16: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.