Show
Ignore:
Timestamp:
06/12/07 20:56:52 (7 years ago)
Author:
robert
Message:

Refactored the dome correction code to support movie flipping.

Files:
1 modified

Legend:

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

    r6947 r6948  
    309309} 
    310310 
    311 osg::Geometry* createDomeDistortionMesh(const osg::Vec3& origin, const osg::Vec3& widthVector, const osg::Vec3& heightVector, 
    312                                         osg::ArgumentParser& arguments) 
    313 { 
    314     double sphere_radius = 1.0; 
    315     if (arguments.read("--radius", sphere_radius)) {} 
    316  
    317     double collar_radius = 0.45; 
    318     if (arguments.read("--collar", collar_radius)) {} 
    319  
    320     double rotationDegrees = 180.0; 
    321     if (arguments.read("--rotation", rotationDegrees)) {} 
    322  
     311class DomeModel 
     312{ 
     313public: 
     314 
     315    DomeModel(osg::ArgumentParser& arguments): 
     316        sphere_radius(1.0), 
     317        collar_radius(0.45), 
     318        rotationDegrees(180.0), 
     319        distance(0.0), 
     320        flip(false), 
     321        texcoord_flip(false) 
     322    { 
     323        if (arguments.read("--radius", sphere_radius)) {} 
     324        if (arguments.read("--collar", collar_radius)) {} 
     325        if (arguments.read("--rotation", rotationDegrees)) {} 
     326 
     327        distance = sqrt(sphere_radius*sphere_radius - collar_radius*collar_radius); 
     328        if (arguments.read("--distance", distance)) {} 
     329 
     330        if (arguments.read("--flip")) { flip = true; } 
     331    } 
     332     
     333    double sphere_radius; 
     334    double collar_radius; 
     335    double rotationDegrees; 
     336    double distance; 
     337    bool flip; 
     338    bool texcoord_flip; 
     339 
     340}; 
     341 
     342 
     343osg::Geometry* createDomeDistortionMesh(const osg::Vec3& origin, const osg::Vec3& widthVector, const osg::Vec3& heightVector, DomeModel& domeModel) 
     344{ 
    323345    osg::Vec3d center(0.0,0.0,0.0); 
    324346    osg::Vec3d eye(0.0,0.0,0.0); 
    325347     
    326     double distance = sqrt(sphere_radius*sphere_radius - collar_radius*collar_radius); 
    327     if (arguments.read("--distance", distance)) {} 
    328      
    329348    bool centerProjection = false; 
    330349 
    331     osg::Vec3d projector = eye - osg::Vec3d(0.0,0.0, distance); 
     350    osg::Vec3d projector = eye - osg::Vec3d(0.0,0.0, domeModel.distance); 
    332351     
    333352     
    334353    osg::notify(osg::NOTICE)<<"Projector position = "<<projector<<std::endl; 
    335     osg::notify(osg::NOTICE)<<"distance = "<<distance<<std::endl; 
     354    osg::notify(osg::NOTICE)<<"distance = "<<domeModel.distance<<std::endl; 
    336355 
    337356 
     
    364383    float screenRadius = heightVector.length() * 0.5f; 
    365384 
    366     double rotation = osg::DegreesToRadians(rotationDegrees); 
     385    double rotation = osg::DegreesToRadians(domeModel.rotationDegrees); 
    367386 
    368387    osg::Vec3 cursor = bottom; 
     
    370389     
    371390    int midSteps = noSteps/2; 
    372      
    373     bool flip = false; 
    374     if (arguments.read("--flip")) { flip = true; } 
    375391     
    376392    for(i=0;i<midSteps;++i) 
     
    386402            if (phi > osg::PI_2) phi = osg::PI_2; 
    387403 
    388             double f = distance * sin(phi); 
    389             double e = distance * cos(phi) + sqrt( sphere_radius*sphere_radius - f*f); 
     404            double f = domeModel.distance * sin(phi); 
     405            double e = domeModel.distance * cos(phi) + sqrt( domeModel.sphere_radius*domeModel.sphere_radius - f*f); 
    390406            double l = e * cos(phi); 
    391407            double h = e * sin(phi); 
    392             double gamma = atan2(h, l-distance); 
     408            double gamma = atan2(h, l-domeModel.distance); 
    393409 
    394410            osg::Vec2 texcoord(theta/(2.0*osg::PI), 1.0-gamma/osg::PI); 
     
    396412            // osg::notify(osg::NOTICE)<<"cursor = "<<cursor<< " theta = "<<theta<< "phi="<<phi<<" gamma = "<<gamma<<" texcoord="<<texcoord<<std::endl; 
    397413 
    398             if (flip) 
     414            if (domeModel.flip) 
    399415                vertices->push_back(osg::Vec3(cursor.x(), top.y()-(cursor.y()-origin.y()),cursor.z())); 
    400416            else 
     
    402418             
    403419            colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); 
    404             texcoords->push_back(texcoord); 
     420            texcoords->push_back( domeModel.texcoord_flip ? osg::Vec2(texcoord.x(), 1.0f - texcoord.y()) : texcoord); 
    405421 
    406422            if (j+1<midSteps) cursor += dx; 
     
    414430            if (phi > osg::PI_2) phi = osg::PI_2; 
    415431 
    416             double f = distance * sin(phi); 
    417             double e = distance * cos(phi) + sqrt( sphere_radius*sphere_radius - f*f); 
     432            double f = domeModel.distance * sin(phi); 
     433            double e = domeModel.distance * cos(phi) + sqrt( domeModel.sphere_radius*domeModel.sphere_radius - f*f); 
    418434            double l = e * cos(phi); 
    419435            double h = e * sin(phi); 
    420             double gamma = atan2(h, l-distance); 
     436            double gamma = atan2(h, l-domeModel.distance); 
    421437 
    422438            osg::Vec2 texcoord(theta/(2.0*osg::PI), 1.0-gamma/osg::PI); 
     
    424440            // osg::notify(osg::NOTICE)<<"cursor = "<<cursor<< " theta = "<<theta<< "phi="<<phi<<" gamma = "<<gamma<<" texcoord="<<texcoord<<std::endl; 
    425441 
    426             if (flip) 
     442            if (domeModel.flip) 
    427443                vertices->push_back(osg::Vec3(cursor.x(), top.y()-(cursor.y()-origin.y()),cursor.z())); 
    428444            else 
     
    430446 
    431447            colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); 
    432             texcoords->push_back(texcoord); 
     448            texcoords->push_back( domeModel.texcoord_flip ? osg::Vec2(texcoord.x(), 1.0f - texcoord.y()) : texcoord); 
    433449 
    434450            cursor += dx; 
     
    448464            if (phi > osg::PI_2) phi = osg::PI_2; 
    449465 
    450             double f = distance * sin(phi); 
    451             double e = distance * cos(phi) + sqrt( sphere_radius*sphere_radius - f*f); 
     466            double f = domeModel.distance * sin(phi); 
     467            double e = domeModel.distance * cos(phi) + sqrt( domeModel.sphere_radius*domeModel.sphere_radius - f*f); 
    452468            double l = e * cos(phi); 
    453469            double h = e * sin(phi); 
    454             double gamma = atan2(h, l-distance); 
     470            double gamma = atan2(h, l-domeModel.distance); 
    455471 
    456472            osg::Vec2 texcoord(theta/(2.0*osg::PI), 1.0-gamma/osg::PI); 
     
    458474            // osg::notify(osg::NOTICE)<<"cursor = "<<cursor<< " theta = "<<theta<< "phi="<<phi<<" gamma = "<<gamma<<" texcoord="<<texcoord<<std::endl; 
    459475 
    460             if (flip) 
     476            if (domeModel.flip) 
    461477                vertices->push_back(osg::Vec3(cursor.x(), top.y()-(cursor.y()-origin.y()),cursor.z())); 
    462478            else 
     
    464480 
    465481            colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); 
    466             texcoords->push_back(texcoord); 
     482            texcoords->push_back( domeModel.texcoord_flip ? osg::Vec2(texcoord.x(), 1.0f - texcoord.y()) : texcoord); 
    467483 
    468484            cursor += dx; 
     
    516532    while (arguments.read("--width",width)) {} 
    517533    while (arguments.read("--height",height)) {} 
     534 
     535    DomeModel domeModel(arguments); 
    518536 
    519537    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; 
     
    536554    } 
    537555 
    538     osg::ref_ptr<osg::Drawable> distortionCorrectionMash = createDomeDistortionMesh(osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(width,0.0f,0.0f), osg::Vec3(0.0f,height,0.0f), arguments); 
    539  
    540556    osg::Texture* texture = 0; 
    541557    for(int i=1;i<arguments.argc() && !texture;++i) 
     
    549565            if (image) 
    550566            { 
     567                domeModel.texcoord_flip = image->getOrigin()==osg::Image::TOP_LEFT; 
     568             
    551569#if 1             
    552570                texture = new osg::TextureRectangle(image); 
     
    566584    { 
    567585        osg::Geode* geode = new osg::Geode(); 
    568         geode->addDrawable(distortionCorrectionMash.get()); 
     586        geode->addDrawable( createDomeDistortionMesh(osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(width,0.0f,0.0f), osg::Vec3(0.0f,height,0.0f), domeModel) ); 
    569587 
    570588        // new we need to add the texture to the mesh, we do so by creating a