Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Scene.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/Scene.h                            **
00010 ** Module : ICLGeom                                                **
00011 ** Authors: Christof Elbrechter, Erik Weitnauer, Daniel Dornbusch  **
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 <ICLGeom/SceneObject.h>
00038 #include <ICLGeom/Camera.h>
00039 #include <ICLGeom/SceneLight.h>
00040 #include <ICLCore/Img.h>
00041 
00042 #ifdef HAVE_QT
00043 #include <ICLQt/MouseHandler.h>
00044 #include <ICLQt/DrawWidget3D.h>
00045 #include <ICLGeom/SceneMouseHandler.h>
00046 #endif
00047 
00048 #include <ICLUtils/Lockable.h>
00049 #include <ICLUtils/Configurable.h>
00050 #include <ICLUtils/SmartArray.h>
00051 #include <map>
00052 
00053 namespace icl{
00055   namespace qt {class ICLDrawWidget;}
00058   namespace geom{
00059  
00061 
00113 
00120 
00127     class Scene : public utils::Lockable, public utils::Configurable{
00128       public:
00129 
00131       friend class SceneObject;
00132     
00133       struct GLCallback;
00134     
00136       Scene();
00137     
00139       ~Scene();
00140     
00142       Scene(const Scene &scene);
00143     
00145       Scene &operator=(const Scene &scene);
00146 
00148 
00159       void addCamera(const Camera &cam, float visSize=1.0);
00160     
00162       void removeCamera(int index);
00163     
00165 
00174       Camera &getCamera(int camIndex = 0);
00175     
00177       const Camera &getCamera(int camIndex =0) const;
00178     
00180       inline int getCameraCount() const { return (int)m_cameras.size(); }
00181 
00183 
00186       std::vector<Camera*> getAllCameras(int firstIndex=0, int num=-1);
00187     
00189 
00193       void addObject(SceneObject *object, bool passOwnerShip=false);
00194     
00196 
00197       void removeObject(int idx);
00198     
00200 
00201       void removeObject(const SceneObject *obj);
00202 
00204 
00205       void removeObjects(int startIndex, int endIndex=-1);
00206     
00208       inline int getObjectCount() const { return m_objects.size(); }
00209     
00211 
00212       SceneObject *getObject(int index) throw (utils::ICLException);
00213 
00215 
00216       const SceneObject *getObject(int index) const throw (utils::ICLException);
00217     
00219 
00224       SceneObject *getObject(const std::vector<int> recursiveIndices) throw (utils::ICLException);
00225 
00227 
00232       SceneObject *getObject(const std::vector<int> recursiveIndices) const throw (utils::ICLException);
00233     
00235 
00237       std::vector<int> findPath(const SceneObject *o) const throw (utils::ICLException);
00238     
00240 
00241       void clear(bool camerasToo=false);
00242 
00243 #ifdef HAVE_QT
00244 
00245       qt::MouseHandler *getMouseHandler(int camIndex=0);
00246     
00248       void setMouseHandler(SceneMouseHandler* sceneMouseHandler, int camIndex=0);
00249     
00251 
00252       qt::ICLDrawWidget3D::GLCallback *getGLCallback(int camIndex);
00253     
00254 #ifdef HAVE_GLX
00255       enum DepthBufferMode{
00256         RawDepth01,      
00257         DistToCamPlane,  
00258         DistToCamCenter  
00259       };
00260     
00261     
00263 
00271       static void enableSharedOffscreenRendering();
00272     
00274       static void disableSharedOffscreenRendering();
00275     
00277 
00310       const core::Img8u &render(int camIndx, const core::ImgBase *background=0, core::Img32f *depthBuffer=0, 
00311                                 DepthBufferMode mode=DistToCamCenter) const throw (utils::ICLException);
00312     
00314       void freeAllPBuffers();
00315     
00317       void freePBuffer(const utils::Size &size);
00318       
00320       mutable GLuint shadowTexture;
00321       
00323       mutable GLuint shadowFBO;
00324       
00326       mutable unsigned int shadowResolution;
00327       
00329       mutable icl::qt::GLFragmentShader* m_perPixelShader;
00330       
00332       void createShadowFBO(unsigned int size = 512, unsigned int lights = 8) const;
00333       
00335       void freeShadowFBO() const;
00336 #endif
00337 #endif
00338 
00340 
00341       void setDrawCamerasEnabled(bool enabled);
00342     
00344       bool getDrawCamerasEnabled() const;
00345 
00347       void setDrawLightsEnabled(bool enabled, float lightSize=1);
00348     
00350       bool getDrawLightsEnabled() const;
00351     
00353       void setDrawCoordinateFrameEnabled(bool enabled, float size=120);
00354     
00356       bool getDrawCoordinateFrameEnabled() const;
00357 
00359       void setDrawObjectFramesEnabled(bool enabled, float size);
00360       
00362       bool getDrawObjectFramesEnabled() const;
00363       
00365 
00368       SceneLight &getLight(int index) throw (utils::ICLException);
00369 
00371 
00373       const SceneLight &getLight(int index) const throw (utils::ICLException);
00374 
00375     
00377 
00378       void setLightingEnabled(bool flag);
00379 
00381 
00383       void setGlobalAmbientLight(const GeomColor &color);
00384       
00386 
00388       inline Hit findObject(int camIndex, int xScreen, int yScreen){
00389         return findObject(getCamera(camIndex).getViewRay(utils::Point(xScreen,yScreen)));
00390       }
00391     
00393 
00395       Hit findObject(const ViewRay &v);
00396     
00397     
00399       std::vector<Hit> findObjects(const ViewRay &v);
00400     
00402       inline std::vector<Hit> findObjects(int camIndex, int xScreen, int ySceen){
00403         return findObjects(getCamera(camIndex).getViewRay(utils::Point(xScreen,ySceen)));
00404       }
00405     
00407 
00414       void setBounds(float minX, float maxX=0, float minY=0, float mayY=0, float minZ=0, float maxZ=0);
00415       
00417 
00419       void setBackgroundColor(const GeomColor &color);
00420 
00422       GeomColor getBackgroundColor() const;
00423       
00424       protected:
00425 
00427       void createDisplayList(SceneObject *o) const;
00428 
00430       void freeDisplayList(SceneObject *o) const;
00431 
00432 #ifdef HAVE_QT
00433 
00434       void renderScene(int camIndex, qt::ICLDrawWidget3D *widget=0) const;
00435       
00437       void renderShadow(const unsigned int light, unsigned int size = 512) const;
00438       
00440       void recompilePerPixelShader() const;
00441       
00443       void renderSceneObjectRecursiveShadow(SceneObject *o) const;
00444 #endif
00445 
00447       void renderSceneObjectRecursive(SceneObject *o) const{
00448         renderSceneObjectRecursive(0, o);
00449       };
00450       
00452       void renderSceneObjectRecursive(const std::vector<geom::Mat> *project2shadow, SceneObject *o) const;
00453 
00455       void renderObjectFramesRecursive(SceneObject *o, SceneObject *cs) const;
00456 
00458 
00460       float getMaxSceneDim() const;
00461     
00463       void extendMaxSceneDimRecursive(float &minX, float &maxX, 
00464                                       float &minY, float &maxY, 
00465                                       float &minZ, float &maxZ,
00466                                       SceneObject *o) const;
00467 
00469       std::vector<Camera> m_cameras;
00470 
00472       std::vector<utils::SmartPtr<SceneObject> > m_objects;
00473 
00475       std::vector<utils::SmartPtr<SceneObject> > m_cameraObjects;
00476 
00477 #ifdef HAVE_QT
00478 
00479       std::vector<utils::SmartPtr<SceneMouseHandler> > m_mouseHandlers;
00480 
00482       std::vector<utils::SmartPtr<GLCallback> > m_glCallbacks;
00483 #endif
00484 
00485 
00487       struct PBuffer;
00488     
00489       struct PBufferIndex : public utils::Size{
00490         unsigned int threadID;
00491         PBufferIndex(const utils::Size &size);
00492         bool operator<(const PBufferIndex &other) const;
00493       };
00494     
00496       mutable std::map<PBufferIndex,PBuffer*> m_pbuffers;
00497 
00499       mutable utils::SmartPtr<SceneObject> m_coordinateFrameObject;
00500 
00502       mutable utils::SmartPtr<SceneObject> m_objectFrameObject;
00503       
00505       utils::SmartPtr<SceneLight> m_lights[8];
00506       
00508       mutable int m_previousLightState[8];
00509     
00511       utils::SmartArray<utils::Range32f> m_bounds;
00512       
00514       math::FixedColVector<int,4> m_globalAmbientLight;
00515 
00517       GeomColor m_backgroundColor;
00518 
00519       private:
00521       static void freeDisplayList(void *handle);
00522     };
00523   } // namespace geom
00524 
00525 }
00526 
00527 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines