source: experimental/distortionNG/main.cpp @ 383

Last change on this file since 383 was 383, checked in by Torben Dannhauer, 12 years ago
File size: 11.9 KB
RevLine 
[307]1/* osgVisual test. distortionNG, experimental.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
[310]19#include "extViewer.h"
[309]20
[307]21#include <osg/ArgumentParser>
[309]22#include <osg/PolygonOffset>
23
[307]24#include <osgDB/ReadFile>
[369]25#include <osgDB/WriteFile>      // Testweise zum dist plugin verifizieren
[307]26
27#include <osgViewer/Viewer>
28#include <osgViewer/ViewerEventHandlers>
29
30#include <osgGA/TrackballManipulator>
31#include <osgGA/FlightManipulator>
32#include <osgGA/DriveManipulator>
33#include <osgGA/KeySwitchMatrixManipulator>
34#include <osgGA/StateSetManipulator>
35#include <osgGA/AnimationPathManipulator>
36#include <osgGA/TerrainManipulator>
[352]37
38#include "DistortionManipulator.h"
[372]39#include "DistortionSetupStrategyProjectSyntropy.h"
[352]40
[307]41int main(int argc, char** argv)
42{
43    osg::ArgumentParser arguments(&argc, argv);
44
45    // construct the viewer.
[311]46    extViewer viewer(arguments);
[307]47
[342]48        osg::Image* intMap = osgDB::readImageFile("intensitymap.png");
[331]49        if (!intMap)
[353]50                osg::notify(osg::WARN) << "Couldn't find intensity map, skip intensityMap setup." << std::endl;
51
[352]52        // Create DistortionSet
53        osg::ref_ptr<osgViewer::DistortionSet> _distortionSet = new osgViewer::DistortionSet();
54        _distortionSet->setIntensityMap( intMap );
[383]55        _distortionSet->setDistortionMeshDimensions(20, 20);
56       
[369]57
58// ---- test plugin read / write
[370]59        //osgDB::writeObjectFile( *_distortionSet, "distortionset.dist" );
[369]60        _distortionSet = NULL;
[370]61        _distortionSet = dynamic_cast<osgViewer::DistortionSet*>( osgDB::readObjectFile( "distortionset.dist" ) );
[369]62// ------------ Test ende -----------
63
[352]64       
65        viewer.setUpViewForManualDistortion(_distortionSet, 0);
66   
[331]67
68        // set up the camera manipulators.
[353]69    osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
[307]70
[353]71    keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
72    keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
73    keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
74    keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
[307]75
[353]76    std::string pathfile;
77    char keyForAnimationPath = '5';
78    while (arguments.read("-p",pathfile))
79    {
80        osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
81        if (apm || !apm->valid()) 
[307]82        {
[353]83            unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
84            keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
85            keyswitchManipulator->selectMatrixManipulator(num);
86            ++keyForAnimationPath;
[307]87        }
88    }
89
[353]90        // Add keyswitch manipulator for multiple scene controls-
91    viewer.setCameraManipulator( keyswitchManipulator.get() );
92
[352]93        // Add the distortion manipulator
[380]94        osgViewer::DistortionManipulator* distortionManip = new osgViewer::DistortionManipulator(&viewer, _distortionSet);
[376]95
96        DistortionSetupStrategyProjectSyntropy* psStrategy = new DistortionSetupStrategyProjectSyntropy();
[383]97        psStrategy->setDistortionInputFiles( "./resources/ProjectSyntropy/Vertices/warpmap_2.csv",
98                                                                                "./resources/ProjectSyntropy/TexCoords/warpmap_2.csv",
[376]99                                                                                "./resources/ProjectSyntropy/blending_1.bmp",
100                                                                                "./resources/ProjectSyntropy/Vertices/frustum_1.csv",
101                                                                                DistortionSetupStrategy::VERTEXDISTORTION );
102        distortionManip->setDistortionSetupStrategy( psStrategy );
[372]103        viewer.addEventHandler(distortionManip);
[307]104
105    // add the state manipulator
106    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
[361]107       
[307]108    // add the stats handler
109    viewer.addEventHandler(new osgViewer::StatsHandler);
110
111    // add the record camera path handler
112    viewer.addEventHandler(new osgViewer::RecordCameraPathHandler);
113
114    // add the window size toggle handler
115    viewer.addEventHandler(new osgViewer::WindowSizeHandler);
116
117        // add the thread model handler
118    viewer.addEventHandler(new osgViewer::ThreadingHandler);
119
120    // add the help handler
121    viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
122
123    // add the LOD Scale handler
124    viewer.addEventHandler(new osgViewer::LODScaleHandler);
125
126    // add the screen capture handler
127    viewer.addEventHandler(new osgViewer::ScreenCaptureHandler);
128
129    // load the nodes from the commandline arguments.
130    osg::Node* rootnode = osgDB::readNodeFiles(arguments);
131
132    if (!rootnode)
133    {
[309]134                rootnode = osgDB::readNodeFile("cow.osgt");
135                if(!rootnode)
136                {
[361]137                        OSG_WARN<<"Warning: no valid data loaded, please specify a database on the command line."<<std::endl;
[309]138                        return 1;
139                }
[325]140    }
[362]141        viewer.setSceneData( rootnode );
[361]142
[309]143        // run the viewers main loop
144        return viewer.run();
[329]145}
146
147/*
148ToDo:
[340]149
150The intersection works and the selector highlighter is set and displayed correctly.
151Next steps:
152 * Hide the highlighter and disable the intersection/drag tracking on diabled edit mode.
153 * catch up dragging values and translate them in the correct coordinate frame.
154 * apply dragging values:
155 *      a) either on the texture coordinate while the grid is constant,
156 *  b) or apply them on the grid node coordinates while the texture coordinates are constant (check/compare solutions!)
[329]157 *
[340]158 
[329]159
[342]160 *
161 * Layout vom Distortion System
162 *
[350]163 * Das distortion System unterstützt Verzerrungen, die pro Kanal jeweils nur eine camera verwenden.
164 * Verzerrungen, die das Bild je Kanal aus mehreren Cams zusammensetzen um Views>=180° zu erreichen werden nicht unterstützt da hierfür ein Camera Setup nötig wäre, welches nicht im distortion-Container gespeichert wird (Sonderfall, zu speziell für das Dist-Framework)
[342]165 *
166 *
167 * modul                                                                Funktionen / Description
168 *
[369]169 * [] plugin .dist                      [done]          load / Save distContainers                                      loads and saves via serializers the distortion container from/to file.
[366]170 *                                                                                                                                                                      Ensures the Coordinates are normalized before saving the container.
171 *                                                                              Load:
[370]172 *                                                      [done]          * use ReaderWriterOSG2 to load container
[366]173 *                                                                              * invoke osgViewer::setUpViewForManualDistortion(distSet, screenNum, projectorMatrix) to set up distortion
174 *                                                                              Save:
175 *                                                                              * check if container is normalized. if not, copy and normalize container
[370]176 *                                                      [done]          * use ReaderWriterOSG2 to save container
[361]177 * 
[342]178 *
[366]179 * [done] Distortion Container                  Der Container wird immer normalisiert gespeichert, alle Koordinaten (Mesh+TexCoords) werden im Range [0-1]angespeichert.
180 *                                                                              Er beinhaltet die folgenden Distortion Details:
[342]181 *                                                                              - Blendmap (osgImage)
182 *                                                                              - Mesh Dimensions (rows, columns)
[345]183 *                                                                              - Mesh Type (GLenum QUAD_STRIP, ...)
[367]184 *                                                                              - Mesh Coordinates [0-1]
[366]185 *                                                                              - Texture Coordinates [0-1]
[342]186 *                                                                              - textureUnit for Scene RTT (z.B.: 0)
187 *                                                                              - textureUnit for blendmap (z.B.: 1)
188 *                                                                              - screenNum
[366]189 *                                                                              - slaveCam projectionOffset (wird mit der vom master multipliziert und auf die slave cam angewendet, meist Identity)
[342]190 *                                                                              - slaveCam viewOffset   (wird mit der vom master multipliziert und auf die slave cam angewendet, verwendet für translation und rotation offset)
191 *                                                                             
[374]192 *                                                      []                      Todo: get set methode für das Mesh implementieren.
[350]193 *                                                                             
[366]194 *                                                                              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
[342]195 *
[366]196 * osgViewer                                                    im construtor (anfangs ersatzweise in der main.cpp) : -dist <distFile> => load and apply distortion [via setUpViewForManualDistortion(std::string distFile)]
197 *                                                                              setUpIntensityMapBlending(string filepath, int rrtTexUnit=1, int scenTexUnit=0):        Simple function providing a file path, and optionally the texUnits to use.
198 *                                                                                                                                                                                                                                                      Will invoke the more detailed funtion (below) for setup.
[342]199 *                                                                              setUpIntensityMapBlending(osg::StateSet* stateset, osg::Image* intensityMap, unsigned int screenNum, int rttSceneTextureUnit, int intensityMapTextureUnit)
[366]200 *                                                                              setUpViewForManualDistortion(osgViewer::DistortionSet* distSet, unsigned int screenNum, const osg::Matrixd& projectorMatrix)
201 *                                                                              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.
[342]202 *
203 * distortionManipulator                                abgeleitet von osgGA::GUIEventHandler
204 *                                                                              Grundfunktionen:
[361]205 *                                                      [done]          - Key to Show Distortion Mesh / Intensity Map / none
[371]206 *                                                      [done]          - 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).
207 *                                                      [.]                     - Key to toggle MANUAL mode between blending und distortion setup
[348]208 *                                                      [done]          - Key to toggle distortion setup:
[343]209 *                                                                                              DISABLED distortion/blending modifications are forbidden (ggfs. beim Verlassen von DISABLED ggf. auf singleThreaded wechseln und beim aktivieren von DISABLED wieder auf das alte threadingmodel. Alternativ  die data variance des meshes/Blendmap beeinflussen)
210 *                                                                                              MANUAL distortion/blending modification via mouse Selection and drag'n'drop
211 *                                                                                              DELEGATED distortion/blending modification by calling a foreign class or calling a subclassed function
[348]212 *                                                      [done]          - Key to control if mouse drags affects mesh coordinates or rttScene texture coordinates. Default: Mesh coordinates.
[342]213 *                                                                              - Key to reset Distortion.
[349]214 *                                                      [done]          - Key to reset Blending.
[356]215 *                                                      [done]          - Key to Show/Hide distortion HUD
[342]216 *                                                                             
217 *                                                                              Die Funktion  handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa ):
[356]218 *                                                      [done]                  DISABLED: ignorieren
219 *                                                      [.]                             MANUAL : auf mouse selection und drags horchen und auf Distortion Anwenden. bei keys die obigen funktionen realisieren
220 *                                                      [.]                             DELEGATED : beim typ FRAME die Distortion zur Veränderung stellen indem der Pointer an die bearbeitende Funktion übergeben wird:
[342]221 *                                                                                                                              subsclassed function aufrufen bzw foreignClass->delegateDistortionSetup(distContainer& container)
222 *                                                                                     
[357]223 *                                                      [done]                  Vertex Highlighter für die manuelle Verzerrung soll nur eingeblendet werden wenn der Betriebsmodus MANUAL ist.
[343]224 *                                                                     
[342]225 *
[360]226 * direct to do:
[342]227 *
[366]228 * Plugin fertigstellen
229 * extViewer::setUpViewForManualDistortion(std::string distFile)
230 * Load Funktion vom osgViewer (ersatzweise Anfangs in der main.cpp) fertigstellen : -dist <distFile> weiterreichen and extViewer::setUpViewForManualDistortion(std::string distFile)
[360]231 * Laden des Containers.
[366]232 * reset distortion implementieren.
[360]233 * Anwenden der distortion auf die Meshknoten oder die texturekoordinaten
234 *
235 *
[329]236*/
Note: See TracBrowser for help on using the repository browser.