05 void Tetrahedron::mouseMoveEvent(QMouseEvent *event)
06 {
07 GLfloat dx = GLfloat(event->x - lastPos.x) / width;
08 GLfloat dy = GLfloat(event->y - lastPos.y) / height;
09 if (event->buttons & Qt::LeftButton) {
10 rotationX += 180 * dy;
11 rotationY += 180 * dx;
12 updateGL;
13 } else if (event->buttons & Qt::RightButton) {
14 rotationX += 180 * dy;
15 rotationZ += 180 * dx;
16 updateGL;
17 }
18 lastPos = event->pos;
19 }
Функции класса
После модификации переменных
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) {