Show
Ignore:
Timestamp:
03/30/09 11:55:40 (6 years ago)
Author:
robert
Message:

Added shader to convert images into greyscale when rendering in anaglyphic

Files:
1 modified

Legend:

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

    r9967 r9972  
    3535typedef std::vector<std::string> FileList; 
    3636 
     37#include <osg/Program> 
     38#include <osg/Shader> 
     39 
     40osg::StateSet* createColorToGreyscaleStateSet() 
     41{ 
     42    osg::StateSet* stateset = new osg::StateSet; 
     43     
     44    osg::Program* program = new osg::Program; 
     45    stateset->setAttribute(program); 
     46     
     47    const char* fragSource = 
     48    { 
     49        "uniform sampler2D baseTexture;\n" 
     50        "uniform mat4 colorMatrix;\n" 
     51        "void main(void)\n" 
     52        "{\n" 
     53        "    vec4 color = texture2D( baseTexture, gl_TexCoord[0].st );\n" 
     54        "    gl_FragColor = colorMatrix * color;\n" 
     55        "}\n" 
     56    }; 
     57    program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragSource)); 
     58     
     59    stateset->addUniform(new osg::Uniform("baseTexture",0)); 
     60 
     61    osg::Matrixf colorMatrix( 
     62        0.3f, 0.3f, 0.3f, 0.0f, 
     63        0.59f, 0.59f, 0.59f, 0.0f, 
     64        0.11f, 0.11f, 0.11f, 0.0f, 
     65        0.0f, 0.0f, 0.0f, 1.0f 
     66    );     
     67     
     68    stateset->addUniform(new osg::Uniform("colorMatrix",colorMatrix)); 
     69 
     70    return stateset; 
     71} 
     72 
     73 
    3774osg::Geode* createSectorForImage(osg::Image* image, osg::TexMat* texmat, float s,float t, float radius, float height, float length) 
    3875{ 
     
    104141} 
    105142 
    106 osg::Group * loadImages(std::string image1, std::string image2, osg::TexMat* texmatLeft, osg::TexMat* texmatRight, float radius, float height, float length) { 
    107         osg::ref_ptr<osg::Image> imageLeft = osgDB::readImageFile(image1); 
    108         osg::ref_ptr<osg::Image> imageRight = osgDB::readImageFile(image2); 
    109         if (imageLeft.valid() && imageRight.valid()) 
    110         { 
    111             osg::ImageStream* streamLeft = dynamic_cast<osg::ImageStream*>(imageLeft.get()); 
    112             if (streamLeft) streamLeft->play(); 
    113          
    114             osg::ImageStream* streamRight = dynamic_cast<osg::ImageStream*>(imageRight.get()); 
    115             if (streamRight) streamRight->play(); 
    116              
    117          
    118             float average_s = (imageLeft->s()+imageRight->s())*0.5f; 
    119             float average_t = (imageLeft->t()+imageRight->t())*0.5f; 
    120             osg::Geode* geodeLeft = createSectorForImage(imageLeft.get(),texmatLeft,average_s,average_t, radius, height, length); 
    121             geodeLeft->setNodeMask(0x01); 
    122  
    123             osg::Geode* geodeRight = createSectorForImage(imageRight.get(),texmatRight,average_s,average_t, radius, height, length); 
    124             geodeRight->setNodeMask(0x02); 
    125  
    126             osg::Group * imageGroup = new osg::Group; 
    127              
    128             imageGroup->addChild(geodeLeft); 
    129             imageGroup->addChild(geodeRight); 
    130             return imageGroup; 
    131         } 
    132         else 
    133         { 
    134             std::cout << "Warning: Unable to load both image files, '"<<image1<<"' & '"<<image2<<"', required for stereo imaging."<<std::endl; 
    135             return 0; 
    136         } 
     143osg::Group * loadImages(std::string image1, std::string image2, osg::TexMat* texmatLeft, osg::TexMat* texmatRight, float radius, float height, float length) 
     144{ 
     145    osg::ref_ptr<osg::Image> imageLeft = osgDB::readImageFile(image1); 
     146    osg::ref_ptr<osg::Image> imageRight = osgDB::readImageFile(image2); 
     147    if (imageLeft.valid() && imageRight.valid()) 
     148    { 
     149        osg::ImageStream* streamLeft = dynamic_cast<osg::ImageStream*>(imageLeft.get()); 
     150        if (streamLeft) streamLeft->play(); 
     151 
     152        osg::ImageStream* streamRight = dynamic_cast<osg::ImageStream*>(imageRight.get()); 
     153        if (streamRight) streamRight->play(); 
     154 
     155 
     156        float average_s = (imageLeft->s()+imageRight->s())*0.5f; 
     157        float average_t = (imageLeft->t()+imageRight->t())*0.5f; 
     158        osg::Geode* geodeLeft = createSectorForImage(imageLeft.get(),texmatLeft,average_s,average_t, radius, height, length); 
     159        geodeLeft->setNodeMask(0x01); 
     160 
     161        osg::Geode* geodeRight = createSectorForImage(imageRight.get(),texmatRight,average_s,average_t, radius, height, length); 
     162        geodeRight->setNodeMask(0x02); 
     163 
     164        osg::Group * imageGroup = new osg::Group; 
     165 
     166        imageGroup->addChild(geodeLeft); 
     167        imageGroup->addChild(geodeRight); 
     168        return imageGroup; 
     169    } 
     170    else 
     171    { 
     172        std::cout << "Warning: Unable to load both image files, '"<<image1<<"' & '"<<image2<<"', required for stereo imaging."<<std::endl; 
     173        return 0; 
     174    } 
    137175} 
    138176 
     
    484522 
    485523    // construct the viewer. 
    486     osgViewer::Viewer viewer; 
     524    osgViewer::Viewer viewer(arguments); 
    487525 
    488526    // register the handler to add keyboard and mouse handling. 
     
    582620 
    583621 
     622 
    584623    viewer.getCamera()->setCullMask(0xffffffff); 
    585624    viewer.getCamera()->setCullMaskLeft(0x00000001); 
     
    589628    osg::DisplaySettings::instance()->setStereo(true); 
    590629 
     630    if (osg::DisplaySettings::instance()->getStereoMode()==osg::DisplaySettings::ANAGLYPHIC) 
     631    { 
     632        rootNode->setStateSet(createColorToGreyscaleStateSet()); 
     633    } 
     634     
     635     
    591636    // set the scene to render 
    592637    viewer.setSceneData(rootNode.get());