Computational Embodied Neuroscience Simulator  1.1
3D simulation library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
cens_graphics.h
Go to the documentation of this file.
1 // Computational Embodied Neuroscience Simulator (CENS) Library
2 // Copyright (c) 2010 Francesco Mannella
3 //
4 // cens_graphics.h
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 #ifndef CENS_GRAPHICS_H
23 #define CENS_GRAPHICS_H
24 #include <vector>
25 
26 // linear algebra
27 #include <Eigen/Core>
28 #include <Eigen/Geometry>
29 using namespace Eigen;
30 
31 
32 // OpenGL
33 #include <GL/glut.h>
34 #include <GL/freeglut.h>
35 
36 //ImageMagick
37 #include <Magick++.h>
38 
39 #include "cens_graphics_shape.h"
40 #include "cens_pixelmap.h"
41 #include "cens_parameter_manager.h"
42 
43 static const char* graph_params_filename = "graphics_parameters";
44 
45 namespace cens
46 {
47 
50 
77  class CENSGraphics
78  {
79 
80  public:
81 
83  // CONSTRUCTORS /////////////////////////////////////////////////
85 
86  CENSGraphics():m_grParameterManager(graph_params_filename)
87  {
88 
89  m_grTextureEnabled=false;
90  m_grTextureInitialized=false;
91  m_grAxisEnabled=false;
92  m_grSimEnabled=false;
93  m_grCameraAxisEnabled=false;
94  m_grObjectAxesEnabled=false;
95  m_grJointAxesEnabled=false;
96 
97  };
98 
99  ~CENSGraphics();
100 
102  // PUBLIC METHODS ///////////////////////////////////////////////
104 
106  virtual void initCENSGraphics( int argc, char ** argv );
107 
109  virtual void loop();
110 
112  virtual void stepToStepLoop();
113 
115  virtual void step(
116  int ts );
117 
119  virtual void display();
120 
122  virtual bool isIdle();
123 
125  virtual void quit();
126 
128  virtual void keyboard( unsigned char key, int x, int y );
129 
130  protected:
131 
133  // PROTECTED STRUCTS ////////////////////////////////////////////
135 
139  struct CENSCamera
140  {
141 
143 
144  CENSCamera(const CENSCamera& copy)
145  {
146  m_cHandle = copy.m_cHandle;
147  m_cScreenWidth = copy.m_cScreenWidth;
148  m_cScreenHeight = copy.m_cScreenHeight;
149  m_cScreenXGap = copy.m_cScreenXGap;
150  m_cScreenYGap = copy.m_cScreenYGap;
151  m_cScreenTitle = copy.m_cScreenTitle;
152  m_cFoV = copy.m_cFoV;
153  m_cRatio = copy.m_cRatio;
154  m_cNear = copy.m_cNear;
155  m_cFar = copy.m_cFar;
156  m_cAngle = copy.m_cAngle;
157  m_cDistance = copy.m_cDistance;
158  m_cHeight = copy.m_cHeight;
159  m_cOrigin = copy.m_cOrigin;
160  m_cTarget = copy.m_cTarget;
161  m_cUp = copy.m_cUp;
162  m_cMov = copy.m_cMov;
163  m_cGap = copy.m_cGap;
164  }
165 
167  {
168 
169  }
170 
178  void initPixels() {
179 
180  int data_length =
181  m_cScreenWidth *
182  m_cScreenHeight * 3 ;
183  unsigned char *pixels =
184  new unsigned char[ data_length ];
185 
186  m_cPixmap=CENSPixelMap();
187  m_cPixmap.setData(pixels,
188  m_cScreenWidth,m_cScreenHeight);
189 
190  delete pixels;
191 
192  }
193 
215  std::string m_cScreenTitle;
217  float m_cFoV;
219  float m_cRatio;
221  float m_cNear;
223  float m_cFar;
225  float m_cAngle;
227  float m_cDistance;
229  float m_cHeight;
231  Vector3f m_cOrigin;
233  Vector3f m_cTarget;
235  Vector3f m_cUp;
241  float m_cMov;
247  float m_cGap;
249  unsigned char *m_cPixels;
257 
258  };
259 
263  struct CENSLight
264  {
265 
267  Vector4f m_lAmbient;
269  Vector4f m_lDiffuse;
271  Vector4f m_lSpecular;
273  Vector4f m_lPosition0;
275  Vector4f m_lPosition1;
276 
277  };
278 
280  // PROTECTED METHODS ////////////////////////////////////////////
282 
283  // SETTINGS /////////////////////////////////////////////////////
284 
286  void beginRendering();
287 
289  void endRendering();
290 
292  void initTextures();
293 
295  void initTexture(const GLubyte *source = 0, int width = 0, int height = 0);
296 
297  public:
298 
299  // CAMERA ///////////////////////////////////////////////////////
300 
302  void initCamera(CENSCamera &camera, CENSLight light);
303 
305  void focusCamera(int camera_index);
306 
308  void updateCamera(int camera_index);
309 
311  const CENSPixelMap &getCameraPixelMap(int camera_index );
312 
314  void saveCameraPixelMap(int camera_index, int t, const char * type="jpg" );
315 
316  // RENDERING ////////////////////////////////////////////////////
317 
319  void drawLine(
320  const Vector3f &v1,
321  const Vector3f &v2,
322  const Vector3f color);
323 
327  void drawPlane(
328  const Vector3f& orig,
329  const Vector3f& vec0,
330  const Vector3f& vec1 );
331 
333  void drawBox(
334  const Vector3f& orig,
335  const Matrix3f& rotMat,
336  const Vector3f& halfExtent );
337 
339  void drawSphere(
340  const Vector3f& orig,
341  const Matrix3f& rot,
342  float radius );
343 
345  void drawConvex(
346  const Vector3f& orig,
347  const Matrix3f& rot ,
348  const std::vector<Vector3f> &vtx,
349  const unsigned int *idx,
350  int nvtxs,
351  int nidxs,
352  int ntrns );
353 
355  void drawAxis(
356  const Vector3f &origin,
357  const Matrix3f &rotation,
358  float length );
359 
361  inline void drawVector( const Vector3f& v )
362  {
363  glVertex3d(v[0], v[1], v[2]);
364  }
365 
367  void drawTriangle(
368  const Vector3f &v1,
369  const Vector3f &v2,
370  const Vector3f &v3,
371  const Vector3f n );
372 
373  // KEYBOARD /////////////////////////////////////////////////////
374 
375  void stepLeft();
376  void stepRight();
377  void stepFront();
378  void stepBack();
379  void zoomIn();
380  void zoomOut();
381  void toggleStop();
382  void toggleTexture();
383  void toggleAxis();
384  void toggleObjectAxes();
385  void toggleJointAxes();
386  void toggleCameraAxis();
388  protected:
389 
391  // PROTECTED MEMBERS ////////////////////////////////////////////
393 
397  int m_grArgc;
399  char ** m_grArgv;
400 
402  Vector3f m_grEnvColor;
404  Vector3f m_grGroundColor;
406  Vector3f m_grObjectColor;
408  Vector3f m_grBoxColor;
412  Vector3f m_grSphereColor;
414  Vector3f m_grSoftColor;
417 
422 
424  bool m_grHide;
427 
438 
444  std::vector< CENSCamera *> m_grEyeCameras;
446  std::vector< CENSCamera *> m_grCameras;
448  std::vector< CENSGraphicsShape *> m_grShapes;
449 
452  };
453 
454 
455 
457  extern CENSGraphics* cens_graphics;
458 
459 }
460 
461 #endif // CENS_GRAPHICS_H)
Computational Embodied Neuroscience Simulator library.
Definition: cens_engine.cpp:29
A C++ wrapper for OpenGL.
Definition: cens_graphics.h:77
std::vector< CENSCamera * > m_grCameras
Vector3f m_grObjectColor
void drawVector(const Vector3f &v)
Vector3f m_grCompoundColor
CENSGraphics * cens_graphics
CENSCamera m_grViewCamera
std::vector< CENSGraphicsShape * > m_grShapes
Vector3f m_grGroundColor
Vector3f m_grSphereColor
Light settings of the scene.
Vector3f m_grCapsuleColor
CENSParameterManager m_grParameterManager
Manager of parameters' initializarion.
Use a byte vector as a matrix.
Definition: cens_pixelmap.h:40
static const char * graph_params_filename
Definition: cens_graphics.h:43
parameters of a camera
std::vector< CENSCamera * > m_grEyeCameras
CENSCamera(const CENSCamera &copy)