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

Revision 5381, 8.4 kB (checked in by robert, 8 years ago)

Added viewer.cleanup_frame() to all examples.

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