root/OpenSceneGraph/trunk/examples/osgslice/osgslice.cpp @ 3797

Revision 3797, 8.2 kB (checked in by robert, 10 years ago)

Removed include of Producer/RenderSurface

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// C++ source file - (C) 2003 Robert Osfield, released under the OSGPL.
2//
3// Simple example of use of Producer::RenderSurface to create an OpenGL
4// graphics window, and OSG for rendering.
5
6#include <osg/Timer>
7#include <osgUtil/SceneView>
8#include <osgDB/ReadFile>
9#include <osgDB/WriteFile>
10
11#include <osgProducer/Viewer>
12
13#include <sstream>
14
15#define MIN_NEARFAROFFSET 0.1
16
17class SliceProcessor
18{
19    public:
20
21        SliceProcessor() : _sliceNumber(128), _sliceDelta(0.1), _nearPlane(0.0), _farPlane(MIN_NEARFAROFFSET)
22        {
23            //
24        }
25        SliceProcessor( const double& objectRadius, unsigned int numberSlices) : _sliceNumber(numberSlices)
26        {
27            _sliceDelta = (objectRadius*2) / _sliceNumber;
28            _nearPlane = objectRadius;                          // note: distance from viewpoint is going to be set 2x radius
29            _farPlane = _nearPlane+MIN_NEARFAROFFSET;
30            _image = new osg::Image;
31            if( _sliceDelta > MIN_NEARFAROFFSET )
32            {
33                _nearFarOffset = MIN_NEARFAROFFSET;
34            }
35            else
36            {
37                _nearFarOffset = _sliceDelta;
38            }
39            _image->allocateImage( _sliceNumber, _sliceNumber,_sliceNumber, GL_RGBA, GL_UNSIGNED_BYTE );
40           
41        }
42        // needs 3D-Texture object
43        osg::Image* _image;
44        unsigned int _sliceNumber;
45        double _sliceDelta;
46        double _nearPlane;
47        double _farPlane;
48        double _nearFarOffset;
49       
50    // needs function to do rendering and slicing
51};
52
53int main( int argc, char **argv )
54{
55        // use an ArgumentParser object to manage the program arguments.
56    osg::ArgumentParser arguments(&argc,argv);
57
58    // set up the usage document, in case we need to print out how to use this program.
59    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of osg::AnimationPath and UpdateCallbacks for adding animation to your scenes.");
60    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
61    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
62    arguments.getApplicationUsage()->addCommandLineOption("-o <filename>","Object to be loaded");
63   
64    if( arguments.read( "-h" ) || arguments.read( "--help" ) )
65    {
66        std::cout << "Argumentlist:" << std::endl;
67        std::cout << "\t-o <filename> sets object to be loaded and sliced" << std::endl;
68        std::cout << "\t--slices <unsigned int> sets number of slices through the object" << std::endl;
69        std::cout << "\t--near <double> sets start for near clipping plane" << std::endl;
70        std::cout << "\t--far <double> sets start for far clipping plane" << std::endl;
71       
72        return 1;
73    }
74   
75    std::string outputName("volume_tex.dds");
76    while( arguments.read( "-o", outputName ) ) { }
77
78
79    unsigned int numberSlices = 128;
80    while( arguments.read( "--slices", numberSlices) ) { }
81
82    double nearClip=0.0f;
83    double farClip=0.0f;
84    while( arguments.read( "--near",nearClip ) ) { }
85    while( arguments.read( "--far", farClip) ) { }
86
87   
88    // load the scene.
89    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles( arguments );
90    if (!loadedModel)
91    {
92        std::cout << "No data loaded." << std::endl;
93        return 1;
94    }
95
96    const osg::BoundingSphere& bs = loadedModel->getBound();
97    SliceProcessor* sp = new SliceProcessor( (double)bs.radius(), numberSlices );
98    if (nearClip!=0.0) sp->_nearPlane = nearClip;
99    if (farClip!=0.0) sp->_farPlane = farClip;
100
101    // any option left unread are converted into errors to write out later.
102    arguments.reportRemainingOptionsAsUnrecognized();
103
104    // report any errors if they have occured when parsing the program aguments.
105    if (arguments.errors())
106    {
107        arguments.writeErrorMessages(std::cout);
108        return 1;
109    }
110   
111    // create the window to draw to.
112    osg::ref_ptr<Producer::RenderSurface> renderSurface = new Producer::RenderSurface;
113    renderSurface->setWindowName("osgsimple");
114    renderSurface->setWindowRectangle(100,100,numberSlices,numberSlices);
115    renderSurface->useBorder(true);
116
117    // make sure we have a alpha channel in the colour buffer, to enable
118    // alpha on read back.
119    Producer::VisualChooser* rs_vc = renderSurface->getVisualChooser();
120    if (!rs_vc)
121    {
122        rs_vc = new Producer::VisualChooser;
123        rs_vc->setSimpleConfiguration();
124        renderSurface->setVisualChooser(rs_vc);
125    }
126    rs_vc->setAlphaSize(8);
127
128    // create the graphics context.
129    renderSurface->realize();
130   
131    // create the view of the scene.
132    osg::ref_ptr<osgUtil::SceneView> sceneView = new osgUtil::SceneView;
133    sceneView->setDefaults();
134    sceneView->setSceneData(loadedModel.get());
135
136    // initialize the view to look at the center of the scene graph
137    osg::Matrix viewMatrix;
138    // distance from viewport to object's center is set to be 2x bs.radius()
139    viewMatrix.makeLookAt(bs.center()-osg::Vec3(0.0,2.0f*bs.radius(),0.0),bs.center(),osg::Vec3(0.0f,0.0f,1.0f));
140   
141    // turn off autocompution of near and far clipping planes
142    sceneView->setComputeNearFarMode(osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR);
143
144    // set the clear color of the background to make sure that the alpha is 0.0.
145    sceneView->setClearColor(osg::Vec4(0.0f,0.0f,0.0f,0.0f));
146
147    // record the timer tick at the start of rendering.     
148    osg::Timer_t start_tick = osg::Timer::instance()->tick();
149   
150    std::cout << "radius: " << bs.radius() << std::endl;
151   
152    unsigned int frameNum = 0;
153    double tmpNear, tmpFar;
154    std::string baseImageName("shot_");
155    std::string tmpImageName;
156   
157    osg::Image* tmpImage = new osg::Image;
158   
159    // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.)
160    for( unsigned int i = 0 ; i < sp->_sliceNumber && renderSurface->isRealized() ; ++i )
161    {
162        // set up the frame stamp for current frame to record the current time and frame number so that animtion code can advance correctly
163        osg::ref_ptr<osg::FrameStamp> frameStamp = new osg::FrameStamp;
164        frameStamp->setReferenceTime(osg::Timer::instance()->delta_s(start_tick,osg::Timer::instance()->tick()));
165        frameStamp->setFrameNumber(frameNum++);
166       
167        // pass frame stamp to the SceneView so that the update, cull and draw traversals all use the same FrameStamp
168        sceneView->setFrameStamp(frameStamp.get());
169       
170        // update the viewport dimensions, incase the window has been resized.
171        sceneView->setViewport(0,0,renderSurface->getWindowWidth(),renderSurface->getWindowHeight());
172       
173       
174        // set the view
175        sceneView->setViewMatrix(viewMatrix);
176       
177        // set Projection Matrix
178        tmpNear = sp->_nearPlane+i*sp->_sliceDelta;
179        tmpFar = sp->_farPlane+(i*sp->_sliceDelta)+sp->_nearFarOffset;
180        sceneView->setProjectionMatrixAsOrtho(-(bs.radius()+bs.radius()/2), bs.radius()+bs.radius()/2,-bs.radius(), bs.radius(), tmpNear, tmpFar);
181
182        // do the update traversal the scene graph - such as updating animations
183        sceneView->update();
184       
185        // do the cull traversal, collect all objects in the view frustum into a sorted set of rendering bins
186        sceneView->cull();
187       
188        // draw the rendering bins.
189        sceneView->draw();
190               
191        // Swap Buffers
192        renderSurface->swapBuffers();
193       
194        std::cout << "before readPixels: _r = " << sp->_image->r() << std::endl;
195       
196        tmpImage->readPixels(sceneView->getViewport()->x(),sceneView->getViewport()->y(),sceneView->getViewport()->width(),sceneView->getViewport()->height(),GL_RGBA,GL_UNSIGNED_BYTE);
197       
198//        std::cout << "vor copySubImage: _r = " << sp->_image->r() << std::endl;
199        sp->_image->copySubImage( 0, 0, i, tmpImage );
200
201        /*
202        std::ostringstream o;
203        o << baseImageName << i << ".rgba";
204        tmpImageName = o.str();
205        osgDB::writeImageFile( *(sp->_image), tmpImageName );
206        std::cout << "Wrote image to file: " << tmpImageName << std::endl;
207        */
208    }
209    osgDB::writeImageFile( *(sp->_image), outputName);
210
211    return 0;
212}
Note: See TracBrowser for help on using the browser.