Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SceneObject.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLGeom/src/ICLGeom/SceneObject.h                      **
00010 ** Module : ICLGeom                                                **
00011 ** Authors: Christof Elbrechter                                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #ifndef ICL_HAVE_OPENGL
00034 #if WIN32
00035 #pragma WARNING("this header must not be included if ICL_HAVE_OPENGL is not defined")
00036 #else
00037 #warning "this header must not be included if ICL_HAVE_OPENGL is not defined"
00038 #endif
00039 #endif
00040 
00041 #include <ICLUtils/CompatMacros.h>
00042 #include <ICLUtils/Mutex.h>
00043 #include <ICLGeom/Primitive.h>
00044 #include <ICLGeom/ViewRay.h>
00045 #include <ICLGeom/Hit.h>
00046 #include <ICLQt/GLFragmentShader.h>
00047 
00048 namespace icl{
00049   namespace geom{
00050   
00052     class Scene;
00053     class ShaderUtil;
00057 
00058 
00139     class SceneObject{
00140       public:
00141       
00143       friend class Scene;
00144       
00146       ICLGeom_API SceneObject();
00147       
00149 
00163       ICLGeom_API SceneObject(const std::string &type,const float *params);
00164       
00166       static inline SceneObject *cube(float x, float y, float z, float r){
00167         const float p[] = { x,y,z,r };
00168         return new SceneObject("cube",p);
00169       }
00170   
00172       static inline SceneObject *cuboid(float x, float y, float z, float dx, float dy, float dz){
00173         const float p[] = { x,y,z,dx,dy,dz };
00174         return new SceneObject("cuboid",p);
00175       }
00176   
00178       static inline SceneObject *sphere(float x, float y, float z, float r, int rzSteps, int xySlices){
00179         const float p[] = { x,y,z,r, float(rzSteps), float(xySlices) };
00180         return new SceneObject("sphere",p);
00181       }
00182   
00184       static inline SceneObject *spheroid(float x, float y, float z, float rx, float ry, float rz, int rzSteps, int xySlices){
00185         const float p[] = { x,y,z,rx, ry, rz, float(rzSteps), float(xySlices) };
00186         return new SceneObject("spheroid",p);
00187       }
00188   
00190       static inline SceneObject *superquadric(float x, float y, float z, float rx, float ry, float rz, 
00191                                               float dx, float dy, float dz, float e1, float e2, int rzSteps, int xySlices){
00192         const float p[] = { x,y,z,rx, ry, rz, dx, dy, dz, e1, e2, float(rzSteps), float(xySlices) };
00193         return new SceneObject("superquadric",p);
00194       }
00195   
00196       
00198       ICLGeom_API SceneObject(const std::string &objFileName) throw (utils::ICLException);
00199   
00201 
00203       SceneObject(const SceneObject &other) { 
00204         m_displayListHandle = 0;
00205         m_fragmentShader = 0;
00206         *this = other; 
00207         m_parent = 0;
00208       }
00209   
00211 
00213       ICLGeom_API SceneObject &operator=(const SceneObject &other);
00214       
00216       ICLGeom_API virtual ~SceneObject();
00217       
00219 
00221       ICLGeom_API std::vector<Vec> &getVertices();
00222   
00224       ICLGeom_API const std::vector<Vec> &getVertices() const;
00225   
00227 
00229       ICLGeom_API std::vector<GeomColor> &getVertexColors();
00230   
00232       ICLGeom_API const std::vector<GeomColor> &getVertexColors() const;
00233   
00234   
00236       ICLGeom_API std::vector<Primitive*> &getPrimitives();
00237   
00239       ICLGeom_API const std::vector<Primitive*> &getPrimitives() const;
00240   
00242       ICLGeom_API void setVisible(int oredTypes, bool visible, bool recursive = true);
00243       
00245       ICLGeom_API bool isVisible(Primitive::Type t) const;
00246       
00248 
00251       ICLGeom_API void addVertex(const Vec &p, const GeomColor &color = GeomColor(255, 0, 0, 255));
00252   
00254       ICLGeom_API void addSharedTexture(utils::SmartPtr<qt::GLImg> gli);
00255       
00257       ICLGeom_API void addSharedTexture(const core::ImgBase *image, core::scalemode sm = core::interpolateLIN);
00258   
00259   
00261       ICLGeom_API void addNormal(const Vec &n);
00262       
00264 
00265       ICLGeom_API void addLine(int x, int y, const GeomColor &color = GeomColor(100, 100, 100, 255));
00266   
00268 
00269       ICLGeom_API void addTriangle(int a, int b, int c, int na, int nb, int nc,
00270                        const GeomColor &color=GeomColor(0,100,250,255));
00271       
00273       inline void addTriangle(int a, int b, int c, const GeomColor &color=GeomColor(0,100,250,255)){
00274         addTriangle(a,b,c,-1,-1,-1,color);
00275       }
00276   
00277   
00279 
00280       ICLGeom_API void addQuad(int a, int b, int c, int d, int na, int nb, int nc, int nd,
00281                    const GeomColor &color=GeomColor(0,100,250,255)); 
00282   
00284       inline void addQuad(int a, int b, int c, int d, const GeomColor &color=GeomColor(0,100,250,255)){
00285         addQuad(a,b,c,d,-1,-1,-1,-1,color);
00286       }
00287   
00289 
00290       ICLGeom_API void addPolygon(int nPoints, const int *vertexIndices, const GeomColor &color = GeomColor(0, 100, 250, 255),
00291                       const int *normalIndices=0);
00292   
00293       
00295       ICLGeom_API void addTexture(int a, int b, int c, int d,
00296                       const core::ImgBase *texture, 
00297                       int na, int nb, int nc, int nd,
00298                       bool createTextureOnce=true,
00299                       core::scalemode sm = core::interpolateLIN);
00300   
00302       inline void addTexture(int a, int b, int c, int d, 
00303                              const core::ImgBase *texture, 
00304                              bool createTextureOnce=true,
00305                              core::scalemode sm = core::interpolateLIN){
00306         addTexture(a,b,c,d,texture,-1,-1,-1,-1,createTextureOnce,sm);
00307       }
00308   
00310 
00312       ICLGeom_API void addTexture(int a, int b, int c, int d,
00313                       int sharedTextureIndex,
00314                       int na=-1, int nb=-1, int nc=-1, int nd=-1);
00315   
00317       ICLGeom_API void addTexture(const core::ImgBase *image, int numPoints, const int *vertexIndices,
00318                       const utils::Point32f *texCoords, const int *normalIndices = 0,
00319                       bool createTextureOnce=true);
00320                  
00321   
00323       ICLGeom_API void addTextureGrid(int w, int h, const core::ImgBase *image,
00324                           const icl32f *px, const icl32f *py, const icl32f *pz,
00325                           const icl32f *pnx=0, const icl32f *pny=0, const icl32f *pnz=0,
00326                           int stride = 1,bool createTextureOnce=true,core::scalemode sm=core::interpolateLIN);
00327   
00329 
00330       ICLGeom_API void addTwoSidedTextureGrid(int w, int h, const core::ImgBase *front, const core::ImgBase *back,
00331                              const icl32f *px, const icl32f *py, const icl32f *pz,
00332                              const icl32f *pnx=0, const icl32f *pny=0, const icl32f *pnz=0,
00333                              int stride = 1,bool createFrontOnce=true,
00334                                   bool createBackOnce=true, core::scalemode sm=core::interpolateLIN);
00335   
00336       
00338       ICLGeom_API void addTwoSidedTGrid(int w, int h, const Vec *vertices, const Vec *normals = 0,
00339                             const GeomColor &frontColor=GeomColor(0,100,255,255), 
00340                             const GeomColor &backColor=GeomColor(255,0,100,255),
00341                             const GeomColor &lineColor=GeomColor(0,255,100,255),
00342                             bool drawLines=false, bool drawQuads=true);
00343   
00344   
00346 
00361       ICLGeom_API void addTextTexture(int a, int b, int c, int d, const std::string &text,
00362                           const GeomColor &color, 
00363                           int na, int nb, int nc, int nd,
00364                           int textSize,core::scalemode sm = core::interpolateLIN);
00365                           
00366   
00368       inline void addTextTexture(int a, int b, int c, int d, const std::string &text,
00369                           const GeomColor &color=GeomColor(255,255,255,255), 
00370                                  int textSize=30, core::scalemode sm = core::interpolateLIN){
00371         addTextTexture(a,b,c,d,text,color,-1,-1,-1,-1,textSize, sm);
00372       }
00373       
00375 
00380       ICLGeom_API void addText(int a, const std::string &text, float billboardHeight = 10,
00381                    const GeomColor &color=GeomColor(255,255,255,255),
00382                    int textRenderSize=30, core::scalemode sm=core::interpolateLIN);
00383   
00385 
00388       inline void addCustomPrimitive(Primitive *p){
00389         m_primitives.push_back(p);
00390       }
00391       
00393 
00395       SceneObject *addCube(float x, float y, float z, float d){
00396         return addCuboid(x,y,z,d,d,d);
00397       }
00398   
00400 
00402       ICLGeom_API SceneObject *addCuboid(float x, float y, float z, float dx, float dy, float dz);
00403   
00405 
00407       SceneObject *addSphere(float x, float y, float z, float r,int rzSteps, int xySlices){
00408         return addSpheroid(x,y,z,r,r,r,rzSteps,xySlices);
00409       }
00410   
00412 
00414       ICLGeom_API SceneObject *addSpheroid(float x, float y, float z, float rx, float ry, float rz, int rzSteps, int xySlices);
00415       
00417 
00419       ICLGeom_API SceneObject *addCylinder(float x, float y, float z, float rx, float ry, float h, int steps);
00420   
00422 
00424       ICLGeom_API SceneObject *addCone(float x, float y, float z, float rx, float ry, float h, int steps);
00425   
00426       
00428       ICLGeom_API void setColor(Primitive::Type t, const GeomColor &color, bool recursive = true);
00429       
00431       ICLGeom_API void setPointSize(float pointSize, bool recursive = true);
00432   
00434       ICLGeom_API void setLineWidth(float lineWidth, bool recursive = true);
00435   
00436      
00437       
00439       ICLGeom_API virtual SceneObject *copy() const;
00440   
00442 
00443       virtual void prepareForRendering() {}
00444   
00446 
00450       virtual void customRender() {}
00451       
00453 
00458       virtual void complexCustomRender(icl::geom::ShaderUtil* util) {customRender();}
00459   
00461       ICLGeom_API void setColorsFromVertices(Primitive::Type t, bool on, bool recursive = true);
00462       
00464       ICLGeom_API bool getSmoothShading() const;
00465       
00467       ICLGeom_API void setSmoothShading(bool on, bool recursive = true);
00468   
00469   
00471 
00472 
00475       ICLGeom_API virtual void setTransformation(const Mat &m);
00476       
00478       ICLGeom_API void removeTransformation();
00479   
00481       ICLGeom_API virtual void transform(const Mat &m);
00482   
00484 
00485       template<class T>
00486       void transform(const T &m){
00487         transform(Mat(m(0,0),m(1,0),m(2,0),m(3,0),
00488                       m(0,1),m(1,1),m(2,1),m(3,1),
00489                       m(0,2),m(1,2),m(2,2),m(3,2),
00490                       m(0,3),m(1,3),m(2,3),m(3,3)));
00491       } 
00492       
00494       ICLGeom_API virtual void rotate(float rx, float ry, float rz, 
00495                           icl::math::AXES axes=icl::math::AXES_DEFAULT);
00496   
00498       template<class T>
00499       inline void rotate(const T &t, icl::math::AXES axes=icl::math::AXES_DEFAULT) 
00500         { rotate((float)t[0],(float)t[1],(float)t[2]); }
00501       
00503       ICLGeom_API virtual void translate(float dx, float dy, float dz);
00504   
00506       template<class T>
00507       inline void translate(const T &t) { translate((float)t[0],(float)t[1],(float)t[2]); }
00508   
00510       ICLGeom_API virtual void scale(float sx, float sy, float sz);
00511       
00513       template<class T>
00514       inline void scale(const T &t) { scale((float)t[0],(float)t[1],(float)t[2]); }
00515   
00517 
00520       ICLGeom_API Mat getTransformation(bool relative = false) const;
00521       
00523 
00524       ICLGeom_API bool hasTransformation(bool relative = false) const;
00525       
00527       ICLGeom_API SceneObject *getParent();
00528       
00530       ICLGeom_API const SceneObject *getParent() const;
00531   
00533 
00539       ICLGeom_API void addChild(SceneObject *child, bool passOwnerShip = true);
00540       
00542 
00546       void addChild(utils::SmartPtr<SceneObject> child);
00547       
00549 
00550       ICLGeom_API void removeChild(SceneObject *child);
00551   
00553       ICLGeom_API void removeAllChildren();
00554       
00556       ICLGeom_API bool hasChildren() const;
00557       
00559       ICLGeom_API int getChildCount() const;
00560       
00562       ICLGeom_API SceneObject *getChild(int index);
00563 
00565       ICLGeom_API const SceneObject *getChild(int index) const;
00566 
00568       utils::SmartPtr<SceneObject> getChildPtr(int index);
00569       
00571       ICLGeom_API bool hasChild(const SceneObject *o) const;
00574 
00575 
00587       ICLGeom_API void createAutoNormals(bool smooth = true);
00588       
00590 
00592       virtual GeomColor getDefaultVertexColor() const{ return GeomColor(255,0,0,255); }
00593       
00595 
00603       ICLGeom_API Hit hit(const ViewRay &v, bool recursive = true);
00604       
00606       const Hit hit(const ViewRay &v, bool recursive=true) const{
00607         return const_cast<SceneObject*>(this)->hit(v,recursive);
00608       }
00609       
00611       ICLGeom_API std::vector<Hit> hits(const ViewRay &v, bool recursive = true);
00612   
00614       ICLGeom_API std::vector<Vec> getTransformedVertices() const;
00615       
00617 
00619       ICLGeom_API Vec getClosestVertex(const Vec &pWorld, bool relative = false) throw (utils::ICLException);
00620       
00622       ICLGeom_API void setVisible(bool visible, bool recursive = true);
00623       
00625       bool isVisible() const { return m_isVisible; }
00626   
00628       void hide(bool recursive=true){ setVisible(false); }
00629   
00631       void show(bool recursive=true){ setVisible(true); }
00632   
00633   
00635 
00638       inline void setLockingEnabled(bool enabled) { 
00639         m_mutex.lock();
00640         m_enableLocking = enabled; 
00641         m_mutex.unlock();
00642       }
00643       
00645       bool getLockingEnabled() const {
00646         return m_enableLocking;
00647       }
00648   
00650 
00654       virtual void lock() const{
00655         if(!m_enableLocking) return;
00656         m_mutex.lock();
00657       }
00658       
00660 
00664       virtual void unlock() const{
00665         if(!m_enableLocking) return;
00666         m_mutex.unlock();
00667       }
00668       
00669       friend struct Primitive;
00670       
00671   
00673 
00675       inline void setPointSmoothingEnabled(bool enabled=true){
00676         m_pointSmoothingEnabled = enabled;
00677       }
00678   
00680 
00682       inline void setLineSmoothingEnabled(bool enabled=true){
00683         m_lineSmoothingEnabled = enabled;
00684       }
00685    
00687 
00689       inline void setPolygonSmoothingEnabled(bool enabled=true){
00690         m_polygonSmoothingEnabled = enabled;
00691       }
00692   
00694       ICLGeom_API void clearAllPrimitives();
00695       
00697 
00698       ICLGeom_API void createDisplayList();
00699       
00701       ICLGeom_API void freeDisplayList();
00702       
00704 
00705       ICLGeom_API void setFragmentShader(qt::GLFragmentShader *shader);
00706       
00708       inline qt::GLFragmentShader *getFragmentShader() { return m_fragmentShader; }
00709       
00711       inline const qt::GLFragmentShader *getFragmentShader() const{ return m_fragmentShader; }
00712 
00713       inline void setCastShadowsEnabled(bool castShadows = true) { m_castShadows = castShadows; }
00714 
00715       inline bool getCastShadowsEnabled() { return m_castShadows; }
00716 
00717       inline void setReceiveShadowsEnabled(bool receiveShadows = true) { m_receiveShadows = receiveShadows; }
00718 
00719       inline bool getReceiveShadowsEnabled() { return m_receiveShadows; }
00720 
00722       inline void setShininess(icl8u value){
00723         m_shininess = value;
00724       }
00725       
00727 
00728       inline void setSpecularReflectance(const GeomColor &values){
00729         m_specularReflectance = values*(1.0/255);
00730       }
00731       
00733       inline bool getDepthTestEnabled() const{ return m_depthTestEnabled;  }
00734       
00736       inline void setDepthTestEnabled(bool enabled){
00737         m_depthTestEnabled = enabled;
00738       }
00739 
00740       protected:
00742       static void collect_hits_recursive(SceneObject *obj, const ViewRay &v, 
00743                                          std::vector<Hit> &hits, 
00744                                          bool recursive);
00745       
00746       std::vector<Vec> m_vertices;
00747       std::vector<Vec> m_normals;
00748       
00749       std::vector<GeomColor> m_vertexColors;
00750       std::vector<Primitive*> m_primitives;
00751       std::vector<utils::SmartPtr<qt::GLImg> > m_sharedTextures;
00752       int m_visibleMask;
00753   
00754       bool m_lineColorsFromVertices;
00755       bool m_triangleColorsFromVertices;
00756       bool m_quadColorsFromVertices;
00757       bool m_polyColorsFromVertices;
00758   
00759       float m_pointSize;
00760       float m_lineWidth;
00761       
00762       bool m_useSmoothShading;
00763       bool m_isVisible;
00764       
00766       Mat m_transformation;
00767       bool m_hasTransformation;
00768       SceneObject *m_parent;
00769       std::vector<utils::SmartPtr<SceneObject> > m_children;
00770   
00771       mutable utils::Mutex m_mutex; 
00772       bool m_enableLocking; 
00773   
00774       bool m_pointSmoothingEnabled;
00775       bool m_lineSmoothingEnabled;
00776       bool m_polygonSmoothingEnabled;
00777       bool m_depthTestEnabled; 
00778       
00779       icl8u m_shininess;
00780       GeomColor m_specularReflectance;
00781 
00782 
00783       
00784       private:
00785   
00787       void *m_displayListHandle;
00789 
00792       int m_createDisplayListNextTime;
00793   
00795       qt::GLFragmentShader *m_fragmentShader;
00796       
00797       bool m_castShadows;
00798       bool m_receiveShadows;
00799   
00800     };
00801   } // namespace geom
00802 }
00803 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines