- Timestamp:
- 02/21/07 20:41:03 (6 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgdistortion/osgdistortion.cpp
r6245 r6252 14 14 #include <osg/io_utils> 15 15 16 16 17 #include <osgGA/TrackballManipulator> 17 18 #include <osgGA/FlightManipulator> 18 19 #include <osgGA/DriveManipulator> 20 #include <osgGA/KeySwitchMatrixManipulator> 19 21 #include <osgGA/StateSetManipulator> 22 #include <osgGA/AnimationPathManipulator> 23 #include <osgGA/TerrainManipulator> 20 24 21 25 #include <osgUtil/SmoothingVisitor> … … 208 212 camera->setGraphicsContext(gc.get()); 209 213 camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y, camera_width, camera_height)); 214 210 215 GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 211 216 camera->setDrawBuffer(buffer); … … 279 284 viewer.assignSceneDataToCameras(); 280 285 } 281 282 struct DrawCallback : public osg::Drawable::DrawCallback283 {284 285 DrawCallback() {}286 287 virtual void drawImplementation(osg::State& state,const osg::Drawable* drawable) const288 {289 osg::notify(osg::NOTICE)<<"I am here"<<std::endl;290 osg::notify(osg::NOTICE)<<" Projection matrix "<<state.getProjectionMatrix()<<std::endl;291 osg::notify(osg::NOTICE)<<" ModelView matrix "<<state.getModelViewMatrix()<<std::endl;292 293 drawable->drawImplementation(state);294 }295 };296 297 286 298 287 void setDomeCorrection(osgViewer::Viewer& viewer, osg::ArgumentParser& /*arguments*/) … … 331 320 int center_x = width/2; 332 321 int center_y = height/2; 333 int camera_width = 256; 334 int camera_height = 256; 335 336 int tex_width = 256; 337 int tex_height = 256; 338 322 int camera_width = 512; 323 int camera_height = 512; 324 325 int tex_width = 512; 326 int tex_height = 512; 327 328 #define CUBE_MAP 329 #ifdef CUBE_MAP 339 330 osg::TextureCubeMap* texture = new osg::TextureCubeMap; 331 #else 332 osg::Texture2D* texture = new osg::Texture2D; 333 #endif 340 334 texture->setTextureSize(tex_width, tex_height); 341 335 texture->setInternalFormat(GL_RGB); … … 343 337 texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); 344 338 texture->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE); 345 texture->setFilter(osg::TextureCubeMap::MIN_FILTER,osg::TextureCubeMap::LINEAR); 346 texture->setFilter(osg::TextureCubeMap::MAG_FILTER,osg::TextureCubeMap::LINEAR); 339 texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); 340 texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); 341 342 #if 0 343 osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::SEPERATE_WINDOW; 344 GLenum buffer = GL_FRONT; 345 #else 346 osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::FRAME_BUFFER_OBJECT; 347 GLenum buffer = GL_FRONT; 348 #endif 347 349 348 350 // front face … … 351 353 camera->setName("Front face camera"); 352 354 camera->setGraphicsContext(gc.get()); 353 camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y, camera_width, camera_height)); 354 GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 355 camera->setDrawBuffer(buffer); 356 camera->setReadBuffer(buffer); 357 355 camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); 356 camera->setDrawBuffer(buffer); 357 camera->setReadBuffer(buffer); 358 camera->setAllowEventFocus(false); 359 // tell the camera to use OpenGL frame buffer object where supported. 360 camera->setRenderTargetImplementation(renderTargetImplementation); 361 358 362 // attach the texture and use it as the color buffer. 363 #ifdef CUBE_MAP 359 364 camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Z); 360 365 #else 366 camera->attach(osg::Camera::COLOR_BUFFER, texture); 367 #endif 361 368 viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd()); 362 369 } 370 363 371 364 372 // top face … … 367 375 camera->setName("Top face camera"); 368 376 camera->setGraphicsContext(gc.get()); 369 camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y+camera_height, camera_width, camera_height)); 370 GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 371 camera->setDrawBuffer(buffer); 372 camera->setReadBuffer(buffer); 377 camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); 378 GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 379 camera->setDrawBuffer(buffer); 380 camera->setReadBuffer(buffer); 381 camera->setAllowEventFocus(false); 382 383 // tell the camera to use OpenGL frame buffer object where supported. 384 camera->setRenderTargetImplementation(renderTargetImplementation); 373 385 374 386 // attach the texture and use it as the color buffer. 387 #ifdef CUBE_MAP 375 388 camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Y); 389 #else 390 camera->attach(osg::Camera::COLOR_BUFFER, texture); 391 #endif 376 392 377 393 viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 1.0,0.0,0.0)); … … 383 399 camera->setName("Left face camera"); 384 400 camera->setGraphicsContext(gc.get()); 385 camera->setViewport(new osg::Viewport(center_x-camera_width*3/2, center_y, camera_width, camera_height)); 386 GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 387 camera->setDrawBuffer(buffer); 388 camera->setReadBuffer(buffer); 401 camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); 402 camera->setDrawBuffer(buffer); 403 camera->setReadBuffer(buffer); 404 camera->setAllowEventFocus(false); 405 406 // tell the camera to use OpenGL frame buffer object where supported. 407 camera->setRenderTargetImplementation(renderTargetImplementation); 389 408 390 409 // attach the texture and use it as the color buffer. 410 #ifdef CUBE_MAP 391 411 camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_X); 412 #else 413 camera->attach(osg::Camera::COLOR_BUFFER, texture); 414 #endif 392 415 393 416 viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 0.0,1.0,0.0)); … … 399 422 camera->setName("Right face camera"); 400 423 camera->setGraphicsContext(gc.get()); 401 camera->setViewport(new osg::Viewport(center_x+camera_width/2, center_y, camera_width, camera_height)); 402 GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 403 camera->setDrawBuffer(buffer); 404 camera->setReadBuffer(buffer); 424 camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); 425 camera->setDrawBuffer(buffer); 426 camera->setReadBuffer(buffer); 427 camera->setAllowEventFocus(false); 428 429 // tell the camera to use OpenGL frame buffer object where supported. 430 camera->setRenderTargetImplementation(renderTargetImplementation); 405 431 406 432 // attach the texture and use it as the color buffer. 433 #ifdef CUBE_MAP 407 434 camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_X); 435 #else 436 camera->attach(osg::Camera::COLOR_BUFFER, texture); 437 #endif 408 438 409 439 viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 0.0,1.0,0.0)); … … 415 445 camera->setGraphicsContext(gc.get()); 416 446 camera->setName("Bottom face camera"); 417 camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y-camera_height, camera_width, camera_height)); 418 GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 419 camera->setDrawBuffer(buffer); 420 camera->setReadBuffer(buffer); 447 camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); 448 camera->setDrawBuffer(buffer); 449 camera->setReadBuffer(buffer); 450 camera->setAllowEventFocus(false); 451 452 // tell the camera to use OpenGL frame buffer object where supported. 453 camera->setRenderTargetImplementation(renderTargetImplementation); 421 454 422 455 // attach the texture and use it as the color buffer. 456 #ifdef CUBE_MAP 423 457 camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Y); 458 #else 459 camera->attach(osg::Camera::COLOR_BUFFER, texture); 460 #endif 424 461 425 462 viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 1.0,0.0,0.0)); … … 431 468 camera->setName("Back face camera"); 432 469 camera->setGraphicsContext(gc.get()); 433 camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y-2*camera_height, camera_width, camera_height)); 434 GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 435 camera->setDrawBuffer(buffer); 436 camera->setReadBuffer(buffer); 470 camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); 471 camera->setDrawBuffer(buffer); 472 camera->setReadBuffer(buffer); 473 camera->setAllowEventFocus(false); 474 475 // tell the camera to use OpenGL frame buffer object where supported. 476 camera->setRenderTargetImplementation(renderTargetImplementation); 437 477 438 478 // attach the texture and use it as the color buffer. 479 #ifdef CUBE_MAP 439 480 camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Z); 481 #else 482 camera->attach(osg::Camera::COLOR_BUFFER, texture); 483 #endif 440 484 441 485 viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-180.0f), 1.0,0.0,0.0)); … … 465 509 466 510 osg::Vec3Array* vertices = new osg::Vec3Array; 467 osg::Vec 2Array* texcoords = new osg::Vec2Array;511 osg::Vec3Array* texcoords = new osg::Vec3Array; 468 512 osg::Vec4Array* colors = new osg::Vec4Array; 469 513 … … 472 516 osg::Vec3 dy = yAxis*(height/((float)(noSteps-1))); 473 517 474 osg::Vec 2 bottom_texcoord(0.0f,0.0f);475 osg::Vec 2 dx_texcoord(1.0f/(float)(noSteps-1),0.0f);476 osg::Vec 2 dy_texcoord(0.0f,1.0f/(float)(noSteps-1));518 osg::Vec3 bottom_texcoord(0.0f,0.0f,0.0f); 519 osg::Vec3 dx_texcoord(1.0f/(float)(noSteps-1),0.0f,0.0f); 520 osg::Vec3 dy_texcoord(0.0f,1.0f/(float)(noSteps-1),0.0f); 477 521 478 522 osg::Vec3 cursor = bottom; 479 osg::Vec 2texcoord = bottom_texcoord;523 osg::Vec3 texcoord = bottom_texcoord; 480 524 int i,j; 481 525 for(i=0;i<noSteps;++i) 482 526 { 483 527 osg::Vec3 cursor = bottom+dy*(float)i; 484 osg::Vec 2texcoord = bottom_texcoord+dy_texcoord*(float)i;528 osg::Vec3 texcoord = bottom_texcoord+dy_texcoord*(float)i; 485 529 for(j=0;j<noSteps;++j) 486 530 { 487 531 vertices->push_back(cursor); 488 texcoords->push_back(osg::Vec 2((sin(texcoord.x()*osg::PI-osg::PI*0.5)+1.0f)*0.5f,(sin(texcoord.y()*osg::PI-osg::PI*0.5)+1.0f)*0.5f));532 texcoords->push_back(osg::Vec3((sin(texcoord.x()*osg::PI-osg::PI*0.5)+1.0f)*0.5f,(1.0-sin(texcoord.y()*osg::PI-osg::PI*0.5))*0.5f,1.0)); 489 533 colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); 490 534 … … 501 545 502 546 polyGeom->setTexCoordArray(0,texcoords); 503 504 polyGeom->setDrawCallback(new DrawCallback);505 506 547 507 548 for(i=0;i<noSteps-1;++i) … … 530 571 camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); 531 572 camera->setClearColor( osg::Vec4(0.1,0.1,1.0,1.0) ); 532 camera->setViewport(new osg::Viewport(0, 0, width /2, height/2));573 camera->setViewport(new osg::Viewport(0, 0, width, height)); 533 574 GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 534 575 camera->setDrawBuffer(buffer); 535 576 camera->setReadBuffer(buffer); 536 577 camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF); 578 camera->setAllowEventFocus(false); 537 579 //camera->setInheritanceMask(camera->getInheritanceMask() & ~osg::CullSettings::CLEAR_COLOR & ~osg::CullSettings::COMPUTE_NEAR_FAR_MODE); 538 580 //camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); … … 546 588 camera->setName("DistortionCorrectionCamera"); 547 589 548 osg::notify(osg::NOTICE)<<"Original Projection matrix "<<camera->getProjectionMatrix()<<std::endl;549 osg::notify(osg::NOTICE)<<" View matrix "<<camera->getViewMatrix()<<std::endl;550 551 552 590 viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd()); 553 591 } … … 593 631 } 594 632 633 // set up the camera manipulators. 634 { 635 osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; 636 637 keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); 638 keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); 639 keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); 640 keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() ); 641 642 std::string pathfile; 643 char keyForAnimationPath = '5'; 644 while (arguments.read("-p",pathfile)) 645 { 646 osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile); 647 if (apm || !apm->valid()) 648 { 649 unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); 650 keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm ); 651 keyswitchManipulator->selectMatrixManipulator(num); 652 ++keyForAnimationPath; 653 } 654 } 655 656 viewer.setCameraManipulator( keyswitchManipulator.get() ); 657 } 658 595 659 viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); 596 660
