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

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

Wired up RTT cubemap cameras and simple distortion mesh.

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