Results 1 to 4 of 4

Thread: Putting an opengl widget in to a mainwindow

  1. #1
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Putting an opengl widget in to a mainwindow

    Hello,
    I'm trying make a connection between an opengl window and a tree object.
    For example there will be a cube, a circle, and a piramid in the openglwidget on the left.
    And in the tree there will be nodes: cube, circle and piramid.
    When user selects the cube, cube will be higlighted in the graphic.
    But I can not even succeed to place the required items on mainwindow.
    Could you help me please.
    Here is my Header:
    Qt Code:
    1. #ifndef QTGL_H
    2. #define QTGL_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QtOpenGL>
    6. #include "ui_qtgl.h"
    7.  
    8. class QtGL : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. QtGL(QWidget *parent = 0, Qt::WFlags flags = 0);
    14. ~QtGL();
    15. QGLWidget *glwidget;
    16.  
    17.  
    18. protected:
    19. void initializeGL();
    20. void resizeGL(int width, int height);
    21. void paintGL();
    22. void mousePressEvent(QMouseEvent *event);
    23. void mouseMoveEvent(QMouseEvent *event);
    24. void mouseDoubleClickEvent(QMouseEvent *event);
    25.  
    26. private:
    27. void draw();
    28. int faceAtPosition(const QPoint &pos);
    29.  
    30. GLfloat rotationX;
    31. GLfloat rotationY;
    32. GLfloat rotationZ;
    33. QColor faceColors[4];
    34. QPoint lastPos;
    35.  
    36. Ui::QtGLClass ui;
    37. };
    38.  
    39. #endif // QTGL_H
    To copy to clipboard, switch view to plain text mode 

    and here is my cpp:

    Qt Code:
    1. #include "qtgl.h"
    2. #include <QtOpenGL>
    3.  
    4. QtGL::QtGL(QWidget *parent, Qt::WFlags flags)
    5. : QMainWindow(parent, flags)
    6. {
    7. ui.setupUi(this);
    8. glwidget = new QGLWidget(parent);
    9.  
    10. }
    11.  
    12. QtGL::~QtGL()
    13. {
    14.  
    15. }
    16.  
    17. void QtGL::initializeGL()
    18. {
    19. glwidget->qglClearColor(Qt::black);
    20. glShadeModel(GL_FLAT);
    21. glEnable(GL_DEPTH_TEST);
    22. glEnable(GL_CULL_FACE);
    23. }
    24.  
    25. void QtGL::resizeGL(int width, int height)
    26. {
    27. glViewport(0, 0, width, height);
    28. glMatrixMode(GL_PROJECTION);
    29. glLoadIdentity();
    30. GLfloat x = GLfloat(width) / height;
    31. glFrustum(-x, +x, -1.0, +1.0, 4.0, 15.0);
    32. glMatrixMode(GL_MODELVIEW);
    33. }
    34.  
    35. void QtGL::paintGL()
    36. {
    37. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    38. draw();
    39. }
    40.  
    41. void QtGL::draw()
    42. {
    43. static const GLfloat P1[3] = { 0.0, -1.0, +2.0 };
    44. static const GLfloat P2[3] = { +1.73205081, -1.0, -1.0 };
    45. static const GLfloat P3[3] = { -1.73205081, -1.0, -1.0 };
    46. static const GLfloat P4[3] = { 0.0, +2.0, 0.0 };
    47.  
    48. static const GLfloat * const coords[4][3] = {
    49. { P1, P2, P3 }, { P1, P3, P4 }, { P1, P4, P2 }, { P2, P4, P3 }
    50. };
    51.  
    52. glMatrixMode(GL_MODELVIEW);
    53. glLoadIdentity();
    54. glTranslatef(0.0, 0.0, -10.0);
    55. glRotatef(rotationX, 1.0, 0.0, 0.0);
    56. glRotatef(rotationY, 0.0, 1.0, 0.0);
    57. glRotatef(rotationZ, 0.0, 0.0, 1.0);
    58.  
    59. for (int i = 0; i < 4; ++i) {
    60. glLoadName(i);
    61. glBegin(GL_TRIANGLES);
    62. glwidget->qglColor(faceColors[i]);
    63. for (int j = 0; j < 3; ++j) {
    64. glVertex3f(coords[i][j][0], coords[i][j][1],
    65. coords[i][j][2]);
    66. }
    67. glEnd();
    68. }
    69. }
    70.  
    71. void QtGL::mousePressEvent(QMouseEvent *event)
    72. {
    73. lastPos = event->pos();
    74. }
    75.  
    76. void QtGL::mouseMoveEvent(QMouseEvent *event)
    77. {
    78. GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
    79. GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();
    80. if (event->buttons() & Qt::LeftButton) {
    81. rotationX += 180 * dy;
    82. rotationY += 180 * dx;
    83. glwidget->updateGL();
    84. } else if (event->buttons() & Qt::RightButton) {
    85. rotationX += 180 * dy;
    86. rotationZ += 180 * dx;
    87. glwidget->updateGL();
    88. }
    89. lastPos = event->pos();
    90. }
    91.  
    92. void QtGL::mouseDoubleClickEvent(QMouseEvent *event)
    93. {
    94. int face = faceAtPosition(event->pos());
    95. if (face != -1) {
    96. QColor color = QColorDialog::getColor(faceColors[face], this);
    97. if (color.isValid()) {
    98. faceColors[face] = color;
    99. glwidget->updateGL();
    100. }
    101. }
    102. }
    103.  
    104. int QtGL::faceAtPosition(const QPoint &pos)
    105. {
    106. const int MaxSize = 512;
    107. GLuint buffer[MaxSize];
    108. GLint viewport[4];
    109.  
    110. glwidget->makeCurrent();
    111.  
    112. glGetIntegerv(GL_VIEWPORT, viewport);
    113. glSelectBuffer(MaxSize, buffer);
    114. glRenderMode(GL_SELECT);
    115.  
    116. glInitNames();
    117. glPushName(0);
    118.  
    119. glMatrixMode(GL_PROJECTION);
    120. glPushMatrix();
    121. glLoadIdentity();
    122. gluPickMatrix(GLdouble(pos.x()), GLdouble(viewport[3] - pos.y()),
    123. 5.0, 5.0, viewport);
    124. GLfloat x = GLfloat(width()) / height();
    125. glFrustum(-x, x, -1.0, 1.0, 4.0, 15.0);
    126. draw();
    127. glMatrixMode(GL_PROJECTION);
    128. glPopMatrix();
    129.  
    130. if (!glRenderMode(GL_RENDER))
    131. return -1;
    132. return buffer[3];
    133. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Putting an opengl widget in to a mainwindow

    You are supposed to subclass QGLWidget, and reimplement all those xxxGL() methods in that class, not in a QMainWindow subclass.
    J-P Nurmi

  3. #3
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Re: Putting an opengl widget in to a mainwindow

    So, u mean i need to write 2 ".h", and 2 ".cpp" file??

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Putting an opengl widget in to a mainwindow

    Quote Originally Posted by bod View Post
    So, u mean i need to write 2 ".h", and 2 ".cpp" file??
    If you have two classes in the end, that would be a good practice.
    J-P Nurmi

Similar Threads

  1. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  2. Replies: 1
    Last Post: 20th November 2007, 18:42
  3. Replies: 1
    Last Post: 11th September 2007, 13:34
  4. QwtPlot widget in MainWindow
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2007, 11:48
  5. Crash when minimizing OpenGL widget
    By MistaPain in forum Qt Programming
    Replies: 5
    Last Post: 7th October 2006, 16:58

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.