Changeset 313


Ignore:
Timestamp:
Sep 4, 2011, 11:24:46 AM (13 years ago)
Author:
Torben Dannhauer
Message:
 
Location:
experimental/distortionNG
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • experimental/distortionNG/distortionNG.cpp

    r310 r313  
    11#include "distortionNG.h"
    22
    3 #include<osg/Texture2D>
    43#include<osg/Point>
    5 #include<osg/ComputeBoundsVisitor>
     4
    65
    76#include<osgViewer/Viewer>
    8 
    9 #include<osgDB/ReadFile>
    10 
    11 #include <osgUtil/SmoothingVisitor>
    127
    138
     
    2116}
    2217
    23 osg::Geometry* distortionNG::createMesh( unsigned int column, unsigned int row )
    24 {
    25         osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array(column * row);
    26         osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array(column * row);
    27         for ( unsigned int i=0; i<row; ++i )
    28         {
    29                 for ( unsigned int j=0; j<column; ++j )
    30                 {
    31                         (*vertices)[i*column + j].set( (float)i, (float)j, 0.0f );
    32                         (*texcoords)[i*column + j].set( (float)i/(float)row, (float)j/(float)column );
    33                 }
    34         }
    3518
    36         osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
    37         geom->setUseDisplayList( false );
    38         geom->setUseVertexBufferObjects( true );
    39         geom->setVertexArray( vertices.get() );
    40         geom->setTexCoordArray( 0, texcoords.get() );
    41         for ( unsigned int i=0; i<row-1; ++i )
    42         {
    43                 osg::ref_ptr<osg::DrawElementsUInt> de = new osg::DrawElementsUInt(GL_QUAD_STRIP, column*2);
    44                 for ( unsigned int j=0; j<column; ++j )
    45                 {
    46                         (*de)[j*2 + 0] = i*column + j;
    47                         (*de)[j*2 + 1] = (i+1)*column + j;
    48                 }
    49                 geom->addPrimitiveSet( de.get() );
    50         }
    51 
    52 
    53 
    54         osg::ComputeBoundsVisitor cbv;
    55         cbv.applyDrawable(geom);
    56         osg::BoundingBox box = cbv.getBoundingBox();
    57         if (!box.valid())
    58                 std::cout << "Invalid bounding box!";
    59         geom->setInitialBound(box);
    60 
    61         osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
    62         texture->setImage( osgDB::readImageFile("Images/osg256.png") );
    63         texture->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR );
    64         texture->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR );
    65         geom->getOrCreateStateSet()->setTextureAttributeAndModes( 0, texture.get() );
    66 
    67         // Create normals
    68         osgUtil::SmoothingVisitor::smooth( *geom );
    69 
    70         return geom.release();
    71 }
    7219
    7320osg::Geode* distortionHandler::createVertexHighlighter()
  • experimental/distortionNG/distortionNG.h

    r310 r313  
    3838        distortionNG();
    3939        virtual ~distortionNG();
    40 
    41         static osg::Geometry* createMesh(unsigned int column, unsigned int row);
    4240};
  • experimental/distortionNG/extViewer.cpp

    r311 r313  
    11#include "extViewer.h"
    22
     3#include<osg/Texture2D>
     4#include<osg/TextureRectangle>
     5#include<osg/TexMat>
     6#include<osg/ComputeBoundsVisitor>
     7
     8#include<osgDB/ReadFile>
     9
     10#include <osgUtil/SmoothingVisitor>
     11
    312extViewer::extViewer() : Viewer()
    413{
     
    2130}
    2231
    23 void extViewer::setUpViewForDistortion()
    24 {
    25     OSG_INFO<<"View::setUpViewForDistortion"<<std::endl;
     32void extViewer::setUpViewForManualDistortion(osg::Image* intensityMap, unsigned int screenNum)
     33{
     34        OSG_INFO<<"View::setUpViewForManualDistortion(sn="<<screenNum<<", im="<<intensityMap<<")"<<std::endl;
     35
    2636    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
    2737    if (!wsi)
     
    3141    }
    3242
    33     osg::GraphicsContext::ScreenIdentifier si;
     43        osg::GraphicsContext::ScreenIdentifier si;
    3444    si.readDISPLAY();
    3545
     
    4252    wsi->getScreenResolution(si, width, height);
    4353
    44            
    4554        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
    4655    traits->hostName = si.hostName;
     
    5564    traits->sharedContext = 0;
    5665
    57 
    58     osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
     66        bool applyIntensityMapAsColours = true;
     67
     68        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
    5969    if (!gc)
    6070    {
     
    6272        return;
    6373    }
    64 }
     74
     75    int tex_width = width;
     76    int tex_height = height;
     77
     78    int camera_width = tex_width;
     79    int camera_height = tex_height;
     80
     81    osg::TextureRectangle* texture = new osg::TextureRectangle;
     82
     83    texture->setTextureSize(tex_width, tex_height);
     84    texture->setInternalFormat(GL_RGB);
     85    texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
     86    texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
     87    texture->setWrap(osg::Texture::WRAP_S,osg::Texture::CLAMP_TO_EDGE);
     88    texture->setWrap(osg::Texture::WRAP_T,osg::Texture::CLAMP_TO_EDGE);
     89
     90
     91#if 0
     92    osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::SEPERATE_WINDOW;
     93    GLenum buffer = GL_FRONT;
     94#else
     95    osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::FRAME_BUFFER_OBJECT;
     96    GLenum buffer = GL_FRONT;
     97#endif
     98
     99        // Scene camera
     100    {
     101        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
     102        camera->setName("Scene camera");
     103        camera->setGraphicsContext(gc.get());
     104        camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height));
     105        camera->setDrawBuffer(buffer);
     106        camera->setReadBuffer(buffer);
     107        camera->setAllowEventFocus(false);
     108        // tell the camera to use OpenGL frame buffer object where supported.
     109        camera->setRenderTargetImplementation(renderTargetImplementation);
     110
     111        // attach the texture and use it as the color buffer.
     112        camera->attach(osg::Camera::COLOR_BUFFER, texture);
     113
     114        addSlave(camera.get(), osg::Matrixd(), osg::Matrixd());
     115    }
     116
     117    // distortion correction set up.
     118    {
     119        osg::Geode* geode = new osg::Geode();
     120        //geode->addDrawable(createParoramicSphericalDisplayDistortionMesh(osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(width,0.0f,0.0f), osg::Vec3(0.0f,height,0.0f), radius, collar, applyIntensityMapAsColours ? intensityMap : 0, projectorMatrix));
     121                geode->addDrawable(createMesh(16, 19));
     122
     123        // new we need to add the texture to the mesh, we do so by creating a
     124        // StateSet to contain the Texture StateAttribute.
     125        osg::StateSet* stateset = geode->getOrCreateStateSet();
     126        stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON);
     127        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
     128
     129        osg::TexMat* texmat = new osg::TexMat;
     130        texmat->setScaleByTextureRectangleSize(true);
     131        stateset->setTextureAttributeAndModes(0, texmat, osg::StateAttribute::ON);
     132
     133        if (!applyIntensityMapAsColours && intensityMap)
     134        {
     135            stateset->setTextureAttributeAndModes(1, new osg::Texture2D(intensityMap), osg::StateAttribute::ON);
     136        }
     137
     138        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
     139        camera->setGraphicsContext(gc.get());
     140        camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
     141        camera->setClearColor( osg::Vec4(0.0,0.0,0.0,1.0) );
     142        camera->setViewport(new osg::Viewport(0, 0, width, height));
     143        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
     144        camera->setDrawBuffer(buffer);
     145        camera->setReadBuffer(buffer);
     146        camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
     147        camera->setAllowEventFocus(false);
     148        camera->setInheritanceMask(camera->getInheritanceMask() & ~osg::CullSettings::CLEAR_COLOR & ~osg::CullSettings::COMPUTE_NEAR_FAR_MODE);
     149        //camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
     150
     151        camera->setProjectionMatrixAsOrtho2D(0,width,0,height);
     152        camera->setViewMatrix(osg::Matrix::identity());
     153
     154        // add subgraph to render
     155        camera->addChild(geode);
     156
     157        camera->setName("DistortionCorrectionCamera");
     158
     159        addSlave(camera.get(), osg::Matrixd(), osg::Matrixd(), false);
     160    }
     161}
     162
     163osg::Geometry* extViewer::createMesh( unsigned int column, unsigned int row )
     164{
     165        osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array(column * row);
     166        osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array(column * row);
     167        for ( unsigned int i=0; i<row; ++i )
     168        {
     169                for ( unsigned int j=0; j<column; ++j )
     170                {
     171                        (*vertices)[i*column + j].set( (float)i, (float)j, 0.0f );
     172                        (*texcoords)[i*column + j].set( (float)i/(float)row, (float)j/(float)column );
     173                }
     174        }
     175
     176        osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
     177        geom->setUseDisplayList( false );
     178        geom->setUseVertexBufferObjects( true );
     179        geom->setVertexArray( vertices.get() );
     180        geom->setTexCoordArray( 0, texcoords.get() );
     181        for ( unsigned int i=0; i<row-1; ++i )
     182        {
     183                osg::ref_ptr<osg::DrawElementsUInt> de = new osg::DrawElementsUInt(GL_QUAD_STRIP, column*2);
     184                for ( unsigned int j=0; j<column; ++j )
     185                {
     186                        (*de)[j*2 + 0] = i*column + j;
     187                        (*de)[j*2 + 1] = (i+1)*column + j;
     188                }
     189                geom->addPrimitiveSet( de.get() );
     190        }
     191
     192
     193
     194        osg::ComputeBoundsVisitor cbv;
     195        cbv.applyDrawable(geom);
     196        osg::BoundingBox box = cbv.getBoundingBox();
     197        if (!box.valid())
     198                std::cout << "Invalid bounding box!";
     199        geom->setInitialBound(box);
     200
     201        osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
     202        texture->setImage( osgDB::readImageFile("Images/osg256.png") );
     203        texture->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR );
     204        texture->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR_MIPMAP_LINEAR );
     205        geom->getOrCreateStateSet()->setTextureAttributeAndModes( 0, texture.get() );
     206
     207        // Create normals
     208        osgUtil::SmoothingVisitor::smooth( *geom );
     209
     210        return geom.release();
     211}
  • experimental/distortionNG/extViewer.h

    r311 r313  
    1414
    1515                /** Convenience method for projection on curved screens using a slave camera rendering scene and a second camera doing distortion correction to present on a nonplaner display.*/
    16         void setUpViewForDistortion();
     16        void setUpViewForManualDistortion(osg::Image* intensityMap, unsigned int screenNum=0);
     17
     18                static osg::Geometry* createMesh(unsigned int column, unsigned int row);
    1719};
  • experimental/distortionNG/main.cpp

    r311 r313  
    107107
    108108        osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    109         geode->addDrawable( distortionNG::createMesh(16, 9) );
     109        geode->addDrawable( extViewer::createMesh(16, 9) );
    110110        geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
    111111        geode->getOrCreateStateSet()->setAttributeAndModes( new osg::PolygonOffset(1.0f, 1.0f) );
Note: See TracChangeset for help on using the changeset viewer.