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

Revision 4553, 8.3 kB (checked in by robert, 9 years ago)

Fixed typo of CameraNode::setRenderTargetImplementation(..)

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