Computational Embodied Neuroscience Simulator  1.1
3D simulation library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
cens_graphics.cpp
Go to the documentation of this file.
1 // Computational Embodied Neuroscience Simulator (CENS) Library
2 // Copyright (c) 2010 Francesco Mannella
3 //
4 // cens_graphics.cpp
5 // Copyright (c) 2010 Francesco Mannella
6 //
7 // This file is part of CENS library.
8 //
9 // CENS library is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // CENS library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with CENS library. If not, see <http://www.gnu.org/licenses/>.
21 
22 #include <iomanip>
23 #include <sstream>
24 #include "cens_graphics.h"
25 #include "cens_utils.h"
26 
27 using namespace Magick;
28 
29 namespace cens {
30 
34 
35  void glutKeyboardCallback(unsigned char key, int x, int y)
36  {
37  cens_graphics->keyboard(key,x,y);
38  }
39 
41 
43  {
45  }
46 
48 
49  void glutTimerCallback( int value )
50  {
51  cens_graphics->step(value);
52  if( value ) glutPostRedisplay();
53  glutTimerFunc(value,glutTimerCallback,value);
54  }
55 
56 
60 
62 
64 
65  CENSGraphics::~CENSGraphics() {
66 
67  }
68 
72 
73  void CENSGraphics::initCENSGraphics( int argc, char ** argv )
74  {
75 
77  // PARAMETERS //////////////////////////////////////////////
78 
79  m_grHide=false;
80  if (argc==2) {
81  if( std::string(argv[1]).compare("--hide")==0 ) {
82  m_grHide = true;
83  }
84  }
85 
86  // INITIALIZE ////////////////////////////////////
87 
88  m_grArgc = argc;
89  m_grArgv = argv;
90 
91 
93 
94 
96  // VIEWPOINT ///////////////////////////////////////////////
97 
98  m_grParameterManager.addParameter("SCREEN_WIDTH",m_grViewCamera.m_cScreenWidth);
99  m_grParameterManager.addParameter("SCREEN_HEIGHT",m_grViewCamera.m_cScreenHeight);
100  m_grParameterManager.addParameter("SCREEN_XGAP",m_grViewCamera.m_cScreenXGap);
101  m_grParameterManager.addParameter("SCREEN_YGAP",m_grViewCamera.m_cScreenYGap);
102  m_grParameterManager.addParameter("TIMESTEP",m_grTimestep);
103  m_grParameterManager.addParameter("SCREEN_TITLE",m_grViewCamera.m_cScreenTitle);
104  m_grParameterManager.addParameter("FIELD_OF_VIEW",m_grViewCamera.m_cFoV);
105  m_grParameterManager.addParameter("RATIO",m_grViewCamera.m_cRatio);
106  m_grParameterManager.addParameter("NEAR",m_grViewCamera.m_cNear);
107  m_grParameterManager.addParameter("FAR",m_grViewCamera.m_cFar);
108  m_grParameterManager.addParameter("ANGLE",m_grViewCamera.m_cAngle);
109  m_grParameterManager.addParameter("DISTANCE",m_grViewCamera.m_cDistance);
110  m_grParameterManager.addParameter("HEIGHT",m_grViewCamera.m_cHeight);
111  m_grParameterManager.addParameter("TARGET",m_grViewCamera.m_cTarget);
112  m_grParameterManager.addParameter("UP",m_grViewCamera.m_cUp);
113  m_grParameterManager.addParameter("MOV",m_grViewCamera.m_cMov);
114  m_grParameterManager.addParameter("GAP",m_grViewCamera.m_cGap);
115 
116 
117  // ////////////////////////////////////////////////////////////
118  // // COLORS //////////////////////////////////////////////////
119 
120  m_grParameterManager.addParameter("ENV_COLOR",m_grEnvColor);
121  m_grParameterManager.addParameter("GROUND_COLOR",m_grGroundColor);
122  m_grParameterManager.addParameter("OBJECT_COLOR",m_grObjectColor);
123  m_grParameterManager.addParameter("BOX_COLOR",m_grBoxColor);
124  m_grParameterManager.addParameter("CAPSULE_COLOR",m_grCapsuleColor);
125  m_grParameterManager.addParameter("SPHERE_COLOR",m_grSphereColor);
126  m_grParameterManager.addParameter("COMPOUND_COLOR",m_grCompoundColor);
127  m_grParameterManager.addParameter("SOFT_COLOR",m_grSoftColor);
128 
129  // ///////////////////////////////////////////////////////////
130  // // LIGHTS /////////////////////////////////////////////////
131 
132  m_grParameterManager.addParameter("LIGHT_AMBIENT", m_grLight.m_lAmbient);
133  m_grParameterManager.addParameter("LIGHT_DIFFUSE", m_grLight.m_lDiffuse);
134  m_grParameterManager.addParameter("LIGHT_SPECULAR", m_grLight.m_lSpecular);
135  m_grParameterManager.addParameter("LIGHT_POSITION0", m_grLight.m_lPosition0);
136  m_grParameterManager.addParameter("LIGHT_POSITION1", m_grLight.m_lPosition1);
137 
139 
140  if (!m_grParameterManager.loadParameters())
141  {
142 
143  m_grViewCamera.m_cScreenWidth = 800;
144  m_grViewCamera.m_cScreenHeight = 600;
145  m_grViewCamera.m_cScreenXGap = 10;
146  m_grViewCamera.m_cScreenYGap = 10;
147  m_grTimestep = 10;
148  m_grViewCamera.m_cScreenTitle = "Demo";
149  m_grViewCamera.m_cFoV = 70.0;
150  m_grViewCamera.m_cRatio = 800/600;
151  m_grViewCamera.m_cNear = 1.0;
152  m_grViewCamera.m_cFar = 10000.0;
153  m_grViewCamera.m_cAngle = 0.75;
154  m_grViewCamera.m_cDistance = 85.0;
155  m_grViewCamera.m_cHeight = 4.5;
156  m_grViewCamera.m_cTarget << 0.0, 0.0, 1.0;
157  m_grViewCamera.m_cUp << 0.0, 0.0, 1.0;
158  m_grViewCamera.m_cMov = 45.0;
159  m_grViewCamera.m_cGap = 0.2;
160 
161 
162  // ////////////////////////////////////////////////////////////
163  // // COLORS //////////////////////////////////////////////////
164 
165  m_grEnvColor << 0.1, 0.1, 0.0;
166  m_grGroundColor << 0.6, 0.2, 0.2;
167  m_grObjectColor << 0.2, 0.6, 0.2;
168  m_grBoxColor << 0.8, 0.8, 0.2;
169  m_grCapsuleColor << 0.4, 0.4, 0.6;
170  m_grSphereColor << 0.7, 0.1, 0.7;
171  m_grCompoundColor << 0.2, 0.6, 0.2;
172  m_grSoftColor << 0.0, 0.6, 0.0;
173 
174  // ///////////////////////////////////////////////////////////
175  // // LIGHTS /////////////////////////////////////////////////
176 
177  m_grLight.m_lAmbient << 0.2, 0.2, 0.2, 1.0;
178  m_grLight.m_lDiffuse << 1.0, 1.0, 1.0, 1.0;
179  m_grLight.m_lSpecular << 1.0, 1.0, 1.0, 1.0;
180  m_grLight.m_lPosition0 << 1.0, 10.0, 1.0, 0.0;
181  m_grLight.m_lPosition1 << -1.0, -10.0, -1.0, 0.0;
182 
183  m_grParameterManager.saveParameters();
184  }
185 
187 
188  glutInit( &(m_grArgc), m_grArgv);
189 
190 
192  // SCREEN //////////////////////////////////////////////////
193 
194  // mainCamera
195  initCamera(m_grViewCamera,m_grLight);
196  glutKeyboardFunc(glutKeyboardCallback);
197  glutTimerFunc(0,glutTimerCallback, m_grTimestep);
198 
200 
201  m_grAxisEnabled = false;
202  m_grSimEnabled = true;
203  m_grTextureEnabled = false;
204  m_grObjectAxesEnabled = false;
205  m_grJointAxesEnabled = false;
206  m_grCameraAxisEnabled = false;
207 
208  }
209 
211 
212  void CENSGraphics::loop( )
213  {
214  glutMainLoop();
215  }
216 
217  void CENSGraphics::stepToStepLoop( )
218  {
219  glutMainLoopEvent ();
220  }
221 
223 
224  void CENSGraphics::step( int value )
225  {
226  for(unsigned int x=0; x<m_grCameras.size(); x++) {
227  if(x > 0 || (not isIdle()) ) {
228  focusCamera(x);
229  beginRendering();
230  updateCamera(x);
231  display();
232  endRendering();
233  }
234  }
235  }
237 
238  void CENSGraphics::display( )
239  {
240 
241  }
242 
244 
245  bool CENSGraphics::isIdle()
246  {
247  return m_grHide;
248  };
249 
251 
252  void CENSGraphics::quit()
253  {
254  glutLeaveMainLoop();
255  };
256 
258 
259  void CENSGraphics::keyboard( unsigned char key, int x, int y )
260  {
261 
262  switch (key) {
263  case 'q' : quit(); break;
264  case 'l' : stepLeft(); break;
265  case 'j' : stepRight(); break;
266  case 'i' : stepFront(); break;
267  case 'm' : stepBack(); break;
268  case '4' : stepLeft(); break;
269  case '6' : stepRight(); break;
270  case '8' : stepFront(); break;
271  case '2' : stepBack(); break;
272  case '7' : stepLeft(); stepFront(); break;
273  case '9' : stepRight(); stepFront(); break;
274  case '1' : stepLeft(); stepBack(); break;
275  case '3' : stepRight(); stepBack(); break;
276  case 'z' : zoomIn(); break;
277  case 'x' : zoomOut(); break;
278  case 't' : toggleTexture(); break;
279  case 'a' : toggleAxis(); break;
280  case 'd' : toggleObjectAxes(); break;
281  case 'f' : toggleJointAxes(); break;
282  case 's' : toggleCameraAxis(); break;
283  case 'w' : toggleStop(); break;
284  default : break;
285  }
286 
287  }
288 
292 
293  void CENSGraphics::beginRendering()
294  {
295 
296  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); /* clear the display */
297 
298  }
299 
301 
302  void CENSGraphics::endRendering()
303  {
304  glutSwapBuffers();
305  }
306 
308 
309  void CENSGraphics::initTextures( ) {
310 
311  m_grTextureInitialized=true;
312 
313  for(unsigned int i=0; i<m_grShapes.size();i++)
314  m_grShapes[i]->initTexture();
315  }
316 
318 
319  void CENSGraphics::initTexture( const GLubyte *source, int width, int height ) {
320 
321  m_grTextureInitialized=true;
322 
323  GLubyte *image;
324 
325  if(source==0) {
326 
327  width = 256;
328  height = 256;
329  image = new GLubyte[256*256*3];
330  for(int y=0;y<256;++y)
331  {
332  const int t=y>>4;
333  GLubyte*pi=image+y*256*3;
334  for(int x=0;x<256;++x)
335  {
336  const int s=x>>4;
337  const GLubyte b=180;
338  GLubyte c = b+(((s+t)&1)&1)*(255-b);
339  pi[0]=pi[1]=pi[2]=c;pi+=3;
340  }
341  }
342 
343  } else {
344 
345  image = new GLubyte[width*height*3];
346  for(int x=0;x<width*height*3; x++) {
347  image[x] = source[x];
348  }
349 
350  }
351 
352  // texture init
353  GLuint texture_id;
354 
355  glGenTextures( 1, &texture_id );
356  glBindTexture( GL_TEXTURE_2D, texture_id );
357 
358  if(image)
359  gluBuild2DMipmaps(
360  GL_TEXTURE_2D,3,width,height,
361  GL_RGB,GL_UNSIGNED_BYTE,
362  image);
363 
364  glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
365  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
366  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
367  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
368  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
369 
370  delete image;
371 
372  glEnable( GL_TEXTURE_2D );
373 
374  }
375 
377  // CAMERA METHODS ////////////////////////////////////////////////////////////////////////////
379 
380  void CENSGraphics::initCamera(CENSCamera &camera, CENSLight light) {
381 
382  m_grCameras.push_back(&camera);
383  camera.initPixels();
384 
385  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB |
386  GLUT_DEPTH | GLUT_STENCIL);
387  glutInitWindowSize(camera.m_cScreenWidth, camera.m_cScreenHeight);
388  glutInitWindowPosition(camera.m_cScreenXGap, camera.m_cScreenYGap);
389  camera.m_cHandle = glutCreateWindow(camera.m_cScreenTitle.c_str());
390  if(m_grHide) {
391  glutHideWindow();
392  glutIconifyWindow();
393  }
394 
395  glMatrixMode(GL_PROJECTION);
396  glLoadIdentity();
397 
398  gluPerspective(
399  camera.m_cFoV,
400  camera.m_cRatio,
401  camera.m_cNear,
402  camera.m_cFar);
403 
404  glMatrixMode(GL_MODELVIEW);
405  glLoadIdentity();
406 
408  // OPENGL 3D ENABLING //////////////////////////////////////
409 
410  glEnable(GL_DEPTH_TEST);
411  glClearDepth(1.0f);
412  glDepthFunc(GL_LESS);
413  glShadeModel(GL_SMOOTH);
414  glLineWidth(1.0);
415  glPointSize(1.0);
416  glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
417  glEnable(GL_CULL_FACE);
418  glClearColor(m_grEnvColor(0),m_grEnvColor(1),m_grEnvColor(2),1.0);
419  glEnable(GL_COLOR_MATERIAL);
420 
422  // LIGHTING ////////////////////////////////////////////////
423 
424  GLfloat m_lAmbient[] = {
425  light.m_lAmbient[0],light.m_lAmbient[1],
426  light.m_lAmbient[2],light.m_lAmbient[3] };
427 
428  GLfloat m_lDiffuse[] = {
429  light.m_lDiffuse[0],light.m_lDiffuse[1],
430  light.m_lDiffuse[2],light.m_lDiffuse[3] };
431 
432  GLfloat m_lSpecular[] = {
433  light.m_lSpecular[0],light.m_lSpecular[1],
434  light.m_lSpecular[2],light.m_lSpecular[3] };
435 
436  GLfloat m_lPosition0[] = {
437  light.m_lPosition0[0],light.m_lPosition0[1],
438  light.m_lPosition0[2],light.m_lPosition0[3] };
439 
440  GLfloat m_lPosition1[] = {
441  light.m_lPosition1[0],light.m_lPosition1[1],
442  light.m_lPosition1[2],light.m_lPosition1[3] };
443 
444  glLightfv(GL_LIGHT0, GL_AMBIENT, m_lAmbient);
445  glLightfv(GL_LIGHT0, GL_DIFFUSE, m_lDiffuse);
446  glLightfv(GL_LIGHT0, GL_SPECULAR, m_lSpecular);
447  glLightfv(GL_LIGHT0, GL_POSITION, m_lPosition0);
448 
449  glLightfv(GL_LIGHT1, GL_AMBIENT, m_lAmbient);
450  glLightfv(GL_LIGHT1, GL_DIFFUSE, m_lDiffuse);
451  glLightfv(GL_LIGHT1, GL_SPECULAR, m_lSpecular);
452  glLightfv(GL_LIGHT1, GL_POSITION, m_lPosition1);
453 
454 
455  glEnable(GL_LIGHTING);
456  glEnable(GL_LIGHT0);
457  //glEnable(GL_LIGHT1);
458 
459  // //////////////////////////////////////////////////////////////
460  //
461  // initTextures();
462 
464 
465  glutDisplayFunc(glutDisplayCallback);
466 
467  }
468 
470 
471  void CENSGraphics::focusCamera( int camera_index ) {
472 
473  CENSCamera &camera = *m_grCameras[camera_index];
474 
475  glutSetWindow(camera.m_cHandle);
476  glMatrixMode(GL_MODELVIEW);
477  glLoadIdentity();
478 
479  }
480 
482 
483  void CENSGraphics::updateCamera( int camera_index) {
484 
485  CENSCamera &camera = *m_grCameras[camera_index];
486 
487  // reset the viewpoint of all implemented cameras
488 
489  if(&camera == &m_grViewCamera) {
490 
491  gluLookAt(
492 
493  camera.m_cDistance*cos(camera.m_cAngle),
494  camera.m_cDistance*sin(camera.m_cAngle),
495  camera.m_cHeight,
496 
497  camera.m_cTarget(0),
498  camera.m_cTarget(1),
499  camera.m_cTarget(2),
500 
501  camera.m_cUp(0),
502  camera.m_cUp(1),
503  camera.m_cUp(2) );
504  }
505  else
506  gluLookAt(
507 
508  camera.m_cOrigin(0),
509  camera.m_cOrigin(1),
510  camera.m_cOrigin(2),
511 
512  camera.m_cTarget(0),
513  camera.m_cTarget(1),
514  camera.m_cTarget(2),
515 
516  camera.m_cUp(0),
517  camera.m_cUp(1),
518  camera.m_cUp(2) );
519 
520  glutPostRedisplay();
521 
522  }
523 
525 
526  const CENSPixelMap &CENSGraphics::getCameraPixelMap( int camera_index ) {
527 
528  CENSCamera &camera = *m_grCameras[camera_index];
529 
530  int data_length =
531  camera.m_cScreenWidth *
532  camera.m_cScreenHeight * 3 ;
533  unsigned char *pixels =
534  new unsigned char[ data_length ];
535 
536  glutSetWindow(camera.m_cHandle);
537  glReadPixels(
538  1,1,camera.m_cScreenWidth,
539  camera.m_cScreenHeight,
540  GL_RGB,GL_BYTE,pixels);
541  glutSwapBuffers();
542  glutSetWindow(m_grViewCamera.m_cHandle);
543 
544  camera.m_cPixmap.getBlob()
545  .updateNoCopy(pixels,data_length);
546 
547 
548  return camera.m_cPixmap;
549 
550  }
551 
553 
554  void CENSGraphics::saveCameraPixelMap( int camera_index, int t, const char * type ) {
555 
556  CENSCamera &camera = *m_grCameras[camera_index];
557 
558  int data_length =
559  camera.m_cScreenWidth *
560  camera.m_cScreenHeight * 4;
561  unsigned int *pixels =
562  new unsigned int[ data_length ];
563 
564  glutSetWindow(camera.m_cHandle);
565  glReadPixels(
566  1,1,camera.m_cScreenWidth,
567  camera.m_cScreenHeight,
568  GL_RGBA,GL_UNSIGNED_BYTE,pixels);
569  glutSwapBuffers();
570  glutSetWindow(m_grViewCamera.m_cHandle);
571 
572  camera.m_cPixmap.getBlob()
573  .updateNoCopy(pixels,data_length);
574 
575  Image image(
576  camera.m_cScreenWidth,
577  camera.m_cScreenHeight,
578  "RGBA",
579  CharPixel,
580  pixels );
581  image.flip();
582 
583  std::stringstream filename;
584  filename
585  << std::setfill('0') << std::setw(3)
586  << camera.m_cHandle
587  << "_"
588  << std::setfill('0') << std::setw(8)
589  << t
590  << "." << type;
591  image.write(filename.str().c_str());
592 
593  }
594 
598 
599  void CENSGraphics::drawPlane( const Vector3f& orig, const Vector3f& vec0, const Vector3f& vec1 )
600  {
601 
602  glPushMatrix();
603 
604  glColor3f(
605  m_grGroundColor(0),
606  m_grGroundColor(1),
607  m_grGroundColor(2));
608 
609  float vecLen = 20.f;
610  Vector3f pt0 = orig + vec0*vecLen;
611  Vector3f pt1 = orig - vec0*vecLen;
612  Vector3f pt2 = orig + vec1*vecLen;
613  Vector3f pt3 = orig - vec1*vecLen;
614 
615  glBegin(GL_QUADS);
616 
617  glVertex3f(pt0.x(),pt0.y(),pt0.z());
618  glVertex3f(pt2.x(),pt2.y(),pt2.z());
619  glVertex3f(pt1.x(),pt1.y(),pt1.z());
620  glVertex3f(pt3.x(),pt3.y(),pt3.z());
621 
622  glEnd();
623 
624  glPopMatrix();
625 
626  }
628 
629 
630  void CENSGraphics::drawBox(
631  const Vector3f& org, const Matrix3f& rotMat, const Vector3f& halfExtent )
632  {
633  // triples of vertice indexes
634  static int indices[12][3] = {
635  {0,1,2},
636  {3,2,1},
637  {4,0,6},
638  {6,0,2},
639  {5,1,4},
640  {4,1,0},
641  {7,3,1},
642  {7,1,5},
643  {5,4,7},
644  {7,4,6},
645  {7,2,3},
646  {7,6,2} };
647 
648 
649  // triples of texture coordinates for each triangle
650  static int tex_coords[12][6] = {
651  {1,1,0,1,1,0},
652  {0,0,1,0,0,1},
653  {1,1,0,1,1,0},
654  {1,0,0,1,0,0},
655  {1,1,0,1,1,0},
656  {1,0,0,1,0,0},
657  {1,1,0,1,0,0},
658  {0,0,1,1,1,0},
659  {1,0,1,1,0,0},
660  {0,0,1,1,1,0},
661  {1,0,0,1,0,0},
662  {0,1,1,1,1,0} };
663 
664 
665  // defining half extents
666 
667  Vector3f dx(rotMat(0,0),rotMat(0,1),rotMat(0,2));
668  Vector3f dy(rotMat(1,0),rotMat(1,1),rotMat(1,2));
669  Vector3f dz(rotMat(2,0),rotMat(2,1),rotMat(2,2));
670 
671  dx *= halfExtent[0];
672  dy *= halfExtent[1];
673  dz *= halfExtent[2];
674 
675  // defining vertices coordinates
676 
677  Vector3f vertices[8]={
678  org+dx+dy+dz,
679  org-dx+dy+dz,
680  org+dx-dy+dz,
681  org-dx-dy+dz,
682  org+dx+dy-dz,
683  org-dx+dy-dz,
684  org+dx-dy-dz,
685  org-dx-dy-dz };
686 
687  // rendering
688 
689  glPushMatrix();
690 
691  glColor3f(
692  m_grBoxColor(0),
693  m_grBoxColor(1),
694  m_grBoxColor(2));
695 
696  glBegin(GL_TRIANGLES);
697 
698  int si=12;
699  for (int i=0;i<si;i++)
700  {
701 
702  const Vector3f& v1 = vertices[indices[i][0]];;
703  const Vector3f& v2 = vertices[indices[i][1]];
704  const Vector3f& v3 = vertices[indices[i][2]];
705 
706  // enable reflection on the triangle
707 
708  Vector3f normal = (v3-v1).cross(v2-v1);
709  normal.normalize ();
710  glNormal3f(normal.x(),normal.y(),normal.z());
711 
712  // draws a triangle
713 
714  glTexCoord2f(tex_coords[i][0],tex_coords[i][1]);
715  glVertex3f (v1.x(), v1.y(), v1.z());
716  glTexCoord2f(tex_coords[i][2],tex_coords[i][3]);
717  glVertex3f (v2.x(), v2.y(), v2.z());
718  glTexCoord2f(tex_coords[i][4],tex_coords[i][5]);
719  glVertex3f (v3.x(), v3.y(), v3.z());
720 
721  }
722  glEnd();
723 
724  glPopMatrix();
725 
726  }
727 
729 
730  void CENSGraphics::drawSphere( const Vector3f& org, const Matrix3f& rot, float radius ) {
731 
732  int i, j;
733  int lats = 20;
734  int longs = 20;
735 
736  glPushMatrix();
737 
738  glColor3f(
739  m_grSphereColor(0),
740  m_grSphereColor(1),
741  m_grSphereColor(2));
742 
743  float xtag = 1/(float)lats;
744  float ytag = 1/(float)longs;
745 
746  for(i = 0; i <= lats; i++) {
747 
748  float lat0 = M_PI * (-float(0.5) + (i - 1) * xtag);
749  float z0 = radius*sin(lat0);
750  float zr0 = radius*cos(lat0);
751 
752  float lat1 = M_PI * (-float(0.5) + i * xtag);
753  float z1 = radius*sin(lat1);
754  float zr1 = radius*cos(lat1);
755 
756  glBegin(GL_QUAD_STRIP);
757  for(j = 0; j <= longs; j++) {
758  float lng = 2 * M_PI * (j - 1) * ytag;
759  float x = cos(lng);
760  float y = sin(lng);
761 
762  Vector3f ver1(x * zr1, y * zr1, z1);
763  Vector3f ver2(x * zr0, y * zr0, z0);
764 
765  ver1 = org + rot*ver1;
766  ver2 = org + rot*ver2;
767 
768  glNormal3f(ver1.x(),ver1.y(),ver1.z());
769  glTexCoord2f(xtag*( i ), ytag*( j ));
770  glVertex3f(ver1.x(),ver1.y(),ver1.z());
771 
772  glNormal3f(ver2.x(),ver2.y(),ver2.z());
773  glTexCoord2f(xtag*( i - 1 ), ytag*( j ));
774  glVertex3f(ver2.x(),ver2.y(),ver2.z());
775 
776  }
777  glEnd();
778 
779  }
780 
781  glPopMatrix();
782 
783  }
784 
786 
787  void CENSGraphics::drawLine(const Vector3f &v1,const Vector3f &v2, const Vector3f color) {
788 
789  glPushMatrix();
790 
791  glColor3f( color.x(), color.y(),color.z());
792 
793  glBegin(GL_LINES);
794 
795  // draws a line
796 
797  glVertex3f (v1.x(), v1.y(), v1.z());
798  glVertex3f (v2.x(), v2.y(), v2.z());
799 
800  glEnd();
801 
802  glPopMatrix();
803 
804  }
805 
806 
807 
809 
810  void CENSGraphics::drawTriangle(const Vector3f &v1,const Vector3f &v2,const Vector3f &v3, const Vector3f n) {
811 
812  glPushMatrix();
813 
814  glColor3f(
815  .4,
816  .6,
817  .2);
818 
819  glBegin(GL_TRIANGLES);
820 
821  // enable reflection on the triangle
822 
823  glNormal3f(n.x(),n.y(),n.z());
824 
825  // draws a triangle
826 
827  glVertex3f (v1.x(), v1.y(), v1.z());
828  glVertex3f (v2.x(), v2.y(), v2.z());
829  glVertex3f (v3.x(), v3.y(), v3.z());
830 
831 
832  glEnd();
833 
834  glPopMatrix();
835 
836  }
837 
838 
840 
841  void CENSGraphics::drawConvex( const Vector3f& org, const Matrix3f& rot,
842  const std::vector<Vector3f> &vtx, const unsigned int *idx, int nvtxs, int nidxs, int ntrns ) {
843 
844  if (ntrns > 0)
845  {
846  int index = 0;
847 
848  glPushMatrix();
849 
850  glColor3f(
851  m_grCapsuleColor(0),
852  m_grCapsuleColor(1),
853  m_grCapsuleColor(2));
854 
855  glBegin(GL_TRIANGLES);
856 
857  for (int i = 0; i < ntrns; i++)
858  {
859 
860  int i1 = index++;
861  int i2 = index++;
862  int i3 = index++;
863 
864  assert(i1 < nidxs &&
865  i2 < nidxs
866  && i3 < nidxs);
867 
868  int index1 = idx[i1];
869  int index2 = idx[i2];
870  int index3 = idx[i3];
871  assert(index1 < nvtxs &&
872  index2 < nvtxs &&
873  index3 < nvtxs );
874 
875  Vector3f v1 = org + rot*vtx[index1];
876  Vector3f v2 = org + rot*vtx[index2];
877  Vector3f v3 = org + rot*vtx[index3];
878 
879  Vector3f axis = (v3-v1).cross(v2-v1);
880  axis.normalize ();
881 
882  glNormal3f(axis.x(),axis.y(),axis.z());
883 
884  float div=10.;
885 
886  glTexCoord2f(v1.x()/div,v1.z()/div);
887  glVertex3f (v1.x(), v1.y(), v1.z());
888  glTexCoord2f(v2.x()/div,v2.z()/div);
889  glVertex3f (v2.x(), v2.y(), v2.z());
890  glTexCoord2f(v3.x()/div,v3.z()/div);
891  glVertex3f (v3.x(), v3.y(), v3.z());
892 
893  }
894  glEnd();
895 
896  glPopMatrix();
897 
898  }
899 
900  }
901 
903 
904  void CENSGraphics::drawAxis(const Vector3f &origin, const Matrix3f &rotation, float length) {
905 
906  Vector3f o = origin;
907  Vector3f x = origin + rotation*Vector3f(length,0,0);
908  Vector3f y = origin + rotation*Vector3f(0,length,0);
909  Vector3f z = origin + rotation*Vector3f(0,0,length);
910 
911  glPushMatrix();
912 
913  glLineWidth(2.0);
914 
915  glBegin(GL_LINES);
916 
917  glColor3f(1,0,0);
918  glVertex3f (o.x(), o.y(), o.z());
919  glVertex3f (x.x(), x.y(), x.z());
920 
921  glColor3f(0,1,0);
922  glVertex3f (o.x(), o.y(), o.z());
923  glVertex3f (y.x(), y.y(), y.z());
924 
925  glColor3f(0,0,1);
926  glVertex3f (o.x(), o.y(), o.z());
927  glVertex3f (z.x(), z.y(), z.z());
928 
929  glEnd();
930 
931  glLineWidth(1.0);
932 
933  glPopMatrix();
934 
935  }
936 
940 
941  void CENSGraphics::stepLeft()
942  {
943  m_grViewCamera.m_cAngle -= m_grViewCamera.m_cMov;
944  }
945 
947 
948  void CENSGraphics::stepRight()
949  {
950  m_grViewCamera.m_cAngle += m_grViewCamera.m_cMov;
951  }
952 
954 
955  void CENSGraphics::stepFront()
956  {
957  m_grViewCamera.m_cHeight += 4*m_grViewCamera.m_cGap;
958  }
960 
961  void CENSGraphics::stepBack()
962  {
963 
964  m_grViewCamera.m_cHeight -= 4*m_grViewCamera.m_cGap;
965  }
966 
968 
969  void CENSGraphics::zoomIn()
970  {
971  m_grViewCamera.m_cDistance -= 4*m_grViewCamera.m_cGap;
972  }
974 
975  void CENSGraphics::zoomOut()
976  {
977  m_grViewCamera.m_cDistance += 4*m_grViewCamera.m_cGap;
978  }
979 
981 
982  void CENSGraphics::toggleTexture()
983  {
984  if(m_grTextureEnabled) {
985 
986  for(unsigned int i=0; i<m_grEyeCameras.size(); i++) {
987  glutSetWindow(m_grEyeCameras[i]->m_cHandle);
988  glDisable( GL_TEXTURE_2D );
989  }
990 
991  // reset the main viewpoint
992  glutSetWindow(m_grViewCamera.m_cHandle);
993  glDisable( GL_TEXTURE_2D );
994 
995  m_grTextureEnabled = false;
996 
997  } else {
998  for(unsigned int i=0; i<m_grEyeCameras.size(); i++) {
999  glutSetWindow(m_grEyeCameras[i]->m_cHandle);
1000  glEnable( GL_TEXTURE_2D );
1001  }
1002 
1003  // reset the main viewpoint
1004  glutSetWindow(m_grViewCamera.m_cHandle);
1005  glEnable( GL_TEXTURE_2D );
1006 
1007  m_grTextureEnabled = true;
1008  }
1009  }
1010 
1012 
1013  void CENSGraphics::toggleAxis()
1014  {
1015  if(m_grAxisEnabled) {
1016  m_grAxisEnabled = false;
1017  } else {
1018  m_grAxisEnabled = true;
1019  }
1020  }
1021 
1023 
1024  void CENSGraphics::toggleObjectAxes()
1025  {
1026  if(m_grObjectAxesEnabled) {
1027  m_grObjectAxesEnabled = false;
1028  } else {
1029  m_grObjectAxesEnabled = true;
1030  }
1031  }
1032 
1034 
1035  void CENSGraphics::toggleJointAxes()
1036  {
1037  if(m_grJointAxesEnabled) {
1038  m_grJointAxesEnabled = false;
1039  } else {
1040  m_grJointAxesEnabled = true;
1041  }
1042  }
1043 
1045 
1046  void CENSGraphics::toggleCameraAxis()
1047  {
1048  if(m_grCameraAxisEnabled) {
1049  m_grCameraAxisEnabled = false;
1050  } else {
1051  m_grCameraAxisEnabled = true;
1052  }
1053  }
1055 
1056  void CENSGraphics::toggleStop()
1057  {
1058  if(m_grSimEnabled) {
1059  m_grSimEnabled = false;
1060  } else {
1061  m_grSimEnabled = true;
1062  }
1063  }
1064 
1066 
1067 }
Computational Embodied Neuroscience Simulator library.
Definition: cens_engine.cpp:29
A C++ wrapper for OpenGL.
Definition: cens_graphics.h:77
virtual void keyboard(unsigned char key, int x, int y)
CENSGraphics * cens_graphics
void glutKeyboardCallback(unsigned char key, int x, int y)
Light settings of the scene.
virtual void display()
Use a byte vector as a matrix.
Definition: cens_pixelmap.h:40
void glutDisplayCallback(void)
void glutTimerCallback(int value)
parameters of a camera
virtual void step(int ts)