Use qDebug() plus some new debug messages
[agianapa.git] / qt / sphere-orbit-camera-gui / myglwidget.cpp
blobc390018a5ac6f86d45a1c6a57a737154978721fa
1 #include <iostream>
2 #include <QKeyEvent>
3 #include <QtOpenGL>
5 #include <cmath>
7 #include "axis.h"
8 #include "camera.h"
9 #include "myglwidget.h"
11 // Non-default arguments must come first
12 MyGLWidget::MyGLWidget(Axis *pAxisX, Axis *pAxisY, Axis *pAxisZ,
13 Camera *pCamera, Sphere * pSphere,
14 unsigned int timerInterval, QWidget *parent)
15 : QGLWidget(parent)
17 qDebug("MyGLWidget::MyGLWidget()");
19 m_pAxisX = pAxisX;
20 m_pAxisY = pAxisY;
21 m_pAxisZ = pAxisZ;
23 m_pCamera = pCamera;
24 m_pSphere = pSphere;
26 m_timerInterval = timerInterval;
28 // Worker thread populates the data pool with coordinations and angles.
29 // It automatically blocks once the pool has reached a maximum number
30 // of data and wakes up when data resource are low.
31 startWorkerThread();
33 // Setup timer
34 connect(&m_timer, SIGNAL(timeout()), this, SLOT(pullData()));
37 MyGLWidget::~MyGLWidget()
39 qDebug("MyGLWidget::~MyGLWidget()");
42 void MyGLWidget::initializeGL()
44 qDebug("MyGLWidget::initializeGL()");
46 // Enable backface culling
47 glEnable(GL_CULL_FACE);
49 // Clear color is set to black
50 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
51 glShadeModel(GL_SMOOTH);
53 // Reset modelview matrix
54 glMatrixMode(GL_MODELVIEW);
55 glLoadIdentity();
57 // We don't need to call drawSphere() here,
58 // since the resizeGL() will be called upon
59 // the initial creation of the window.
62 void MyGLWidget::paintGL()
64 qDebug("MyGLWidget::paintGL()");
66 // Clear scene
67 glClear(GL_COLOR_BUFFER_BIT);
69 glMatrixMode(GL_PROJECTION);
70 glLoadIdentity();
71 gluPerspective(90, 1, 0.01, 100);
73 // Reset modelview matrix
74 glMatrixMode(GL_MODELVIEW);
75 glLoadIdentity();
77 //gluLookAt(1, 1, 1, 0, 0, -1, 0, 1, 0);
78 gluLookAt(m_pCamera->getEyeX(),
79 m_pCamera->getEyeY(),
80 m_pCamera->getEyeZ(),
81 m_pCamera->getCenterX(),
82 m_pCamera->getCenterY(),
83 m_pCamera->getCenterZ(),
84 m_pCamera->getUpX(),
85 m_pCamera->getUpY(),
86 m_pCamera->getUpZ());
88 // Draw global axis system
89 m_pAxisX->draw(0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
90 m_pAxisY->draw(0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
91 m_pAxisZ->draw(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
93 // Rotate appropriately
94 std::cout << "x = " << m_xCoord << "\ty = " << m_yCoord << "\tz = " << m_zCoord
95 << "\txR = " << m_xRot << "\tyR = " << m_yRot << "\tzR = " << m_zRot
96 << "\n";
98 glTranslatef(0, -m_xCoord/700, 0);
100 glRotatef(m_xRot, 1.0f, 0.0f, 0.0f);
101 glRotatef(m_yRot, 0.0f, 1.0f, 0.0f);
102 glRotatef(m_zRot, 0.0f, 0.0f, 1.0f);
104 // Local Axis
105 if (m_pAxisX->isLocal() || m_pAxisY->isLocal() || m_pAxisZ->isLocal()) {
106 m_pAxisX->draw(0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
107 m_pAxisY->draw(0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
108 m_pAxisZ->draw(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
111 // Draw sphere
112 m_pSphere->draw();
114 glFlush();
117 void MyGLWidget::resizeGL(int width, int height)
119 qDebug("MyGLWidget::resizeGL()\n"
120 "New width = %d\tNew height = %d", width, height);
122 // Update viewport to cover the whole screen
123 glViewport(0, 0, width, height);
125 // Reset projection matrix
126 glMatrixMode(GL_PROJECTION);
127 glLoadIdentity();
128 //gluPerspective(45.0, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
130 // Reset modelview matrix
131 glMatrixMode(GL_MODELVIEW);
132 glLoadIdentity();
135 void MyGLWidget::pullData(void)
137 qDebug("MyGLWidget::pullData()");
139 mutex.lock();
140 if (data.size() >= 6) {
141 qDebug("Queue size = %d", data.size());
142 m_xCoord = data.dequeue();
143 m_yCoord = data.dequeue();
144 m_zCoord = data.dequeue();
145 m_xRot = data.dequeue();
146 m_yRot = data.dequeue();
147 m_zRot = data.dequeue();
149 else {
150 mutex.unlock();
151 qDebug("WAKING THREAD");
152 m_thread.condition.wakeOne();
153 return;
155 mutex.unlock();
156 updateGL();
159 void MyGLWidget::startWorkerThread(void)
161 qDebug("MyGLWidget::startWorkerThread()");
163 m_thread.setData(&this->data);
164 m_thread.start();
167 void MyGLWidget::startAnimation(void)
169 qDebug("MyGLWidget::startAnimation()");
171 m_timer.start(m_timerInterval);
174 void MyGLWidget::stopAnimation(void)
176 qDebug("MyGLWidget::stopAnimation()");
178 m_timer.stop();
181 void MyGLWidget::setAxis(axis_t axis, const Axis *pAxis)
183 qDebug("MyGLWidget::setAxis()");
185 switch(axis) {
186 case AXIS_X:
187 m_pAxisX = pAxis;
188 break;
189 case AXIS_Y:
190 m_pAxisY = pAxis;
191 break;
192 case AXIS_Z:
193 m_pAxisZ = pAxis;
194 break;
198 void MyGLWidget::setCamera(const Camera *pCamera)
200 qDebug("MyGLWidget::setCamera()");
202 m_pCamera = pCamera;
205 void MyGLWidget::setSphere(const Sphere *pSphere)
207 qDebug("MyGLWidge::setSphere()");
209 m_pSphere = pSphere;