| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #ifndef OSGPRODUCER_CAMERA |
|---|
| 15 | #define OSGPRODUCER_CAMERA |
|---|
| 16 | |
|---|
| 17 | #include <osg/Referenced> |
|---|
| 18 | #include <osg/Matrix> |
|---|
| 19 | #include <osg/Vec3> |
|---|
| 20 | |
|---|
| 21 | #include "RenderSurface.h" |
|---|
| 22 | |
|---|
| 23 | #include <vector> |
|---|
| 24 | |
|---|
| 25 | namespace osgProducer { |
|---|
| 26 | |
|---|
| 27 | class CameraGroup; |
|---|
| 28 | class RenderSurface; |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | class Camera : public osg::Referenced |
|---|
| 48 | { |
|---|
| 49 | |
|---|
| 50 | public : |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | class Lens : public osg::Referenced |
|---|
| 66 | { |
|---|
| 67 | public : |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | enum Projection { |
|---|
| 71 | Perspective, |
|---|
| 72 | Orthographic, |
|---|
| 73 | Manual |
|---|
| 74 | }; |
|---|
| 75 | |
|---|
| 76 | Lens(); |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | void setMatrix( const osg::Matrix::value_type matrix[16] ); |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | void setPerspective( double hfov, double vfov, |
|---|
| 98 | double nearClip, double farClip ); |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | void setFrustum( double left, double right, |
|---|
| 111 | double bottom, double top, |
|---|
| 112 | double nearClip, double farClip ); |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | void setOrtho( double left, double right, |
|---|
| 117 | double bottom, double top, |
|---|
| 118 | double nearClip, double farClip ); |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | bool convertToOrtho( float d); |
|---|
| 127 | bool convertToPerspective( float d); |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | void apply(float xshear=0.0f, float yshear=0.0); |
|---|
| 131 | void generateMatrix( float xshear, float yshear, osg::Matrix::value_type matrix[16] ); |
|---|
| 132 | Projection getProjectionType() const { return _projection; } |
|---|
| 133 | void getParams( double &left, double &right, double &bottom, double &top, |
|---|
| 134 | double &nearClip, double &farClip ); |
|---|
| 135 | |
|---|
| 136 | float getHorizontalFov() const { return osg::RadiansToDegrees(_hfov); } |
|---|
| 137 | float getVerticalFov() const { return osg::RadiansToDegrees(_vfov); } |
|---|
| 138 | void setAutoAspect(bool ar) { _auto_aspect = ar; } |
|---|
| 139 | bool getAutoAspect() const { return _auto_aspect; } |
|---|
| 140 | void setAspectRatio( double aspectRatio ); |
|---|
| 141 | double getAspectRatio() { return _aspect_ratio; } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | protected: |
|---|
| 145 | ~Lens(){} |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | bool getFrustum( double& left, double& right, |
|---|
| 149 | double& bottom, double& top, |
|---|
| 150 | double& zNear, double& zFar ) const; |
|---|
| 151 | bool getOrtho( double& left, double& right, |
|---|
| 152 | double& bottom, double& top, |
|---|
| 153 | double& zNear, double& zFar ) const; |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | private : |
|---|
| 157 | |
|---|
| 158 | double _ortho_left, _ortho_right, _ortho_bottom, _ortho_top; |
|---|
| 159 | double _left, _right, _bottom, _top, _nearClip, _farClip; |
|---|
| 160 | Projection _projection; |
|---|
| 161 | double _aspect_ratio; |
|---|
| 162 | bool _auto_aspect; |
|---|
| 163 | float _hfov, _vfov; |
|---|
| 164 | osg::Matrix::value_type _matrix[16]; |
|---|
| 165 | |
|---|
| 166 | private : |
|---|
| 167 | void _updateFOV( void ); |
|---|
| 168 | }; |
|---|
| 169 | |
|---|
| 170 | struct Offset { |
|---|
| 171 | enum MultiplyMethod { |
|---|
| 172 | PreMultiply, |
|---|
| 173 | PostMultiply |
|---|
| 174 | }; |
|---|
| 175 | double _xshear; |
|---|
| 176 | double _yshear; |
|---|
| 177 | osg::Matrix::value_type _matrix[16]; |
|---|
| 178 | MultiplyMethod _multiplyMethod; |
|---|
| 179 | Offset(): |
|---|
| 180 | _xshear(0.0), |
|---|
| 181 | _yshear(0.0), |
|---|
| 182 | _multiplyMethod(PreMultiply) {} |
|---|
| 183 | }; |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | public : |
|---|
| 187 | Camera( void ); |
|---|
| 188 | |
|---|
| 189 | void setRenderSurface( RenderSurface *rs ) { _rs = rs; } |
|---|
| 190 | RenderSurface *getRenderSurface() { return _rs.get(); } |
|---|
| 191 | const RenderSurface *getRenderSurface() const { return _rs.get(); } |
|---|
| 192 | |
|---|
| 193 | void setRenderSurfaceWindowRectangle( int x, int y, unsigned int width, unsigned int height, bool resize=true ) |
|---|
| 194 | { _rs->setWindowRectangle(x,y,width,height, resize); } |
|---|
| 195 | |
|---|
| 196 | void setLens( Lens *lens ) |
|---|
| 197 | { |
|---|
| 198 | if( _lens.get() != lens ) |
|---|
| 199 | _lens = lens; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | Lens *getLens() { return _lens.get(); } |
|---|
| 203 | const Lens *getLens() const { return _lens.get(); } |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | void setLensPerspective( double hfov, double vfov, |
|---|
| 209 | double nearClip, double farClip, |
|---|
| 210 | double xshear=0, double yshear=0 ) |
|---|
| 211 | { |
|---|
| 212 | _offset._xshear = xshear; |
|---|
| 213 | _offset._yshear = yshear; |
|---|
| 214 | _lens->setPerspective(hfov,vfov,nearClip,farClip); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | void setLensFrustum( double left, double right, |
|---|
| 220 | double bottom, double top, |
|---|
| 221 | double nearClip, double farClip, |
|---|
| 222 | double xshear=0, double yshear=0 ) |
|---|
| 223 | { |
|---|
| 224 | _offset._xshear = xshear; |
|---|
| 225 | _offset._yshear = yshear; |
|---|
| 226 | _lens->setFrustum(left,right,bottom,top,nearClip, farClip); |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | void setLensOrtho( double left, double right, |
|---|
| 232 | double bottom, double top, |
|---|
| 233 | double nearClip, double farClip , |
|---|
| 234 | double xshear=0, double yshear=0 ) |
|---|
| 235 | { |
|---|
| 236 | _offset._xshear = xshear; |
|---|
| 237 | _offset._yshear = yshear; |
|---|
| 238 | _lens->setOrtho( left, right, bottom, top, nearClip, farClip); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | void setLensShear( double xshear, double yshear ) |
|---|
| 243 | { |
|---|
| 244 | _offset._xshear = xshear; |
|---|
| 245 | _offset._yshear = yshear; |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | void getLensShear( double &xshear, double &yshear ) |
|---|
| 250 | { |
|---|
| 251 | xshear = _offset._xshear; |
|---|
| 252 | yshear = _offset._yshear; |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | bool convertLensToOrtho( float d) { return _lens->convertToOrtho(d); } |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | bool convertLensToPerspective( float d) { return _lens->convertToPerspective(d); } |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | Lens::Projection getLensProjectionType() { return _lens->getProjectionType(); } |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | void applyLens() { _lens->apply(_offset._xshear, _offset._yshear); } |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | void getLensParams( double &left, double &right, double &bottom, double &top, |
|---|
| 273 | double &nearClip, double &farClip, double &xshear, double &yshear ) |
|---|
| 274 | { |
|---|
| 275 | _lens->getParams(left,right,bottom,top,nearClip,farClip ); |
|---|
| 276 | xshear = _offset._xshear; |
|---|
| 277 | yshear = _offset._yshear; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | float getLensHorizontalFov() { return _lens->getHorizontalFov(); } |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | float getLensVerticalFov() { return _lens->getVerticalFov(); } |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | void getLensMatrix(osg::Matrix::value_type matrix[16] ) |
|---|
| 296 | { |
|---|
| 297 | _lens->generateMatrix(_offset._xshear, _offset._yshear, matrix ); |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | void setLensAutoAspect(bool ar) { _lens->setAutoAspect(ar); } |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | bool getLensAutoAspect() { return _lens->getAutoAspect(); } |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | void setLensAspectRatio( double aspectRatio ) { _lens->setAspectRatio(aspectRatio); } |
|---|
| 311 | double getLensAspectRatio() { return _lens->getAspectRatio(); } |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | void setProjectionRectangle( const float left, const float right, |
|---|
| 316 | const float bottom, const float top ); |
|---|
| 317 | |
|---|
| 318 | void getProjectionRectangle( float &left, float &right, |
|---|
| 319 | float &bottom, float &top ) const; |
|---|
| 320 | |
|---|
| 321 | void setProjectionRectangle( int x, int y, unsigned int width, unsigned int height ); |
|---|
| 322 | void getProjectionRectangle( int &x, int &y, unsigned int &width, unsigned int &height ) const ; |
|---|
| 323 | |
|---|
| 324 | osg::Matrix::value_type *getProjectionMatrix () |
|---|
| 325 | { |
|---|
| 326 | _lens->generateMatrix(_offset._xshear, _offset._yshear, _projectionMatrix ); |
|---|
| 327 | return _projectionMatrix; |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | void setViewByLookat( float eyex, float eyey, float eyez, |
|---|
| 331 | float centerx, float centery, float centerz, |
|---|
| 332 | float upx, float upy, float upz ); |
|---|
| 333 | void setViewByLookat( const osg::Vec3 &eye, const osg::Vec3 ¢er, const osg::Vec3 &up ); |
|---|
| 334 | void setViewByMatrix( const osg::Matrix &mat ); |
|---|
| 335 | void setFocalDistance( double focal_distance ) { _focal_distance = focal_distance; } |
|---|
| 336 | const osg::Matrix::value_type *getViewMatrix( void ) const; |
|---|
| 337 | const osg::Matrix::value_type *getPositionAndAttitudeMatrix( void ) const { return _viewMatrix; } |
|---|
| 338 | void applyView(); |
|---|
| 339 | |
|---|
| 340 | void setOffset( const osg::Matrix::value_type matrix[16], |
|---|
| 341 | osg::Matrix::value_type _xshear=0.0, |
|---|
| 342 | osg::Matrix::value_type _yshear=0.0); |
|---|
| 343 | void setOffset( double _xshear, double _yshear); |
|---|
| 344 | void setOffsetMultiplyMethod( Offset::MultiplyMethod method ) |
|---|
| 345 | { |
|---|
| 346 | _offset._multiplyMethod = method; |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | void setClearColor( float red, float green, float blue, float alpha); |
|---|
| 351 | void getClearColor( float& red, float& green, float& blue, float& alpha); |
|---|
| 352 | |
|---|
| 353 | void clear( void ); |
|---|
| 354 | |
|---|
| 355 | void setIndex( unsigned int index ) { _index = index; } |
|---|
| 356 | unsigned int getIndex() const { return _index; } |
|---|
| 357 | |
|---|
| 358 | void setShareLens( bool flag ) { _shareLens = flag; } |
|---|
| 359 | bool getShareLens() { return _shareLens; } |
|---|
| 360 | void setShareView( bool flag ) { _shareView = flag; } |
|---|
| 361 | bool getShareView() { return _shareView; } |
|---|
| 362 | |
|---|
| 363 | protected : |
|---|
| 364 | |
|---|
| 365 | virtual ~Camera( void ); |
|---|
| 366 | osg::ref_ptr<Lens> _lens; |
|---|
| 367 | osg::ref_ptr<RenderSurface> _rs; |
|---|
| 368 | unsigned int _index; |
|---|
| 369 | |
|---|
| 370 | private : |
|---|
| 371 | bool _initialized; |
|---|
| 372 | bool _initialize(void); |
|---|
| 373 | |
|---|
| 374 | bool _enabled; |
|---|
| 375 | |
|---|
| 376 | float _projrectLeft, |
|---|
| 377 | _projrectRight, |
|---|
| 378 | _projrectBottom, |
|---|
| 379 | _projrectTop; |
|---|
| 380 | |
|---|
| 381 | Offset _offset; |
|---|
| 382 | osg::Matrix::value_type _projectionMatrix[16]; |
|---|
| 383 | osg::Matrix::value_type _viewMatrix[16]; |
|---|
| 384 | float _clear_color[4]; |
|---|
| 385 | double _focal_distance; |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | friend class CameraGroup; |
|---|
| 389 | |
|---|
| 390 | bool _shareLens; |
|---|
| 391 | bool _shareView; |
|---|
| 392 | }; |
|---|
| 393 | |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | #endif |
|---|