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

Revision 7098, 27.0 kB (checked in by robert, 7 years ago)

Added support for --sky-light and headlight with local position at 0,0,0

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