| 1 | #include <osg/ComputeBoundsVisitor> |
|---|
| 2 | #include <osg/io_utils> |
|---|
| 3 | #include <osg/Notify> |
|---|
| 4 | #include <osgDB/FileUtils> |
|---|
| 5 | #include <osgDB/FileNameUtils> |
|---|
| 6 | #include "ReaderWriterPOV.h" |
|---|
| 7 | #include "POVWriterNodeVisitor.h" |
|---|
| 8 | |
|---|
| 9 | using namespace std; |
|---|
| 10 | using namespace osg; |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | REGISTER_OSGPLUGIN( Povray, ReaderWriterPOV ) |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | ReaderWriterPOV::ReaderWriterPOV() |
|---|
| 22 | { |
|---|
| 23 | |
|---|
| 24 | supportsExtension( "pov", "POV-Ray format" ); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | static osgDB::ReaderWriter::WriteResult |
|---|
| 29 | writeNodeImplementation( const Node& node, ostream& fout, |
|---|
| 30 | const osgDB::ReaderWriter::Options* options ) |
|---|
| 31 | { |
|---|
| 32 | |
|---|
| 33 | const Camera *camera = dynamic_cast< const Camera* >( &node ); |
|---|
| 34 | |
|---|
| 35 | Vec3d eye, center, up, right; |
|---|
| 36 | double fovy, aspectRatio, tmp; |
|---|
| 37 | if( camera ) { |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | camera->getViewMatrixAsLookAt( eye, center, up ); |
|---|
| 41 | up = Vec3d( 0.,0.,1. ); |
|---|
| 42 | right = Vec3d( 1.,0.,0. ); |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | camera->getProjectionMatrixAsPerspective( fovy, aspectRatio, tmp, tmp ); |
|---|
| 49 | right *= aspectRatio; |
|---|
| 50 | |
|---|
| 51 | } else { |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | ComputeBoundsVisitor cbVisitor; |
|---|
| 57 | const_cast< Node& >( node ).accept( cbVisitor ); |
|---|
| 58 | BoundingBox &bb = cbVisitor.getBoundingBox(); |
|---|
| 59 | BoundingSphere bs( bb ); |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | eye = bs.center() + Vec3( 0., -3.0 * bs.radius(), 0. ); |
|---|
| 63 | center = bs.center(); |
|---|
| 64 | up = Vec3d( 0.,1.,0. ); |
|---|
| 65 | right = Vec3d( 4./3.,0.,0. ); |
|---|
| 66 | |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | fout << "camera { // following POV-Ray, x is right, y is up, and z is to the scene" << endl |
|---|
| 71 | << " location <" << eye[0] << ", " << eye[2] << ", " << eye[1] << ">" << endl |
|---|
| 72 | << " up <" << up[0] << ", " << up[2] << ", " << up[1] << ">" << endl |
|---|
| 73 | << " right <" << right[0] << ", " << right[2] << ", " << right[1] << ">" << endl |
|---|
| 74 | << " look_at <" << center[0] << ", " << center[2] << ", " << center[1] << ">" << endl |
|---|
| 75 | << "}" << endl |
|---|
| 76 | << endl; |
|---|
| 77 | |
|---|
| 78 | POVWriterNodeVisitor povWriter( fout, node.getBound() ); |
|---|
| 79 | if( camera ) |
|---|
| 80 | |
|---|
| 81 | for( int i=0, c=camera->getNumChildren(); i<c; i++ ) |
|---|
| 82 | const_cast< Camera* >( camera )->getChild( i )->accept( povWriter ); |
|---|
| 83 | |
|---|
| 84 | else |
|---|
| 85 | |
|---|
| 86 | const_cast< Node* >( &node )->accept( povWriter ); |
|---|
| 87 | |
|---|
| 88 | notify( NOTICE ) << "ReaderWriterPOV::writeNode() Done. (" |
|---|
| 89 | << povWriter.getNumProducedTriangles() |
|---|
| 90 | << " triangles written)" << endl; |
|---|
| 91 | |
|---|
| 92 | return osgDB::ReaderWriter::WriteResult::FILE_SAVED; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | osgDB::ReaderWriter::WriteResult |
|---|
| 97 | ReaderWriterPOV::writeNode( const Node& node, const string& fileName, |
|---|
| 98 | const osgDB::ReaderWriter::Options* options ) const |
|---|
| 99 | { |
|---|
| 100 | |
|---|
| 101 | string ext = osgDB::getLowerCaseFileExtension( fileName ); |
|---|
| 102 | if( !acceptsExtension( ext ) ) return WriteResult::FILE_NOT_HANDLED; |
|---|
| 103 | |
|---|
| 104 | notify( NOTICE ) << "ReaderWriterPOV::writeNode() Writing file " |
|---|
| 105 | << fileName.data() << endl; |
|---|
| 106 | |
|---|
| 107 | osgDB::ofstream fout( fileName.c_str(), ios::out | ios::trunc ); |
|---|
| 108 | if( !fout ) |
|---|
| 109 | return WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 110 | else |
|---|
| 111 | return writeNodeImplementation( node, fout, options ); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | osgDB::ReaderWriter::WriteResult |
|---|
| 116 | ReaderWriterPOV::writeNode( const Node& node, ostream& fout, |
|---|
| 117 | const osgDB::ReaderWriter::Options* options ) const |
|---|
| 118 | { |
|---|
| 119 | notify( osg::NOTICE ) << "ReaderWriterPOV::writeNode() Writing to " |
|---|
| 120 | << "stream" << endl; |
|---|
| 121 | |
|---|
| 122 | return writeNodeImplementation( node, fout, options ); |
|---|
| 123 | } |
|---|