iCub-main
animationview.cpp
Go to the documentation of this file.
1 /*
2  * animationview.cpp
3  */
4 
5 /*
6  * Copyright (C) 2009 RobotCub Consortium
7  * Author: Alessandro Scalzo alessandro.scalzo@iit.it
8  * CopyPolicy: Released under the terms of the GNU GPL v2.0.
9  *
10  * Based on:
11  *
12  * Qavimator
13  * Copyright (C) 2006 by Zi Ree *
14  * Zi Ree @ SecondLife *
15  * Released under the terms of the GNU GPL v2.0.
16  */
17 
18 /*
19 #ifdef __APPLE__
20 #include <OpenGL/glu.h>
21 #include <GLUT/glut.h>
22 #include <QApplication.h>
23 #else
24 #if defined(WIN32) || defined(WIN64)
25 #include <windows.h>
26 #endif
27 #include <GL/glu.h>
28 #include <GL/glut.h>
29 #endif
30 */
31 
32 #include <qevent.h>
33 #include <qcursor.h>
34 
35 #include "animationview.h"
36 
37 #include "settings.h"
38 
39 #define KEY_SHIFT 1
40 #define KEY_CTRL 2
41 #define KEY_ALT 4
42 
43 AnimationView::AnimationView(QWidget* parent) : QGLWidget(parent)
44 {
45  m_bInitialized=false;
46 
47  // fake glut initialization
48  int args=1;
49  // make sure the "iCubimator" string is not const char*
50  char arg0[]="iCubimator";
51  char* arg[]={arg0};
52  glutInit(&args,arg);
53 
54  // init
55  dragX=0;
56  dragY=0;
57  changeX=0;
58  changeY=0;
59  changeZ=0;
60  xSelect=false;
61  ySelect=false;
62  zSelect=false;
63 
64  leftMouseButton=false;
65  modifier=0;
66 
67  setMouseTracking(true);
68 
69 
70 
71  connect(&mTimer,SIGNAL(timeout()),this,SLOT(timerTimeout()));
72 }
73 
74 void AnimationView::init(yarp::os::ResourceFinder& config)
75 {
76  yarp::os::Bottle skinParams=config.findGroup("SKIN");
77  if (!skinParams.isNull())
78  {
80  skinParams.find("force_gain").asFloat64(),
81  skinParams.find("force_thr").asFloat64(),
82  skinParams.find("torque_gain").asFloat64(),
83  skinParams.find("torque_thr").asFloat64()
84  );
85  }
86 
87  mObjectsManager=new ObjectsManager(config.find("objport").asString().c_str(),config.find("texport").asString().c_str(),config.find("forceport").asString().c_str());
88  //printf("objport=%s\n",config.find("objport").asString().c_str());
89 
90  mSubtitlesManager = new SubtitlesManager("/speechText:i", "/dbgText:i");
91 
92  setFocusPolicy(Qt::StrongFocus);
93 
95 
96  if (!pBVH->Create(config)){
97  exit(-1);
98  }
99 }
100 
102 {
103  if (mObjectsManager){
104  delete mObjectsManager;
105  }
106  if (mSubtitlesManager){
107  delete mSubtitlesManager;
108  }
109 
110 
111  if (pBVH){
112  delete pBVH;
113  }
114 }
115 
117 {
118  float alpha=(100-Settings::floorTranslucency())/100.0;
119 
120  //glEnable(GL_DEPTH_TEST);
121  glBegin(GL_QUADS);
122  for (int i=-10; i<10; ++i)
123  {
124  for (int j=-10; j<10; ++j)
125  {
126  if ((i+j)%2)
127  {
128  glColor4f(0.1,0.1,0.1,alpha);
129  }
130  else
131  {
132  glColor4f(0.6,0.6,0.6,alpha);
133  }
134 
135  glVertex3f(i*40,0,j*40);
136  glVertex3f(i*40,0,(j+1)*40);
137  glVertex3f((i+1)*40,0,(j+1)*40);
138  glVertex3f((i+1)*40,0,j*40);
139  }
140  }
141  glEnd();
142 }
143 
145 {
146  gluPerspective(60.0,((float)width())/height(),1,2000);
147 }
148 
150 {
151  draw();
152 }
153 
155 {
156  draw();
157 }
158 
160 {
161  GLfloat position0[]={0.0,80.0,100.0,1.0};
162  GLfloat ambient0[]={0.2,0.2,0.2,1.0};
163  //GLfloat diffuse0[]={0.5,0.5,0.5,0.2};
164  //GLfloat specular0[]={0.5,0.5,0.2,0.5};
165 
166  GLfloat position1[]={0.0,80.0,-100.0,1.0};
167  GLfloat ambient1[]={0.2,0.2,0.2,1.0};
168  GLfloat diffuse1[]={0.5,0.5,0.5,1.0};
169  GLfloat specular1[]={1.0,1.0,1.0,1.0};
170 
171  glViewport(0,0,width(),height());
172 
173  glEnable(GL_BLEND);
174  glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
175 
176  glLightfv(GL_LIGHT0,GL_AMBIENT,ambient0);
177  //glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
178  //glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);
179 
180  glLightfv(GL_LIGHT1,GL_AMBIENT,ambient1);
181  glLightfv(GL_LIGHT1,GL_DIFFUSE,diffuse1);
182  glLightfv(GL_LIGHT1,GL_SPECULAR,specular1);
183 
184  glEnable(GL_NORMALIZE);
185  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
186  //glEnable(GL_TEXTURE_2D);
187 
188  glMatrixMode(GL_PROJECTION);
189  glLoadIdentity();
190  setProjection();
191 
192  glLightfv(GL_LIGHT0,GL_POSITION,position0);
193  glLightfv(GL_LIGHT1,GL_POSITION,position1);
194 
195  m_bInitialized=true;
196 }
197 
199 {
200  if(!isValid()) initializeGL();
201 
202  if(Settings::fog())
203  {
204  glEnable(GL_FOG);
205  {
206  GLfloat fogColor[4]={0.5,0.5,0.5,0.3};
207  int fogMode=GL_EXP; // GL_EXP2, GL_LINEAR
208  glFogi(GL_FOG_MODE,fogMode);
209  glFogfv(GL_FOG_COLOR,fogColor);
210  glFogf(GL_FOG_DENSITY,0.005);
211  glHint(GL_FOG_HINT,GL_DONT_CARE);
212  glFogf(GL_FOG_START,200.0);
213  glFogf(GL_FOG_END,2000.0);
214  }
215  }
216  else
217  glDisable(GL_FOG);
218 
219  glClearColor(0.5,0.5,0.5,0.3); /* fog color */
220  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
221  glDisable(GL_LIGHTING);
222  glEnable(GL_DEPTH_TEST);
223  glEnable(GL_COLOR_MATERIAL);
224  glShadeModel(GL_FLAT);
225  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
226 
228 
229  drawFloor();
230 
231  glPushMatrix();
232 
233  glRotated(-90.0,1.0,0.0,0.0);
234  glRotated( 90.0,0.0,0.0,1.0);
235 
236  pBVH->draw();
238 
239  glPopMatrix();
240 }
241 
242 void AnimationView::mouseMoveEvent(QMouseEvent* event)
243 {
244  if(leftMouseButton)
245  {
246  QPoint dragPos=QCursor::pos();
247 
248  // since MacOSX doesn't like the old "drag and snap back" solution, we're going for a
249  // more elaborate solution
250 
251  // calculate drag distance from last dragging point
252  dragX=dragPos.x()-oldDragX;
253  dragY=dragPos.y()-oldDragY;
254 
255  // remember the current position as last dragging point
256  oldDragX=dragPos.x();
257  oldDragY=dragPos.y();
258 
259  // if mouse has moved sufficiently far enough from first clicking position ...
260  if(abs(clickPos.x()-dragPos.x())>100 || abs(clickPos.y()-dragPos.y())>100)
261  {
262  // set remembered drag position back to first click position
263  oldDragX=clickPos.x();
264  oldDragY=clickPos.y();
265  // set mouse cursor back to first click position
266  QCursor::setPos(clickPos);
267  }
268 
269  if(modifier & KEY_SHIFT)
270  camera.pan(dragX/2,dragY/2,0);
271  else if(modifier & KEY_ALT)
272  {
273  camera.pan(0,0,dragY);
274  camera.rotate(0,dragX);
275  }
276  else
278 
279  repaint();
280  }
281 }
282 
283 void AnimationView::mousePressEvent(QMouseEvent* event)
284 {
285  if (event->button()==Qt::LeftButton)
286  {
287  clickPos=QCursor::pos();
288  // put in position for distance calculation
289  oldDragX=clickPos.x();
290  oldDragY=clickPos.y();
291  leftMouseButton=true;
292  }
293 }
294 
295 void AnimationView::mouseReleaseEvent(QMouseEvent* event)
296 {
297  if(event->button()==Qt::LeftButton)
298  {
299  leftMouseButton=false;
300  }
301 
302  /*
303  if(event->button()==Qt::LeftButton)
304  {
305  // move mouse cursor back to the beginning of the dragging process
308  // show mouse cursor again
309  setCursor(Qt::ArrowCursor);
310  leftMouseButton=false;
311  propDragging=0;
312  }
313  */
314 }
315 
316 void AnimationView::mouseDoubleClickEvent(QMouseEvent* event)
317 {
318 }
319 
320 void AnimationView::wheelEvent(QWheelEvent* event)
321 {
322  camera.pan(0,0,-event->delta()/12);
323  repaint();
324 }
325 
326 void AnimationView::keyPressEvent(QKeyEvent* event)
327 {
328  switch(event->key())
329  {
330  case Qt::Key_PageUp:
331  camera.pan(0,0,-5);
332  repaint();
333  break;
334  case Qt::Key_PageDown:
335  camera.pan(0,0,5);
336  repaint();
337  break;
338  case Qt::Key_Shift:
340  xSelect = true;
341  repaint();
342  break;
343  case Qt::Key_Alt:
344  modifier|=KEY_ALT;
345  ySelect = true;
346  repaint();
347  break;
348  case Qt::Key_Control:
350  zSelect = true;
351  repaint();
352  break;
353  }
354  event->ignore();
355 }
356 
357 void AnimationView::keyReleaseEvent(QKeyEvent* event)
358 {
359  switch(event->key())
360  {
361  case Qt::Key_Shift:
363  xSelect = false;
364  repaint();
365  break;
366  case Qt::Key_Alt:
367  modifier&=!KEY_ALT;
368  ySelect = false;
369  repaint();
370  break;
371  case Qt::Key_Control:
372  modifier&=!KEY_CTRL;
373  zSelect = false;
374  repaint();
375  break;
376  }
377  event->ignore();
378 }
379 
380 /*
381 void AnimationView::drawCircle(int axis,float radius,int width)
382 {
383  GLint circle_points=100;
384  float angle;
385 
386  glLineWidth(width);
387  switch(axis)
388  {
389  case 0: glColor4f(1,0,0,1); break;
390  case 1: glColor4f(0,1,0,1); break;
391  case 2: glColor4f(0,0,1,1); break;
392  }
393  glBegin(GL_LINE_LOOP);
394  for(int i=0;i<circle_points;i++)
395  {
396  angle=2*M_PI*i/circle_points;
397  switch(axis)
398  {
399  case 0: glVertex3f(0,radius*cos(angle),radius*sin(angle)); break;
400  case 1: glVertex3f(radius*cos(angle),0,radius*sin(angle)); break;
401  case 2: glVertex3f(radius*cos(angle),radius*sin(angle),0); break;
402  }
403  }
404  glEnd();
405  glBegin(GL_LINES);
406  switch(axis)
407  {
408  case 0: glVertex3f(-10,0,0); glVertex3f(10,0,0); break;
409  case 1: glVertex3f(0,-10,0); glVertex3f(0,10,0); break;
410  case 2: glVertex3f(0,0,-10); glVertex3f(0,0,10); break;
411  }
412  glEnd();
413 
414 }
415 */
416 
418 {
419  camera.reset();
420  repaint();
421 }
422 
423 // handle widget resizes
424 void AnimationView::resizeEvent(QResizeEvent* newSize)
425 {
426  if (!m_bInitialized) return;
427 
428  int w=newSize->size().width();
429  int h=newSize->size().height();
430 
431  // reset coordinates
432  glMatrixMode(GL_PROJECTION);
433  glLoadIdentity();
434 
435  // resize GL viewport
436  glViewport(0,0,w,h);
437 
438  // set up aspect ratio
439  setProjection();
440 
441  glMatrixMode(GL_MODELVIEW);
442  glLoadIdentity();
443 }
#define KEY_SHIFT
#define KEY_ALT
#define KEY_CTRL
void timerTimeout()
Definition: animationview.h:80
virtual void keyReleaseEvent(QKeyEvent *event)
SubtitlesManager * mSubtitlesManager
virtual void keyPressEvent(QKeyEvent *event)
virtual void mouseDoubleClickEvent(QMouseEvent *event)
AnimationView(QWidget *parent)
virtual void wheelEvent(QWheelEvent *event)
virtual void paintGL()
virtual void mouseMoveEvent(QMouseEvent *event)
virtual void mousePressEvent(QMouseEvent *event)
virtual void initializeGL()
ObjectsManager * mObjectsManager
virtual void paintOverlayGL()
virtual void mouseReleaseEvent(QMouseEvent *event)
virtual void resizeEvent(QResizeEvent *newSize)
void init(yarp::os::ResourceFinder &config)
Definition: bvh.h:59
bool Create(yarp::os::ResourceFinder &config)
Definition: bvh.cpp:48
void draw()
Definition: bvh.h:67
void reset()
Definition: camera.cpp:66
void setModelView()
Definition: camera.cpp:57
void rotate(float x, float y)
Definition: camera.cpp:34
void pan(float x, float y, float z)
Definition: camera.cpp:44
static void setParams(double fg, double ft, double tg, double tt)
Definition: bvhnode.h:165
static bool fog()
Definition: settings.cpp:43
static int floorTranslucency()
Definition: settings.cpp:53