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