root / controller / GLWidget.cpp @ e49e1de39e949c564e39598f12b6953cc6df71ba
History | View | Annotate | Download (4.2 kB)
| 1 | /****************************************************************************
|
|---|---|
| 2 | ** |
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
| 4 | ** All rights reserved. |
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) |
| 6 | ** |
| 7 | ** This file is part of the examples of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 10 | ** Commercial Usage |
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in |
| 12 | ** accordance with the Qt Commercial License Agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and Nokia. |
| 15 | ** |
| 16 | ** GNU Lesser General Public License Usage |
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 18 | ** General Public License version 2.1 as published by the Free Software |
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
| 20 | ** packaging of this file. Please review the following information to |
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 23 | ** |
| 24 | ** In addition, as a special exception, Nokia gives you certain additional |
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception |
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 27 | ** |
| 28 | ** GNU General Public License Usage |
| 29 | ** Alternatively, this file may be used under the terms of the GNU |
| 30 | ** General Public License version 3.0 as published by the Free Software |
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the |
| 32 | ** packaging of this file. Please review the following information to |
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be |
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. |
| 35 | ** |
| 36 | ** If you have questions regarding the use of this file, please contact |
| 37 | ** Nokia at qt-info@nokia.com. |
| 38 | ** $QT_END_LICENSE$ |
| 39 | ** |
| 40 | ****************************************************************************/ |
| 41 | |
| 42 | #include <QtGui> |
| 43 | #include <QtOpenGL> |
| 44 | |
| 45 | #include <math.h> |
| 46 | |
| 47 | #include "GLWidget.h" |
| 48 | #include "qtlogo.h" |
| 49 | |
| 50 | |
| 51 | #ifndef GL_MULTISAMPLE
|
| 52 | #define GL_MULTISAMPLE 0x809D |
| 53 | #endif
|
| 54 | |
| 55 | GLWidget::GLWidget(QWidget *parent) |
| 56 | : QGLWidget(QGLFormat(QGL::SampleBuffers), parent) |
| 57 | {
|
| 58 | logo = 0;
|
| 59 | yawRot = 0;
|
| 60 | pitchRot = 0;
|
| 61 | rollRot = 0;
|
| 62 | |
| 63 | qtGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0); |
| 64 | qtPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0); |
| 65 | } |
| 66 | |
| 67 | GLWidget::~GLWidget() |
| 68 | {
|
| 69 | } |
| 70 | |
| 71 | QSize GLWidget::minimumSizeHint() const
|
| 72 | {
|
| 73 | return QSize(50, 50); |
| 74 | } |
| 75 | |
| 76 | QSize GLWidget::sizeHint() const
|
| 77 | {
|
| 78 | return QSize(400, 400); |
| 79 | } |
| 80 | |
| 81 | static void qNormalizeAngle(int &angle) |
| 82 | {
|
| 83 | while (angle < -180) |
| 84 | angle = 180;
|
| 85 | while (angle > 180) |
| 86 | angle = 180;
|
| 87 | } |
| 88 | |
| 89 | void GLWidget::setYawRotation(int angle) |
| 90 | {
|
| 91 | qNormalizeAngle(angle); |
| 92 | if (angle != yawRot) {
|
| 93 | yawRot = angle; |
| 94 | emit yawRotationChanged(angle); |
| 95 | updateGL(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void GLWidget::setPitchRotation(int angle) |
| 100 | {
|
| 101 | qNormalizeAngle(angle); |
| 102 | if (angle != pitchRot) {
|
| 103 | pitchRot = angle; |
| 104 | emit pitchRotationChanged(angle); |
| 105 | updateGL(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void GLWidget::setRollRotation(int angle) |
| 110 | {
|
| 111 | qNormalizeAngle(angle); |
| 112 | if (angle != rollRot) {
|
| 113 | rollRot = angle; |
| 114 | emit rollRotationChanged(angle); |
| 115 | updateGL(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void GLWidget::initializeGL()
|
| 120 | {
|
| 121 | qglClearColor(qtPurple.dark()); |
| 122 | |
| 123 | logo = new QtLogo(this, 64); |
| 124 | logo->setColor(qtGreen.dark()); |
| 125 | |
| 126 | glEnable(GL_DEPTH_TEST); |
| 127 | glEnable(GL_CULL_FACE); |
| 128 | glShadeModel(GL_SMOOTH); |
| 129 | glEnable(GL_LIGHTING); |
| 130 | glEnable(GL_LIGHT0); |
| 131 | glEnable(GL_MULTISAMPLE); |
| 132 | static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 }; |
| 133 | glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); |
| 134 | } |
| 135 | |
| 136 | void GLWidget::paintGL()
|
| 137 | {
|
| 138 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 139 | glLoadIdentity(); |
| 140 | |
| 141 | glTranslatef(0.0, 0.0, -10.0); |
| 142 | |
| 143 | qDebug() << "FOO:" << yawRot << pitchRot << rollRot;
|
| 144 | |
| 145 | glRotatef(-rollRot , 0.0, 0.0, 1.0); |
| 146 | glRotatef( pitchRot, 1.0, 0.0, 0.0); |
| 147 | glRotatef(-yawRot, 0.0, 1.0, 0.0); |
| 148 | |
| 149 | logo->draw(); |
| 150 | } |
| 151 | |
| 152 | void GLWidget::resizeGL(int width, int height) |
| 153 | {
|
| 154 | int side = qMin(width, height);
|
| 155 | glViewport((width - side) / 2, (height - side) / 2, side, side); |
| 156 | |
| 157 | glMatrixMode(GL_PROJECTION); |
| 158 | glLoadIdentity(); |
| 159 | #ifdef QT_OPENGL_ES_1
|
| 160 | glOrthof(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0); |
| 161 | #else
|
| 162 | glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0); |
| 163 | #endif
|
| 164 | glMatrixMode(GL_MODELVIEW); |
| 165 | } |