Show
Ignore:
Timestamp:
02/22/07 12:17:45 (8 years ago)
Author:
robert
Message:

Moved distortion mesh creation out into seperate function

Files:
1 modified

Legend:

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

    r6252 r6260  
    8888        osg::Vec3 xAxis(1.0f,0.0f,0.0f); 
    8989        osg::Vec3 yAxis(0.0f,1.0f,0.0f); 
    90         osg::Vec3 zAxis(0.0f,0.0f,1.0f); 
    9190        float height = 1024.0f; 
    9291        float width = 1280.0f; 
     
    285284} 
    286285 
    287 void setDomeCorrection(osgViewer::Viewer& viewer, osg::ArgumentParser& /*arguments*/) 
     286osg::Geometry* createDomeDistortionMesh(const osg::Vec3& origin, const osg::Vec3& widthVector, const osg::Vec3& heightVector, osg::ArgumentParser& arguments) 
     287{ 
     288    // create the quad to visualize. 
     289    osg::Geometry* geometry = new osg::Geometry(); 
     290 
     291    geometry->setSupportsDisplayList(false); 
     292 
     293    osg::Vec3 xAxis(widthVector); 
     294    float width = widthVector.length(); 
     295    xAxis /= width; 
     296 
     297    osg::Vec3 yAxis(heightVector); 
     298    float height = heightVector.length(); 
     299    yAxis /= height; 
     300     
     301    int noSteps = 50; 
     302 
     303    osg::Vec3Array* vertices = new osg::Vec3Array; 
     304    osg::Vec3Array* texcoords = new osg::Vec3Array; 
     305    osg::Vec4Array* colors = new osg::Vec4Array; 
     306 
     307    osg::Vec3 bottom = origin; 
     308    osg::Vec3 dx = xAxis*(width/((float)(noSteps-1))); 
     309    osg::Vec3 dy = yAxis*(height/((float)(noSteps-1))); 
     310 
     311    osg::Vec3 bottom_texcoord(0.0f,0.0f,0.0f); 
     312    osg::Vec3 dx_texcoord(1.0f/(float)(noSteps-1),0.0f,0.0f); 
     313    osg::Vec3 dy_texcoord(0.0f,1.0f/(float)(noSteps-1),0.0f); 
     314 
     315    osg::Vec3 cursor = bottom; 
     316    osg::Vec3 texcoord = bottom_texcoord; 
     317    int i,j; 
     318    for(i=0;i<noSteps;++i) 
     319    { 
     320        osg::Vec3 cursor = bottom+dy*(float)i; 
     321        osg::Vec3 texcoord = bottom_texcoord+dy_texcoord*(float)i; 
     322        for(j=0;j<noSteps;++j) 
     323        { 
     324            vertices->push_back(cursor); 
     325            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)); 
     326            colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); 
     327 
     328            cursor += dx; 
     329            texcoord += dx_texcoord; 
     330        } 
     331    } 
     332 
     333    // pass the created vertex array to the points geometry object. 
     334    geometry->setVertexArray(vertices); 
     335 
     336    geometry->setColorArray(colors); 
     337    geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); 
     338 
     339    geometry->setTexCoordArray(0,texcoords); 
     340 
     341    for(i=0;i<noSteps-1;++i) 
     342    { 
     343        osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP); 
     344        for(j=0;j<noSteps;++j) 
     345        { 
     346            elements->push_back(j+(i+1)*noSteps); 
     347            elements->push_back(j+(i)*noSteps); 
     348        } 
     349        geometry->addPrimitiveSet(elements); 
     350    } 
     351     
     352    return geometry; 
     353}; 
     354 
     355void setDomeCorrection(osgViewer::Viewer& viewer, osg::ArgumentParser& arguments) 
    288356{ 
    289357  
     
    314382    } 
    315383 
    316     gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f)); 
    317     gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    318  
    319  
    320     int center_x = width/2; 
    321     int center_y = height/2; 
    322     int camera_width = 512; 
    323     int camera_height = 512; 
    324      
    325384    int tex_width = 512; 
    326385    int tex_height = 512; 
    327386 
    328 #define CUBE_MAP 
    329 #ifdef CUBE_MAP     
     387    int camera_width = tex_width; 
     388    int camera_height = tex_height; 
     389 
    330390    osg::TextureCubeMap* texture = new osg::TextureCubeMap; 
    331 #else 
    332     osg::Texture2D* texture = new osg::Texture2D; 
    333 #endif     
     391 
    334392    texture->setTextureSize(tex_width, tex_height); 
    335393    texture->setInternalFormat(GL_RGB); 
    336     texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); 
    337     texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); 
    338     texture->setWrap(osg::Texture::WRAP_R, osg::Texture::CLAMP_TO_EDGE); 
    339394    texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); 
    340395    texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); 
     
    361416 
    362417        // attach the texture and use it as the color buffer. 
    363 #ifdef CUBE_MAP     
    364418        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Z); 
    365 #else 
    366         camera->attach(osg::Camera::COLOR_BUFFER, texture); 
    367 #endif 
     419 
    368420        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd()); 
    369421    } 
     
    408460 
    409461        // attach the texture and use it as the color buffer. 
    410 #ifdef CUBE_MAP     
    411462        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_X); 
    412 #else 
    413         camera->attach(osg::Camera::COLOR_BUFFER, texture); 
    414 #endif 
    415463 
    416464        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 0.0,1.0,0.0)); 
     
    431479 
    432480        // attach the texture and use it as the color buffer. 
    433 #ifdef CUBE_MAP     
    434481        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_X); 
    435 #else 
    436         camera->attach(osg::Camera::COLOR_BUFFER, texture); 
    437 #endif 
    438482 
    439483        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 0.0,1.0,0.0)); 
     
    454498 
    455499        // attach the texture and use it as the color buffer. 
    456 #ifdef CUBE_MAP     
    457500        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Y); 
    458 #else 
    459         camera->attach(osg::Camera::COLOR_BUFFER, texture); 
    460 #endif 
    461501 
    462502        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 1.0,0.0,0.0)); 
     
    477517 
    478518        // attach the texture and use it as the color buffer. 
    479 #ifdef CUBE_MAP     
    480519        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Z); 
    481 #else 
    482         camera->attach(osg::Camera::COLOR_BUFFER, texture); 
    483 #endif 
    484520 
    485521        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-180.0f), 1.0,0.0,0.0)); 
     
    494530    // distortion correction set up. 
    495531    { 
    496          
    497         // create the quad to visualize. 
    498         osg::Geometry* polyGeom = new osg::Geometry(); 
    499  
    500         polyGeom->setSupportsDisplayList(false); 
    501  
    502         osg::Vec3 origin(0.0f,0.0f,0.0f); 
    503         osg::Vec3 xAxis(1.0f,0.0f,0.0f); 
    504         osg::Vec3 yAxis(0.0f,1.0f,0.0f); 
    505         osg::Vec3 zAxis(0.0f,0.0f,1.0f); 
    506         float height = 1024.0f; 
    507         float width = 1280.0f; 
    508         int noSteps = 50; 
    509  
    510         osg::Vec3Array* vertices = new osg::Vec3Array; 
    511         osg::Vec3Array* texcoords = new osg::Vec3Array; 
    512         osg::Vec4Array* colors = new osg::Vec4Array; 
    513  
    514         osg::Vec3 bottom = origin; 
    515         osg::Vec3 dx = xAxis*(width/((float)(noSteps-1))); 
    516         osg::Vec3 dy = yAxis*(height/((float)(noSteps-1))); 
    517  
    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); 
    521  
    522         osg::Vec3 cursor = bottom; 
    523         osg::Vec3 texcoord = bottom_texcoord; 
    524         int i,j; 
    525         for(i=0;i<noSteps;++i) 
    526         { 
    527             osg::Vec3 cursor = bottom+dy*(float)i; 
    528             osg::Vec3 texcoord = bottom_texcoord+dy_texcoord*(float)i; 
    529             for(j=0;j<noSteps;++j) 
    530             { 
    531                 vertices->push_back(cursor); 
    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)); 
    533                 colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); 
    534  
    535                 cursor += dx; 
    536                 texcoord += dx_texcoord; 
    537             } 
    538         } 
    539  
    540         // pass the created vertex array to the points geometry object. 
    541         polyGeom->setVertexArray(vertices); 
    542  
    543         polyGeom->setColorArray(colors); 
    544         polyGeom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); 
    545  
    546         polyGeom->setTexCoordArray(0,texcoords); 
    547  
    548         for(i=0;i<noSteps-1;++i) 
    549         { 
    550             osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP); 
    551             for(j=0;j<noSteps;++j) 
    552             { 
    553                 elements->push_back(j+(i+1)*noSteps); 
    554                 elements->push_back(j+(i)*noSteps); 
    555             } 
    556             polyGeom->addPrimitiveSet(elements); 
    557         } 
    558  
    559  
    560         // new we need to add the texture to the Drawable, we do so by creating a  
     532        osg::Geode* geode = new osg::Geode(); 
     533        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), arguments)); 
     534 
     535        // new we need to add the texture to the mesh, we do so by creating a  
    561536        // StateSet to contain the Texture StateAttribute. 
    562         osg::StateSet* stateset = polyGeom->getOrCreateStateSet(); 
     537        osg::StateSet* stateset = geode->getOrCreateStateSet(); 
    563538        stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON); 
    564539        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); 
    565  
    566         osg::Geode* geode = new osg::Geode(); 
    567         geode->addDrawable(polyGeom); 
    568540 
    569541        osg::ref_ptr<osg::Camera> camera = new osg::Camera; 
     
    580552        //camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); 
    581553         
    582         camera->setProjectionMatrixAsOrtho2D(0,1280,0,1024); 
     554        camera->setProjectionMatrixAsOrtho2D(0,width,0,height); 
    583555        camera->setViewMatrix(osg::Matrix::identity()); 
    584556 
     
    657629    } 
    658630 
    659     viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); 
     631    // viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); 
    660632 
    661633    // add the state manipulator