Changeset 366 for experimental


Ignore:
Timestamp:
May 13, 2012, 6:21:34 PM (12 years ago)
Author:
Torben Dannhauer
Message:
 
Location:
experimental/distortionNG
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • experimental/distortionNG/ReaderWriterDist.cpp

    r365 r366  
    55{
    66        supportsExtension( "dist", "distortion set loader");
     7        extensionToAdd = ".dist";
    78
     9        // Configure Compression and instantiate read/write-options
     10        bool asAscii = true;
     11        bool compressionEnabled = false;
    812
    9         extensionToAdd = ".dist";
     13        std::string readOptionString = "";
     14        std::string writeOptionString = "";
     15        if(asAscii)
     16        {
     17                readOptionString = "Ascii";
     18                writeOptionString = "Ascii";
     19        }
     20        if (compressionEnabled)
     21                writeOptionString+=" Compressor=zlib";
     22        readOptions = new osgDB::Options( readOptionString.c_str() );
     23        writeOptions = new osgDB::Options( writeOptionString.c_str() );
     24
     25        // Get ReaderWriter
     26        rw = osgDB::Registry::instance()->getReaderWriterForExtension("osgt"); 
     27
    1028}
    1129
    1230ReaderWriterDist::ReadResult ReaderWriterDist::readNode( const std::string& file, const osgDB::ReaderWriter::Options* options) const
    1331{
    14         // Check if its a valid file for this pseudo loader
    1532        std::string ext = osgDB::getLowerCaseFileExtension( file );
    1633        if( !acceptsExtension(ext) )
    1734                return ReadResult::FILE_NOT_HANDLED;
    1835
    19         // Strip the pseudo-loader extension
    20     std::string realName = osgDB::getNameLessExtension( file );
    21 
    22         // check if stripped file exists
    23         std::string fileName = osgDB::findDataFile( realName, options );
     36        // check if file exists
     37        std::string fileName = osgDB::findDataFile( file, options );
    2438        if( fileName.empty() )
    2539                return ReadResult::FILE_NOT_FOUND;
    2640
    27         // If options are available: Save it'S original KdTree Hint.
    28         Options::BuildKdTreesHint originalHint = Options::NO_PREFERENCE;
    29         if(options)
     41        std::ifstream stream( fileName.c_str(), std::ios::in );
     42        if (!stream)    return ReadResult::ERROR_IN_READING_FILE;
     43        return readNode( stream, options );
     44}
     45
     46ReaderWriterDist::ReadResult ReaderWriterDist:: readNode( std::istream& fin, const osgDB::ReaderWriter::Options* options ) const
     47{
     48        if ( rw )
    3049        {
    31                 originalHint = options->getBuildKdTreesHint();
    32                 const_cast<osgDB::ReaderWriter::Options*>(options)->setBuildKdTreesHint(Options::DO_NOT_BUILD_KDTREES);
     50                osgDB::ReaderWriter::ReadResult rr = rw->readObject( fin, readOptions );
     51                if (rr.success())
     52                {
     53                        return( rr.takeObject() );
     54                }
     55                else
     56                        OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to convert stream to node" << std::endl;
    3357        }
     58        else
     59                OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl;
     60}
    3461
    35         // Load real file
    36         osg::Node *node = osgDB::readNodeFile( fileName, options );
    37     if( !node )
    38     {
    39         // propagate the read failure upwards
    40         OSG_WARN << "Subfile \"" << fileName << "\" could not be loaded" << std::endl;
    41         return ReadResult::FILE_NOT_HANDLED;
    42     }
     62ReaderWriterDist::WriteResult writeNode( const osg::Node& node, const std::string& fileName, const Options* options ) const
     63{
     64        std::string ext = osgDB::getLowerCaseFileExtension( file );
     65        if( !acceptsExtension(ext) )
     66                return WriteResult::FILE_NOT_HANDLED;
    4367
    44         return node;
     68        std::ifstream stream( fileName.c_str(), std::ios::in );
     69        if (!stream)    return ReadResult::ERROR_IN_READING_FILE;
     70        return readNode( stream, options );
     71// ------------------
     72    WriteResult result = WriteResult::FILE_SAVED;
     73    std::ios::openmode mode = std::ios::out;
     74    osg::ref_ptr<Options> local_opt = prepareWriting( result, fileName, mode, options );
     75    if ( !result.success() ) return result;
     76
     77    osgDB::ofstream fout( fileName.c_str(), mode );
     78    if ( !fout ) return WriteResult::ERROR_IN_WRITING_FILE;
     79
     80    result = writeNode( node, fout, local_opt.get() );
     81    fout.close();
     82    return result;
     83}
     84
     85ReaderWriterDist::WriteResult writeNode( const osg::Node& node, std::ostream& fout, const Options* options ) const
     86{
     87        if ( rw )
     88        {
     89                osgDB::ReaderWriter::WriteResult wr = rw->writeObject( node, fout, writeOptions );
     90                if (wr.success() )                     
     91                {
     92                        OSG_NOTIFY( osg::WARN ) << "schreiben geschafft!"<< std::endl;
     93                }
     94                else OSG_NOTIFY( osg::WARN ) << "ERROR: Save failed: " << wr.message() << std::endl;
     95                return(wr);
     96        }
     97        OSG_NOTIFY( osg::WARN ) << "ERROR: Unable to get ReaderWriter for internally used extension" << std::endl;
    4598}
    4699
  • experimental/distortionNG/ReaderWriterDist.h

    r365 r366  
    11#pragma once
     2
     3
    24
    35#include <osg/Notify>
     
    1416        ReaderWriterDist();
    1517
    16         virtual const char* className() const { return "terrain modification pseudo-loader"; };
     18        virtual const char* className() const { return "distortion set loader"; };
    1719        virtual ReadResult readNode( const std::string& file, const osgDB::ReaderWriter::Options* options) const;
     20        virtual ReadResult readNode( std::istream& fin, const osgDB::ReaderWriter::Options* options ) const;
     21    virtual WriteResult writeNode( const osg::Node& node, const std::string& fileName, const osgDB::ReaderWriter::Options* options ) const;
     22    virtual WriteResult writeNode( const osg::Node& node, std::ostream& fout, const osgDB::ReaderWriter::Options* options ) const;
     23
    1824
    1925private:
    2026        std::string extensionToAdd;
     27
     28        osg::ref_ptr<osgDB::Options> readOptions;
     29        osg::ref_ptr<osgDB::Options> writeOptions;
     30        osg::ref_ptr<osgDB::ReaderWriter> rw;
    2131};
  • experimental/distortionNG/main.cpp

    r362 r366  
    149149 * modul                                                                Funktionen / Description
    150150 *
    151  * [postponed] plugin .dist                             load / Save distContainers                                      loads and saves via serializers the distortion container from/to file.
     151 * [] plugin .dist                                              load / Save distContainers                                      loads and saves via serializers the distortion container from/to file.
     152 *                                                                                                                                                                      Ensures the Coordinates are normalized before saving the container.
     153 *                                                                              Load:
     154 *                                                                              * use serializer to load container
     155 *                                                                              * invoke osgViewer::setUpViewForManualDistortion(distSet, screenNum, projectorMatrix) to set up distortion
     156 *                                                                              Save:
     157 *                                                                              * check if container is normalized. if not, copy and normalize container
     158 *                                                                              * use serializer to save container
    152159 * 
    153160 *
    154  * [done] Distortion Container                                  Beinhaltet die folgenden Distortion Details:
     161 * [done] Distortion Container                  Der Container wird immer normalisiert gespeichert, alle Koordinaten (Mesh+TexCoords) werden im Range [0-1]angespeichert.
     162 *                                                                              Er beinhaltet die folgenden Distortion Details:
    155163 *                                                                              - Blendmap (osgImage)
    156164 *                                                                              - Mesh Dimensions (rows, columns)
    157165 *                                                                              - Mesh Type (GLenum QUAD_STRIP, ...)
     166 *                                                                              - Mesh Coordinates [0-1]
     167 *                                                                              - Texture Coordinates [0-1]
    158168 *                                                                              - textureUnit for Scene RTT (z.B.: 0)
    159169 *                                                                              - textureUnit for blendmap (z.B.: 1)
    160170 *                                                                              - screenNum
    161  *                                                                              - slaveCam projectionOffset (wird mit der vom master multipliziert und auf die slave cam angewendet, meist identity)
     171 *                                                                              - slaveCam projectionOffset (wird mit der vom master multipliziert und auf die slave cam angewendet, meist Identity)
    162172 *                                                                              - slaveCam viewOffset   (wird mit der vom master multipliziert und auf die slave cam angewendet, verwendet für translation und rotation offset)
    163173 *                                                                             
    164174 *                                                                              need to add setter/getter as well as the serializer macros.
    165175 *                                                                             
    166  *                                                                              Besitz: Da osgViewer distSets ohne Manipulationsmöglichkeit konsumieren können soll, muss der COntainer vom Vierwer verwaltet werden.
    167  *
    168  * osgViewer                                                    load and apply distortion (apply via setupDistortion(...)
    169  *                                                                              setUpIntensityMapBlending(string filepath, int rrtTexUnit=1, int scenTexUnit=0): simple function providing a file path, and optionally the texUnits to use. Wil invoke the more detailed funtion for setup.
     176 *                                                                              Besitz: Da osgViewer distSets ohne Manipulationsmöglichkeit konsumieren können soll, muss der Container vom Viewer verwaltet werden. der manipulator greift darauf zu, das plugin kann container entgegen nehmen oder zurückgeben, hat aber keinen direkten kontakt zur container instanz von osgViewer
     177 *
     178 * osgViewer                                                    im construtor (anfangs ersatzweise in der main.cpp) : -dist <distFile> => load and apply distortion [via setUpViewForManualDistortion(std::string distFile)]
     179 *                                                                              setUpIntensityMapBlending(string filepath, int rrtTexUnit=1, int scenTexUnit=0):        Simple function providing a file path, and optionally the texUnits to use.
     180 *                                                                                                                                                                                                                                                      Will invoke the more detailed funtion (below) for setup.
    170181 *                                                                              setUpIntensityMapBlending(osg::StateSet* stateset, osg::Image* intensityMap, unsigned int screenNum, int rttSceneTextureUnit, int intensityMapTextureUnit)
    171  *
     182 *                                                                              setUpViewForManualDistortion(osgViewer::DistortionSet* distSet, unsigned int screenNum, const osg::Matrixd& projectorMatrix)
     183 *                                                                              setUpViewForManualDistortion(std::string distFile, unsigned int screenNum, const osg::Matrixd& projectorMatrix) is invoked by -dist <distFile>, reads distSet via osgDB::readObjectFile(), calls setUpViewForManualDistortion(osgViewer::DistortionSet* distSet, unsigned int screenNum, const osg::Matrixd& projectorMatrix) to setup the distortion.
    172184 *
    173185 * distortionManipulator                                abgeleitet von osgGA::GUIEventHandler
    174186 *                                                                              Grundfunktionen:
    175187 *                                                      [done]          - Key to Show Distortion Mesh / Intensity Map / none
    176  *                                                                              - Key to Save distortion Container  - via plugin
     188 *                                                                              - Key to Save distortion Container  - via osgDB::writeObjectFile(const osg::Object& object, const std::string& filename, const Options* options). osgDB & registry determines and loads the approriate plugin bythe file ending (.dist).
    177189 *                                                      [.]             - Key to toggle MANUAL mode between blending und distortion setup
    178190 *                                                      [done]          - Key to toggle distortion setup:
     
    196208 * direct to do:
    197209 *
    198  * speichern des containers in ein .dist file
     210 * Plugin fertigstellen
     211 * save funktion des manipulators via osgDB::writeObjectFile fertig stellen
     212 * extViewer::setUpViewForManualDistortion(std::string distFile)
     213 * Load Funktion vom osgViewer (ersatzweise Anfangs in der main.cpp) fertigstellen : -dist <distFile> weiterreichen and extViewer::setUpViewForManualDistortion(std::string distFile)
    199214 * Laden des Containers.
    200  * reset distortion funktioniert.
     215 * reset distortion implementieren.
    201216 * Anwenden der distortion auf die Meshknoten oder die texturekoordinaten
    202217 *
    203  * - umstellung der koordinaten auf [0-1]
    204  * oder alternativ:
    205  * - speichern der hieght und width im container so daß das mesh an andere Bildschirmgrößen angepasst werden kann.
    206  
    207218 *
    208219*/
Note: See TracChangeset for help on using the changeset viewer.