| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #ifdef WIN32 |
|---|
| 15 | #include <windows.h> |
|---|
| 16 | #endif |
|---|
| 17 | |
|---|
| 18 | #include <string.h> |
|---|
| 19 | #include <algorithm> |
|---|
| 20 | |
|---|
| 21 | #include "Camera.h" |
|---|
| 22 | |
|---|
| 23 | using namespace osgProducer; |
|---|
| 24 | |
|---|
| 25 | Camera::Camera( void ) |
|---|
| 26 | { |
|---|
| 27 | _index = 0; |
|---|
| 28 | |
|---|
| 29 | _projrectLeft = 0.0; |
|---|
| 30 | _projrectRight = 1.0; |
|---|
| 31 | _projrectBottom = 0.0; |
|---|
| 32 | _projrectTop = 1.0; |
|---|
| 33 | |
|---|
| 34 | osg::Matrix::value_type id[] = { |
|---|
| 35 | 1, 0, 0, 0, |
|---|
| 36 | 0, 1, 0, 0, |
|---|
| 37 | 0, 0, 1, 0, |
|---|
| 38 | 0, 0, 0, 1 |
|---|
| 39 | }; |
|---|
| 40 | |
|---|
| 41 | memcpy( _viewMatrix, id, sizeof(osg::Matrix::value_type[16])); |
|---|
| 42 | _offset._xshear = _offset._yshear = 0.0f; |
|---|
| 43 | memcpy( _offset._matrix, id, sizeof(osg::Matrix::value_type[16])); |
|---|
| 44 | _offset._multiplyMethod = Offset::PreMultiply; |
|---|
| 45 | |
|---|
| 46 | _lens = new Lens; |
|---|
| 47 | _lens->setAutoAspect(true); |
|---|
| 48 | _rs = new RenderSurface; |
|---|
| 49 | |
|---|
| 50 | _clear_color[0] = 0.2f; |
|---|
| 51 | _clear_color[1] = 0.2f; |
|---|
| 52 | _clear_color[2] = 0.4f; |
|---|
| 53 | _clear_color[3] = 1.0f; |
|---|
| 54 | |
|---|
| 55 | _focal_distance = 1.0; |
|---|
| 56 | |
|---|
| 57 | _shareLens = true; |
|---|
| 58 | _shareView = true; |
|---|
| 59 | |
|---|
| 60 | _enabled = true; |
|---|
| 61 | _initialized = false; |
|---|
| 62 | |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | Camera::~Camera( void ) |
|---|
| 66 | { |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | const osg::Matrix::value_type * Camera::getViewMatrix( void ) const |
|---|
| 71 | { |
|---|
| 72 | return _viewMatrix; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | void Camera::setViewByMatrix( const osg::Matrix &mat ) |
|---|
| 76 | { |
|---|
| 77 | osg::Matrix m; |
|---|
| 78 | if ( _offset._multiplyMethod == Offset::PostMultiply ) |
|---|
| 79 | m = osg::Matrix( _offset._matrix ) * mat; |
|---|
| 80 | else if( _offset._multiplyMethod == Offset::PreMultiply ) |
|---|
| 81 | m = mat * osg::Matrix( _offset._matrix ); |
|---|
| 82 | memcpy( _viewMatrix, m.ptr(), sizeof( osg::Matrix::value_type[16] )); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | void Camera::setViewByLookat( const osg::Vec3 &eye, const osg::Vec3 ¢er, const osg::Vec3 &up ) |
|---|
| 86 | { |
|---|
| 87 | osg::Matrix m; |
|---|
| 88 | m.makeLookAt(eye,center,up); |
|---|
| 89 | setViewByMatrix( m ); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | void Camera::setViewByLookat( float eyeX, float eyeY, float eyeZ, |
|---|
| 93 | float centerX, float centerY, float centerZ, |
|---|
| 94 | float upX, float upY, float upZ ) |
|---|
| 95 | { |
|---|
| 96 | setViewByLookat( osg::Vec3(eyeX, eyeY, eyeZ), |
|---|
| 97 | osg::Vec3(centerX, centerY, centerZ ), |
|---|
| 98 | osg::Vec3(upX, upY, upZ) ); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | Camera::Lens::Lens( void ) |
|---|
| 103 | { |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | _left = -0.32; |
|---|
| 115 | _right = 0.32; |
|---|
| 116 | _bottom = -0.26; |
|---|
| 117 | _top = 0.26; |
|---|
| 118 | _ortho_left = -1.0; |
|---|
| 119 | _ortho_right = 1.0; |
|---|
| 120 | _ortho_bottom = -1.0; |
|---|
| 121 | _ortho_top = 1.0; |
|---|
| 122 | _nearClip = 1.0; |
|---|
| 123 | _farClip = 1e6; |
|---|
| 124 | _updateFOV(); |
|---|
| 125 | _projection = Perspective; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | void Camera::Lens::setAspectRatio( double aspectRatio ) |
|---|
| 129 | { |
|---|
| 130 | _aspect_ratio = aspectRatio; |
|---|
| 131 | _left = -0.5 * (_top - _bottom) * _aspect_ratio; |
|---|
| 132 | _right = 0.5 * (_top - _bottom) * _aspect_ratio; |
|---|
| 133 | _ortho_left = -0.5 * (_ortho_top - _ortho_bottom) * _aspect_ratio; |
|---|
| 134 | _ortho_right = 0.5 * (_ortho_top - _ortho_bottom) * _aspect_ratio; |
|---|
| 135 | |
|---|
| 136 | if( _projection == Perspective ) |
|---|
| 137 | _updateFOV(); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | void Camera::Lens::setPerspective( double hfov, double vfov, |
|---|
| 141 | double nearClip, double farClip ) |
|---|
| 142 | { |
|---|
| 143 | _hfov = osg::DegreesToRadians(hfov); |
|---|
| 144 | _vfov = osg::DegreesToRadians(vfov); |
|---|
| 145 | _aspect_ratio = tan(0.5*_hfov)/tan(0.5*_vfov); |
|---|
| 146 | |
|---|
| 147 | _nearClip = nearClip; |
|---|
| 148 | _farClip = farClip; |
|---|
| 149 | |
|---|
| 150 | _left = -_nearClip * tan(_hfov/2.0); |
|---|
| 151 | _right = _nearClip * tan(_hfov/2.0); |
|---|
| 152 | _bottom = -_nearClip * tan(_vfov/2.0); |
|---|
| 153 | _top = _nearClip * tan(_vfov/2.0); |
|---|
| 154 | |
|---|
| 155 | _projection = Perspective; |
|---|
| 156 | setAutoAspect(false); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | void Camera::Lens::setFrustum( double left, double right, |
|---|
| 160 | double bottom, double top, |
|---|
| 161 | double nearClip, double farClip ) |
|---|
| 162 | { |
|---|
| 163 | _left = left; |
|---|
| 164 | _right = right; |
|---|
| 165 | _bottom = bottom; |
|---|
| 166 | _top = top; |
|---|
| 167 | _nearClip = nearClip; |
|---|
| 168 | _farClip = farClip; |
|---|
| 169 | _projection = Perspective; |
|---|
| 170 | _updateFOV(); |
|---|
| 171 | setAutoAspect(false); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | void Camera::Lens::setOrtho( double left, double right, |
|---|
| 175 | double bottom, double top, |
|---|
| 176 | double nearClip, double farClip ) |
|---|
| 177 | { |
|---|
| 178 | _ortho_left = left; |
|---|
| 179 | _ortho_right = right; |
|---|
| 180 | _ortho_bottom = bottom; |
|---|
| 181 | _ortho_top = top; |
|---|
| 182 | _nearClip = nearClip; |
|---|
| 183 | _farClip = farClip; |
|---|
| 184 | _projection = Orthographic; |
|---|
| 185 | setAutoAspect(false); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | void Camera::Lens::setMatrix( const osg::Matrix::value_type matrix[16] ) |
|---|
| 189 | { |
|---|
| 190 | memcpy( _matrix, matrix, sizeof(osg::Matrix::value_type[16]) ); |
|---|
| 191 | _projection = Manual; |
|---|
| 192 | setAutoAspect(false); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | bool Camera::Lens::getFrustum( double& left, double& right, |
|---|
| 196 | double& bottom, double& top, |
|---|
| 197 | double& zNear, double& zFar ) const |
|---|
| 198 | { |
|---|
| 199 | |
|---|
| 200 | if (_matrix[3]!=0.0 || _matrix[7]!=0.0 || _matrix[11]!=-1.0 || _matrix[15]!=0.0) return false; |
|---|
| 201 | |
|---|
| 202 | zNear = _matrix[14] / (_matrix[10]-1.0); |
|---|
| 203 | zFar = _matrix[14] / (1.0+_matrix[10]); |
|---|
| 204 | |
|---|
| 205 | left = zNear * (_matrix[8]-1.0) / _matrix[0]; |
|---|
| 206 | right = zNear * (1.0+_matrix[8]) / _matrix[0]; |
|---|
| 207 | |
|---|
| 208 | top = zNear * (1.0+_matrix[9]) / _matrix[5]; |
|---|
| 209 | bottom = zNear * (_matrix[9]-1.0) / _matrix[5]; |
|---|
| 210 | |
|---|
| 211 | return true; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | bool Camera::Lens::getOrtho( double& left, double& right, |
|---|
| 215 | double& bottom, double& top, |
|---|
| 216 | double& zNear, double& zFar ) const |
|---|
| 217 | { |
|---|
| 218 | |
|---|
| 219 | if (_matrix[3]!=0.0 || _matrix[7]!=0.0 || _matrix[11]!=0.0 || _matrix[15]!=1.0) return false; |
|---|
| 220 | |
|---|
| 221 | zNear = (_matrix[14]+1.0) / _matrix[10]; |
|---|
| 222 | zFar = (_matrix[14]-1.0) / _matrix[10]; |
|---|
| 223 | |
|---|
| 224 | left = -(1.0+_matrix[12]) / _matrix[0]; |
|---|
| 225 | right = (1.0-_matrix[12]) / _matrix[0]; |
|---|
| 226 | |
|---|
| 227 | bottom = -(1.0+_matrix[13]) / _matrix[5]; |
|---|
| 228 | top = (1.0-_matrix[13]) / _matrix[5]; |
|---|
| 229 | |
|---|
| 230 | return true; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | bool Camera::Lens::convertToOrtho( float d ) |
|---|
| 234 | { |
|---|
| 235 | |
|---|
| 236 | if( _projection == Manual ) |
|---|
| 237 | { |
|---|
| 238 | |
|---|
| 239 | if( !getFrustum(_left,_right,_bottom,_top,_nearClip,_farClip) ) |
|---|
| 240 | return false; |
|---|
| 241 | |
|---|
| 242 | _updateFOV(); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | double s = d * tan(_vfov*0.5); |
|---|
| 246 | _ortho_bottom = -s; |
|---|
| 247 | _ortho_top = s; |
|---|
| 248 | _ortho_left = -s*_aspect_ratio; |
|---|
| 249 | _ortho_right = s*_aspect_ratio; |
|---|
| 250 | _projection = Orthographic; |
|---|
| 251 | return true; |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | bool Camera::Lens::convertToPerspective( float d ) |
|---|
| 255 | { |
|---|
| 256 | |
|---|
| 257 | if( _projection == Manual ) |
|---|
| 258 | { |
|---|
| 259 | |
|---|
| 260 | if( !getOrtho(_ortho_left,_ortho_right,_ortho_bottom,_ortho_top,_nearClip,_farClip) ) |
|---|
| 261 | return false; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | double hfov = 2 * atan( 0.5 * (_ortho_right - _ortho_left)/d); |
|---|
| 265 | double vfov = 2 * atan( 0.5 * (_ortho_top - _ortho_bottom)/d); |
|---|
| 266 | |
|---|
| 267 | _left = -_nearClip * tan(hfov*0.5); |
|---|
| 268 | _right = _nearClip * tan(hfov*0.5); |
|---|
| 269 | _bottom = -_nearClip * tan(vfov*0.5); |
|---|
| 270 | _top = _nearClip * tan(vfov*0.5); |
|---|
| 271 | |
|---|
| 272 | _projection = Perspective; |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | return true; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | void Camera::Lens::apply(float xshear, float yshear) |
|---|
| 279 | { |
|---|
| 280 | osg::Matrix::value_type _matrix[16]; |
|---|
| 281 | generateMatrix(xshear,yshear,_matrix); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | void Camera::Lens::getParams( double &left, double &right, double &bottom, double &top, |
|---|
| 285 | double &nearClip, double &farClip ) |
|---|
| 286 | { |
|---|
| 287 | if( _projection == Perspective ) |
|---|
| 288 | { |
|---|
| 289 | left = _left; |
|---|
| 290 | right = _right; |
|---|
| 291 | bottom = _bottom; |
|---|
| 292 | top = _top; |
|---|
| 293 | } |
|---|
| 294 | else if( _projection == Orthographic ) |
|---|
| 295 | { |
|---|
| 296 | left = _ortho_left; |
|---|
| 297 | right = _ortho_right; |
|---|
| 298 | bottom = _ortho_bottom; |
|---|
| 299 | top = _ortho_top; |
|---|
| 300 | } |
|---|
| 301 | else if( _projection == Manual ) |
|---|
| 302 | { |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | if(getFrustum(left,right,bottom,top,nearClip,farClip)) |
|---|
| 306 | return; |
|---|
| 307 | |
|---|
| 308 | if(getOrtho(left,right,bottom,top,nearClip,farClip)) |
|---|
| 309 | return; |
|---|
| 310 | |
|---|
| 311 | left = _left; |
|---|
| 312 | right = _right; |
|---|
| 313 | bottom = _bottom; |
|---|
| 314 | top = _top; |
|---|
| 315 | } |
|---|
| 316 | nearClip = _nearClip; |
|---|
| 317 | farClip = _farClip; |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | void Camera::setProjectionRectangle( const float left, const float right, |
|---|
| 321 | const float bottom, const float top ) |
|---|
| 322 | { |
|---|
| 323 | _projrectLeft = left; |
|---|
| 324 | _projrectRight = right; |
|---|
| 325 | _projrectBottom = bottom; |
|---|
| 326 | _projrectTop = top; |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | void Camera::getProjectionRectangle( float &left, float &right, |
|---|
| 330 | float &bottom, float &top ) const |
|---|
| 331 | { |
|---|
| 332 | left = _projrectLeft; |
|---|
| 333 | right = _projrectRight; |
|---|
| 334 | bottom = _projrectBottom; |
|---|
| 335 | top = _projrectTop; |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | void Camera::setProjectionRectangle( int x, int y, unsigned int width, unsigned int height ) |
|---|
| 339 | { |
|---|
| 340 | int _x, _y; |
|---|
| 341 | unsigned int _w, _h; |
|---|
| 342 | |
|---|
| 343 | _rs->getWindowRectangle( _x, _y, _w, _h ); |
|---|
| 344 | #if 0 |
|---|
| 345 | if( _w == osgProducer::RenderSurface::UnknownDimension || _h == Producer::RenderSurface::UnknownDimension) |
|---|
| 346 | { |
|---|
| 347 | unsigned int ww; |
|---|
| 348 | unsigned int hh; |
|---|
| 349 | _rs->getScreenSize( ww, hh ); |
|---|
| 350 | if( _w == osgProducer::RenderSurface::UnknownDimension ) |
|---|
| 351 | _w = ww; |
|---|
| 352 | if( _h == osgProducer::RenderSurface::UnknownDimension ) |
|---|
| 353 | _h = hh; |
|---|
| 354 | } |
|---|
| 355 | #endif |
|---|
| 356 | |
|---|
| 357 | _projrectLeft = float(x - _x)/float(_w); |
|---|
| 358 | _projrectRight = float((x + width) - _x)/float(_w); |
|---|
| 359 | _projrectBottom = float(y - _y)/float(_h); |
|---|
| 360 | _projrectTop = float((y+height) - _y)/float(_h); |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | void Camera::getProjectionRectangle( int &x, int &y, unsigned int &width, unsigned int &height ) const |
|---|
| 364 | { |
|---|
| 365 | int _x, _y; |
|---|
| 366 | unsigned int _w, _h; |
|---|
| 367 | float fx, fy, fw, fh; |
|---|
| 368 | |
|---|
| 369 | _rs->getWindowRectangle( _x, _y, _w, _h ); |
|---|
| 370 | #if 0 |
|---|
| 371 | if( _w == Producer::RenderSurface::UnknownDimension || _h == Producer::RenderSurface::UnknownDimension ) |
|---|
| 372 | { |
|---|
| 373 | unsigned int ww; |
|---|
| 374 | unsigned int hh; |
|---|
| 375 | _rs->getScreenSize( ww, hh ); |
|---|
| 376 | if( _w == Producer::RenderSurface::UnknownDimension ) |
|---|
| 377 | _w = ww; |
|---|
| 378 | if( _h == Producer::RenderSurface::UnknownDimension ) |
|---|
| 379 | _h = hh; |
|---|
| 380 | } |
|---|
| 381 | #endif |
|---|
| 382 | |
|---|
| 383 | fx = _projrectLeft * _w; |
|---|
| 384 | fy = _projrectBottom * _h; |
|---|
| 385 | fw = _w * _projrectRight; |
|---|
| 386 | fh = _h * _projrectTop; |
|---|
| 387 | |
|---|
| 388 | x = int(fx); |
|---|
| 389 | y = int(fy); |
|---|
| 390 | |
|---|
| 391 | width = int(fw) - x; |
|---|
| 392 | height = int(fh) - y; |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | void Camera::setClearColor( float r, float g, float b, float a ) |
|---|
| 396 | { |
|---|
| 397 | _clear_color[0] = r; |
|---|
| 398 | _clear_color[1] = g; |
|---|
| 399 | _clear_color[2] = b; |
|---|
| 400 | _clear_color[3] = a; |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | void Camera::getClearColor( float& red, float& green, float& blue, float& alpha) |
|---|
| 404 | { |
|---|
| 405 | red = _clear_color[0]; |
|---|
| 406 | green = _clear_color[1]; |
|---|
| 407 | blue = _clear_color[2]; |
|---|
| 408 | alpha = _clear_color[3]; |
|---|
| 409 | } |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | void Camera::clear( void ) |
|---|
| 413 | { |
|---|
| 414 | #if 0 |
|---|
| 415 | if( !_initialized ) _initialize(); |
|---|
| 416 | int x, y; |
|---|
| 417 | unsigned int w, h; |
|---|
| 418 | getProjectionRectangle( x, y, w, h ); |
|---|
| 419 | glViewport( x, y, w, h ); |
|---|
| 420 | glScissor( x, y, w, h ); |
|---|
| 421 | glClearColor( _clear_color[0], _clear_color[1], _clear_color[2], _clear_color[3] ); |
|---|
| 422 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT ); |
|---|
| 423 | #endif |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | |
|---|
| 427 | #if 0 |
|---|
| 428 | void Camera::Lens::_updateMatrix( void ) |
|---|
| 429 | { |
|---|
| 430 | switch( _projection ) |
|---|
| 431 | { |
|---|
| 432 | case Perspective : |
|---|
| 433 | _matrix[ 0] = (2 * _nearClip)/(_right - _left); |
|---|
| 434 | _matrix[ 1] = 0.0; |
|---|
| 435 | _matrix[ 2] = 0.0; |
|---|
| 436 | _matrix[ 3] = 0.0; |
|---|
| 437 | |
|---|
| 438 | _matrix[ 4] = 0.0; |
|---|
| 439 | _matrix[ 5] = (2 * _nearClip)/(_top-_bottom); |
|---|
| 440 | _matrix[ 6] = 0.0; |
|---|
| 441 | _matrix[ 7] = 0.0; |
|---|
| 442 | |
|---|
| 443 | _matrix[ 8] = (_right + _left)/(_right-_left); |
|---|
| 444 | _matrix[ 9] = (_top+_bottom)/(_top-_bottom); |
|---|
| 445 | _matrix[10] = -(_farClip + _nearClip)/(_farClip-_nearClip); |
|---|
| 446 | _matrix[11] = -1.0; |
|---|
| 447 | |
|---|
| 448 | _matrix[12] = 0.0; |
|---|
| 449 | _matrix[13] = 0.0; |
|---|
| 450 | _matrix[14] = -(2 * _farClip * _nearClip)/(_farClip-_nearClip); |
|---|
| 451 | _matrix[15] = 0.0; |
|---|
| 452 | |
|---|
| 453 | _matrix[ 8] += -_xshear; |
|---|
| 454 | _matrix[ 9] += -_yshear; |
|---|
| 455 | |
|---|
| 456 | _hfov = 2.0 * atan(((_right - _left) * 0.5)/_nearClip); |
|---|
| 457 | _vfov = 2.0 * atan(((_top - _bottom) * 0.5)/_nearClip); |
|---|
| 458 | |
|---|
| 459 | break; |
|---|
| 460 | |
|---|
| 461 | case Orthographic : |
|---|
| 462 | |
|---|
| 463 | _matrix[ 0] = 2/(_ortho_right - _ortho_left); |
|---|
| 464 | _matrix[ 1] = 0.0; |
|---|
| 465 | _matrix[ 2] = 0.0; |
|---|
| 466 | _matrix[ 3] = 0.0; |
|---|
| 467 | |
|---|
| 468 | _matrix[ 4] = 0.0; |
|---|
| 469 | _matrix[ 5] = 2/(_ortho_top - _ortho_bottom); |
|---|
| 470 | _matrix[ 6] = 0.0; |
|---|
| 471 | _matrix[ 7] = 0.0; |
|---|
| 472 | |
|---|
| 473 | _matrix[ 8] = 0.0; |
|---|
| 474 | _matrix[ 9] = 0.0; |
|---|
| 475 | |
|---|
| 476 | _matrix[10] = -2.0/(_farClip - _nearClip); |
|---|
| 477 | _matrix[11] = 0.0; |
|---|
| 478 | |
|---|
| 479 | _matrix[12] = -(_ortho_right+_ortho_left)/(_ortho_right-_ortho_left); |
|---|
| 480 | _matrix[13] = -(_ortho_top+_ortho_bottom)/(_ortho_top-_ortho_bottom); |
|---|
| 481 | |
|---|
| 482 | _matrix[14] = -(_farClip+_nearClip)/(_farClip-_nearClip); |
|---|
| 483 | _matrix[15] = 1.0; |
|---|
| 484 | |
|---|
| 485 | _matrix[12] += _xshear; |
|---|
| 486 | _matrix[13] += _yshear; |
|---|
| 487 | |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | break; |
|---|
| 492 | } |
|---|
| 493 | } |
|---|
| 494 | #endif |
|---|
| 495 | |
|---|
| 496 | void Camera::Lens::generateMatrix(float xshear, float yshear, osg::Matrix::value_type matrix[16] ) |
|---|
| 497 | { |
|---|
| 498 | switch( _projection ) |
|---|
| 499 | { |
|---|
| 500 | case Perspective : |
|---|
| 501 | matrix[ 0] = (2 * _nearClip)/(_right - _left); |
|---|
| 502 | matrix[ 1] = 0.0; |
|---|
| 503 | matrix[ 2] = 0.0; |
|---|
| 504 | matrix[ 3] = 0.0; |
|---|
| 505 | |
|---|
| 506 | matrix[ 4] = 0.0; |
|---|
| 507 | matrix[ 5] = (2 * _nearClip)/(_top-_bottom); |
|---|
| 508 | matrix[ 6] = 0.0; |
|---|
| 509 | matrix[ 7] = 0.0; |
|---|
| 510 | |
|---|
| 511 | matrix[ 8] = (_right + _left)/(_right-_left); |
|---|
| 512 | matrix[ 9] = (_top+_bottom)/(_top-_bottom); |
|---|
| 513 | matrix[10] = -(_farClip + _nearClip)/(_farClip-_nearClip); |
|---|
| 514 | matrix[11] = -1.0; |
|---|
| 515 | |
|---|
| 516 | matrix[12] = 0.0; |
|---|
| 517 | matrix[13] = 0.0; |
|---|
| 518 | matrix[14] = -(2 * _farClip * _nearClip)/(_farClip-_nearClip); |
|---|
| 519 | matrix[15] = 0.0; |
|---|
| 520 | |
|---|
| 521 | matrix[ 8] += -xshear; |
|---|
| 522 | matrix[ 9] += -yshear; |
|---|
| 523 | |
|---|
| 524 | |
|---|
| 525 | break; |
|---|
| 526 | |
|---|
| 527 | case Orthographic : |
|---|
| 528 | |
|---|
| 529 | matrix[ 0] = 2/(_ortho_right - _ortho_left); |
|---|
| 530 | matrix[ 1] = 0.0; |
|---|
| 531 | matrix[ 2] = 0.0; |
|---|
| 532 | matrix[ 3] = 0.0; |
|---|
| 533 | |
|---|
| 534 | matrix[ 4] = 0.0; |
|---|
| 535 | matrix[ 5] = 2/(_ortho_top - _ortho_bottom); |
|---|
| 536 | matrix[ 6] = 0.0; |
|---|
| 537 | matrix[ 7] = 0.0; |
|---|
| 538 | |
|---|
| 539 | matrix[ 8] = 0.0; |
|---|
| 540 | matrix[ 9] = 0.0; |
|---|
| 541 | |
|---|
| 542 | matrix[10] = -2.0/(_farClip - _nearClip); |
|---|
| 543 | matrix[11] = 0.0; |
|---|
| 544 | |
|---|
| 545 | matrix[12] = -(_ortho_right+_ortho_left)/(_ortho_right-_ortho_left); |
|---|
| 546 | matrix[13] = -(_ortho_top+_ortho_bottom)/(_ortho_top-_ortho_bottom); |
|---|
| 547 | |
|---|
| 548 | matrix[14] = -(_farClip+_nearClip)/(_farClip-_nearClip); |
|---|
| 549 | matrix[15] = 1.0; |
|---|
| 550 | |
|---|
| 551 | matrix[12] += xshear; |
|---|
| 552 | matrix[13] += yshear; |
|---|
| 553 | |
|---|
| 554 | break; |
|---|
| 555 | |
|---|
| 556 | case Manual: |
|---|
| 557 | |
|---|
| 558 | memcpy( matrix, _matrix, sizeof(osg::Matrix::value_type[16])); |
|---|
| 559 | |
|---|
| 560 | if(xshear || yshear) |
|---|
| 561 | { |
|---|
| 562 | if (matrix[3]!=0.0 || matrix[7]!=0.0 || matrix[11]!=0.0 || matrix[15]!=1.0) |
|---|
| 563 | { |
|---|
| 564 | |
|---|
| 565 | matrix[ 8] += -xshear; |
|---|
| 566 | matrix[ 9] += -yshear; |
|---|
| 567 | } |
|---|
| 568 | else |
|---|
| 569 | { |
|---|
| 570 | matrix[12] += xshear; |
|---|
| 571 | matrix[13] += yshear; |
|---|
| 572 | } |
|---|
| 573 | } |
|---|
| 574 | break; |
|---|
| 575 | } |
|---|
| 576 | } |
|---|
| 577 | |
|---|
| 578 | void Camera::Lens::_updateFOV() |
|---|
| 579 | { |
|---|
| 580 | _hfov = 2.0 * atan(((_right - _left) * 0.5)/_nearClip); |
|---|
| 581 | _vfov = 2.0 * atan(((_top - _bottom) * 0.5)/_nearClip); |
|---|
| 582 | _aspect_ratio = tan(0.5*_hfov)/tan(0.5*_vfov); |
|---|
| 583 | } |
|---|
| 584 | |
|---|
| 585 | |
|---|
| 586 | void Camera::setOffset( const osg::Matrix::value_type matrix[16], const osg::Matrix::value_type xshear, const osg::Matrix::value_type yshear ) |
|---|
| 587 | { |
|---|
| 588 | memcpy( _offset._matrix, matrix, sizeof(osg::Matrix::value_type[16])); |
|---|
| 589 | _offset._xshear = xshear; |
|---|
| 590 | _offset._yshear = yshear; |
|---|
| 591 | } |
|---|
| 592 | |
|---|
| 593 | void Camera::setOffset( double xshear, double yshear ) |
|---|
| 594 | { |
|---|
| 595 | _offset._xshear = xshear; |
|---|
| 596 | _offset._yshear = yshear; |
|---|
| 597 | } |
|---|
| 598 | |
|---|
| 599 | #if 0 |
|---|
| 600 | void Camera::setSyncBarrier( RefBarrier *b ) |
|---|
| 601 | { |
|---|
| 602 | _syncBarrier = b; |
|---|
| 603 | } |
|---|
| 604 | |
|---|
| 605 | void Camera::setFrameBarrier( RefBarrier *b ) |
|---|
| 606 | { |
|---|
| 607 | _frameBarrier = b; |
|---|
| 608 | } |
|---|
| 609 | |
|---|
| 610 | int Camera::cancel() |
|---|
| 611 | { |
|---|
| 612 | #if 1 |
|---|
| 613 | _done = true; |
|---|
| 614 | #endif |
|---|
| 615 | |
|---|
| 616 | Thread::cancel(); |
|---|
| 617 | return 0; |
|---|
| 618 | } |
|---|
| 619 | |
|---|
| 620 | void Camera::advance() |
|---|
| 621 | { |
|---|
| 622 | _rs->makeCurrent(); |
|---|
| 623 | _rs->swapBuffers(); |
|---|
| 624 | } |
|---|
| 625 | |
|---|
| 626 | void Camera::run( void ) |
|---|
| 627 | { |
|---|
| 628 | if( !_syncBarrier.valid() || !_frameBarrier.valid() ) |
|---|
| 629 | { |
|---|
| 630 | std::cerr << "Camera::run() : Threaded Camera requires a Barrier\n"; |
|---|
| 631 | return; |
|---|
| 632 | } |
|---|
| 633 | |
|---|
| 634 | _done = false; |
|---|
| 635 | |
|---|
| 636 | _initialize(); |
|---|
| 637 | _syncBarrier->block(); |
|---|
| 638 | while( !_done ) |
|---|
| 639 | { |
|---|
| 640 | |
|---|
| 641 | |
|---|
| 642 | _frameBarrier->block(); |
|---|
| 643 | |
|---|
| 644 | if (_done) break; |
|---|
| 645 | |
|---|
| 646 | |
|---|
| 647 | |
|---|
| 648 | frame(false); |
|---|
| 649 | |
|---|
| 650 | if (_done) break; |
|---|
| 651 | |
|---|
| 652 | |
|---|
| 653 | |
|---|
| 654 | _syncBarrier->block(); |
|---|
| 655 | |
|---|
| 656 | if (_done) break; |
|---|
| 657 | |
|---|
| 658 | |
|---|
| 659 | |
|---|
| 660 | advance(); |
|---|
| 661 | } |
|---|
| 662 | |
|---|
| 663 | |
|---|
| 664 | } |
|---|
| 665 | |
|---|
| 666 | |
|---|
| 667 | |
|---|
| 668 | bool Camera::removePreCullCallback( Callback *cb ) |
|---|
| 669 | { |
|---|
| 670 | return _removeCallback( preCullCallbacks, cb ); |
|---|
| 671 | } |
|---|
| 672 | |
|---|
| 673 | bool Camera::removePostCullCallback( Callback *cb ) |
|---|
| 674 | { |
|---|
| 675 | return _removeCallback( postCullCallbacks, cb ); |
|---|
| 676 | } |
|---|
| 677 | |
|---|
| 678 | bool Camera::removePreDrawCallback( Callback *cb ) |
|---|
| 679 | { |
|---|
| 680 | return _removeCallback( preDrawCallbacks, cb ); |
|---|
| 681 | } |
|---|
| 682 | |
|---|
| 683 | bool Camera::removePostDrawCallback( Callback *cb ) |
|---|
| 684 | { |
|---|
| 685 | return _removeCallback( postDrawCallbacks, cb ); |
|---|
| 686 | } |
|---|
| 687 | |
|---|
| 688 | bool Camera::removePostSwapCallback( Callback *cb ) |
|---|
| 689 | { |
|---|
| 690 | return _removeCallback( postSwapCallbacks, cb ); |
|---|
| 691 | } |
|---|
| 692 | |
|---|
| 693 | |
|---|
| 694 | bool Camera::_removeCallback( std::vector < ref_ptr<Callback> > &callbackList, Callback *callback ) |
|---|
| 695 | { |
|---|
| 696 | std::vector < Producer::ref_ptr< Producer::Camera::Callback> >::iterator p; |
|---|
| 697 | p = std::find( callbackList.begin(), callbackList.end(), callback ); |
|---|
| 698 | if( p == callbackList.end() ) |
|---|
| 699 | return false; |
|---|
| 700 | |
|---|
| 701 | callbackList.erase( p ); |
|---|
| 702 | return true; |
|---|
| 703 | } |
|---|
| 704 | |
|---|
| 705 | Camera::FrameTimeStampSet::FrameTimeStampSet(): |
|---|
| 706 | _pipeStatsDoubleBufferIndex(0), |
|---|
| 707 | _pipeStatsFirstSync(true), |
|---|
| 708 | _initialized(false) |
|---|
| 709 | { |
|---|
| 710 | for( unsigned int i = 0; i < LastPipeStatsID; i++ ) |
|---|
| 711 | _pipeStats[i] = 0.0; |
|---|
| 712 | } |
|---|
| 713 | |
|---|
| 714 | Camera::FrameTimeStampSet::~FrameTimeStampSet() |
|---|
| 715 | { |
|---|
| 716 | } |
|---|
| 717 | |
|---|
| 718 | void Camera::FrameTimeStampSet::syncPipeStats() |
|---|
| 719 | { |
|---|
| 720 | if( !_initialized ) |
|---|
| 721 | return; |
|---|
| 722 | |
|---|
| 723 | if( _pipeStatsFirstSync == true ) |
|---|
| 724 | { |
|---|
| 725 | _pipeStatsFirstSync = false; |
|---|
| 726 | return; |
|---|
| 727 | } |
|---|
| 728 | |
|---|
| 729 | |
|---|
| 730 | for( int i = 0; i < LastPipeStatsID; i++ ) |
|---|
| 731 | { |
|---|
| 732 | if( _pipeStatsSetMask[1 - _pipeStatsDoubleBufferIndex] & (1<<i) ) |
|---|
| 733 | _pipeStats[i] = PipeTimer::instance()->getElapsedTime( _pipeStatsNames[i][ 1 - _pipeStatsDoubleBufferIndex] ); |
|---|
| 734 | } |
|---|
| 735 | |
|---|
| 736 | _pipeStatsFrameNumber = _frameNumber - 1; |
|---|
| 737 | _pipeStatsDoubleBufferIndex = 1 - _pipeStatsDoubleBufferIndex; |
|---|
| 738 | _pipeStatsSetMask[_pipeStatsDoubleBufferIndex] = 0; |
|---|
| 739 | } |
|---|
| 740 | |
|---|
| 741 | void Camera::FrameTimeStampSet::beginPipeTimer( PipeStatsID id) |
|---|
| 742 | { |
|---|
| 743 | if( !_initialized ) |
|---|
| 744 | _init(); |
|---|
| 745 | |
|---|
| 746 | PipeTimer::instance()->begin( _pipeStatsNames[id][_pipeStatsDoubleBufferIndex] ); |
|---|
| 747 | _pipeStatsSetMask[_pipeStatsDoubleBufferIndex] |= (1<<id); |
|---|
| 748 | } |
|---|
| 749 | |
|---|
| 750 | void Camera::FrameTimeStampSet::endPipeTimer() |
|---|
| 751 | { |
|---|
| 752 | if( !_initialized ) |
|---|
| 753 | return; |
|---|
| 754 | |
|---|
| 755 | PipeTimer::instance()->end() ; |
|---|
| 756 | } |
|---|
| 757 | |
|---|
| 758 | void Camera::FrameTimeStampSet::_init() |
|---|
| 759 | { |
|---|
| 760 | if( _initialized ) |
|---|
| 761 | return; |
|---|
| 762 | |
|---|
| 763 | for( unsigned int i = 0; i < (unsigned int)LastPipeStatsID; i++ ) |
|---|
| 764 | PipeTimer::instance()->genQueries( 2, _pipeStatsNames[i] ); |
|---|
| 765 | |
|---|
| 766 | _pipeStatsSetMask[0] = 0; |
|---|
| 767 | _pipeStatsSetMask[1] = 0; |
|---|
| 768 | |
|---|
| 769 | _initialized = true; |
|---|
| 770 | } |
|---|
| 771 | |
|---|
| 772 | const Camera::FrameTimeStampSet &Camera::getFrameStats() |
|---|
| 773 | { |
|---|
| 774 | return _frameStamps; |
|---|
| 775 | } |
|---|
| 776 | #endif |
|---|