worldsim/include/renderworld.h

00001 /********************************************************************************
00002  *  WorldSim -- library for robot simulations                                   *
00003  *  Copyright (C) 2008-2011 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #ifndef RENDERWORLD_H
00021 #define RENDERWORLD_H
00022 
00023 #include "worldsimconfig.h"
00024 #include "world.h"
00025 #include "resourcesuser.h"
00026 #include <QObject>
00027 #include "qglviewer/qglviewer.h"
00028 #include <QWidget>
00029 #include <QVector>
00030 #include <QMap>
00031 #include <QString>
00032 #include <QColor>
00033 #include <QMutex>
00034 
00035 namespace farsa {
00036 
00037 class RenderWorld;
00038 class PhyDOF;
00039 class RenderWObjectContainer;
00040 
00052 class FARSA_WSIM_API RenderWObject : public QObject {
00053     Q_OBJECT
00054 public:
00055     RenderWObject( WObject* o, RenderWObjectContainer* container ) : obj(o) {
00056         this->contain = container;
00057     };
00058     virtual ~RenderWObject() { };
00059     virtual void render( QGLContext* gw ) = 0;
00063     virtual void renderAABB( RenderWorld* gw ) {
00064         UNUSED_PARAM( gw );
00065     };
00075     virtual void calculateAABB( wVector& minPoint, wVector& maxPoint, const wMatrix tm ) {
00076         UNUSED_PARAM( tm );
00077         //--- return the following points if in your case there is no meaningful AABB
00078         minPoint = wVector(0, 0, 0);
00079         maxPoint = wVector(0, 0, 0);
00080     };
00089     virtual void calculateOBB( wVector& dimension, wVector& minPoint, wVector& maxPoint ) {
00090         UNUSED_PARAM( dimension );
00091         //--- return the following points if in your case there is no meaningful OBB
00092         minPoint = wVector(0,0,0);
00093         maxPoint = wVector(0,0,0);
00094     };
00096     WObject* object() {
00097         return obj;
00098     };
00100     RenderWObjectContainer* container() {
00101         return contain;
00102     };
00113     virtual void objectAlreadyDestroyed()
00114     {
00115     }
00116 protected:
00117     WObject* obj;
00118     RenderWObjectContainer* contain;
00119 };
00120 
00131 class FARSA_WSIM_TEMPLATE WAbstractCreator {
00132 public:
00133     virtual ~WAbstractCreator() { };
00135     virtual RenderWObject* create( WObject*, RenderWObjectContainer* container ) const = 0;
00136 };
00137 
00140 template<class T>
00141 class FARSA_WSIM_TEMPLATE WCreator : public WAbstractCreator {
00143     virtual RenderWObject* create( WObject* wobj, RenderWObjectContainer* container ) const {
00144         return ( new T(wobj, container) );
00145     };
00146 };
00147 
00161 class FARSA_WSIM_API RenderWObjectContainer : public ConcurrentResourcesUser {
00162 public:
00164     RenderWObjectContainer( QString wResName = "world" );
00166     virtual ~RenderWObjectContainer();
00168     const QVector<RenderWObject*> graphics() {
00169         return graphs;
00170     };
00173     void setWorld( World* newworld );
00175     QImage textureImage( QString texture ) {
00176         return (*textmap)[texture];
00177     };
00185     bool addTextureImage( QString filename, QString texturename );
00187     RenderWObject* operator[]( const WObject* );
00192     static RenderWObject* createRenderWObjectFor( const WObject*, RenderWObjectContainer* );
00196     template<class renderwobject>
00197     static void registerRenderWObjectFor( QString classname ) {
00198         initFactory();
00199         (*fac)[classname] = new WCreator<renderwobject>();
00200     };
00202     void applyTexture( QGLContext* gw, QString texts );
00204     void setupColorTexture( QGLContext*, RenderWObject* obj );
00206     void drawSkyGroundBox( QGLContext* );
00208     static void drawSphere( wVector pos, real radius );
00210     static void drawCylinder( wVector axis, wVector centre, float len, float radius, QColor c = Qt::green );
00212     static void drawCylinder( wVector start, wVector end, float radius, QColor c = Qt::green );
00214     static void drawCylinder( const wMatrix& mat, float len, float radius, QColor c = Qt::green );
00216     static void drawWireBox( wVector dims, wMatrix matrix );
00218     static void drawWireBox( wVector min, wVector max, const wMatrix& tm );
00220     static void drawWireBox( wVector min, wVector max );
00222     static void drawTorus( real outRad, real innRad, const wMatrix& mat, real angle = 2.0*PI_GRECO, QColor c=Qt::red );
00224     static void drawTorus( wVector axis, wVector centre, real outRad, real innRad, real angle = 2.0*PI_GRECO );
00225 protected:
00227     World* world() {
00228         return worldv;
00229     };
00230 
00232     void addObject( WObject* );
00234     void removeObject( WObject* );
00235 
00251     virtual void resourceChanged(QString name, ResourceChangeType changeType);
00252 
00254     QMutex mutex;
00255 private:
00257     const QString worldResourceName;
00259     World* worldv;
00261     QVector<RenderWObject*> graphs;
00263     static QMap<QString, WAbstractCreator*>* fac;
00265     static bool facInited;
00267     static void initFactory();
00269     static QMap<QString, QImage> *textmap;
00275     static unsigned int textmapRefCounter;
00277     QMap<QString, GLuint> textGLId;
00279     QImage skyb[6];
00280 };
00281 
00292 class FARSA_WSIM_API RenderWorld : public QGLViewer, public RenderWObjectContainer {
00293     Q_OBJECT
00294 public:
00296     RenderWorld( QWidget* parent = NULL, QString wResName = "world" );
00298     virtual ~RenderWorld();
00300     void drawArrow( const wVector& from, const wVector& to, float radius=-1, int nbSubdivisions=12, QColor c = Qt::red );
00301 public slots:
00303     void wireframe( bool b );
00305     void showSkyGround( bool b );
00307     void showObjects( bool b );
00309     void showJoints( bool b );
00311     void showAABBs( bool b );
00313     void showContacts( bool b );
00315     void showForces( bool b );
00317     void contextMenu( const QPoint& );
00318 private slots:
00320     void slotAddObject( WObject* );
00322     void slotRemoveObject( WObject* );
00324     void onWorldResize();
00325 protected:
00327     virtual void init();
00329     virtual void draw();
00331     virtual void drawWithNames();
00333     virtual void postSelection(const QPoint& point);
00335     virtual void keyPressEvent(QKeyEvent *e);
00336 
00352     virtual void resourceChanged(QString name, ResourceChangeType changeType);
00353 
00354 private:
00363     virtual void customEvent(QEvent* event);
00364 
00366     int currentSelected;
00367 
00369     void drawDOF( PhyDOF* dof, bool );
00371     void drawKineChains();
00372 
00374     bool wiref;
00376     bool showskygroundbox;
00378     bool showobjs;
00380     bool showjoints;
00382     bool showaabbs;
00384     bool showcontacts;
00386     bool showforces;
00387 };
00388 
00389 } // end namespace farsa
00390 
00391 #endif