root/OpenSceneGraph/trunk/src/osgPlugins/pov/ReaderWriterPOV.cpp @ 12292

Revision 12292, 3.8 kB (checked in by robert, 3 years ago)

Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

  • Property svn:eol-style set to native
Line 
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
9using namespace std;
10using namespace osg;
11
12
13// Register with Registry to instantiate the Povray writer.
14REGISTER_OSGPLUGIN( Povray, ReaderWriterPOV )
15
16
17/**
18 * Constructor.
19 * Initializes the ReaderWriterPOV.
20 */
21ReaderWriterPOV::ReaderWriterPOV()
22{
23    // Set supported extensions and options
24    supportsExtension( "pov", "POV-Ray format" );
25}
26
27
28static osgDB::ReaderWriter::WriteResult
29writeNodeImplementation( const Node& node, ostream& fout,
30                         const osgDB::ReaderWriter::Options* options )
31{
32   // get camera on the top of scene graph
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      // view matrix
40      camera->getViewMatrixAsLookAt( eye, center, up );
41      up = Vec3d( 0.,0.,1. );
42      right = Vec3d( 1.,0.,0. );
43      //up.normalize();
44      //right = (center-eye) ^ up;
45      //right.normalize();
46
47      // projection matrix
48      camera->getProjectionMatrixAsPerspective( fovy, aspectRatio, tmp, tmp );
49      right *= aspectRatio;
50
51   } else {
52
53      // get POV-Ray camera setup from scene bounding sphere
54      // constructed from bounding box
55      // (bounding box computes model center more precisely than bounding sphere)
56      ComputeBoundsVisitor cbVisitor;
57      const_cast< Node& >( node ).accept( cbVisitor );
58      BoundingBox &bb = cbVisitor.getBoundingBox();
59      BoundingSphere bs( bb );
60
61      // set
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   // camera
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
96osgDB::ReaderWriter::WriteResult
97ReaderWriterPOV::writeNode( const Node& node, const string& fileName,
98                            const osgDB::ReaderWriter::Options* options ) const
99{
100   // accept extension
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
115osgDB::ReaderWriter::WriteResult
116ReaderWriterPOV::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}
Note: See TracBrowser for help on using the browser.