| 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 | |
|---|
| 25 | using namespace osg; |
|---|
| 26 | |
|---|
| 27 | osg::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 | |
|---|
| 41 | { |
|---|
| 42 | osg::CameraNode* camera = new osg::CameraNode; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | camera->setClearColor(clearColour); |
|---|
| 46 | camera->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | camera->setReferenceFrame(osg::Transform::RELATIVE_RF); |
|---|
| 50 | camera->setProjectionMatrix(osg::Matrixd::identity()); |
|---|
| 51 | camera->setViewMatrix(osg::Matrixd::identity()); |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | camera->setViewport(0,0,tex_width,tex_height); |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | camera->setRenderOrder(osg::CameraNode::PRE_RENDER); |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | camera->setRenderTargetImplementation(osg::CameraNode::FRAME_BUFFER_OBJECT); |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | camera->attach(osg::CameraNode::COLOR_BUFFER, texture); |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | camera->addChild(subgraph); |
|---|
| 67 | |
|---|
| 68 | distortionNode->addChild(camera); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | { |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 139 | |
|---|
| 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 | |
|---|
| 148 | osg::CameraNode* camera = new osg::CameraNode; |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
|---|
| 152 | camera->setViewMatrix(osg::Matrix::identity()); |
|---|
| 153 | camera->setProjectionMatrixAsOrtho2D(0,1280,0,1024); |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | camera->setRenderOrder(osg::CameraNode::NESTED_RENDER); |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | camera->setRenderTargetImplementation(osg::CameraNode::FRAME_BUFFER_OBJECT); |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | camera->attach(osg::CameraNode::COLOR_BUFFER, texture); |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | camera->addChild(geode); |
|---|
| 166 | |
|---|
| 167 | distortionNode->addChild(camera); |
|---|
| 168 | } |
|---|
| 169 | return distortionNode; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | int main( int argc, char **argv ) |
|---|
| 174 | { |
|---|
| 175 | |
|---|
| 176 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 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 | |
|---|
| 184 | osgProducer::Viewer viewer(arguments); |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 194 | { |
|---|
| 195 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 196 | return 1; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 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 | |
|---|
| 217 | osg::Node* loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 218 | if (!loadedModel) |
|---|
| 219 | { |
|---|
| 220 | |
|---|
| 221 | return 1; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | osg::Node* distortionNode = createDistortionSubgraph(loadedModel, viewer.getClearColor()); |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | viewer.setSceneData( distortionNode ); |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | viewer.realize(); |
|---|
| 231 | |
|---|
| 232 | while( !viewer.done() ) |
|---|
| 233 | { |
|---|
| 234 | |
|---|
| 235 | viewer.sync(); |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | viewer.update(); |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | viewer.frame(); |
|---|
| 243 | |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | viewer.sync(); |
|---|
| 248 | |
|---|
| 249 | return 0; |
|---|
| 250 | } |
|---|