renderworld.h
1 /********************************************************************************
2  * WorldSim -- library for robot simulations *
3  * Copyright (C) 2008-2011 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #ifndef RENDERWORLD_H
21 #define RENDERWORLD_H
22 
23 #include "worldsimconfig.h"
24 #include "world.h"
25 #include "resourcesuser.h"
26 #include <QObject>
27 #include "qglviewer/qglviewer.h"
28 #include <QWidget>
29 #include <QVector>
30 #include <QMap>
31 #include <QString>
32 #include <QColor>
33 #include <QMutex>
34 
35 namespace farsa {
36 
37 class RenderWorld;
38 class PhyDOF;
39 class RenderWObjectContainer;
40 
52 class FARSA_WSIM_API RenderWObject : public QObject {
53  Q_OBJECT
54 public:
55  RenderWObject( WObject* o, RenderWObjectContainer* container ) : obj(o) {
56  this->contain = container;
57  };
58  virtual ~RenderWObject() { };
59  virtual void render( QGLContext* gw ) = 0;
63  virtual void renderAABB( RenderWorld* gw ) {
64  UNUSED_PARAM( gw );
65  };
75  virtual void calculateAABB( wVector& minPoint, wVector& maxPoint, const wMatrix tm ) {
76  UNUSED_PARAM( tm );
77  //--- return the following points if in your case there is no meaningful AABB
78  minPoint = wVector(0, 0, 0);
79  maxPoint = wVector(0, 0, 0);
80  };
89  virtual void calculateOBB( wVector& dimension, wVector& minPoint, wVector& maxPoint ) {
90  UNUSED_PARAM( dimension );
91  //--- return the following points if in your case there is no meaningful OBB
92  minPoint = wVector(0,0,0);
93  maxPoint = wVector(0,0,0);
94  };
97  return obj;
98  };
101  return contain;
102  };
113  virtual void objectAlreadyDestroyed()
114  {
115  }
122  void drawLabel();
123 protected:
124  WObject* obj;
125  RenderWObjectContainer* contain;
126 };
127 
138 class FARSA_WSIM_TEMPLATE WAbstractCreator {
139 public:
140  virtual ~WAbstractCreator() { };
142  virtual RenderWObject* create( WObject*, RenderWObjectContainer* container ) const = 0;
143 };
144 
147 template<class T>
148 class FARSA_WSIM_TEMPLATE WCreator : public WAbstractCreator {
150  virtual RenderWObject* create( WObject* wobj, RenderWObjectContainer* container ) const {
151  return ( new T(wobj, container) );
152  };
153 };
154 
168 class FARSA_WSIM_API RenderWObjectContainer : public ConcurrentResourcesUser {
169 public:
171  RenderWObjectContainer( QString wResName = "world" );
173  virtual ~RenderWObjectContainer();
175  const QVector<RenderWObject*> graphics() {
176  return graphs;
177  };
180  void setWorld( World* newworld );
182  QImage textureImage( QString texture ) {
183  return (*textmap)[texture];
184  };
192  static bool addTextureImage( QString filename, QString texturename );
194  RenderWObject* operator[]( const WObject* );
199  static RenderWObject* createRenderWObjectFor( const WObject*, RenderWObjectContainer* );
203  template<class renderwobject>
204  static void registerRenderWObjectFor( QString classname ) {
205  initFactory();
206  (*fac)[classname] = new WCreator<renderwobject>();
207  };
209  void applyTexture( QGLContext* gw, QString texts );
211  void setupColorTexture( QGLContext*, RenderWObject* obj );
213  void drawSkyGroundBox( QGLContext* );
215  static void drawSphere( wVector pos, real radius );
217  static void drawCylinder( wVector axis, wVector centre, float len, float radius, QColor c = Qt::green );
219  static void drawCylinder( wVector start, wVector end, float radius, QColor c = Qt::green );
221  static void drawCylinder( const wMatrix& mat, float len, float radius, QColor c = Qt::green );
223  static void drawCone( const wMatrix& mat, float len, float radius, QColor c = Qt::green );
225  static void drawArrow( const wVector& direction, const wVector& start, float radius, float tipRadius, float tipLength, QColor c = Qt::green );
227  static void drawWireBox( wVector dims, wMatrix matrix );
229  static void drawWireBox( wVector min, wVector max, const wMatrix& tm );
231  static void drawWireBox( wVector min, wVector max );
233  static void drawTorus( real outRad, real innRad, const wMatrix& mat, real angle = 2.0*PI_GRECO, QColor c=Qt::red );
235  static void drawTorus( wVector axis, wVector centre, real outRad, real innRad, real angle = 2.0*PI_GRECO );
236 protected:
239  return worldv;
240  };
241 
243  void addObject( WObject* );
245  void removeObject( WObject* );
246 
262  virtual void resourceChanged(QString name, ResourceChangeType changeType);
263 
265  QMutex mutex;
266 private:
269  static void initializeTextmap();
271  const QString worldResourceName;
273  World* worldv;
275  QVector<RenderWObject*> graphs;
277  static QMap<QString, WAbstractCreator*>* fac;
279  static bool facInited;
281  static void initFactory();
283  static QMap<QString, QImage>* textmap;
289  static unsigned int textmapRefCounter;
291  static QMutex textmapMutex;
293  QMap<QString, GLuint> textGLId;
295  QImage skyb[6];
296 };
297 
308 class FARSA_WSIM_API RenderWorld : public QGLViewer, public RenderWObjectContainer {
309  Q_OBJECT
310 public:
312  RenderWorld( QWidget* parent = NULL, QString wResName = "world" );
314  virtual ~RenderWorld();
316  void drawArrow( const wVector& from, const wVector& to, float radius=-1, int nbSubdivisions=12, QColor c = Qt::red );
318  bool viewerStateRestoredFromFile() const { return m_viewerStateRestoredFromFile; }
319 public slots:
321  void wireframe( bool b );
323  void showSkyGround( bool b );
325  void showObjects( bool b );
327  void showJoints( bool b );
329  void showAABBs( bool b );
331  void showContacts( bool b );
333  void showForces( bool b );
335  void showLocalAxes( bool b );
337  void showLabels( bool b );
339  void showTimeAndStep( bool b );
341  void contextMenu( const QPoint& );
342 private slots:
344  void slotAddObject( WObject* );
346  void slotRemoveObject( WObject* );
348  void onWorldResize();
349 protected:
351  virtual void init();
353  virtual void draw();
355  virtual void drawWithNames();
357  virtual void postSelection(const QPoint& point);
359  virtual void keyPressEvent(QKeyEvent *e);
360 
376  virtual void resourceChanged(QString name, ResourceChangeType changeType);
377 
378 private:
387  virtual void customEvent(QEvent* event);
388 
390  int currentSelected;
391 
393  void drawDOF( PhyDOF* dof, bool );
395  void drawKineChains();
396 
398  bool wiref;
400  bool showskygroundbox;
402  bool showobjs;
404  bool showjoints;
406  bool showaabbs;
408  bool showcontacts;
410  bool showforces;
412  bool showlocalaxes;
414  bool showlabels;
416  bool showtimeandstep;
418  bool m_viewerStateRestoredFromFile;
419 };
420 
421 } // end namespace farsa
422 
423 #endif
World * world()
return the World.
Definition: renderworld.h:238
WObject * object()
return the WObject that this RenderWObject display on OpenGL
Definition: renderworld.h:96
World's Object class.
Definition: wobject.h:39
RenderWorld class.
Definition: renderworld.h:308
QMutex mutex
The mutex protecting accesses to members of this class.
Definition: renderworld.h:265
QImage textureImage(QString texture)
Return the corresponding QImage for the texture name passed.
Definition: renderworld.h:182
bool viewerStateRestoredFromFile() const
Returns true if viewer state has been restored from file.
Definition: renderworld.h:318
#define UNUSED_PARAM(a)
FARSA_UTIL_TEMPLATE const T max(const T &t1, const U &t2)
Abstract Creator of RenderWObject objects.
Definition: renderworld.h:138
World class.
Definition: world.h:223
virtual void objectAlreadyDestroyed()
This function is called before the destruction if the WObject we are supposed to draw has already bee...
Definition: renderworld.h:113
virtual void calculateAABB(wVector &minPoint, wVector &maxPoint, const wMatrix tm)
Returns the min and max points of the Axis-Aligned Bounding Box (AABB)
Definition: renderworld.h:75
virtual void renderAABB(RenderWorld *gw)
The function rendering the Axis-Aligned Bounding Box (AABB)
Definition: renderworld.h:63
Template facility to create WAbstractCreator specialization.
Definition: renderworld.h:148
wMatrix class
Definition: wmatrix.h:48
static void registerRenderWObjectFor(QString classname)
template method for register new RenderWObject classes for new WObject
Definition: renderworld.h:204
A versatile 3D OpenGL viewer based on QGLWidget.
Definition: qglviewer.h:62
float real
const QVector< RenderWObject * > graphics()
return all RenderWObject presents
Definition: renderworld.h:175
RenderWObject class.
Definition: renderworld.h:52
RenderWObjectContainer * container()
return the Container on which this object is, or the OpenGL windows where it is displayed ...
Definition: renderworld.h:100
FARSA_UTIL_TEMPLATE const T min(const T &t1, const U &t2)
virtual void calculateOBB(wVector &dimension, wVector &minPoint, wVector &maxPoint)
Returns the dimension of the Oriented Bounding Box (OBB) in the object local frame.
Definition: renderworld.h:89
PhyDOF class.
Definition: phyjoint.h:50
RenderWObjectContainer class.
Definition: renderworld.h:168