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.GPL  **
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 HAVE_OPENGL
00034 #warning "this header must not be included if HAVE_OPENGL is not defined"
00035 #else
00036 
00037 #include <ICLUtils/Mutex.h>
00038 #include <ICLGeom/Primitive.h>
00039 #include <ICLGeom/ViewRay.h>
00040 #include <ICLGeom/Hit.h>
00041 #include <ICLQt/GLFragmentShader.h>
00042 
00043 namespace icl{
00044   namespace geom{
00045   
00047     class Scene;
00051 
00052 
00133     class SceneObject{
00134       public:
00135       
00137       friend class Scene;
00138       
00140       SceneObject();
00141       
00143 
00155       SceneObject(const std::string &type,const float *params);
00156       
00158       static inline SceneObject *cube(float x, float y, float z, float r){
00159         const float p[] = { x,y,z,r };
00160         return new SceneObject("cube",p);
00161       }
00162   
00164       static inline SceneObject *cuboid(float x, float y, float z, float dx, float dy, float dz){
00165         const float p[] = { x,y,z,dx,dy,dz };
00166         return new SceneObject("cuboid",p);
00167       }
00168   
00170       static inline SceneObject *sphere(float x, float y, float z, float r, int rzSteps, int xySlices){
00171         const float p[] = { x,y,z,r, float(rzSteps), float(xySlices) };
00172         return new SceneObject("sphere",p);
00173       }
00174   
00176       static inline SceneObject *spheroid(float x, float y, float z, float rx, float ry, float rz, int rzSteps, int xySlices){
00177         const float p[] = { x,y,z,rx, ry, rz, float(rzSteps), float(xySlices) };
00178         return new SceneObject("spheroid",p);
00179       }
00180   
00182       static inline SceneObject *superquadric(float x, float y, float z, float rx, float ry, float rz, 
00183                                               float dx, float dy, float dz, float e1, float e2, int rzSteps, int xySlices){
00184         const float p[] = { x,y,z,rx, ry, rz, dx, dy, dz, e1, e2, float(rzSteps), float(xySlices) };
00185         return new SceneObject("superquadric",p);
00186       }
00187   
00188       
00190       SceneObject(const std::string &objFileName) throw (utils::ICLException);
00191   
00193 
00195       SceneObject(const SceneObject &other) { 
00196         m_displayListHandle = 0;
00197         m_fragmentShader = 0;
00198         *this = other; 
00199         m_parent = 0;
00200       }
00201   
00203 
00205       SceneObject &operator=(const SceneObject &other);
00206       
00208       virtual ~SceneObject();
00209       
00211 
00213       std::vector<Vec> &getVertices();
00214   
00216       const std::vector<Vec> &getVertices() const;
00217   
00219 
00221       std::vector<GeomColor> &getVertexColors();
00222   
00224       const std::vector<GeomColor> &getVertexColors() const;
00225   
00226   
00228       std::vector<Primitive*> &getPrimitives();
00229   
00231       const std::vector<Primitive*> &getPrimitives() const;
00232   
00234       void setVisible(int oredTypes, bool visible, bool recursive=true);
00235       
00237       bool isVisible(Primitive::Type t) const;
00238       
00240 
00243       void addVertex(const Vec &p, const GeomColor &color=GeomColor(255,0,0,255));
00244   
00246       void addSharedTexture(utils::SmartPtr<qt::GLImg> gli);
00247       
00249       void addSharedTexture(const core::ImgBase *image, core::scalemode sm=core::interpolateLIN);
00250   
00251   
00253       void addNormal(const Vec &n);
00254       
00256 
00257       void addLine(int x, int y, const GeomColor &color=GeomColor(100,100,100,255));
00258   
00260 
00261       void addTriangle(int a, int b, int c, int na, int nb, int nc,
00262                        const GeomColor &color=GeomColor(0,100,250,255));
00263       
00265       inline void addTriangle(int a, int b, int c, const GeomColor &color=GeomColor(0,100,250,255)){
00266         addTriangle(a,b,c,-1,-1,-1,color);
00267       }
00268   
00269   
00271 
00272       void addQuad(int a, int b, int c, int d, int na, int nb, int nc, int nd, 
00273                    const GeomColor &color=GeomColor(0,100,250,255)); 
00274   
00276       inline void addQuad(int a, int b, int c, int d, const GeomColor &color=GeomColor(0,100,250,255)){
00277         addQuad(a,b,c,d,-1,-1,-1,-1,color);
00278       }
00279   
00281 
00282       void addPolygon(int nPoints,const int *vertexIndices, const GeomColor &color=GeomColor(0,100,250,255), 
00283                       const int *normalIndices=0);
00284   
00285       
00287       void addTexture(int a, int b, int c, int d, 
00288                       const core::ImgBase *texture, 
00289                       int na, int nb, int nc, int nd,
00290                       bool createTextureOnce=true,
00291                       core::scalemode sm = core::interpolateLIN);
00292   
00294       inline void addTexture(int a, int b, int c, int d, 
00295                              const core::ImgBase *texture, 
00296                              bool createTextureOnce=true,
00297                              core::scalemode sm = core::interpolateLIN){
00298         addTexture(a,b,c,d,texture,-1,-1,-1,-1,createTextureOnce,sm);
00299       }
00300   
00302 
00304       void addTexture(int a, int b, int c, int d, 
00305                       int sharedTextureIndex,
00306                       int na=-1, int nb=-1, int nc=-1, int nd=-1);
00307   
00309       void addTexture(const core::ImgBase *image, int numPoints, const int *vertexIndices,
00310                       const utils::Point32f *texCoords, const int *normalIndices = 0,
00311                       bool createTextureOnce=true);
00312                  
00313   
00315       void addTextureGrid(int w, int h, const core::ImgBase *image,
00316                           const icl32f *px, const icl32f *py, const icl32f *pz,
00317                           const icl32f *pnx=0, const icl32f *pny=0, const icl32f *pnz=0,
00318                           int stride = 1,bool createTextureOnce=true,core::scalemode sm=core::interpolateLIN);
00319   
00321 
00322       void addTwoSidedTextureGrid(int w, int h, const core::ImgBase *front, const core::ImgBase *back,
00323                              const icl32f *px, const icl32f *py, const icl32f *pz,
00324                              const icl32f *pnx=0, const icl32f *pny=0, const icl32f *pnz=0,
00325                              int stride = 1,bool createFrontOnce=true,
00326                                   bool createBackOnce=true, core::scalemode sm=core::interpolateLIN);
00327   
00328   
00330 
00345       void addTextTexture(int a, int b, int c, int d, const std::string &text,
00346                           const GeomColor &color, 
00347                           int na, int nb, int nc, int nd,
00348                           int textSize,core::scalemode sm = core::interpolateLIN);
00349                           
00350   
00352       inline void addTextTexture(int a, int b, int c, int d, const std::string &text,
00353                           const GeomColor &color=GeomColor(255,255,255,255), 
00354                                  int textSize=30, core::scalemode sm = core::interpolateLIN){
00355         addTextTexture(a,b,c,d,text,color,-1,-1,-1,-1,textSize, sm);
00356       }
00357       
00359 
00364       void addText(int a, const std::string &text, float billboardHeight=10, 
00365                    const GeomColor &color=GeomColor(255,255,255,255),
00366                    int textRenderSize=30, core::scalemode sm=core::interpolateLIN);
00367   
00369 
00372       inline void addCustomPrimitive(Primitive *p){
00373         m_primitives.push_back(p);
00374       }
00375       
00377 
00379       SceneObject *addCube(float x, float y, float z, float d){
00380         return addCuboid(x,y,z,d,d,d);
00381       }
00382   
00384 
00386       SceneObject *addCuboid(float x, float y, float z, float dx, float dy, float dz);
00387   
00389 
00391       SceneObject *addSphere(float x, float y, float z, float r,int rzSteps, int xySlices){
00392         return addSpheroid(x,y,z,r,r,r,rzSteps,xySlices);
00393       }
00394   
00396 
00398       SceneObject *addSpheroid(float x, float y, float z, float rx, float ry, float rz, int rzSteps, int xySlices);
00399       
00401 
00403       SceneObject *addCylinder(float x, float y, float z, float rx, float ry, float h, int steps);
00404   
00406 
00408       SceneObject *addCone(float x, float y, float z, float rx, float ry, float h, int steps);
00409   
00410       
00412       void setColor(Primitive::Type t,const GeomColor &color,bool recursive=true);
00413       
00415       void setPointSize(float pointSize, bool recursive=true);
00416   
00418       void setLineWidth(float lineWidth, bool recursive=true);
00419   
00420      
00421       
00423       virtual SceneObject *copy() const;
00424   
00426 
00427       virtual void prepareForRendering() {}
00428   
00430 
00434       virtual void customRender() {}
00435   
00437       void setColorsFromVertices(Primitive::Type t, bool on, bool recursive=true);
00438       
00440       bool getSmoothShading() const;
00441       
00443       void setSmoothShading(bool on, bool recursive=true);
00444   
00445   
00447 
00448 
00451       virtual void setTransformation(const Mat &m);
00452       
00454       void removeTransformation();
00455   
00457       virtual void transform(const Mat &m);
00458   
00460 
00461       template<class T>
00462       void transform(const T &m){
00463         transform(Mat(m(0,0),m(1,0),m(2,0),m(3,0),
00464                       m(0,1),m(1,1),m(2,1),m(3,1),
00465                       m(0,2),m(1,2),m(2,2),m(3,2),
00466                       m(0,3),m(1,3),m(2,3),m(3,3)));
00467       } 
00468       
00470       virtual void rotate(float rx, float ry, float rz);
00471   
00473       template<class T>
00474       inline void rotate(const T &t) { rotate((float)t[0],(float)t[1],(float)t[2]); }
00475       
00477       virtual void translate(float dx, float dy, float dz);
00478   
00480       template<class T>
00481       inline void translate(const T &t) { translate((float)t[0],(float)t[1],(float)t[2]); }
00482   
00484       virtual void scale(float sx, float sy, float sz);
00485       
00487       template<class T>
00488       inline void scale(const T &t) { scale((float)t[0],(float)t[1],(float)t[2]); }
00489   
00491 
00494       Mat getTransformation(bool relative=false) const;
00495       
00497 
00498       bool hasTransformation(bool relative=false) const;
00499       
00501       SceneObject *getParent();
00502       
00504       const SceneObject *getParent() const;
00505   
00507 
00513       void addChild(SceneObject *child, bool passOwnerShip=true);
00514       
00516 
00517       void removeChild(SceneObject *child);
00518   
00520       void removeAllChildren();
00521       
00523       bool hasChildren() const;
00524       
00526       int getChildCount() const;
00527       
00529       SceneObject *getChild(int index);
00530       
00532       const SceneObject *getChild(int index) const;
00533       
00535       bool hasChild(const SceneObject *o) const;
00538 
00539 
00551       void createAutoNormals(bool smooth=true);
00552       
00554 
00556       virtual GeomColor getDefaultVertexColor() const{ return GeomColor(255,0,0,255); }
00557       
00559 
00567       Hit hit(const ViewRay &v, bool recursive=true);
00568       
00570       const Hit hit(const ViewRay &v, bool recursive=true) const{
00571         return const_cast<SceneObject*>(this)->hit(v,recursive);
00572       }
00573       
00575       std::vector<Hit> hits(const ViewRay &v, bool recursive=true);
00576   
00578       std::vector<Vec> getTransformedVertices() const;
00579       
00581 
00583       Vec getClosestVertex(const Vec &pWorld, bool relative=false) throw (utils::ICLException);
00584       
00586       void setVisible(bool visible, bool recursive=true);
00587       
00589       bool isVisible() const { return m_isVisible; }
00590   
00592       void hide(bool recursive=true){ setVisible(false); }
00593   
00595       void show(bool recursive=true){ setVisible(true); }
00596   
00597   
00599 
00602       inline void setLockingEnabled(bool enabled) { 
00603         m_mutex.lock();
00604         m_enableLocking = enabled; 
00605         m_mutex.unlock();
00606       }
00607       
00609       bool getLockingEnabled() const {
00610         return m_enableLocking;
00611       }
00612   
00614 
00618       virtual void lock(){
00619         if(!m_enableLocking) return;
00620         m_mutex.lock();
00621       }
00622       
00624 
00628       virtual void unlock(){
00629         if(!m_enableLocking) return;
00630         m_mutex.unlock();
00631       }
00632       
00633       friend struct Primitive;
00634       
00635   
00637 
00639       inline void setPointSmoothingEnabled(bool enabled=true){
00640         m_pointSmoothingEnabled = enabled;
00641       }
00642   
00644 
00646       inline void setLineSmoothingEnabled(bool enabled=true){
00647         m_lineSmoothingEnabled = enabled;
00648       }
00649    
00651 
00653       inline void setPolygonSmoothingEnabled(bool enabled=true){
00654         m_polygonSmoothingEnabled = enabled;
00655       }
00656   
00658       void clearAllPrimitives(); 
00659       
00661 
00662       void createDisplayList();
00663       
00665       void freeDisplayList();
00666       
00668 
00669       void setFragmentShader(qt::GLFragmentShader *shader);
00670       
00672       inline qt::GLFragmentShader *getFragmentShader() { return m_fragmentShader; }
00673       
00675       inline const qt::GLFragmentShader *getFragmentShader() const{ return m_fragmentShader; }
00676 
00678       inline void setShininess(icl8u value){
00679         m_shininess = value;
00680       }
00681       
00683 
00684       inline void setSpecularReflectance(const GeomColor &values){
00685         m_specularReflectance = values*(1.0/255);
00686       }
00687       
00689       inline bool getDepthTestEnabled() const{ return m_depthTestEnabled;  }
00690       
00692       inline void setDepthTestEnabled(bool enabled){
00693         m_depthTestEnabled = enabled;
00694       }
00695 
00696       protected:
00698       static void collect_hits_recursive(SceneObject *obj, const ViewRay &v, 
00699                                          std::vector<Hit> &hits, 
00700                                          bool recursive);
00701       
00702       std::vector<Vec> m_vertices;
00703       std::vector<Vec> m_normals;
00704       
00705       std::vector<GeomColor> m_vertexColors;
00706       std::vector<Primitive*> m_primitives;
00707       std::vector<utils::SmartPtr<qt::GLImg> > m_sharedTextures;
00708       int m_visibleMask;
00709   
00710       bool m_lineColorsFromVertices;
00711       bool m_triangleColorsFromVertices;
00712       bool m_quadColorsFromVertices;
00713       bool m_polyColorsFromVertices;
00714   
00715       float m_pointSize;
00716       float m_lineWidth;
00717       
00718       bool m_useSmoothShading;
00719       bool m_isVisible;
00720       
00722       Mat m_transformation;
00723       bool m_hasTransformation;
00724       SceneObject *m_parent;
00725       std::vector<utils::SmartPtr<SceneObject> > m_children;
00726   
00727       utils::Mutex m_mutex; 
00728       bool m_enableLocking; 
00729   
00730       bool m_pointSmoothingEnabled;
00731       bool m_lineSmoothingEnabled;
00732       bool m_polygonSmoothingEnabled;
00733       bool m_depthTestEnabled; 
00734       
00735       icl8u m_shininess;
00736       GeomColor m_specularReflectance;
00737 
00738 
00739       
00740       private:
00741   
00743       void *m_displayListHandle;
00745 
00748       int m_createDisplayListNextTime;
00749   
00751       qt::GLFragmentShader *m_fragmentShader;
00752   
00753     };
00754   } // namespace geom
00755 }
00756 
00757 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines