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

Revision 6264, 23.7 kB (checked in by robert, 8 years ago)

Added bool View::Slave::_useMastersSceneData to help manage HUD's and RTT
cameras that are embedded in the Viewer's View

  • 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,
287                                        osg::ArgumentParser& arguments)
288{
289    double sphere_radius = 1.0;
290    double collar_radius = 0.45;
291    osg::Vec3d center(0.0,0.0,0.0);
292    osg::Vec3d eye(0.0,0.0,0.0);
293   
294    double distance = sqrt(sphere_radius*sphere_radius - collar_radius*collar_radius);
295    osg::Vec3d projector = eye - osg::Vec3d(0.0,0.0, distance);
296   
297    osg::notify(osg::NOTICE)<<"Projector position = "<<projector<<std::endl;
298
299
300    // create the quad to visualize.
301    osg::Geometry* geometry = new osg::Geometry();
302
303    geometry->setSupportsDisplayList(false);
304
305    osg::Vec3 xAxis(widthVector);
306    float width = widthVector.length();
307    xAxis /= width;
308
309    osg::Vec3 yAxis(heightVector);
310    float height = heightVector.length();
311    yAxis /= height;
312   
313    int noSteps = 100;
314
315    osg::Vec3Array* vertices = new osg::Vec3Array;
316    osg::Vec3Array* texcoords = new osg::Vec3Array;
317    osg::Vec4Array* colors = new osg::Vec4Array;
318
319    osg::Vec3 bottom = origin;
320    osg::Vec3 dx = xAxis*(width/((float)(noSteps-1)));
321    osg::Vec3 dy = yAxis*(height/((float)(noSteps-1)));
322   
323    osg::Vec3d screenCenter = origin + widthVector*0.5f + heightVector*0.5f;
324    float screenRadius = heightVector.length() * 0.5f;
325
326    osg::Vec3 cursor = bottom;
327    int i,j;
328    for(i=0;i<noSteps;++i)
329    {
330        osg::Vec3 cursor = bottom+dy*(float)i;
331        for(j=0;j<noSteps;++j)
332        {
333            osg::Vec2 delta(cursor.x() - screenCenter.x(), cursor.y() - screenCenter.y());
334            double theta = atan2(-delta.y(), delta.x());
335            double phi = osg::PI_2 * delta.length() / screenRadius;
336            if (phi > osg::PI_2) phi = osg::PI_2;
337           
338            phi *= 2.0;
339           
340            // osg::notify(osg::NOTICE)<<"theta = "<<theta<< "phi="<<phi<<std::endl;
341           
342            osg::Vec3 texcoord(sin(phi) * cos(theta),
343                               sin(phi) * sin(theta),
344                               cos(phi));
345       
346            vertices->push_back(cursor);
347            colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
348            texcoords->push_back(texcoord);
349           
350            cursor += dx;
351        }
352        // osg::notify(osg::NOTICE)<<std::endl;
353    }
354
355    // pass the created vertex array to the points geometry object.
356    geometry->setVertexArray(vertices);
357
358    geometry->setColorArray(colors);
359    geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
360
361    geometry->setTexCoordArray(0,texcoords);
362
363    for(i=0;i<noSteps-1;++i)
364    {
365        osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP);
366        for(j=0;j<noSteps;++j)
367        {
368            elements->push_back(j+(i+1)*noSteps);
369            elements->push_back(j+(i)*noSteps);
370        }
371        geometry->addPrimitiveSet(elements);
372    }
373   
374    return geometry;
375};
376
377void setDomeCorrection(osgViewer::Viewer& viewer, osg::ArgumentParser& arguments)
378{
379 
380    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
381    if (!wsi)
382    {
383        osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
384        return;
385    }
386
387    unsigned int width, height;
388    wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
389
390    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
391    traits->x = 0;
392    traits->y = 0;
393    traits->width = width;
394    traits->height = height;
395    traits->windowDecoration = false;
396    traits->doubleBuffer = true;
397    traits->sharedContext = 0;
398
399    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
400    if (!gc)
401    {
402        osg::notify(osg::NOTICE)<<"GraphicsWindow has not been created successfully."<<std::endl;
403        return;
404    }
405
406    int tex_width = 512;
407    int tex_height = 512;
408
409    int camera_width = tex_width;
410    int camera_height = tex_height;
411
412    osg::TextureCubeMap* texture = new osg::TextureCubeMap;
413
414    texture->setTextureSize(tex_width, tex_height);
415    texture->setInternalFormat(GL_RGB);
416    texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
417    texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
418   
419#if 0   
420    osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::SEPERATE_WINDOW;
421    GLenum buffer = GL_FRONT;
422#else
423    osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::FRAME_BUFFER_OBJECT;
424    GLenum buffer = GL_FRONT;
425#endif
426
427    // front face
428    {
429        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
430        camera->setName("Front face camera");
431        camera->setGraphicsContext(gc.get());
432        camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height));
433        camera->setDrawBuffer(buffer);
434        camera->setReadBuffer(buffer);
435        camera->setAllowEventFocus(false);
436        // tell the camera to use OpenGL frame buffer object where supported.
437        camera->setRenderTargetImplementation(renderTargetImplementation);
438
439        // attach the texture and use it as the color buffer.
440        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Y);
441
442        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd());
443    }
444
445   
446    // top face
447    {
448        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
449        camera->setName("Top face camera");
450        camera->setGraphicsContext(gc.get());
451        camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height));
452        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
453        camera->setDrawBuffer(buffer);
454        camera->setReadBuffer(buffer);
455        camera->setAllowEventFocus(false);
456
457        // tell the camera to use OpenGL frame buffer object where supported.
458        camera->setRenderTargetImplementation(renderTargetImplementation);
459
460        // attach the texture and use it as the color buffer.
461        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Z);
462
463        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 1.0,0.0,0.0));
464    }
465
466    // left face
467    {
468        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
469        camera->setName("Left face camera");
470        camera->setGraphicsContext(gc.get());
471        camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height));
472        camera->setDrawBuffer(buffer);
473        camera->setReadBuffer(buffer);
474        camera->setAllowEventFocus(false);
475
476        // tell the camera to use OpenGL frame buffer object where supported.
477        camera->setRenderTargetImplementation(renderTargetImplementation);
478
479        // attach the texture and use it as the color buffer.
480        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_X);
481
482        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 0.0,1.0,0.0) * osg::Matrixd::rotate(osg::inDegrees(-90.0f), 0.0,0.0,1.0));
483    }
484
485    // right face
486    {
487        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
488        camera->setName("Right face camera");
489        camera->setGraphicsContext(gc.get());
490        camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height));
491        camera->setDrawBuffer(buffer);
492        camera->setReadBuffer(buffer);
493        camera->setAllowEventFocus(false);
494
495        // tell the camera to use OpenGL frame buffer object where supported.
496        camera->setRenderTargetImplementation(renderTargetImplementation);
497
498        // attach the texture and use it as the color buffer.
499        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_X);
500
501        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 0.0,1.0,0.0 ) * osg::Matrixd::rotate(osg::inDegrees(90.0f), 0.0,0.0,1.0));
502    }
503
504    // bottom face
505    {
506        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
507        camera->setGraphicsContext(gc.get());
508        camera->setName("Bottom face camera");
509        camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height));
510        camera->setDrawBuffer(buffer);
511        camera->setReadBuffer(buffer);
512        camera->setAllowEventFocus(false);
513
514        // tell the camera to use OpenGL frame buffer object where supported.
515        camera->setRenderTargetImplementation(renderTargetImplementation);
516
517        // attach the texture and use it as the color buffer.
518        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Z);
519
520        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 1.0,0.0,0.0) * osg::Matrixd::rotate(osg::inDegrees(180.0f), 0.0,0.0,1.0));
521    }
522
523    // back face
524    {
525        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
526        camera->setName("Back face camera");
527        camera->setGraphicsContext(gc.get());
528        camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height));
529        camera->setDrawBuffer(buffer);
530        camera->setReadBuffer(buffer);
531        camera->setAllowEventFocus(false);
532
533        // tell the camera to use OpenGL frame buffer object where supported.
534        camera->setRenderTargetImplementation(renderTargetImplementation);
535
536        // attach the texture and use it as the color buffer.
537        camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Y);
538
539        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(180.0f), 1.0,0.0,0.0));
540    }
541   
542    viewer.getCamera()->setProjectionMatrixAsPerspective(90.0f, 1.0, 1, 1000.0);
543
544
545
546    // distortion correction set up.
547    {
548        osg::Geode* geode = new osg::Geode();
549        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));
550
551        // new we need to add the texture to the mesh, we do so by creating a
552        // StateSet to contain the Texture StateAttribute.
553        osg::StateSet* stateset = geode->getOrCreateStateSet();
554        stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON);
555        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
556
557        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
558        camera->setGraphicsContext(gc.get());
559        camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
560        camera->setClearColor( osg::Vec4(0.1,0.1,1.0,1.0) );
561        camera->setViewport(new osg::Viewport(0, 0, width, height));
562        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
563        camera->setDrawBuffer(buffer);
564        camera->setReadBuffer(buffer);
565        camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
566        camera->setAllowEventFocus(false);
567        //camera->setInheritanceMask(camera->getInheritanceMask() & ~osg::CullSettings::CLEAR_COLOR & ~osg::CullSettings::COMPUTE_NEAR_FAR_MODE);
568        //camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
569       
570        camera->setProjectionMatrixAsOrtho2D(0,width,0,height);
571        camera->setViewMatrix(osg::Matrix::identity());
572
573        // add subgraph to render
574        camera->addChild(geode);
575       
576        camera->setName("DistortionCorrectionCamera");
577
578        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd(), false);
579    }
580   
581    viewer.getCamera()->setNearFarRatio(0.0001f);
582}
583
584
585int main(int argc, char** argv)
586{
587    // use an ArgumentParser object to manage the program arguments.
588    osg::ArgumentParser arguments(&argc,argv);
589
590    // construct the viewer.
591    osgViewer::Viewer viewer;
592   
593    // load the nodes from the commandline arguments.
594    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
595    if (!loadedModel)
596    {
597        osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<<std::endl;
598        return 1;
599    }
600
601    if (arguments.read("--dome"))
602    {   
603
604        setDomeCorrection(viewer, arguments);
605   
606        viewer.setSceneData( loadedModel );
607    }
608    else if (arguments.read("--faces"))
609    {   
610
611        setDomeFaces(viewer, arguments);
612   
613        viewer.setSceneData( loadedModel );
614    }
615    else
616    {
617        osg::Node* distortionNode = createDistortionSubgraph(loadedModel, viewer.getCamera()->getClearColor());
618
619        // add model to the viewer.
620        viewer.setSceneData( distortionNode );
621    }
622
623
624    // set up the camera manipulators.
625    {
626        osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
627
628        keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
629        keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
630        keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
631        keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
632
633        std::string pathfile;
634        char keyForAnimationPath = '5';
635        while (arguments.read("-p",pathfile))
636        {
637            osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
638            if (apm || !apm->valid())
639            {
640                unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
641                keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
642                keyswitchManipulator->selectMatrixManipulator(num);
643                ++keyForAnimationPath;
644            }
645        }
646
647        viewer.setCameraManipulator( keyswitchManipulator.get() );
648    }
649
650    // viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);
651
652    // add the state manipulator
653    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
654   
655    // add the stats handler
656    viewer.addEventHandler(new osgViewer::StatsHandler);
657
658    return viewer.run();
659}
660;
Note: See TracBrowser for help on using the browser.