root/OpenSceneGraph/trunk/src/osgPlugins/cfg/ReaderWriterCFG.cpp @ 13041

Revision 13041, 8.4 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 2007 Cedric Pinson
2 *
3 * This application is open source and may be redistributed and/or modified
4 * freely and without restriction, both in commercial and non commercial
5 * applications, as long as this copyright notice is maintained.
6 *
7 * This application is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 *
11 * Authors:
12 *  Cedric Pinson <mornifle@plopbyte.net>
13 *
14 */
15
16
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#ifndef DATADIR
22#define DATADIR "."
23#endif // DATADIR
24
25#include <osg/GraphicsContext>
26#include "RenderSurface.h"
27#include "CameraConfig.h"
28#include <osgDB/FileNameUtils>
29#include <osgDB/FileUtils>
30#include <osgDB/Registry>
31#include <osgDB/Input>
32#include <osgDB/Output>
33#include <osgDB/ReadFile>
34#include <osgDB/WriteFile>
35#include <osg/io_utils>
36
37using namespace osgProducer;
38static osg::GraphicsContext::Traits* buildTrait(RenderSurface& rs)
39{
40    VisualChooser& vc = *rs.getVisualChooser();
41
42    osg::GraphicsContext::Traits* traits = new osg::GraphicsContext::Traits;
43
44    for (std::vector<VisualChooser::VisualAttribute>::iterator it = vc._visual_attributes.begin();
45        it != vc._visual_attributes.end();
46        it++)
47    {
48        switch(it->_attribute)
49        {
50            case(VisualChooser::UseGL):            break; // on by default in osgViewer
51            case(VisualChooser::BufferSize):       break; // no present mapping
52            case(VisualChooser::Level):            traits->level = it->_parameter; break;
53            case(VisualChooser::RGBA):             break; // automatically set in osgViewer
54            case(VisualChooser::DoubleBuffer):     traits->doubleBuffer = true; break;
55            case(VisualChooser::Stereo):           traits->quadBufferStereo = true; break;
56            case(VisualChooser::AuxBuffers):       break; // no present mapping
57            case(VisualChooser::RedSize):          traits->red = it->_parameter; break;
58            case(VisualChooser::GreenSize):        traits->green = it->_parameter; break;
59            case(VisualChooser::BlueSize):         traits->blue = it->_parameter; break;
60            case(VisualChooser::AlphaSize):        traits->alpha = it->_parameter; break;
61            case(VisualChooser::DepthSize):        traits->depth = it->_parameter; break;
62            case(VisualChooser::StencilSize):      traits->stencil = it->_parameter; break;
63            case(VisualChooser::AccumRedSize):     break; // no present mapping
64            case(VisualChooser::AccumGreenSize):   break; // no present mapping
65            case(VisualChooser::AccumBlueSize):    break; // no present mapping
66            case(VisualChooser::AccumAlphaSize):   break; // no present mapping
67            case(VisualChooser::Samples):          traits->samples = it->_parameter; break;
68            case(VisualChooser::SampleBuffers):    traits->sampleBuffers = 1; break;
69        }
70    }
71
72    OSG_INFO<<"ReaderWriterCFG buildTrait traits->depth="<<traits->depth<<std::endl;
73    OSG_INFO<<"ReaderWriterCFG buildTrait traits->samples="<<traits->samples<<std::endl;
74    OSG_INFO<<"ReaderWriterCFG buildTrait traits->sampleBuffers="<<traits->sampleBuffers<<std::endl;
75
76
77    traits->hostName = rs.getHostName();
78    traits->displayNum = rs.getDisplayNum();
79    traits->screenNum = rs.getScreenNum();
80    traits->windowName = rs.getWindowName();
81    traits->x = rs.getWindowOriginX();
82    traits->y = rs.getWindowOriginY();
83    traits->width = rs.getWindowWidth();
84    traits->height = rs.getWindowHeight();
85    traits->windowDecoration = rs.usesBorder();
86    traits->sharedContext = 0;
87    traits->pbuffer = (rs.getDrawableType()==osgProducer::RenderSurface::DrawableType_PBuffer);
88
89    traits->overrideRedirect = rs.usesOverrideRedirect();
90
91    return traits;
92 }
93
94static osgViewer::View* load(const std::string& file, const osgDB::ReaderWriter::Options* option)
95{
96    osg::ref_ptr<CameraConfig> config = new CameraConfig;
97    //std::cout << "Parse file " << file << std::endl;
98    config->parseFile(file);
99
100    RenderSurface* rs = 0;
101    Camera* cm = 0;
102    std::map<RenderSurface*,osg::ref_ptr<osg::GraphicsContext> > surfaces;
103    osg::ref_ptr<osgViewer::View> _view = new osgViewer::View;
104
105    for (int i = 0; i < (int)config->getNumberOfCameras(); i++)
106    {
107        cm = config->getCamera(i);
108        rs = cm->getRenderSurface();
109        if (rs->getDrawableType() != osgProducer::RenderSurface::DrawableType_Window)
110            continue;
111
112        osg::ref_ptr<const osg::GraphicsContext::Traits> traits;
113        osg::ref_ptr<osg::GraphicsContext> gc;
114        if (surfaces.find(rs) != surfaces.end())
115        {
116            gc = surfaces[rs];
117            traits = gc.valid() ? gc->getTraits() : 0;
118        }
119        else
120        {
121            osg::GraphicsContext::Traits* newtraits = buildTrait(*rs);
122
123#if 0
124            osg::GraphicsContext::ScreenIdentifier si;
125            si.readDISPLAY();
126
127            if (si.displayNum>=0) newtraits->displayNum = si.displayNum;
128            if (si.screenNum>=0) newtraits->screenNum = si.screenNum;
129#endif
130
131            gc = osg::GraphicsContext::createGraphicsContext(newtraits);
132
133            surfaces[rs] = gc.get();
134            traits = gc.valid() ? gc->getTraits() : 0;
135        }
136
137        // std::cout << rs->getWindowName() << " " << rs->getWindowOriginX() << " " << rs->getWindowOriginY() << " " << rs->getWindowWidth() << " " << rs->getWindowHeight() << std::endl;
138
139        if (gc.valid())
140        {
141            OSG_INFO<<"  GraphicsWindow has been created successfully."<<std::endl;
142
143            osg::ref_ptr<osg::Camera> camera = new osg::Camera;
144            camera->setGraphicsContext(gc.get());
145
146
147            int x,y;
148            unsigned int width,height;
149            cm->applyLens();
150            cm->getProjectionRectangle(x, y, width, height);
151            camera->setViewport(new osg::Viewport(x, y, width, height));
152
153            GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
154            camera->setDrawBuffer(buffer);
155            camera->setReadBuffer(buffer);
156
157            osg::Matrix projection(cm->getProjectionMatrix());
158
159            osg::Matrix offset = osg::Matrix::identity();
160            cm->setViewByMatrix(offset);
161            osg::Matrix view = osg::Matrix(cm->getPositionAndAttitudeMatrix());
162
163            // setup projection from parent
164            osg::Matrix offsetProjection = osg::Matrix::inverse(_view->getCamera()->getProjectionMatrix()) * projection;
165            _view->addSlave(camera.get(), offsetProjection, view);
166
167            #if 0
168            std::cout << "Matrix Projection " << projection << std::endl;
169            std::cout << "Matrix Projection master " << _view->getCamera()->getProjectionMatrix() << std::endl;
170            // will work only if it's a post multyply in the producer camera
171            std::cout << "Matrix View " << view << std::endl;
172            std::cout << _view->getCamera()->getProjectionMatrix() * offsetProjection << std::endl;
173            #endif
174        }
175        else
176        {
177            OSG_INFO<<"  GraphicsWindow has not been created successfully."<<std::endl;
178            return 0;
179        }
180
181    }
182
183    // std::cout << "done" << std::endl;
184    return _view.release();
185}
186
187//
188// OSG interface to read/write from/to a file.
189//
190class ReaderWriterProducerCFG : public osgDB::ReaderWriter
191{
192public:
193
194    ReaderWriterProducerCFG()
195    {
196        supportsExtension("cfg","Producer camera configuration file");
197    }
198
199    virtual const char* className() { return "Producer cfg object reader"; }
200
201
202    virtual ReadResult readObject(const std::string& fileName, const Options* options = NULL) const
203    {
204        std::string ext = osgDB::getLowerCaseFileExtension(fileName);
205        if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
206
207        osgDB::FilePathList* filePathList = 0;
208        if(options)
209        {
210            filePathList = const_cast<osgDB::FilePathList*>(&(options->getDatabasePathList()));
211            filePathList->push_back(DATADIR);
212        }
213
214        std::string path = osgDB::findDataFile(fileName);
215        if(path.empty()) return ReadResult::FILE_NOT_FOUND;
216
217        ReadResult result;
218        osg::ref_ptr<osg::View> view = load(path, options);
219        if(! view.valid())
220            result = ReadResult("Error: could not load " + path);
221        else
222            result = ReadResult(view.get());
223
224        if(options && filePathList)
225            filePathList->pop_back();
226
227        return result;
228    }
229
230};
231
232
233REGISTER_OSGPLUGIN(cfg, ReaderWriterProducerCFG)
Note: See TracBrowser for help on using the browser.