Show
Ignore:
Timestamp:
02/21/07 20:41:03 (8 years ago)
Author:
robert
Message:

Wired up RTT cubemap cameras and simple distortion mesh.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgdistortion/osgdistortion.cpp

    r6245 r6252  
    1414#include <osg/io_utils> 
    1515 
     16 
    1617#include <osgGA/TrackballManipulator> 
    1718#include <osgGA/FlightManipulator> 
    1819#include <osgGA/DriveManipulator> 
     20#include <osgGA/KeySwitchMatrixManipulator> 
    1921#include <osgGA/StateSetManipulator> 
     22#include <osgGA/AnimationPathManipulator> 
     23#include <osgGA/TerrainManipulator> 
    2024 
    2125#include <osgUtil/SmoothingVisitor> 
     
    208212        camera->setGraphicsContext(gc.get()); 
    209213        camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y, camera_width, camera_height)); 
     214 
    210215        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 
    211216        camera->setDrawBuffer(buffer); 
     
    279284    viewer.assignSceneDataToCameras(); 
    280285} 
    281  
    282 struct DrawCallback : public osg::Drawable::DrawCallback 
    283 { 
    284  
    285     DrawCallback() {} 
    286  
    287     virtual void drawImplementation(osg::State& state,const osg::Drawable* drawable) const 
    288     { 
    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  
    297286 
    298287void setDomeCorrection(osgViewer::Viewer& viewer, osg::ArgumentParser& /*arguments*/) 
     
    331320    int center_x = width/2; 
    332321    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     
    339330    osg::TextureCubeMap* texture = new osg::TextureCubeMap; 
     331#else 
     332    osg::Texture2D* texture = new osg::Texture2D; 
     333#endif     
    340334    texture->setTextureSize(tex_width, tex_height); 
    341335    texture->setInternalFormat(GL_RGB); 
     
    343337    texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); 
    344338    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 
    347349 
    348350    // front face 
     
    351353        camera->setName("Front face camera"); 
    352354        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 
    358362        // attach the texture and use it as the color buffer. 
     363#ifdef CUBE_MAP     
    359364        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 
    361368        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd()); 
    362369    } 
     370 
    363371     
    364372    // top face 
     
    367375        camera->setName("Top face camera"); 
    368376        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); 
    373385 
    374386        // attach the texture and use it as the color buffer. 
     387#ifdef CUBE_MAP     
    375388        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Y); 
     389#else 
     390        camera->attach(osg::Camera::COLOR_BUFFER, texture); 
     391#endif 
    376392 
    377393        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 1.0,0.0,0.0)); 
     
    383399        camera->setName("Left face camera"); 
    384400        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); 
    389408 
    390409        // attach the texture and use it as the color buffer. 
     410#ifdef CUBE_MAP     
    391411        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_X); 
     412#else 
     413        camera->attach(osg::Camera::COLOR_BUFFER, texture); 
     414#endif 
    392415 
    393416        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 0.0,1.0,0.0)); 
     
    399422        camera->setName("Right face camera"); 
    400423        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); 
    405431 
    406432        // attach the texture and use it as the color buffer. 
     433#ifdef CUBE_MAP     
    407434        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_X); 
     435#else 
     436        camera->attach(osg::Camera::COLOR_BUFFER, texture); 
     437#endif 
    408438 
    409439        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 0.0,1.0,0.0)); 
     
    415445        camera->setGraphicsContext(gc.get()); 
    416446        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); 
    421454 
    422455        // attach the texture and use it as the color buffer. 
     456#ifdef CUBE_MAP     
    423457        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Y); 
     458#else 
     459        camera->attach(osg::Camera::COLOR_BUFFER, texture); 
     460#endif 
    424461 
    425462        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 1.0,0.0,0.0)); 
     
    431468        camera->setName("Back face camera"); 
    432469        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); 
    437477 
    438478        // attach the texture and use it as the color buffer. 
     479#ifdef CUBE_MAP     
    439480        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Z); 
     481#else 
     482        camera->attach(osg::Camera::COLOR_BUFFER, texture); 
     483#endif 
    440484 
    441485        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-180.0f), 1.0,0.0,0.0)); 
     
    465509 
    466510        osg::Vec3Array* vertices = new osg::Vec3Array; 
    467         osg::Vec2Array* texcoords = new osg::Vec2Array; 
     511        osg::Vec3Array* texcoords = new osg::Vec3Array; 
    468512        osg::Vec4Array* colors = new osg::Vec4Array; 
    469513 
     
    472516        osg::Vec3 dy = yAxis*(height/((float)(noSteps-1))); 
    473517 
    474         osg::Vec2 bottom_texcoord(0.0f,0.0f); 
    475         osg::Vec2 dx_texcoord(1.0f/(float)(noSteps-1),0.0f); 
    476         osg::Vec2 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); 
    477521 
    478522        osg::Vec3 cursor = bottom; 
    479         osg::Vec2 texcoord = bottom_texcoord; 
     523        osg::Vec3 texcoord = bottom_texcoord; 
    480524        int i,j; 
    481525        for(i=0;i<noSteps;++i) 
    482526        { 
    483527            osg::Vec3 cursor = bottom+dy*(float)i; 
    484             osg::Vec2 texcoord = bottom_texcoord+dy_texcoord*(float)i; 
     528            osg::Vec3 texcoord = bottom_texcoord+dy_texcoord*(float)i; 
    485529            for(j=0;j<noSteps;++j) 
    486530            { 
    487531                vertices->push_back(cursor); 
    488                 texcoords->push_back(osg::Vec2((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)); 
    489533                colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); 
    490534 
     
    501545 
    502546        polyGeom->setTexCoordArray(0,texcoords); 
    503          
    504         polyGeom->setDrawCallback(new DrawCallback); 
    505  
    506547 
    507548        for(i=0;i<noSteps-1;++i) 
     
    530571        camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); 
    531572        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)); 
    533574        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 
    534575        camera->setDrawBuffer(buffer); 
    535576        camera->setReadBuffer(buffer); 
    536577        camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF); 
     578        camera->setAllowEventFocus(false); 
    537579        //camera->setInheritanceMask(camera->getInheritanceMask() & ~osg::CullSettings::CLEAR_COLOR & ~osg::CullSettings::COMPUTE_NEAR_FAR_MODE); 
    538580        //camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); 
     
    546588        camera->setName("DistortionCorrectionCamera"); 
    547589 
    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  
    552590        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd()); 
    553591    } 
     
    593631    } 
    594632 
     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 
    595659    viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); 
    596660