| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 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 | |
|---|
| 17 | class 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; |
|---|
| 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 | |
|---|
| 43 | osg::Image* _image; |
|---|
| 44 | unsigned int _sliceNumber; |
|---|
| 45 | double _sliceDelta; |
|---|
| 46 | double _nearPlane; |
|---|
| 47 | double _farPlane; |
|---|
| 48 | double _nearFarOffset; |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | }; |
|---|
| 52 | |
|---|
| 53 | int main( int argc, char **argv ) |
|---|
| 54 | { |
|---|
| 55 | |
|---|
| 56 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 102 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | if (arguments.errors()) |
|---|
| 106 | { |
|---|
| 107 | arguments.writeErrorMessages(std::cout); |
|---|
| 108 | return 1; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 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 | |
|---|
| 118 | |
|---|
| 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 | |
|---|
| 129 | renderSurface->realize(); |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | osg::ref_ptr<osgUtil::SceneView> sceneView = new osgUtil::SceneView; |
|---|
| 133 | sceneView->setDefaults(); |
|---|
| 134 | sceneView->setSceneData(loadedModel.get()); |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | osg::Matrix viewMatrix; |
|---|
| 138 | |
|---|
| 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 | |
|---|
| 142 | sceneView->setComputeNearFarMode(osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR); |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | sceneView->setClearColor(osg::Vec4(0.0f,0.0f,0.0f,0.0f)); |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 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 | |
|---|
| 160 | for( unsigned int i = 0 ; i < sp->_sliceNumber && renderSurface->isRealized() ; ++i ) |
|---|
| 161 | { |
|---|
| 162 | |
|---|
| 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 | |
|---|
| 168 | sceneView->setFrameStamp(frameStamp.get()); |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | sceneView->setViewport(0,0,renderSurface->getWindowWidth(),renderSurface->getWindowHeight()); |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | sceneView->setViewMatrix(viewMatrix); |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 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 | |
|---|
| 183 | sceneView->update(); |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | sceneView->cull(); |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | sceneView->draw(); |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 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 | |
|---|
| 199 | sp->_image->copySubImage( 0, 0, i, tmpImage ); |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | } |
|---|
| 209 | osgDB::writeImageFile( *(sp->_image), outputName); |
|---|
| 210 | |
|---|
| 211 | return 0; |
|---|
| 212 | } |
|---|