direcs  2012-09-30
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
compassWidget.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) Markus Knapp *
3  * www.direcs.de *
4  * *
5  * This file is part of direcs. *
6  * *
7  * direcs is free software: you can redistribute it and/or modify it *
8  * under the terms of the GNU General Public License as published *
9  * by the Free Software Foundation, version 3 of the License. *
10  * *
11  * direcs is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with direcs. If not, see <http://www.gnu.org/licenses/>. *
18  * *
19  *************************************************************************/
20 
21 #include "compassWidget.h"
22 
23 
24 CompassWidget::CompassWidget(QWidget *parent) : QGLWidget(parent)
25 {
26  xRot = 0.0;
27  yRot = 0.0;
28  zRot = 0.0;
29 
30  // initial view settings
31  m_mouseAngleH = 0.0;
32  m_mouseAngleV = 0.0;
33  m_mouseLastX = 0;
34  m_mouseLastY = 0;
35 
36  // initialize quadric pointers
37  xAxisCylinder = NULL;
38  yAxisCylinder = NULL;
39  zAxisCylinder = NULL;
40  xAxisCone = NULL;
41  yAxisCone = NULL;
42  zAxisCone = NULL;
43 
44  // initialize texture pointers
46  robotTextureBack = 0;
47  robotTextureLeft = 0;
49 
50  // real init done at initializeGL()
51  cyl_radius = 0.0;
52  cyl_height = 0.0;
53 
54  cubeWidth = 0.0;
55  cubeHeight = 0.0;
56  cubeDepth = 0.0;
57 
58  // set the colors
59  xAxisColor = Qt::red;
60  yAxisColor = Qt::green;
61  zAxisColor = Qt::blue;
62 
63  /*
64  // get color from main window background
65  // So the background color of the OpenGL widget will be like the background color of the main application!
66  \todo buggy. brings a dark gray background?!?? backgroundColor = QApplication::palette().base().color();
67  */
68  backgroundColor = Qt::black;
69 }
70 
71 
73 {
74  makeCurrent();
75 
77  deleteTexture(robotTextureFront);
78 
79  if (robotTextureBack)
80  deleteTexture(robotTextureBack);
81 
82  if (robotTextureLeft)
83  deleteTexture(robotTextureLeft);
84 
86  deleteTexture(robotTextureRight);
87 
88  if (zAxisCone)
89  gluDeleteQuadric (zAxisCone);
90 
91  if (yAxisCone)
92  gluDeleteQuadric (yAxisCone);
93 
94  if (xAxisCone)
95  gluDeleteQuadric (xAxisCone);
96 
97  if (zAxisCylinder)
98  gluDeleteQuadric (zAxisCylinder);
99 
100  if (yAxisCylinder)
101  gluDeleteQuadric (yAxisCylinder);
102 
103  if (xAxisCylinder)
104  gluDeleteQuadric (xAxisCylinder);
105 }
106 
107 
109 {
110 /*
111  robotImageFront.load(":/images/images/bot_front.png");
112  robotImageBack.load(":/images/images/bot_back.png");
113  robotImageLeft.load(":/images/images/bot_left.png");
114  robotImageRight.load(":/images/images/bot_right.png");
115 */
116  glEnable(GL_TEXTURE_2D);
117  glShadeModel(GL_SMOOTH);
118 // qglClearColor(backgroundColor.dark());
119  glClearColor(0.93f, 0.93f, 0.93f, 0.0f); // Grey Background
120 
121  glClearDepth(1.0);
122  glEnable(GL_DEPTH_TEST);
123  glDepthFunc(GL_LEQUAL);
124 
125  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
126 
127 
128  //----------------------- pfeile -------------------------
129  GLfloat LightAmbient[] = {0.25, 0.25, 0.25, 1.0}; // Umgebungslicht
130  GLfloat LightDiffuse[] = {0.4, 0.4, 0.4, 1.0}; // Bei diffusem Licht ist die Richtung des Lichts erkennbar, aus der es kommt
131  GLfloat LightSpecular[] = {0.77, 0.77, 0.77, 1.0}; // Glanz
132 
133  GLfloat LightPosition[] = {0.0, 0.0, 3.0, 1.0};
134  GLfloat spot_direction[] = {0.0, 0.0, -1.0}; // Richtung in die das Spotlight zeigt
135 
136  glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
137  glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
138  glLightfv(GL_LIGHT0, GL_SPECULAR, LightSpecular);
139  glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
140  glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 50.0); // gibt den Winkel an, der zwischen Richtung und max. Auswurf besteht
141  glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
142  glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 10.0); // gibt an, wie stark die lichtstärke nach aussen abnimmt
143 
144  glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
145  glEnable(GL_LIGHT0);
146  glEnable(GL_LIGHTING);
147  glEnable(GL_COLOR_MATERIAL); // neu
148 
149  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
150  glEnable(GL_BLEND); // erforderlich, wenn Materialien durchsichtig sind
151  //----------------------- pfeile -------------------------
152 
153  cyl_radius = 0.03;
154  cyl_height = 0.30;
155 
156  cubeWidth = 0.40;
157  cubeHeight = 0.18;
158  cubeDepth = 0.40;
159 
160  xAxisCylinder = gluNewQuadric();
161  yAxisCylinder = gluNewQuadric();
162  zAxisCylinder = gluNewQuadric();
163 
164  xAxisCone = gluNewQuadric();
165  yAxisCone = gluNewQuadric();
166  zAxisCone = gluNewQuadric();
167 }
168 
169 
171 {
172  // clear last scene
173  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
174 
175  // reset modelview matrix
176  glLoadIdentity();
177 
178  //----------------------- pfeile -------------------------
179  glTranslated(0.0, 0.0, -10.0);
180 
181  // enable roation
182  glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
183  glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
184  glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
185  //----------------------- pfeile -------------------------
186 
187 /*
188  //-------------------------- texture ------------------------
189  glRotatef(m_mouseAngleH, 1.0, 0.0, 0.0);
190  glRotatef(m_mouseAngleV, 0.0, 1.0, 0.0);
191  //glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
192  //-------------------------- texture ------------------------
193 */
194 
195  //----------------------- pfeile -------------------------
196  // X cylinder (red)
197  qglColor(xAxisColor);
198  // object, baseradius, topradius, height, slices, stacks
199  gluCylinder(xAxisCylinder, cyl_radius, cyl_radius, cyl_height, 32, 32);
200  // move
201  glTranslatef(0.0, 0.0, cyl_height);
202  qglColor(xAxisColor);
203  // X cone
204  gluCylinder(xAxisCone, (cyl_radius*1.5), 0.0, cyl_height/2.0, 32, 32);
205 
206  // Y cylinder (green)
207  qglColor(yAxisColor);
208  // move back and rotate one axis
209  glTranslatef(0.0, 0.0, - (cyl_height - cyl_radius));
210  glRotated(90, 1.0, 0.0, 0.0);
211  // object, baseradius, topradius, height, slices, stacks
212  gluCylinder(yAxisCylinder, cyl_radius, cyl_radius, cyl_height, 32, 32);
213  // move
214  glTranslatef(0.0, 0.0, cyl_height);
215  qglColor(yAxisColor);
216  // Y cone
217  gluCylinder(yAxisCone, (cyl_radius*1.5), 0.0, cyl_height/2.0, 32, 32);
218 
219  // Z cylinder (blue)
220  qglColor(zAxisColor);
221  // move back and rotate one axis
222  glTranslatef(0.0, 0.0, - (cyl_height));
223  glRotated(90, 0.0, 1.0, 0.0);
224  // object, baseradius, topradius, height, slices, stacks
225  gluCylinder(zAxisCylinder, cyl_radius, cyl_radius, cyl_height, 32, 32);
226  // move
227  glTranslatef(0.0, 0.0, cyl_height);
228  qglColor(zAxisColor);
229  // Z cone
230  gluCylinder(zAxisCone, (cyl_radius*1.5), 0.0, cyl_height/2.0, 32, 32);
231  //----------------------- pfeile -------------------------
232 
233 /*
234  //-------------------------- texture ------------------------
235  // use mipmapped textures
236  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
237  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
238 
239  // \todo should be located elsewhere
240  static GLfloat no_mat[] = {0.0, 0.0, 0.0, 1.0};
241  static GLfloat mat_diffuse[] = {0.5, 0.5, 0.5, 1.0};
242  static GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
243  static GLfloat low_shininess[] = {2.5};
244 // static GLfloat translucent[] = {1.0, 1.0, 1.0, 0.33};
245 
246  glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
247  glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
248  glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
249  glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
250  glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
251 
252  // bind textures
253  robotTextureFront = bindTexture(robotImageFront, GL_TEXTURE_2D, GL_RGBA);
254  robotTextureBack = bindTexture(robotImageBack, GL_TEXTURE_2D, GL_RGBA);
255  robotTextureLeft = bindTexture(robotImageLeft, GL_TEXTURE_2D, GL_RGBA);
256  robotTextureRight = bindTexture(robotImageRight, GL_TEXTURE_2D, GL_RGBA);
257 
258  // enable texturing
259  glEnable(GL_TEXTURE_2D);
260 
261  // move world 7 units away from the current view point
262  glTranslated(0.0, 0.0, -7.0);
263 
264  // create FRONT texture
265  glBindTexture(GL_TEXTURE_2D, robotTextureFront);
266  glBegin(GL_QUADS);
267  glTexCoord2f(0.0, 0.0); glVertex3f(-cubeWidth, cubeHeight, cubeHeight); // Top Left 1
268  glTexCoord2f(1.0, 0.0); glVertex3f( cubeWidth, cubeHeight, cubeHeight); // Top Right 2
269  glTexCoord2f(1.0, 1.0); glVertex3f( cubeWidth,-cubeHeight, cubeHeight); // Bottom Right 3
270  glTexCoord2f(0.0, 1.0); glVertex3f(-cubeWidth,-cubeHeight, cubeHeight); // Bottom Left 4
271  glEnd();
272 
273 
274  // create RIGHT texture
275  glBindTexture(GL_TEXTURE_2D, robotTextureRight);
276  glBegin(GL_QUADS);
277  glTexCoord2f(0.0, 0.0); glVertex3f( cubeHeight, cubeHeight, cubeWidth); // Top Left 1
278  glTexCoord2f(1.0, 0.0); glVertex3f( cubeHeight, cubeHeight,-cubeWidth); // Top Right 2
279  glTexCoord2f(1.0, 1.0); glVertex3f( cubeHeight,-cubeHeight,-cubeWidth); // Bottom Right 3
280  glTexCoord2f(0.0, 1.0); glVertex3f( cubeHeight,-cubeHeight, cubeWidth); // Bottom Left 4
281  glEnd();
282 
283  // create LEFT texture
284  glBindTexture(GL_TEXTURE_2D, robotTextureLeft);
285  glBegin(GL_QUADS);
286  glTexCoord2f(0.0, 0.0); glVertex3f(-cubeHeight, cubeHeight,-cubeWidth); // Top Left 1
287  glTexCoord2f(1.0, 0.0); glVertex3f(-cubeHeight, cubeHeight, cubeWidth); // Top Right 2
288  glTexCoord2f(1.0, 1.0); glVertex3f(-cubeHeight,-cubeHeight, cubeWidth); // Bottom Right 3
289  glTexCoord2f(0.0, 1.0); glVertex3f(-cubeHeight,-cubeHeight,-cubeWidth); // Bottom Left 4
290  glEnd();
291 
292  // create BACK texture
293  glBindTexture(GL_TEXTURE_2D, robotTextureBack);
294  glBegin(GL_QUADS);
295  glTexCoord2f(0.0, 0.0); glVertex3f( cubeHeight, cubeHeight,-cubeWidth); // Top Left 1
296  glTexCoord2f(1.0, 0.0); glVertex3f(-cubeHeight, cubeHeight,-cubeWidth); // Top Right 2
297  glTexCoord2f(1.0, 1.0); glVertex3f(-cubeHeight,-cubeHeight,-cubeWidth); // Bottom Right 3
298  glTexCoord2f(0.0, 1.0); glVertex3f( cubeHeight,-cubeHeight,-cubeWidth); // Bottom Left 4
299  glEnd();
300 
301  glDisable(GL_TEXTURE_2D);
302  //-------------------------- texture ------------------------
303 */
304 }
305 
306 
307 void CompassWidget::resizeGL(int width, int height)
308 {
309  int side = qMin(width, height);
310  glViewport((width - side) / 2, (height - side) / 2, side, side); // \todo check this viewport stuff
311 
312  glMatrixMode(GL_PROJECTION);
313  glLoadIdentity();
314  glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
315  glMatrixMode(GL_MODELVIEW);
316 }
317 
318 
319 void CompassWidget::mousePressEvent(QMouseEvent *event)
320 {
321  lastPos = event->pos();
322 }
323 
324 
325 void CompassWidget::mouseMoveEvent(QMouseEvent *event)
326 {
327  if (event->buttons() & Qt::LeftButton)
328  {
329  if(lastPos.x() != 0)
330  {
331  m_mouseAngleH -= (lastPos.y() - event->y());
334  }
335  if(lastPos.y() != 0)
336  {
337  m_mouseAngleV += (lastPos.x() - event->x());
339  m_mouseAngleV = m_mouseAngleV > -90 ? m_mouseAngleV : -90;
340  }
341 
342  updateGL();
343 
344  // store current x and y pos
345  lastPos = event->pos();
346  }
347  else if (event->buttons() & Qt::RightButton)
348  {
349  /*
350  if(lastPos.y() != 0)
351  {
352  m_cameraZoom -= (lastPos.y() - event->y());
353  m_cameraZoom = m_cameraZoom >= m_cameraZoomLBound ? m_cameraZoom : m_cameraZoomLBound;
354  m_cameraZoom = m_cameraZoom >= m_cameraZoomUBound ? m_cameraZoomUBound : m_cameraZoom;
355  }
356  */
357 
358  updateGL();
359 
360  // store current x and y pos
361  lastPos = event->pos();
362  }
363 }
364 
365 
367 {
368  while (*angle < 0.0)
369  *angle += 360.0 * 16.0;
370  while (*angle > 360.0 * 16.0)
371  *angle -= 360.0 * 16.0;
372 }
373 
374 
376 {
377  normalizeAngle(&angle);
378  if (angle != xRot)
379  {
380  xRot = angle;
381  //emit xRotationChanged(angle);
382  updateGL();
383  }
384 }
385 
386 
388 {
389  normalizeAngle(&angle);
390  if (angle != yRot)
391  {
392  yRot = angle;
393  //emit yRotationChanged(angle);
394  updateGL();
395  }
396 }
397 
398 
400 {
401  normalizeAngle(&angle);
402  if (angle != zRot)
403  {
404  zRot = angle;
405  //emit zRotationChanged(angle);
406  updateGL();
407  }
408 }
409 
410 
411 void CompassWidget::setAllRotations(float xAngle, float yAngle, float zAngle)
412 {
413 // setXRotation( (int) (xAngle*16) );
414 // setYRotation( (int) (yAngle*16) );
415 // setZRotation( (int) (zAngle*16) );
416  setXRotation( xAngle );
417  setYRotation( yAngle );
418  setZRotation( zAngle );
419 }
420 
421 
423 {
424  return QSize(50, 50);
425 }
426 
427 
429 {
430  return QSize(800, 800);
431 }
432 
433 
435 {
436  backgroundColor = col;
437 }