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

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

Added support for using GL_UNPACK_ROW_LENGTH in conjunction with texture's + osg::Image via new RowLength?
parameter in osg::Image. To support this Image::setData(..) now has a new optional rowLength parameter which
defaults to 0, which provides the original behaviour, Image::setRowLength(int) and int Image::getRowLength() are also provided.

With the introduction of RowLength? support in osg::Image it is now possible to create a sub image where
the t size of the image are smaller than the row length, useful for when you have a large image on the CPU
and which to use a small portion of it on the GPU. However, when these sub images are created the data
within the image is no longer contiguous so data access can no longer assume that all the data is in
one block. The new method Image::isDataContiguous() enables the user to check whether the data is contiguous,
and if not one can either access the data row by row using Image::data(column,row,image) accessor, or use the
new Image::DataIterator? for stepping through each block on memory assocatied with the image.

To support the possibility of non contiguous osg::Image usage of image objects has had to be updated to
check DataContiguous? and handle the case or use access via the DataIerator? or by row by row. To achieve
this a relatively large number of files has had to be modified, in particular the texture classes and
image plugins that doing writing.

  • 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 " << fileName << endl;
105
106   osgDB::ofstream fout( fileName.c_str(), ios::out | ios::trunc );
107   if( !fout )
108      return WriteResult::ERROR_IN_WRITING_FILE;
109   else
110      return writeNodeImplementation( node, fout, options );
111}
112
113
114osgDB::ReaderWriter::WriteResult
115ReaderWriterPOV::writeNode( const Node& node, ostream& fout,
116                            const osgDB::ReaderWriter::Options* options ) const
117{
118   notify( osg::NOTICE ) << "ReaderWriterPOV::writeNode() Writing to "
119                         << "stream" << endl;
120
121   return writeNodeImplementation( node, fout, options );
122}
Note: See TracBrowser for help on using the browser.