После модификации переменных
01 void Tetrahedron::mouseDoubleClickEvent(QMouseEvent *event)
02 {
03 int face = faceAtPosition(event->pos());
04 if (face != -1) {
05 QColor color = QColorDialog::getColor(faceColors[face], this);
06 if (color.isValid()) {
07 faceColors[face] = color;
08 updateGL();
09 }
10 }
11 }
Функция
01 int Tetrahedron::faceAtPosition(const QPoint &pos)
02 {
03 const int MaxSize = 512;
04 GLuint buffer[MaxSize];
05 GLint viewport[4];
06 glGetIntegerv(GL_VIEWPORT, viewport);
07 glSelectBuffer(MaxSize, buffer);
08 glRenderMode(GL_SELECT);
09 glInitNames();
10 glPushName(0);
11 glMatrixMode(GL_PROJECTION);
12 glPushMatrix();
13 glLoadIdentity();
14 gluPickMatrix(GLdouble(pos.x()),
15 GLdouble(viewport[3] - pos.y()),
16 5.0, 5.0, viewport);
17 GLfloat x = GLfloat(width()) / height();
18 glFrustum(-x, x, -1.0, 1.0, 4.0, 15.0);
19 draw();
20 glMatrixMode(GL_PROJECTION);
21 glPopMatrix();
22 if (!glRenderMode(GL_RENDER))
23 return -1;
24 return buffer[3];
25 }
Функция
Ниже приводится файл
01 #include
02 #include
03 #include "tetrahedron.h"
04 using namespace std;
05 int main(int argc, char *argv[])
06 {
07 QApplication app(argc, argv);
08 if (!QGLFormat::hasOpenGL()) {
09 cerr << "This system has no OpenGL support" << endl;
10 return 1;
11 }
12 Tetrahedron tetrahedron;
13 tetrahedron.setWindowTitle(QObject::tr("Tetrahedron"));
14 tetrahedron.resize(300, 300);
15 tetrahedron.show();
16 return app.exec();
17 }
Если система пользователя не поддерживает OpenGL, мы выдаем на консоль сообщение об ошибке и сразу же возвращаем управление.
Для сборки приложения совместно с модулем
QT += opengl
Этим заканчивается разработка приложения Тетраэдр. Более подробную информацию о модуле
Глава 9. Технология «drag-and-drop»