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

Revision 6858, 25.4 kB (checked in by robert, 7 years ago)

Moved HelpHandler? and StatsHandler? classes into ViewerEventHandlers? header, removed HelpHandler? and StatsHandler? headers

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