Main MRPT website > C++ reference for MRPT 1.4.0
COpenGLScene.h
Go to the documentation of this file.
1/* +---------------------------------------------------------------------------+
2 | Mobile Robot Programming Toolkit (MRPT) |
3 | http://www.mrpt.org/ |
4 | |
5 | Copyright (c) 2005-2016, Individual contributors, see AUTHORS file |
6 | See: http://www.mrpt.org/Authors - All rights reserved. |
7 | Released under BSD License. See details in http://www.mrpt.org/License |
8 +---------------------------------------------------------------------------+ */
9#ifndef opengl_COpenGLScene_H
10#define opengl_COpenGLScene_H
11
15
16namespace mrpt
17{
18 /** The namespace for 3D scene representation and rendering. See also the <a href="mrpt-opengl.html" > summary page</a> of the mrpt-opengl library for more info and thumbnails of many of the render primitive.
19 */
20 namespace opengl
21 {
22 // This must be added to any CSerializable derived class:
24
25
26 /** This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives.
27 * The class can be understood as a program to be run over OpenGL, containing a sequence of viewport definitions,
28 * rendering primitives, etc...
29 *
30 * It can contain from 1 up to any number of <b>Viewports</b>, each one
31 * associated a set of OpenGL objects and, optionally, a preferred camera position. Both orthogonal (2D/3D) and projection
32 * camera models can be used for each viewport independently, greatly increasing the possibilities of rendered scenes.
33 *
34 * An object of COpenGLScene always contains at least one viewport (utils::COpenGLViewport), named "main". Optionally, any
35 * number of other viewports may exist. Viewports are referenced by their names, case-sensitive strings. Each viewport contains
36 * a different 3D scene (i.e. they render different objects), though a mechanism exist to share the same 3D scene by a number of
37 * viewports so memory is not wasted replicating the same objects (see COpenGLViewport::setCloneView ).
38 *
39 * The main rendering method, COpenGLScene::render(), assumes a viewport has been set-up for the entire target window. That
40 * method will internally make the required calls to opengl for creating the additional viewports. Note that only the depth
41 * buffer is cleared by default for each (non-main) viewport, to allow transparencies. This can be disabled by the approppriate
42 * member in COpenGLViewport.
43 *
44 * An object COpenGLScene can be saved to a ".3Dscene" file using CFileOutputStream, for posterior visualization from
45 * the standalone application <a href="http://www.mrpt.org/Application:SceneViewer" >SceneViewer</a>.
46 * It can be also displayed in real-time using gui::CDisplayWindow3D.
47 * \ingroup mrpt_opengl_grp
48 */
49 class OPENGL_IMPEXP COpenGLScene : public mrpt::utils::CSerializable
50 {
52 public:
53 /** Constructor
54 */
56
57 /** Destructor:
58 */
59 virtual ~COpenGLScene();
60
61 /** Copy operator:
62 */
63 COpenGLScene & operator =( const COpenGLScene &obj );
64
65 /** Copy constructor:
66 */
68
69 /**
70 * Inserts a set of objects into the scene, in the given viewport ("main" by default). Any iterable object will be accepted.
71 * \sa createViewport,getViewport
72 */
73 template<class T> inline void insertCollection(const T &objs,const std::string &vpn=std::string("main")) {
74 insert(objs.begin(),objs.end(),vpn);
75 }
76 /** Insert a new object into the scene, in the given viewport (by default, into the "main" viewport).
77 * The viewport must be created previously, an exception will be raised if the given name does not correspond to
78 * an existing viewport.
79 * \sa createViewport, getViewport
80 */
81 void insert( const CRenderizablePtr &newObject, const std::string &viewportName=std::string("main"));
82
83 /**
84 * Inserts a set of objects into the scene, in the given viewport ("main" by default).
85 * \sa createViewport,getViewport
86 */
87 template<class T_it> inline void insert(const T_it &begin,const T_it &end,const std::string &vpn=std::string("main")) {
88 for (T_it it=begin;it!=end;it++) insert(*it,vpn);
89 }
90
91 /**Creates a new viewport, adding it to the scene and returning a pointer to the new object.
92 * Names (case-sensitive) cannot be duplicated: if the name provided coincides with an already existing viewport, a pointer to the existing object will be returned.
93 * The first, default viewport, is named "main".
94 */
95 COpenGLViewportPtr createViewport( const std::string &viewportName );
96
97 /** Returns the viewport with the given name, or NULL if it does not exist; note that the default viewport is named "main" and initially occupies the entire rendering area.
98 */
99 COpenGLViewportPtr getViewport( const std::string &viewportName = std::string("main") ) const;
100
101 /** Render this scene */
102 void render() const;
103
104 size_t viewportsCount() const { return m_viewports.size(); }
105
106 /** Clear the list of objects and viewports in the scene, deleting objects' memory, and leaving just the default viewport with the default values.
107 */
108 void clear( bool createMainViewport = true );
109
110 /** If disabled (default), the SceneViewer application will ignore the camera of the "main" viewport and keep the viewport selected by the user by hand; otherwise, the camera in the "main" viewport prevails.
111 * \sa followCamera
112 */
113 void enableFollowCamera( bool enabled ) { m_followCamera = enabled; }
114
115 /** Return the value of "followCamera"
116 * \sa enableFollowCamera
117 */
118 bool followCamera() const { return m_followCamera; }
119
120 /** Returns the first object with a given name, or NULL (an empty smart pointer) if not found.
121 */
122 CRenderizablePtr getByName( const std::string &str, const std::string &viewportName = std::string("main") );
123
124 /** Returns the i'th object of a given class (or of a descendant class), or NULL (an empty smart pointer) if not found.
125 * Example:
126 * \code
127 CSpherePtr obs = myscene.getByClass<CSphere>();
128 * \endcode
129 * By default (ith=0), the first observation is returned.
130 */
131 template <typename T>
132 typename T::SmartPtr getByClass( const size_t &ith = 0 ) const
133 {
135 for (TListViewports::const_iterator it = m_viewports.begin();it!=m_viewports.end();++it)
136 {
137 typename T::SmartPtr o = (*it)->getByClass<T>(ith);
138 if (o.present()) return o;
139 }
140 return typename T::SmartPtr(); // Not found: return empty smart pointer
142 }
143
144
145 /** Removes the given object from the scene (it also deletes the object to free its memory).
146 */
147 void removeObject( const CRenderizablePtr &obj, const std::string &viewportName = std::string("main") );
148
149 /** Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL)
150 */
152
153 /** Retrieves a list of all objects in text form.
154 */
156
157 /** Saves the scene to a 3Dscene file, loadable by the application SceneViewer3D
158 * \sa loadFromFile
159 * \return false on any error.
160 */
161 bool saveToFile(const std::string &fil) const;
162
163 /** Loads the scene from a 3Dscene file, the format used by the application SceneViewer3D.
164 * \sa saveToFile
165 * \return false on any error.
166 */
167 bool loadFromFile(const std::string &fil);
168
169 /** Traces a ray
170 */
171 bool traceRay(const mrpt::poses::CPose3D&o,double &dist) const;
172
173 /** Evaluates the bounding box of the scene in the given viewport (default: "main"). */
174 void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max, const std::string &vpn=std::string("main") ) const;
175
176 /** Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied function
177 * The passed function must accept only one argument of type "const mrpt::opengl::CRenderizablePtr &"
178 */
179 template <typename FUNCTOR>
180 void visitAllObjects( FUNCTOR functor) const
181 {
183 for (TListViewports::const_iterator it = m_viewports.begin();it!=m_viewports.end();++it)
184 for (COpenGLViewport::const_iterator itO = (*it)->begin();itO!=(*it)->end();++itO)
185 internal_visitAllObjects(functor, *itO);
187 }
188
189 /** Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied function
190 * The passed function must accept a first argument of type "const mrpt::opengl::CRenderizablePtr &"
191 * and a second one of type EXTRA_PARAM
192 */
193 template <typename FUNCTOR,typename EXTRA_PARAM>
194 inline void visitAllObjects( FUNCTOR functor, const EXTRA_PARAM &userParam) const {
195 visitAllObjects( std::bind2nd(functor,userParam) );
196 }
197
198 protected:
200
201 typedef std::vector<COpenGLViewportPtr> TListViewports;
202
203 TListViewports m_viewports; //!< The list of viewports, indexed by name.
204
205
206 template <typename FUNCTOR>
207 static void internal_visitAllObjects(FUNCTOR functor, const CRenderizablePtr &o)
208 {
209 functor(o);
210 if (IS_CLASS(o,CSetOfObjects))
211 {
213 for (CSetOfObjects::const_iterator it=obj->begin();it!=obj->end();++it)
214 internal_visitAllObjects(functor,*it);
215 }
216 }
217
218 };
219 DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE( COpenGLScene, mrpt::utils::CSerializable, OPENGL_IMPEXP )
220
221 /** Inserts an openGL object into a scene. Allows call chaining. \sa mrpt::opengl::COpenGLScene::insert */
222 inline COpenGLScenePtr &operator<<(COpenGLScenePtr &s,const CRenderizablePtr &r) {
223 s->insert(r);
224 return s;
225 }
226 /**Inserts any iterable collection of openGL objects into a scene, allowing call chaining. \sa mrpt::opengl::COpenGLScene::insert */
227 template <class T> inline COpenGLScenePtr &operator<<(COpenGLScenePtr &s,const std::vector<T> &v) {
228 s->insert(v.begin(),v.end());
229 return s;
230 }
231 } // end namespace
232
233} // End of namespace
234
235
236#endif
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::utils::CSerializable) is of t...
Definition CObject.h:99
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE(class_name, base_name, _LINKAGE_)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
This class allows the user to create, load, save, and render 3D scenes using OpenGL primitives.
bool followCamera() const
Return the value of "followCamera".
std::vector< COpenGLViewportPtr > TListViewports
static void internal_visitAllObjects(FUNCTOR functor, const CRenderizablePtr &o)
bool saveToFile(const std::string &fil) const
Saves the scene to a 3Dscene file, loadable by the application SceneViewer3D.
void clear(bool createMainViewport=true)
Clear the list of objects and viewports in the scene, deleting objects' memory, and leaving just the ...
void removeObject(const CRenderizablePtr &obj, const std::string &viewportName=std::string("main"))
Removes the given object from the scene (it also deletes the object to free its memory).
virtual ~COpenGLScene()
Destructor:
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const
Traces a ray.
void initializeAllTextures()
Initializes all textures in the scene (See opengl::CTexturedPlane::loadTextureInOpenGL)
COpenGLViewportPtr getViewport(const std::string &viewportName=std::string("main")) const
Returns the viewport with the given name, or NULL if it does not exist; note that the default viewpor...
bool loadFromFile(const std::string &fil)
Loads the scene from a 3Dscene file, the format used by the application SceneViewer3D.
void visitAllObjects(FUNCTOR functor, const EXTRA_PARAM &userParam) const
Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied func...
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max, const std::string &vpn=std::string("main")) const
Evaluates the bounding box of the scene in the given viewport (default: "main").
CRenderizablePtr getByName(const std::string &str, const std::string &viewportName=std::string("main"))
Returns the first object with a given name, or NULL (an empty smart pointer) if not found.
void insert(const T_it &begin, const T_it &end, const std::string &vpn=std::string("main"))
Inserts a set of objects into the scene, in the given viewport ("main" by default).
COpenGLViewportPtr createViewport(const std::string &viewportName)
Creates a new viewport, adding it to the scene and returning a pointer to the new object.
void insertCollection(const T &objs, const std::string &vpn=std::string("main"))
Inserts a set of objects into the scene, in the given viewport ("main" by default).
COpenGLScene(const COpenGLScene &obj)
Copy constructor:
void visitAllObjects(FUNCTOR functor) const
Recursive depth-first visit all objects in all viewports of the scene, calling the user-supplied func...
T::SmartPtr getByClass(const size_t &ith=0) const
Returns the i'th object of a given class (or of a descendant class), or NULL (an empty smart pointer)...
size_t viewportsCount() const
void insert(const CRenderizablePtr &newObject, const std::string &viewportName=std::string("main"))
Insert a new object into the scene, in the given viewport (by default, into the "main" viewport).
void render() const
Render this scene.
void enableFollowCamera(bool enabled)
If disabled (default), the SceneViewer application will ignore the camera of the "main" viewport and ...
TListViewports m_viewports
The list of viewports, indexed by name.
void dumpListOfObjects(mrpt::utils::CStringList &lst)
Retrieves a list of all objects in text form.
CListOpenGLObjects::const_iterator const_iterator
A set of object, which are referenced to the coordinates framework established in this object.
CListOpenGLObjects::const_iterator const_iterator
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition CPose3D.h:73
A class for storing a list of text lines.
Definition CStringList.h:33
EIGEN_STRONG_INLINE iterator begin()
EIGEN_STRONG_INLINE iterator end()
#define MRPT_START
#define MRPT_END
struct OPENGL_IMPEXP COpenGLViewportPtr
struct OPENGL_IMPEXP CRenderizablePtr
struct OPENGL_IMPEXP COpenGLScenePtr
struct OPENGL_IMPEXP CSetOfObjectsPtr
OPENGL_IMPEXP mrpt::utils::CStream & operator<<(mrpt::utils::CStream &out, const mrpt::opengl::CLight &o)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Lightweight 3D point.



Page generated by Doxygen 1.9.7 for MRPT 1.4.0 SVN: at Tue Jun 13 13:56:43 UTC 2023