00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef opengl_COpenGLViewport_H
00029 #define opengl_COpenGLViewport_H
00030
00031 #include <mrpt/utils/CSerializable.h>
00032 #include <mrpt/utils/safe_pointers.h>
00033 #include <mrpt/opengl/CCamera.h>
00034 #include <mrpt/opengl/CSetOfObjects.h>
00035 #include <mrpt/math/lightweight_geom_data.h>
00036
00037 namespace mrpt
00038 {
00039 namespace utils { class CStringList; }
00040
00041 using namespace mrpt::math;
00042
00043
00044
00045 namespace opengl
00046 {
00047 class COpenGLScene;
00048 class CRenderizable;
00049
00050
00051 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE_LINKAGE( COpenGLViewport, mrpt::utils::CSerializable, OPENGL_IMPEXP )
00052
00053
00054
00055
00056
00057 class OPENGL_IMPEXP COpenGLViewport : public mrpt::utils::CSerializable
00058 {
00059 DEFINE_SERIALIZABLE( COpenGLViewport )
00060
00061 friend class COpenGLScene;
00062 public:
00063
00064 typedef CListOpenGLObjects::const_iterator const_iterator;
00065 typedef CListOpenGLObjects::iterator iterator;
00066
00067 inline const_iterator begin() const { return m_objects.begin(); }
00068 inline const_iterator end() const { return m_objects.end(); }
00069 inline iterator begin() { return m_objects.begin(); }
00070 inline iterator end() { return m_objects.end(); }
00071
00072
00073
00074
00075
00076
00077 void setCloneView( const std::string &clonedViewport );
00078
00079
00080
00081
00082 inline void resetCloneView() { m_isCloned=false;m_isClonedCamera=false; }
00083
00084
00085
00086 inline void setCloneCamera(bool enable) { m_isClonedCamera = enable; }
00087
00088
00089
00090
00091
00092 void clear();
00093
00094
00095
00096
00097 void insert( const CRenderizablePtr &newObject );
00098
00099
00100 inline std::string getName() { return m_name; }
00101
00102
00103
00104
00105
00106
00107
00108 void setViewportPosition(
00109 const double x,
00110 const double y,
00111 const double width,
00112 const double height );
00113
00114
00115
00116
00117
00118
00119
00120 void getViewportPosition(
00121 double &x,
00122 double &y,
00123 double &width,
00124 double &height );
00125
00126
00127
00128
00129 void setViewportClipDistances(const double clip_min, const double clip_max);
00130
00131
00132
00133
00134 void getViewportClipDistances(double &clip_min, double &clip_max) const;
00135
00136
00137
00138 inline void setBorderSize( unsigned int lineWidth ) { m_borderWidth = lineWidth; }
00139
00140
00141
00142 inline bool isTransparent() { return m_isTransparent; }
00143
00144
00145
00146 inline void setTransparent( bool trans ) { m_isTransparent=trans; }
00147
00148
00149 inline void setCustomBackgroundColor( const TColorf &color ) { m_custom_backgb_color = true; m_background_color = color; }
00150
00151 inline bool hasCustomBackgroundColor() const { return m_custom_backgb_color; }
00152
00153 inline TColorf getCustomBackgroundColor() const { return m_background_color; }
00154
00155
00156 virtual ~COpenGLViewport();
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 void get3DRayForPixelCoord( const double x_coord, const double y_coord, mrpt::math::TLine3D &out_ray, mrpt::poses::CPose3D *out_cameraPose=NULL ) const;
00167
00168
00169
00170
00171 void getCurrentCameraPose( mrpt::poses::CPose3D &out_cameraPose ) const;
00172
00173
00174
00175 CRenderizablePtr getByName( const std::string &str );
00176
00177
00178
00179
00180
00181
00182
00183
00184 template <typename T>
00185 typename T::SmartPtr getByClass( const size_t &ith = 0 ) const
00186 {
00187 MRPT_START;
00188 size_t foundCount = 0;
00189 const mrpt::utils::TRuntimeClassId* class_ID = T::classinfo;
00190 for (CListOpenGLObjects::const_iterator it = m_objects.begin();it!=m_objects.end();++it)
00191 if ( (*it).present() && (*it)->GetRuntimeClass()->derivedFrom( class_ID ) )
00192 if (foundCount++ == ith)
00193 return typename T::SmartPtr(*it);
00194
00195
00196 for (CListOpenGLObjects::const_iterator it=m_objects.begin();it!=m_objects.end();++it)
00197 {
00198 if ( (*it).present() && (*it)->GetRuntimeClass() == CLASS_ID_NAMESPACE(CSetOfObjects,mrpt::opengl))
00199 {
00200 typename T::SmartPtr o = CSetOfObjectsPtr(*it)->getByClass<T>(ith);
00201 if (o.present()) return o;
00202 }
00203 }
00204 return typename T::SmartPtr();
00205 MRPT_END;
00206 }
00207
00208
00209
00210
00211
00212 void removeObject( const CRenderizablePtr & obj );
00213
00214
00215 inline size_t size() const { return m_objects.size(); }
00216
00217 inline bool empty() const { return m_objects.empty(); }
00218
00219
00220 opengl::CCamera& getCamera() { return m_camera;}
00221
00222 const opengl::CCamera & getCamera() const { return m_camera;}
00223
00224 protected:
00225
00226
00227 COpenGLViewport( COpenGLScene *parent=NULL, const std::string &name=std::string("") );
00228
00229
00230
00231 void initializeAllTextures();
00232
00233
00234
00235 void dumpListOfObjects( utils::CStringList &lst );
00236
00237
00238 void render( const int render_width, const int render_height ) const;
00239
00240
00241 opengl::CCamera m_camera;
00242 utils::safe_ptr<COpenGLScene> m_parent;
00243 bool m_isCloned;
00244 bool m_isClonedCamera;
00245 std::string m_clonedViewport;
00246 std::string m_name;
00247 bool m_isTransparent;
00248 uint32_t m_borderWidth;
00249 double m_view_x, m_view_y,m_view_width,m_view_height;
00250 double m_clip_min,m_clip_max;
00251 bool m_custom_backgb_color;
00252 TColorf m_background_color;
00253
00254 struct TLastProjectiveMatrixInfo
00255 {
00256 TLastProjectiveMatrixInfo() : is_projective(true),eye(0,0,0),pointing(0,0,0),up(0,0,0), FOV(30), viewport_width(640), viewport_height(480), azimuth(0), elev(0), zoom(1)
00257 {}
00258 bool is_projective;
00259 TPoint3D eye;
00260 TPoint3D pointing;
00261 TPoint3D up;
00262 float FOV;
00263 size_t viewport_width, viewport_height;
00264 float azimuth, elev;
00265 float zoom;
00266 };
00267 mutable TLastProjectiveMatrixInfo m_lastProjMat;
00268
00269
00270
00271
00272 opengl::CListOpenGLObjects m_objects;
00273
00274 };
00275
00276
00277
00278
00279 inline COpenGLViewportPtr &operator<<(COpenGLViewportPtr &s,const CRenderizablePtr &r) {
00280 s->insert(r);
00281 return s;
00282 }
00283
00284
00285
00286
00287 inline COpenGLViewportPtr &operator<<(COpenGLViewportPtr &s,const std::vector<CRenderizablePtr> &v) {
00288 for (std::vector<CRenderizablePtr>::const_iterator it=v.begin();it!=v.end();++it) s->insert(*it);
00289 return s;
00290 }
00291
00292 }
00293
00294 }
00295
00296
00297 #endif