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

Revision 4377, 8.2 kB (checked in by robert, 9 years ago)

Ported osgdistortion example across to using osg::CameraNode?.

  • 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
14#include <osgGA/TrackballManipulator>
15#include <osgGA/FlightManipulator>
16#include <osgGA/DriveManipulator>
17
18#include <osgUtil/TransformCallback>
19#include <osgUtil/SmoothingVisitor>
20
21#include <osgDB/Registry>
22#include <osgDB/ReadFile>
23
24#include <osgProducer/Viewer>
25
26using namespace osg;
27
28osg::Node* createDistortionSubgraph(osg::Node* subgraph)
29{
30    osg::Group* distortionNode = new osg::Group;
31   
32    unsigned int tex_width = 1024;
33    unsigned int tex_height = 1024;
34   
35    osg::Texture2D* texture = new osg::Texture2D;
36    texture->setTextureSize(tex_width, tex_height);
37    texture->setInternalFormat(GL_RGBA);
38    texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
39    texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
40   
41    // set up the render to texture camera.
42    {
43        osg::CameraNode* camera = new osg::CameraNode;
44
45        // just inherit the main cameras view
46        camera->setReferenceFrame(osg::Transform::RELATIVE_RF);
47        camera->setProjectionMatrix(osg::Matrixd::identity());
48        camera->setViewMatrix(osg::Matrixd::identity());
49
50        // set viewport
51        camera->setViewport(0,0,tex_width,tex_height);
52        camera->getOrCreateStateSet()->setAttribute(camera->getViewport());
53
54        // set the camera to render before the main camera.
55        camera->setRenderOrder(osg::CameraNode::PRE_RENDER);
56
57        // tell the camera to use OpenGL frame buffer object where supported.
58        camera->setRenderTargetImplmentation(osg::CameraNode::FRAME_BUFFER_OBJECT);
59
60        // attach the texture and use it as the color buffer.
61        camera->attach(osg::CameraNode::COLOR_BUFFER, texture);
62
63        // add subgraph to render
64        camera->addChild(subgraph);
65       
66        distortionNode->addChild(camera);
67   }
68   
69    // set up the render to texture camera.
70    {
71
72        // create the quad to visualize.
73        osg::Geometry* polyGeom = new osg::Geometry();
74
75        polyGeom->setSupportsDisplayList(false);
76
77        osg::Vec3 origin(0.0f,0.0f,0.0f);
78        osg::Vec3 xAxis(1.0f,0.0f,0.0f);
79        osg::Vec3 yAxis(0.0f,1.0f,0.0f);
80        osg::Vec3 zAxis(0.0f,0.0f,1.0f);
81        float height = 1024.0f;
82        float width = 1280.0f;
83        int noSteps = 50;
84
85        osg::Vec3Array* vertices = new osg::Vec3Array;
86        osg::Vec2Array* texcoords = new osg::Vec2Array;
87        osg::Vec4Array* colors = new osg::Vec4Array;
88
89        osg::Vec3 bottom = origin;
90        osg::Vec3 dx = xAxis*(width/((float)(noSteps-1)));
91        osg::Vec3 dy = yAxis*(height/((float)(noSteps-1)));
92
93        osg::Vec2 bottom_texcoord(0.0f,0.0f);
94        osg::Vec2 dx_texcoord(1.0f/(float)(noSteps-1),0.0f);
95        osg::Vec2 dy_texcoord(0.0f,1.0f/(float)(noSteps-1));
96
97        osg::Vec3 cursor = bottom;
98        osg::Vec2 texcoord = bottom_texcoord;
99        int i,j;
100        for(i=0;i<noSteps;++i)
101        {
102            osg::Vec3 cursor = bottom+dy*(float)i;
103            osg::Vec2 texcoord = bottom_texcoord+dy_texcoord*(float)i;
104            for(j=0;j<noSteps;++j)
105            {
106                vertices->push_back(cursor);
107                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));
108                colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
109
110                cursor += dx;
111                texcoord += dx_texcoord;
112            }
113        }
114
115        // pass the created vertex array to the points geometry object.
116        polyGeom->setVertexArray(vertices);
117
118        polyGeom->setColorArray(colors);
119        polyGeom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
120
121        polyGeom->setTexCoordArray(0,texcoords);
122
123
124        for(i=0;i<noSteps-1;++i)
125        {
126            osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP);
127            for(j=0;j<noSteps;++j)
128            {
129                elements->push_back(j+(i+1)*noSteps);
130                elements->push_back(j+(i)*noSteps);
131            }
132            polyGeom->addPrimitiveSet(elements);
133        }
134
135
136        // new we need to add the texture to the Drawable, we do so by creating a
137        // StateSet to contain the Texture StateAttribute.
138        osg::StateSet* stateset = polyGeom->getOrCreateStateSet();
139        stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON);
140        stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
141
142        osg::Geode* geode = new osg::Geode();
143        geode->addDrawable(polyGeom);
144
145        // set up the camera to render the textured quad
146        osg::CameraNode* camera = new osg::CameraNode;
147
148        // just inherit the main cameras view
149        camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
150        camera->setViewMatrix(osg::Matrix::identity());
151        camera->setProjectionMatrixAsOrtho2D(0,1280,0,1024);
152
153        // set the camera to render before the main camera.
154        camera->setRenderOrder(osg::CameraNode::NESTED_RENDER);
155
156        // tell the camera to use OpenGL frame buffer object where supported.
157        camera->setRenderTargetImplmentation(osg::CameraNode::FRAME_BUFFER_OBJECT);
158
159        // attach the texture and use it as the color buffer.
160        camera->attach(osg::CameraNode::COLOR_BUFFER, texture);
161
162        // add subgraph to render
163        camera->addChild(geode);
164       
165        distortionNode->addChild(camera);
166    }
167    return distortionNode;
168}
169
170
171int main( int argc, char **argv )
172{
173    // use an ArgumentParser object to manage the program arguments.
174    osg::ArgumentParser arguments(&argc,argv);
175
176    // set up the usage document, in case we need to print out how to use this program.
177    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates pre rendering of scene to a texture, and then apply this texture to geometry.");
178    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
179    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
180   
181    // construct the viewer.
182    osgProducer::Viewer viewer(arguments);
183
184    // set up the value with sensible default event handlers.
185    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
186
187    // get details on keyboard and mouse bindings used by the viewer.
188    viewer.getUsage(*arguments.getApplicationUsage());
189
190    // if user request help write it out to cout.
191    if (arguments.read("-h") || arguments.read("--help"))
192    {
193        arguments.getApplicationUsage()->write(std::cout);
194        return 1;
195    }
196
197    // any option left unread are converted into errors to write out later.
198    arguments.reportRemainingOptionsAsUnrecognized();
199
200    // report any errors if they have occured when parsing the program aguments.
201    if (arguments.errors())
202    {
203        arguments.writeErrorMessages(std::cout);
204        return 1;
205    }
206   
207    if (arguments.argc()<=1)
208    {
209        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
210        return 1;
211    }
212
213   
214    // load the nodes from the commandline arguments.
215    osg::Node* loadedModel = osgDB::readNodeFiles(arguments);
216    if (!loadedModel)
217    {
218//        write_usage(osg::notify(osg::NOTICE),argv[0]);
219        return 1;
220    }
221   
222    osg::Node* distortionNode = createDistortionSubgraph(loadedModel);
223   
224    // add model to the viewer.
225    viewer.setSceneData( distortionNode );
226
227    // create the windows and run the threads.
228    viewer.realize();
229
230    while( !viewer.done() )
231    {
232        // wait for all cull and draw threads to complete.
233        viewer.sync();
234
235        // update the scene by traversing it with the the update visitor which will
236        // call all node update callbacks and animations.
237        viewer.update();
238         
239        // fire off the cull and draw traversals of the scene.
240        viewer.frame();
241       
242    }
243   
244    // wait for all cull and draw threads to complete before exit.
245    viewer.sync();
246
247    return 0;
248}
Note: See TracBrowser for help on using the browser.