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

Revision 6301, 25.2 kB (checked in by robert, 8 years ago)

Added proper maths for distortion correction of pufferfish dome.

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