Show
Ignore:
Timestamp:
01/11/07 16:19:59 (8 years ago)
Author:
robert
Message:

Ported following examples to osgViewer:

osgteapot
osgtessellate
osgtext
osgtexture1D
osgtexture2D
osgtexture3D
osgtexturerectangle
osgvertexprogram
osgvolume
osgwindows
osgsimple
osgkeyboardmouse

Files:
1 modified

Legend:

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

    r5763 r5962  
    99#include <osgDB/ReadFile> 
    1010 
    11 #include <osgProducer/Viewer> 
    12  
     11#include <osgViewer/Viewer> 
     12 
     13#include <iostream> 
    1314 
    1415// 
     
    2122 
    2223 
    23 class ConstructStateCallback : public osgProducer::OsgCameraGroup::RealizeCallback 
    24 { 
     24class MyGraphicsContext { 
    2525    public: 
    26         ConstructStateCallback(osg::Node* node):_node(node),_initialized(false) {} 
    27          
    28         osg::StateSet* constructState() 
     26        MyGraphicsContext() 
    2927        { 
    30          
    31             // read 4 2d images 
    32             osg::ref_ptr<osg::Image> image_0 = osgDB::readImageFile("Images/lz.rgb"); 
    33             osg::ref_ptr<osg::Image> image_1 = osgDB::readImageFile("Images/reflect.rgb"); 
    34             osg::ref_ptr<osg::Image> image_2 = osgDB::readImageFile("Images/tank.rgb"); 
    35             osg::ref_ptr<osg::Image> image_3 = osgDB::readImageFile("Images/skymap.jpg"); 
    36  
    37             if (!image_0 || !image_1 || !image_2 || !image_3) 
    38             { 
    39                 std::cout << "Warning: could not open files."<<std::endl; 
    40                 return new osg::StateSet; 
    41             } 
    42  
    43             if (image_0->getPixelFormat()!=image_1->getPixelFormat() || image_0->getPixelFormat()!=image_2->getPixelFormat() || image_0->getPixelFormat()!=image_3->getPixelFormat()) 
    44             { 
    45                 std::cout << "Warning: image pixel formats not compatible."<<std::endl; 
    46                 return new osg::StateSet; 
    47             } 
    48  
    49             // get max 3D texture size 
    50             GLint textureSize = osg::Texture3D::getExtensions(0,true)->maxTexture3DSize(); 
    51             if (textureSize > 256) 
    52                 textureSize = 256; 
    53  
    54             // scale them all to the same size. 
    55             image_0->scaleImage(textureSize,textureSize,1); 
    56             image_1->scaleImage(textureSize,textureSize,1); 
    57             image_2->scaleImage(textureSize,textureSize,1); 
    58             image_3->scaleImage(textureSize,textureSize,1); 
    59  
    60  
    61             // then allocated a 3d image to use for texturing. 
    62             osg::Image* image_3d = new osg::Image; 
    63             image_3d->allocateImage(textureSize,textureSize,4, 
    64                                     image_0->getPixelFormat(),image_0->getDataType()); 
    65  
    66             // copy the 2d images into the 3d image. 
    67             image_3d->copySubImage(0,0,0,image_0.get()); 
    68             image_3d->copySubImage(0,0,1,image_1.get()); 
    69             image_3d->copySubImage(0,0,2,image_2.get()); 
    70             image_3d->copySubImage(0,0,3,image_3.get()); 
    71  
    72             image_3d->setInternalTextureFormat(image_0->getInternalTextureFormat());         
    73  
    74             // set up the 3d texture itself, 
    75             // note, well set the filtering up so that mip mapping is disabled, 
    76             // gluBuild3DMipsmaps doesn't do a very good job of handled the 
    77             // inbalanced dimensions of the 256x256x4 texture. 
    78             osg::Texture3D* texture3D = new osg::Texture3D; 
    79             texture3D->setFilter(osg::Texture3D::MIN_FILTER,osg::Texture3D::LINEAR); 
    80             texture3D->setFilter(osg::Texture3D::MAG_FILTER,osg::Texture3D::LINEAR); 
    81             texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::REPEAT); 
    82             texture3D->setImage(image_3d); 
    83  
    84  
    85             // create a texgen to generate a R texture coordinate, the geometry 
    86             // itself will supply the S & T texture coordinates. 
    87             // in the animateStateSet callback well alter this R value to 
    88             // move the texture through the 3d texture, 3d texture filtering 
    89             // will do the blending for us. 
    90             osg::TexGen* texgen = new osg::TexGen; 
    91             texgen->setMode(osg::TexGen::OBJECT_LINEAR); 
    92             texgen->setPlane(osg::TexGen::R, osg::Plane(0.0f,0.0f,0.0f,0.2f)); 
    93  
    94             // create the StateSet to store the texture data 
    95             osg::StateSet* stateset = new osg::StateSet; 
    96             stateset->setTextureMode(0,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); 
    97             stateset->setTextureAttribute(0,texgen); 
    98             stateset->setTextureAttributeAndModes(0,texture3D,osg::StateAttribute::ON); 
    99  
    100             return stateset; 
    101         } 
    102  
    103         virtual void operator()( osgProducer::OsgCameraGroup&, osgProducer::OsgSceneHandler& sh, const Producer::RenderSurface& ) 
    104         { 
    105             { 
    106                 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); 
    107  
    108                 if (!_initialized) 
    109                 { 
    110  
    111                     // only initialize state once, only need for cases where multiple graphics contexts are 
    112                     // if which case this callback can get called multiple times. 
    113                     _initialized = true; 
    114  
    115                     if (_node) _node->setStateSet(constructState()); 
    116                 }             
    117  
    118             } 
    119              
    120             // now safe to con 
    121             sh.init(); 
    122              
     28            osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; 
     29            traits->x = 0; 
     30            traits->y = 0; 
     31            traits->width = 1; 
     32            traits->height = 1; 
     33            traits->windowDecoration = false; 
     34            traits->doubleBuffer = false; 
     35            traits->sharedContext = 0; 
     36            traits->pbuffer = true; 
     37 
     38            _gc = osg::GraphicsContext::createGraphicsContext(traits.get()); 
     39 
     40            if (!_gc) 
     41            { 
     42                traits->pbuffer = false; 
     43                _gc = osg::GraphicsContext::createGraphicsContext(traits.get()); 
     44            } 
     45 
     46            if (_gc.valid())  
     47            { 
     48                _gc->realize(); 
     49                _gc->makeCurrent(); 
     50            } 
    12351        } 
    12452         
     53        bool valid() const { return _gc.valid() && _gc->isRealized(); } 
    12554         
    126         OpenThreads::Mutex  _mutex; 
    127         osg::Node*          _node; 
    128         bool                _initialized; 
    129          
     55    private: 
     56        osg::ref_ptr<osg::GraphicsContext> _gc; 
    13057}; 
     58 
     59 
     60osg::StateSet* createState() 
     61{ 
     62    MyGraphicsContext gc; 
     63    if (!gc.valid())  
     64    { 
     65        osg::notify(osg::NOTICE)<<"Unable to create the graphics context required to build 3d image."<<std::endl; 
     66        return 0; 
     67    } 
     68 
     69    // read 4 2d images 
     70    osg::ref_ptr<osg::Image> image_0 = osgDB::readImageFile("Images/lz.rgb"); 
     71    osg::ref_ptr<osg::Image> image_1 = osgDB::readImageFile("Images/reflect.rgb"); 
     72    osg::ref_ptr<osg::Image> image_2 = osgDB::readImageFile("Images/tank.rgb"); 
     73    osg::ref_ptr<osg::Image> image_3 = osgDB::readImageFile("Images/skymap.jpg"); 
     74 
     75    if (!image_0 || !image_1 || !image_2 || !image_3) 
     76    { 
     77        std::cout << "Warning: could not open files."<<std::endl; 
     78        return new osg::StateSet; 
     79    } 
     80 
     81    if (image_0->getPixelFormat()!=image_1->getPixelFormat() || image_0->getPixelFormat()!=image_2->getPixelFormat() || image_0->getPixelFormat()!=image_3->getPixelFormat()) 
     82    { 
     83        std::cout << "Warning: image pixel formats not compatible."<<std::endl; 
     84        return new osg::StateSet; 
     85    } 
     86 
     87    // get max 3D texture size 
     88    GLint textureSize = osg::Texture3D::getExtensions(0,true)->maxTexture3DSize(); 
     89    if (textureSize > 256) 
     90        textureSize = 256; 
     91 
     92    // scale them all to the same size. 
     93    image_0->scaleImage(textureSize,textureSize,1); 
     94    image_1->scaleImage(textureSize,textureSize,1); 
     95    image_2->scaleImage(textureSize,textureSize,1); 
     96    image_3->scaleImage(textureSize,textureSize,1); 
     97 
     98 
     99    // then allocated a 3d image to use for texturing. 
     100    osg::Image* image_3d = new osg::Image; 
     101    image_3d->allocateImage(textureSize,textureSize,4, 
     102                            image_0->getPixelFormat(),image_0->getDataType()); 
     103 
     104    // copy the 2d images into the 3d image. 
     105    image_3d->copySubImage(0,0,0,image_0.get()); 
     106    image_3d->copySubImage(0,0,1,image_1.get()); 
     107    image_3d->copySubImage(0,0,2,image_2.get()); 
     108    image_3d->copySubImage(0,0,3,image_3.get()); 
     109 
     110    image_3d->setInternalTextureFormat(image_0->getInternalTextureFormat());         
     111 
     112    // set up the 3d texture itself, 
     113    // note, well set the filtering up so that mip mapping is disabled, 
     114    // gluBuild3DMipsmaps doesn't do a very good job of handled the 
     115    // inbalanced dimensions of the 256x256x4 texture. 
     116    osg::Texture3D* texture3D = new osg::Texture3D; 
     117    texture3D->setFilter(osg::Texture3D::MIN_FILTER,osg::Texture3D::LINEAR); 
     118    texture3D->setFilter(osg::Texture3D::MAG_FILTER,osg::Texture3D::LINEAR); 
     119    texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::REPEAT); 
     120    texture3D->setImage(image_3d); 
     121 
     122 
     123    // create a texgen to generate a R texture coordinate, the geometry 
     124    // itself will supply the S & T texture coordinates. 
     125    // in the animateStateSet callback well alter this R value to 
     126    // move the texture through the 3d texture, 3d texture filtering 
     127    // will do the blending for us. 
     128    osg::TexGen* texgen = new osg::TexGen; 
     129    texgen->setMode(osg::TexGen::OBJECT_LINEAR); 
     130    texgen->setPlane(osg::TexGen::R, osg::Plane(0.0f,0.0f,0.0f,0.2f)); 
     131 
     132    // create the StateSet to store the texture data 
     133    osg::StateSet* stateset = new osg::StateSet; 
     134    stateset->setTextureMode(0,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); 
     135    stateset->setTextureAttribute(0,texgen); 
     136    stateset->setTextureAttributeAndModes(0,texture3D,osg::StateAttribute::ON); 
     137 
     138    return stateset; 
     139} 
     140 
    131141 
    132142class UpdateStateCallback : public osg::NodeCallback 
     
    214224    // this current limitation. 
    215225    geode->setUpdateCallback(new UpdateStateCallback()); 
     226    geode->setStateSet(createState()); 
    216227     
    217228    return geode; 
     
    220231 
    221232 
    222 int main( int argc, char **argv ) 
    223 { 
    224  
    225     // use an ArgumentParser object to manage the program arguments. 
    226     osg::ArgumentParser arguments(&argc,argv); 
    227      
    228     // set up the usage document, in case we need to print out how to use this program. 
    229     arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of 3D textures."); 
    230     arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); 
    231     arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); 
    232      
    233  
     233int main(int , char **) 
     234{ 
    234235    // construct the viewer. 
    235     osgProducer::Viewer viewer(arguments); 
    236  
    237     // set up the value with sensible default event handlers. 
    238     viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); 
    239  
    240     // get details on keyboard and mouse bindings used by the viewer. 
    241     viewer.getUsage(*arguments.getApplicationUsage()); 
    242  
    243     // if user request help write it out to cout. 
    244     if (arguments.read("-h") || arguments.read("--help")) 
    245     { 
    246         arguments.getApplicationUsage()->write(std::cout); 
    247         return 1; 
    248     } 
    249  
    250     // any option left unread are converted into errors to write out later. 
    251     arguments.reportRemainingOptionsAsUnrecognized(); 
    252  
    253     // report any errors if they have occured when parsing the program aguments. 
    254     if (arguments.errors()) 
    255     { 
    256         arguments.writeErrorMessages(std::cout); 
    257         return 1; 
    258     } 
    259      
    260     // create a model from the images. 
    261     osg::Node* rootNode = createModel(); 
    262  
    263     if (rootNode)  
    264     { 
    265  
    266         // set the scene to render 
    267         viewer.setSceneData(rootNode); 
    268          
    269         // the construct state uses gl commands to resize images so we are forced 
    270         // to only call it once a valid graphics context has been established, 
    271         // for that we use a realize callback. 
    272         viewer.setRealizeCallback(new ConstructStateCallback(rootNode)); 
    273  
    274         // create the windows and run the threads. 
    275         viewer.realize(); 
    276  
    277         while( !viewer.done() ) 
    278         { 
    279             // wait for all cull and draw threads to complete. 
    280             viewer.sync(); 
    281  
    282             // update the scene by traversing it with the the update visitor which will 
    283             // call all node update callbacks and animations. 
    284             viewer.update(); 
    285  
    286             // fire off the cull and draw traversals of the scene. 
    287             viewer.frame(); 
    288  
    289         } 
    290          
    291         // wait for all cull and draw threads to complete. 
    292         viewer.sync(); 
    293  
    294         // run a clean up frame to delete all OpenGL objects. 
    295         viewer.cleanup_frame(); 
    296  
    297         // wait for all the clean up frame to complete. 
    298         viewer.sync(); 
    299     }     
    300      
    301     return 0; 
    302 } 
     236    osgViewer::Viewer viewer; 
     237 
     238    // create a model from the images and pass it to the viewer. 
     239    viewer.setSceneData(createModel()); 
     240 
     241    return viewer.run(); 
     242}