root/OpenSceneGraph/trunk/examples/osgdistortion/osgdistortion.cpp @ 6260

Revision 6260, 22.8 kB (checked in by robert, 8 years ago)

Moved distortion mesh creation out into seperate function

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/GLExtensions>
2#include <osg/Node>
3#include <osg/Geometry>
4#include <osg/Notify>
5#include <osg/MatrixTransform>
6#include <osg/Texture2D>
7#include <osg/Stencil>
8#include <osg/ColorMask>
9#include <osg/Depth>
10#include <osg/Billboard>
11#include <osg/Material>
12#include <osg/Projection>
13#include <osg/TextureCubeMap>
14#include <osg/io_utils>
15
16
17#include <osgGA/TrackballManipulator>
18#include <osgGA/FlightManipulator>
19#include <osgGA/DriveManipulator>
20#include <osgGA/KeySwitchMatrixManipulator>
21#include <osgGA/StateSetManipulator>
22#include <osgGA/AnimationPathManipulator>
23#include <osgGA/TerrainManipulator>
24
25#include <osgUtil/SmoothingVisitor>
26
27#include <osgDB/Registry>
28#include <osgDB/ReadFile>
29
30#include <osgViewer/Viewer>
31#include <osgViewer/StatsHandler>
32#include <osgViewer/HelpHandler>
33
34using namespace osg;
35
36osg::Node* createDistortionSubgraph(osg::Node* subgraph, const osg::Vec4& clearColour)
37{
38    osg::Group* distortionNode = new osg::Group;
39   
40    unsigned int tex_width = 1024;
41    unsigned int tex_height = 1024;
42   
43    osg::Texture2D* texture = new osg::Texture2D;
44    texture->setTextureSize(tex_width, tex_height);
45    texture->setInternalFormat(GL_RGBA);
46    texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
47    texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
48   
49    // set up the render to texture camera.
50    {
51        osg::Camera* camera = new osg::Camera;
52
53        // set clear the color and depth buffer
54        camera->setClearColor(clearColour);
55        camera->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
56
57        // just inherit the main cameras view
58        camera->setReferenceFrame(osg::Transform::RELATIVE_RF);
59        camera->setProjectionMatrix(osg::Matrixd::identity());
60        camera->setViewMatrix(osg::Matrixd::identity());
61
62        // set viewport
63        camera->setViewport(0,0,tex_width,tex_height);
64
65        // set the camera to render before the main camera.
66        camera->setRenderOrder(osg::Camera::PRE_RENDER);
67
68        // tell the camera to use OpenGL frame buffer object where supported.
69        camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
70
71        // attach the texture and use it as the color buffer.
72        camera->attach(osg::Camera::COLOR_BUFFER, texture);
73
74        // add subgraph to render
75        camera->addChild(subgraph);
76       
77        distortionNode->addChild(camera);
78   }
79   
80    // set up the hud camera
81    {
82        // create the quad to visualize.
83        osg::Geometry* polyGeom = new osg::Geometry();
84
85        polyGeom->setSupportsDisplayList(false);
86
87        osg::Vec3 origin(0.0f,0.0f,0.0f);
88        osg::Vec3 xAxis(1.0f,0.0f,0.0f);
89        osg::Vec3 yAxis(0.0f,1.0f,0.0f);
90        float height = 1024.0f;
91        float width = 1280.0f;
92        int noSteps = 50;
93
94        osg::Vec3Array* vertices = new osg::Vec3Array;
95        osg::Vec2Array* texcoords = new osg::Vec2Array;
96        osg::Vec4Array* colors = new osg::Vec4Array;
97
98        osg::Vec3 bottom = origin;
99        osg::Vec3 dx = xAxis*(width/((float)(noSteps-1)));
100        osg::Vec3 dy = yAxis*(height/((float)(noSteps-1)));
101
102        osg::Vec2 bottom_texcoord(0.0f,0.0f);
103        osg::Vec2 dx_texcoord(1.0f/(float)(noSteps-1),0.0f);
104        osg::Vec2 dy_texcoord(0.0f,1.0f/(float)(noSteps-1));
105
106        osg::Vec3 cursor = bottom;
107        osg::Vec2 texcoord = bottom_texcoord;
108        int i,j;
109        for(i=0;i<noSteps;++i)
110        {
111            osg::Vec3 cursor = bottom+dy*(float)i;
112            osg::Vec2 texcoord = bottom_texcoord+dy_texcoord*(float)i;
113            for(j=0;j<noSteps;++j)
114            {
115                vertices->push_back(cursor);
116                texcoords->push_back(osg::Vec2((sin(texcoord.x()*osg::PI-osg::PI*0.5)+1.0f)*0.5f,(sin(texcoord.y()*osg::PI-osg::PI*0.5)+1.0f)*0.5f));
117                colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
118
119                cursor += dx;
120                texcoord += dx_texcoord;
121            }
122        }
123
124        // pass the created vertex array to the points geometry object.
125        polyGeom->setVertexArray(vertices);
126
127        polyGeom->setColorArray(colors);
128        polyGeom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
129
130        polyGeom->setTexCoordArray(0,texcoords);
131
132
133        for(i=0;i<noSteps-1;++i)
134        {
135            osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP);
136            for(j=0;j<noSteps;++j)
137            {
138                elements->push_back(j+(i+1)*noSteps);
139                elements->push_back(j+(i)*noSteps);
140            }
141            polyGeom->addPrimitiveSet(elements);
142        }
143
144
145        // new we need to add the texture to the Drawable, we do so by creating a
146        // StateSet to contain the Texture StateAttribute.
147        osg::StateSet* stateset = polyGeom->getOrCreateStateSet();
148        stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON);
149        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
150
151        osg::Geode* geode = new osg::Geode();
152        geode->addDrawable(polyGeom);
153
154        // set up the camera to render the textured quad
155        osg::Camera* camera = new osg::Camera;
156
157        // just inherit the main cameras view
158        camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
159        camera->setViewMatrix(osg::Matrix::identity());
160        camera->setProjectionMatrixAsOrtho2D(0,1280,0,1024);
161
162        // set the camera to render before the main camera.
163        camera->setRenderOrder(osg::Camera::NESTED_RENDER);
164
165        // add subgraph to render
166        camera->addChild(geode);
167       
168        distortionNode->addChild(camera);
169    }
170    return distortionNode;
171}
172
173void setDomeFaces(osgViewer::Viewer& viewer, osg::ArgumentParser& /*arguments*/)
174{
175 
176    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
177    if (!wsi)
178    {
179        osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
180        return;
181    }
182
183    unsigned int width, height;
184    wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
185
186    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
187    traits->x = 0;
188    traits->y = 0;
189    traits->width = width;
190    traits->height = height;
191    traits->windowDecoration = true;
192    traits->doubleBuffer = true;
193    traits->sharedContext = 0;
194
195    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
196    if (!gc)
197    {
198        osg::notify(osg::NOTICE)<<"GraphicsWindow has not been created successfully."<<std::endl;
199        return;
200    }
201
202
203    int center_x = width/2;
204    int center_y = height/2;
205    int camera_width = 256;
206    int camera_height = 256;
207
208    // front face
209    {
210        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
211        camera->setGraphicsContext(gc.get());
212        camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y, camera_width, camera_height));
213
214        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
215        camera->setDrawBuffer(buffer);
216        camera->setReadBuffer(buffer);
217
218        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd());
219    }
220   
221    // top face
222    {
223        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
224        camera->setGraphicsContext(gc.get());
225        camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y+camera_height, camera_width, camera_height));
226        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
227        camera->setDrawBuffer(buffer);
228        camera->setReadBuffer(buffer);
229
230        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 1.0,0.0,0.0));
231    }
232
233    // left face
234    {
235        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
236        camera->setGraphicsContext(gc.get());
237        camera->setViewport(new osg::Viewport(center_x-camera_width*3/2, center_y, camera_width, camera_height));
238        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
239        camera->setDrawBuffer(buffer);
240        camera->setReadBuffer(buffer);
241
242        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 0.0,1.0,0.0));
243    }
244
245    // right face
246    {
247        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
248        camera->setGraphicsContext(gc.get());
249        camera->setViewport(new osg::Viewport(center_x+camera_width/2, center_y, camera_width, camera_height));
250        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
251        camera->setDrawBuffer(buffer);
252        camera->setReadBuffer(buffer);
253
254        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 0.0,1.0,0.0));
255    }
256
257    // bottom face
258    {
259        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
260        camera->setGraphicsContext(gc.get());
261        camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y-camera_height, camera_width, camera_height));
262        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
263        camera->setDrawBuffer(buffer);
264        camera->setReadBuffer(buffer);
265
266        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 1.0,0.0,0.0));
267    }
268
269    // back face
270    {
271        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
272        camera->setGraphicsContext(gc.get());
273        camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y-2*camera_height, camera_width, camera_height));
274        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
275        camera->setDrawBuffer(buffer);
276        camera->setReadBuffer(buffer);
277
278        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-180.0f), 1.0,0.0,0.0));
279    }
280   
281    viewer.getCamera()->setProjectionMatrixAsPerspective(90.0f, 1.0, 1, 1000.0);
282
283    viewer.assignSceneDataToCameras();
284}
285
286osg::Geometry* createDomeDistortionMesh(const osg::Vec3& origin, const osg::Vec3& widthVector, const osg::Vec3& heightVector, osg::ArgumentParser& arguments)
287{
288    // create the quad to visualize.
289    osg::Geometry* geometry = new osg::Geometry();
290
291    geometry->setSupportsDisplayList(false);
292
293    osg::Vec3 xAxis(widthVector);
294    float width = widthVector.length();
295    xAxis /= width;
296
297    osg::Vec3 yAxis(heightVector);
298    float height = heightVector.length();
299    yAxis /= height;
300   
301    int noSteps = 50;
302
303    osg::Vec3Array* vertices = new osg::Vec3Array;
304    osg::Vec3Array* texcoords = new osg::Vec3Array;
305    osg::Vec4Array* colors = new osg::Vec4Array;
306
307    osg::Vec3 bottom = origin;
308    osg::Vec3 dx = xAxis*(width/((float)(noSteps-1)));
309    osg::Vec3 dy = yAxis*(height/((float)(noSteps-1)));
310
311    osg::Vec3 bottom_texcoord(0.0f,0.0f,0.0f);
312    osg::Vec3 dx_texcoord(1.0f/(float)(noSteps-1),0.0f,0.0f);
313    osg::Vec3 dy_texcoord(0.0f,1.0f/(float)(noSteps-1),0.0f);
314
315    osg::Vec3 cursor = bottom;
316    osg::Vec3 texcoord = bottom_texcoord;
317    int i,j;
318    for(i=0;i<noSteps;++i)
319    {
320        osg::Vec3 cursor = bottom+dy*(float)i;
321        osg::Vec3 texcoord = bottom_texcoord+dy_texcoord*(float)i;
322        for(j=0;j<noSteps;++j)
323        {
324            vertices->push_back(cursor);
325            texcoords->push_back(osg::Vec3((sin(texcoord.x()*osg::PI-osg::PI*0.5)+1.0f)*0.5f,(1.0-sin(texcoord.y()*osg::PI-osg::PI*0.5))*0.5f,1.0));
326            colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
327
328            cursor += dx;
329            texcoord += dx_texcoord;
330        }
331    }
332
333    // pass the created vertex array to the points geometry object.
334    geometry->setVertexArray(vertices);
335
336    geometry->setColorArray(colors);
337    geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
338
339    geometry->setTexCoordArray(0,texcoords);
340
341    for(i=0;i<noSteps-1;++i)
342    {
343        osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP);
344        for(j=0;j<noSteps;++j)
345        {
346            elements->push_back(j+(i+1)*noSteps);
347            elements->push_back(j+(i)*noSteps);
348        }
349        geometry->addPrimitiveSet(elements);
350    }
351   
352    return geometry;
353};
354
355void setDomeCorrection(osgViewer::Viewer& viewer, osg::ArgumentParser& arguments)
356{
357 
358    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
359    if (!wsi)
360    {
361        osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
362        return;
363    }
364
365    unsigned int width, height;
366    wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
367
368    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
369    traits->x = 0;
370    traits->y = 0;
371    traits->width = width;
372    traits->height = height;
373    traits->windowDecoration = false;
374    traits->doubleBuffer = true;
375    traits->sharedContext = 0;
376
377    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
378    if (!gc)
379    {
380        osg::notify(osg::NOTICE)<<"GraphicsWindow has not been created successfully."<<std::endl;
381        return;
382    }
383
384    int tex_width = 512;
385    int tex_height = 512;
386
387    int camera_width = tex_width;
388    int camera_height = tex_height;
389
390    osg::TextureCubeMap* texture = new osg::TextureCubeMap;
391
392    texture->setTextureSize(tex_width, tex_height);
393    texture->setInternalFormat(GL_RGB);
394    texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
395    texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
396   
397#if 0   
398    osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::SEPERATE_WINDOW;
399    GLenum buffer = GL_FRONT;
400#else
401    osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::FRAME_BUFFER_OBJECT;
402    GLenum buffer = GL_FRONT;
403#endif
404
405    // front face
406    {
407        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
408        camera->setName("Front face camera");
409        camera->setGraphicsContext(gc.get());
410        camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height));
411        camera->setDrawBuffer(buffer);
412        camera->setReadBuffer(buffer);
413        camera->setAllowEventFocus(false);
414        // tell the camera to use OpenGL frame buffer object where supported.
415        camera->setRenderTargetImplementation(renderTargetImplementation);
416
417        // attach the texture and use it as the color buffer.
418        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Z);
419
420        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd());
421    }
422
423   
424    // top face
425    {
426        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
427        camera->setName("Top face camera");
428        camera->setGraphicsContext(gc.get());
429        camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height));
430        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
431        camera->setDrawBuffer(buffer);
432        camera->setReadBuffer(buffer);
433        camera->setAllowEventFocus(false);
434
435        // tell the camera to use OpenGL frame buffer object where supported.
436        camera->setRenderTargetImplementation(renderTargetImplementation);
437
438        // attach the texture and use it as the color buffer.
439#ifdef CUBE_MAP   
440        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Y);
441#else
442        camera->attach(osg::Camera::COLOR_BUFFER, texture);
443#endif
444
445        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 1.0,0.0,0.0));
446    }
447
448    // left face
449    {
450        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
451        camera->setName("Left face camera");
452        camera->setGraphicsContext(gc.get());
453        camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height));
454        camera->setDrawBuffer(buffer);
455        camera->setReadBuffer(buffer);
456        camera->setAllowEventFocus(false);
457
458        // tell the camera to use OpenGL frame buffer object where supported.
459        camera->setRenderTargetImplementation(renderTargetImplementation);
460
461        // attach the texture and use it as the color buffer.
462        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_X);
463
464        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 0.0,1.0,0.0));
465    }
466
467    // right face
468    {
469        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
470        camera->setName("Right face camera");
471        camera->setGraphicsContext(gc.get());
472        camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height));
473        camera->setDrawBuffer(buffer);
474        camera->setReadBuffer(buffer);
475        camera->setAllowEventFocus(false);
476
477        // tell the camera to use OpenGL frame buffer object where supported.
478        camera->setRenderTargetImplementation(renderTargetImplementation);
479
480        // attach the texture and use it as the color buffer.
481        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_X);
482
483        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 0.0,1.0,0.0));
484    }
485
486    // bottom face
487    {
488        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
489        camera->setGraphicsContext(gc.get());
490        camera->setName("Bottom face camera");
491        camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height));
492        camera->setDrawBuffer(buffer);
493        camera->setReadBuffer(buffer);
494        camera->setAllowEventFocus(false);
495
496        // tell the camera to use OpenGL frame buffer object where supported.
497        camera->setRenderTargetImplementation(renderTargetImplementation);
498
499        // attach the texture and use it as the color buffer.
500        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Y);
501
502        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 1.0,0.0,0.0));
503    }
504
505    // back face
506    {
507        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
508        camera->setName("Back face camera");
509        camera->setGraphicsContext(gc.get());
510        camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height));
511        camera->setDrawBuffer(buffer);
512        camera->setReadBuffer(buffer);
513        camera->setAllowEventFocus(false);
514
515        // tell the camera to use OpenGL frame buffer object where supported.
516        camera->setRenderTargetImplementation(renderTargetImplementation);
517
518        // attach the texture and use it as the color buffer.
519        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Z);
520
521        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-180.0f), 1.0,0.0,0.0));
522    }
523   
524    viewer.getCamera()->setProjectionMatrixAsPerspective(90.0f, 1.0, 1, 1000.0);
525
526
527    viewer.assignSceneDataToCameras();
528
529
530    // distortion correction set up.
531    {
532        osg::Geode* geode = new osg::Geode();
533        geode->addDrawable(createDomeDistortionMesh(osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(width,0.0f,0.0f), osg::Vec3(0.0f,height,0.0f), arguments));
534
535        // new we need to add the texture to the mesh, we do so by creating a
536        // StateSet to contain the Texture StateAttribute.
537        osg::StateSet* stateset = geode->getOrCreateStateSet();
538        stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON);
539        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
540
541        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
542        camera->setGraphicsContext(gc.get());
543        camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
544        camera->setClearColor( osg::Vec4(0.1,0.1,1.0,1.0) );
545        camera->setViewport(new osg::Viewport(0, 0, width, height));
546        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
547        camera->setDrawBuffer(buffer);
548        camera->setReadBuffer(buffer);
549        camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
550        camera->setAllowEventFocus(false);
551        //camera->setInheritanceMask(camera->getInheritanceMask() & ~osg::CullSettings::CLEAR_COLOR & ~osg::CullSettings::COMPUTE_NEAR_FAR_MODE);
552        //camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
553       
554        camera->setProjectionMatrixAsOrtho2D(0,width,0,height);
555        camera->setViewMatrix(osg::Matrix::identity());
556
557        // add subgraph to render
558        camera->addChild(geode);
559       
560        camera->setName("DistortionCorrectionCamera");
561
562        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd());
563    }
564}
565
566
567int main(int argc, char** argv)
568{
569    // use an ArgumentParser object to manage the program arguments.
570    osg::ArgumentParser arguments(&argc,argv);
571
572    // construct the viewer.
573    osgViewer::Viewer viewer;
574   
575    // load the nodes from the commandline arguments.
576    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
577    if (!loadedModel)
578    {
579        osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<<std::endl;
580        return 1;
581    }
582
583    if (arguments.read("--dome"))
584    {   
585        viewer.setSceneData( loadedModel );
586
587        setDomeCorrection(viewer, arguments);
588   
589    }
590    else if (arguments.read("--faces"))
591    {   
592        viewer.setSceneData( loadedModel );
593
594        setDomeFaces(viewer, arguments);
595   
596    }
597    else
598    {
599        osg::Node* distortionNode = createDistortionSubgraph(loadedModel, viewer.getCamera()->getClearColor());
600
601        // add model to the viewer.
602        viewer.setSceneData( distortionNode );
603    }
604
605    // set up the camera manipulators.
606    {
607        osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
608
609        keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
610        keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
611        keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
612        keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
613
614        std::string pathfile;
615        char keyForAnimationPath = '5';
616        while (arguments.read("-p",pathfile))
617        {
618            osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
619            if (apm || !apm->valid())
620            {
621                unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
622                keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
623                keyswitchManipulator->selectMatrixManipulator(num);
624                ++keyForAnimationPath;
625            }
626        }
627
628        viewer.setCameraManipulator( keyswitchManipulator.get() );
629    }
630
631    // viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
632
633    // add the state manipulator
634    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
635   
636    // add the stats handler
637    viewer.addEventHandler(new osgViewer::StatsHandler);
638
639    return viewer.run();
640}
641;
Note: See TracBrowser for help on using the browser.