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  }
116 protected:
117  WObject* obj;
118  RenderWObjectContainer* contain;
119 };
120 
131 class FARSA_WSIM_TEMPLATE WAbstractCreator {
132 public:
133  virtual ~WAbstractCreator() { };
135  virtual RenderWObject* create( WObject*, RenderWObjectContainer* container ) const = 0;
136 };
137 
140 template<class T>
141 class FARSA_WSIM_TEMPLATE WCreator : public WAbstractCreator {
143  virtual RenderWObject* create( WObject* wobj, RenderWObjectContainer* container ) const {
144  return ( new T(wobj, container) );
145  };
146 };
147 
161 class FARSA_WSIM_API RenderWObjectContainer : public ConcurrentResourcesUser {
162 public:
164  RenderWObjectContainer( QString wResName = "world" );
166  virtual ~RenderWObjectContainer();
168  const QVector<RenderWObject*> graphics() {
169  return graphs;
170  };
173  void setWorld( World* newworld );
175  QImage textureImage( QString texture ) {
176  return (*textmap)[texture];
177  };
185  bool addTextureImage( QString filename, QString texturename );
187  RenderWObject* operator[]( const WObject* );
192  static RenderWObject* createRenderWObjectFor( const WObject*, RenderWObjectContainer* );
196  template<class renderwobject>
197  static void registerRenderWObjectFor( QString classname ) {
198  initFactory();
199  (*fac)[classname] = new WCreator<renderwobject>();
200  };
202  void applyTexture( QGLContext* gw, QString texts );
204  void setupColorTexture( QGLContext*, RenderWObject* obj );
206  void drawSkyGroundBox( QGLContext* );
208  static void drawSphere( wVector pos, real radius );
210  static void drawCylinder( wVector axis, wVector centre, float len, float radius, QColor c = Qt::green );
212  static void drawCylinder( wVector start, wVector end, float radius, QColor c = Qt::green );
214  static void drawCylinder( const wMatrix& mat, float len, float radius, QColor c = Qt::green );
216  static void drawWireBox( wVector dims, wMatrix matrix );
218  static void drawWireBox( wVector min, wVector max, const wMatrix& tm );
220  static void drawWireBox( wVector min, wVector max );
222  static void drawTorus( real outRad, real innRad, const wMatrix& mat, real angle = 2.0*PI_GRECO, QColor c=Qt::red );
224  static void drawTorus( wVector axis, wVector centre, real outRad, real innRad, real angle = 2.0*PI_GRECO );
225 protected:
228  return worldv;
229  };
230 
232  void addObject( WObject* );
234  void removeObject( WObject* );
235 
251  virtual void resourceChanged(QString name, ResourceChangeType changeType);
252 
254  QMutex mutex;
255 private:
257  const QString worldResourceName;
259  World* worldv;
261  QVector<RenderWObject*> graphs;
263  static QMap<QString, WAbstractCreator*>* fac;
265  static bool facInited;
267  static void initFactory();
269  static QMap<QString, QImage> *textmap;
275  static unsigned int textmapRefCounter;
277  QMap<QString, GLuint> textGLId;
279  QImage skyb[6];
280 };
281 
292 class FARSA_WSIM_API RenderWorld : public QGLViewer, public RenderWObjectContainer {
293  Q_OBJECT
294 public:
296  RenderWorld( QWidget* parent = NULL, QString wResName = "world" );
298  virtual ~RenderWorld();
300  void drawArrow( const wVector& from, const wVector& to, float radius=-1, int nbSubdivisions=12, QColor c = Qt::red );
301 public slots:
303  void wireframe( bool b );
305  void showSkyGround( bool b );
307  void showObjects( bool b );
309  void showJoints( bool b );
311  void showAABBs( bool b );
313  void showContacts( bool b );
315  void showForces( bool b );
317  void contextMenu( const QPoint& );
318 private slots:
320  void slotAddObject( WObject* );
322  void slotRemoveObject( WObject* );
324  void onWorldResize();
325 protected:
327  virtual void init();
329  virtual void draw();
331  virtual void drawWithNames();
333  virtual void postSelection(const QPoint& point);
335  virtual void keyPressEvent(QKeyEvent *e);
336 
352  virtual void resourceChanged(QString name, ResourceChangeType changeType);
353 
354 private:
363  virtual void customEvent(QEvent* event);
364 
366  int currentSelected;
367 
369  void drawDOF( PhyDOF* dof, bool );
371  void drawKineChains();
372 
374  bool wiref;
376  bool showskygroundbox;
378  bool showobjs;
380  bool showjoints;
382  bool showaabbs;
384  bool showcontacts;
386  bool showforces;
387 };
388 
389 } // end namespace farsa
390 
391 #endif