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 );
317 public slots:
319  void wireframe( bool b );
321  void showSkyGround( bool b );
323  void showObjects( bool b );
325  void showJoints( bool b );
327  void showAABBs( bool b );
329  void showContacts( bool b );
331  void showForces( bool b );
333  void showLocalAxes( bool b );
335  void showLabels( bool b );
337  void showTimeAndStep( bool b );
339  void contextMenu( const QPoint& );
340 private slots:
342  void slotAddObject( WObject* );
344  void slotRemoveObject( WObject* );
346  void onWorldResize();
347 protected:
349  virtual void init();
351  virtual void draw();
353  virtual void drawWithNames();
355  virtual void postSelection(const QPoint& point);
357  virtual void keyPressEvent(QKeyEvent *e);
358 
374  virtual void resourceChanged(QString name, ResourceChangeType changeType);
375 
376 private:
385  virtual void customEvent(QEvent* event);
386 
388  int currentSelected;
389 
391  void drawDOF( PhyDOF* dof, bool );
393  void drawKineChains();
394 
396  bool wiref;
398  bool showskygroundbox;
400  bool showobjs;
402  bool showjoints;
404  bool showaabbs;
406  bool showcontacts;
408  bool showforces;
410  bool showlocalaxes;
412  bool showlabels;
414  bool showtimeandstep;
415 };
416 
417 } // end namespace farsa
418 
419 #endif