Show
Ignore:
Timestamp:
02/20/07 22:06:24 (8 years ago)
Author:
robert
Message:

Further work on dome RTT

Files:
1 modified

Legend:

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

    r6242 r6245  
    1111#include <osg/Material> 
    1212#include <osg/Projection> 
     13#include <osg/TextureCubeMap> 
     14#include <osg/io_utils> 
    1315 
    1416#include <osgGA/TrackballManipulator> 
    1517#include <osgGA/FlightManipulator> 
    1618#include <osgGA/DriveManipulator> 
     19#include <osgGA/StateSetManipulator> 
    1720 
    1821#include <osgUtil/SmoothingVisitor> 
     
    2225 
    2326#include <osgViewer/Viewer> 
     27#include <osgViewer/StatsHandler> 
     28#include <osgViewer/HelpHandler> 
    2429 
    2530using namespace osg; 
     
    6974   } 
    7075     
    71     // set up the render to texture camera. 
    72     { 
    73  
     76    // set up the hud camera 
     77    { 
    7478        // create the quad to visualize. 
    7579        osg::Geometry* polyGeom = new osg::Geometry(); 
     
    156160        camera->setRenderOrder(osg::Camera::NESTED_RENDER); 
    157161 
    158         // tell the camera to use OpenGL frame buffer object where supported. 
    159         camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); 
    160  
    161         // attach the texture and use it as the color buffer. 
    162         camera->attach(osg::Camera::COLOR_BUFFER, texture); 
    163  
    164162        // add subgraph to render 
    165163        camera->addChild(geode); 
     
    170168} 
    171169 
    172 void setDomeCorrection(osgViewer::Viewer& viewer, osg::ArgumentParser& arguments) 
     170void setDomeFaces(osgViewer::Viewer& viewer, osg::ArgumentParser& /*arguments*/) 
    173171{ 
    174172  
     
    282280} 
    283281 
     282struct 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 
     297 
     298void setDomeCorrection(osgViewer::Viewer& viewer, osg::ArgumentParser& /*arguments*/) 
     299{ 
     300  
     301    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); 
     302    if (!wsi)  
     303    { 
     304        osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl; 
     305        return; 
     306    } 
     307 
     308    unsigned int width, height; 
     309    wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height); 
     310 
     311    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; 
     312    traits->x = 0; 
     313    traits->y = 0; 
     314    traits->width = width; 
     315    traits->height = height; 
     316    traits->windowDecoration = false; 
     317    traits->doubleBuffer = true; 
     318    traits->sharedContext = 0; 
     319 
     320    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); 
     321    if (!gc) 
     322    { 
     323        osg::notify(osg::NOTICE)<<"GraphicsWindow has not been created successfully."<<std::endl; 
     324        return; 
     325    } 
     326 
     327    gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f)); 
     328    gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     329 
     330 
     331    int center_x = width/2; 
     332    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     
     339    osg::TextureCubeMap* texture = new osg::TextureCubeMap; 
     340    texture->setTextureSize(tex_width, tex_height); 
     341    texture->setInternalFormat(GL_RGB); 
     342    texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); 
     343    texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); 
     344    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); 
     347 
     348    // front face 
     349    { 
     350        osg::ref_ptr<osg::Camera> camera = new osg::Camera; 
     351        camera->setName("Front face camera"); 
     352        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         
     358        // attach the texture and use it as the color buffer. 
     359        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Z); 
     360 
     361        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd()); 
     362    } 
     363     
     364    // top face 
     365    { 
     366        osg::ref_ptr<osg::Camera> camera = new osg::Camera; 
     367        camera->setName("Top face camera"); 
     368        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); 
     373 
     374        // attach the texture and use it as the color buffer. 
     375        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Y); 
     376 
     377        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 1.0,0.0,0.0)); 
     378    } 
     379 
     380    // left face 
     381    { 
     382        osg::ref_ptr<osg::Camera> camera = new osg::Camera; 
     383        camera->setName("Left face camera"); 
     384        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); 
     389 
     390        // attach the texture and use it as the color buffer. 
     391        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_X); 
     392 
     393        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 0.0,1.0,0.0)); 
     394    } 
     395 
     396    // right face 
     397    { 
     398        osg::ref_ptr<osg::Camera> camera = new osg::Camera; 
     399        camera->setName("Right face camera"); 
     400        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); 
     405 
     406        // attach the texture and use it as the color buffer. 
     407        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_X); 
     408 
     409        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 0.0,1.0,0.0)); 
     410    } 
     411 
     412    // bottom face 
     413    { 
     414        osg::ref_ptr<osg::Camera> camera = new osg::Camera; 
     415        camera->setGraphicsContext(gc.get()); 
     416        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); 
     421 
     422        // attach the texture and use it as the color buffer. 
     423        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Y); 
     424 
     425        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 1.0,0.0,0.0)); 
     426    } 
     427 
     428    // back face 
     429    { 
     430        osg::ref_ptr<osg::Camera> camera = new osg::Camera; 
     431        camera->setName("Back face camera"); 
     432        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); 
     437 
     438        // attach the texture and use it as the color buffer. 
     439        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Z); 
     440 
     441        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-180.0f), 1.0,0.0,0.0)); 
     442    } 
     443     
     444    viewer.getCamera()->setProjectionMatrixAsPerspective(90.0f, 1.0, 1, 1000.0); 
     445 
     446 
     447    viewer.assignSceneDataToCameras(); 
     448 
     449 
     450    // distortion correction set up. 
     451    { 
     452         
     453        // create the quad to visualize. 
     454        osg::Geometry* polyGeom = new osg::Geometry(); 
     455 
     456        polyGeom->setSupportsDisplayList(false); 
     457 
     458        osg::Vec3 origin(0.0f,0.0f,0.0f); 
     459        osg::Vec3 xAxis(1.0f,0.0f,0.0f); 
     460        osg::Vec3 yAxis(0.0f,1.0f,0.0f); 
     461        osg::Vec3 zAxis(0.0f,0.0f,1.0f); 
     462        float height = 1024.0f; 
     463        float width = 1280.0f; 
     464        int noSteps = 50; 
     465 
     466        osg::Vec3Array* vertices = new osg::Vec3Array; 
     467        osg::Vec2Array* texcoords = new osg::Vec2Array; 
     468        osg::Vec4Array* colors = new osg::Vec4Array; 
     469 
     470        osg::Vec3 bottom = origin; 
     471        osg::Vec3 dx = xAxis*(width/((float)(noSteps-1))); 
     472        osg::Vec3 dy = yAxis*(height/((float)(noSteps-1))); 
     473 
     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)); 
     477 
     478        osg::Vec3 cursor = bottom; 
     479        osg::Vec2 texcoord = bottom_texcoord; 
     480        int i,j; 
     481        for(i=0;i<noSteps;++i) 
     482        { 
     483            osg::Vec3 cursor = bottom+dy*(float)i; 
     484            osg::Vec2 texcoord = bottom_texcoord+dy_texcoord*(float)i; 
     485            for(j=0;j<noSteps;++j) 
     486            { 
     487                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)); 
     489                colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); 
     490 
     491                cursor += dx; 
     492                texcoord += dx_texcoord; 
     493            } 
     494        } 
     495 
     496        // pass the created vertex array to the points geometry object. 
     497        polyGeom->setVertexArray(vertices); 
     498 
     499        polyGeom->setColorArray(colors); 
     500        polyGeom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); 
     501 
     502        polyGeom->setTexCoordArray(0,texcoords); 
     503         
     504        polyGeom->setDrawCallback(new DrawCallback); 
     505 
     506 
     507        for(i=0;i<noSteps-1;++i) 
     508        { 
     509            osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP); 
     510            for(j=0;j<noSteps;++j) 
     511            { 
     512                elements->push_back(j+(i+1)*noSteps); 
     513                elements->push_back(j+(i)*noSteps); 
     514            } 
     515            polyGeom->addPrimitiveSet(elements); 
     516        } 
     517 
     518 
     519        // new we need to add the texture to the Drawable, we do so by creating a  
     520        // StateSet to contain the Texture StateAttribute. 
     521        osg::StateSet* stateset = polyGeom->getOrCreateStateSet(); 
     522        stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON); 
     523        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); 
     524 
     525        osg::Geode* geode = new osg::Geode(); 
     526        geode->addDrawable(polyGeom); 
     527 
     528        osg::ref_ptr<osg::Camera> camera = new osg::Camera; 
     529        camera->setGraphicsContext(gc.get()); 
     530        camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); 
     531        camera->setClearColor( osg::Vec4(0.1,0.1,1.0,1.0) ); 
     532        camera->setViewport(new osg::Viewport(0, 0, width/2, height/2)); 
     533        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; 
     534        camera->setDrawBuffer(buffer); 
     535        camera->setReadBuffer(buffer); 
     536        camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF); 
     537        //camera->setInheritanceMask(camera->getInheritanceMask() & ~osg::CullSettings::CLEAR_COLOR & ~osg::CullSettings::COMPUTE_NEAR_FAR_MODE); 
     538        //camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); 
     539         
     540        camera->setProjectionMatrixAsOrtho2D(0,1280,0,1024); 
     541        camera->setViewMatrix(osg::Matrix::identity()); 
     542 
     543        // add subgraph to render 
     544        camera->addChild(geode); 
     545         
     546        camera->setName("DistortionCorrectionCamera"); 
     547 
     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        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd()); 
     553    } 
     554} 
     555 
     556 
    284557int main(int argc, char** argv) 
    285558{ 
     
    305578     
    306579    } 
     580    else if (arguments.read("--faces")) 
     581    {     
     582        viewer.setSceneData( loadedModel ); 
     583 
     584        setDomeFaces(viewer, arguments); 
     585     
     586    } 
    307587    else 
    308588    { 
     
    312592        viewer.setSceneData( distortionNode ); 
    313593    } 
     594 
     595    viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); 
     596 
     597    // add the state manipulator 
     598    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); 
     599     
     600    // add the stats handler 
     601    viewer.addEventHandler(new osgViewer::StatsHandler); 
    314602 
    315603    return viewer.run();