| 1 | #include <osgProducer/Viewer> |
|---|
| 2 | |
|---|
| 3 | #include <osg/Projection> |
|---|
| 4 | #include <osg/Geometry> |
|---|
| 5 | #include <osg/Texture> |
|---|
| 6 | #include <osg/TexGen> |
|---|
| 7 | #include <osg/Geode> |
|---|
| 8 | #include <osg/ShapeDrawable> |
|---|
| 9 | #include <osg/PolygonOffset> |
|---|
| 10 | #include <osg/CullFace> |
|---|
| 11 | #include <osg/TextureCubeMap> |
|---|
| 12 | #include <osg/TexMat> |
|---|
| 13 | #include <osg/MatrixTransform> |
|---|
| 14 | #include <osg/Light> |
|---|
| 15 | #include <osg/LightSource> |
|---|
| 16 | #include <osg/PolygonOffset> |
|---|
| 17 | #include <osg/CullFace> |
|---|
| 18 | #include <osg/Material> |
|---|
| 19 | #include <osg/RefNodePath> |
|---|
| 20 | |
|---|
| 21 | #include <osgUtil/TransformCallback> |
|---|
| 22 | |
|---|
| 23 | #include <osg/CameraNode> |
|---|
| 24 | #include <osg/TexGenNode> |
|---|
| 25 | |
|---|
| 26 | using namespace osg; |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | ref_ptr<Group> _create_scene() |
|---|
| 30 | { |
|---|
| 31 | ref_ptr<Group> scene = new Group; |
|---|
| 32 | ref_ptr<Geode> geode_1 = new Geode; |
|---|
| 33 | scene->addChild(geode_1.get()); |
|---|
| 34 | |
|---|
| 35 | ref_ptr<Geode> geode_2 = new Geode; |
|---|
| 36 | ref_ptr<MatrixTransform> transform_2 = new MatrixTransform; |
|---|
| 37 | transform_2->addChild(geode_2.get()); |
|---|
| 38 | transform_2->setUpdateCallback(new osgUtil::TransformCallback(Vec3(0, 0, 0), Y_AXIS, inDegrees(45.0f))); |
|---|
| 39 | scene->addChild(transform_2.get()); |
|---|
| 40 | |
|---|
| 41 | ref_ptr<Geode> geode_3 = new Geode; |
|---|
| 42 | ref_ptr<MatrixTransform> transform_3 = new MatrixTransform; |
|---|
| 43 | transform_3->addChild(geode_3.get()); |
|---|
| 44 | transform_3->setUpdateCallback(new osgUtil::TransformCallback(Vec3(0, 0, 0), Y_AXIS, inDegrees(-22.5f))); |
|---|
| 45 | scene->addChild(transform_3.get()); |
|---|
| 46 | |
|---|
| 47 | const float radius = 0.8f; |
|---|
| 48 | const float height = 1.0f; |
|---|
| 49 | ref_ptr<TessellationHints> hints = new TessellationHints; |
|---|
| 50 | hints->setDetailRatio(2.0f); |
|---|
| 51 | ref_ptr<ShapeDrawable> shape; |
|---|
| 52 | |
|---|
| 53 | shape = new ShapeDrawable(new Box(Vec3(0.0f, -2.0f, 0.0f), 10, 0.1f, 10), hints.get()); |
|---|
| 54 | shape->setColor(Vec4(0.5f, 0.5f, 0.7f, 1.0f)); |
|---|
| 55 | geode_1->addDrawable(shape.get()); |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | shape = new ShapeDrawable(new Sphere(Vec3(-3.0f, 0.0f, 0.0f), radius), hints.get()); |
|---|
| 59 | shape->setColor(Vec4(0.6f, 0.8f, 0.8f, 1.0f)); |
|---|
| 60 | geode_2->addDrawable(shape.get()); |
|---|
| 61 | |
|---|
| 62 | shape = new ShapeDrawable(new Box(Vec3(3.0f, 0.0f, 0.0f), 2 * radius), hints.get()); |
|---|
| 63 | shape->setColor(Vec4(0.4f, 0.9f, 0.3f, 1.0f)); |
|---|
| 64 | geode_2->addDrawable(shape.get()); |
|---|
| 65 | |
|---|
| 66 | shape = new ShapeDrawable(new Cone(Vec3(0.0f, 0.0f, -3.0f), radius, height), hints.get()); |
|---|
| 67 | shape->setColor(Vec4(0.2f, 0.5f, 0.7f, 1.0f)); |
|---|
| 68 | geode_2->addDrawable(shape.get()); |
|---|
| 69 | |
|---|
| 70 | shape = new ShapeDrawable(new Cylinder(Vec3(0.0f, 0.0f, 3.0f), radius, height), hints.get()); |
|---|
| 71 | shape->setColor(Vec4(1.0f, 0.3f, 0.3f, 1.0f)); |
|---|
| 72 | geode_2->addDrawable(shape.get()); |
|---|
| 73 | |
|---|
| 74 | shape = new ShapeDrawable(new Box(Vec3(0.0f, 3.0f, 0.0f), 2, 0.1f, 2), hints.get()); |
|---|
| 75 | shape->setColor(Vec4(0.8f, 0.8f, 0.4f, 1.0f)); |
|---|
| 76 | geode_3->addDrawable(shape.get()); |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | ref_ptr<Material> matirial = new Material; |
|---|
| 80 | matirial->setColorMode(Material::DIFFUSE); |
|---|
| 81 | matirial->setAmbient(Material::FRONT_AND_BACK, Vec4(0, 0, 0, 1)); |
|---|
| 82 | matirial->setSpecular(Material::FRONT_AND_BACK, Vec4(1, 1, 1, 1)); |
|---|
| 83 | matirial->setShininess(Material::FRONT_AND_BACK, 64.0f); |
|---|
| 84 | scene->getOrCreateStateSet()->setAttributeAndModes(matirial.get(), StateAttribute::ON); |
|---|
| 85 | |
|---|
| 86 | return scene; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | osg::RefNodePath createReflector() |
|---|
| 90 | { |
|---|
| 91 | ref_ptr<Group> scene = new Group; |
|---|
| 92 | ref_ptr<Geode> geode_1 = new Geode; |
|---|
| 93 | scene->addChild(geode_1.get()); |
|---|
| 94 | |
|---|
| 95 | const float radius = 0.8f; |
|---|
| 96 | ref_ptr<TessellationHints> hints = new TessellationHints; |
|---|
| 97 | hints->setDetailRatio(2.0f); |
|---|
| 98 | ref_ptr<ShapeDrawable> shape = new ShapeDrawable(new Sphere(Vec3(0.0f, 0.0f, 0.0f), radius * 1.5f), hints.get()); |
|---|
| 99 | shape->setColor(Vec4(0.8f, 0.8f, 0.8f, 1.0f)); |
|---|
| 100 | geode_1->addDrawable(shape.get()); |
|---|
| 101 | |
|---|
| 102 | osg::RefNodePath refNodeList; |
|---|
| 103 | refNodeList.push_back(scene.get()); |
|---|
| 104 | refNodeList.push_back(geode_1.get()); |
|---|
| 105 | |
|---|
| 106 | return refNodeList; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | class UpdateCameraAndTexGenCallback : public osg::NodeCallback |
|---|
| 110 | { |
|---|
| 111 | public: |
|---|
| 112 | |
|---|
| 113 | typedef std::vector< osg::ref_ptr<osg::CameraNode> > CameraList; |
|---|
| 114 | |
|---|
| 115 | UpdateCameraAndTexGenCallback(osg::RefNodePath& reflectorNodePath, CameraList& cameraNodes): |
|---|
| 116 | _reflectorNodePath(reflectorNodePath), |
|---|
| 117 | _cameraNodes(cameraNodes) |
|---|
| 118 | { |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 122 | { |
|---|
| 123 | |
|---|
| 124 | traverse(node,nv); |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | osg::Matrixd matrix = osg::computeLocalToWorld(_reflectorNodePath); |
|---|
| 128 | osg::BoundingSphere bs = _reflectorNodePath.back()->getBound(); |
|---|
| 129 | osg::Vec3 position = bs.center() * matrix; |
|---|
| 130 | |
|---|
| 131 | typedef std::pair<osg::Vec3, osg::Vec3> ImageData; |
|---|
| 132 | const ImageData id[] = |
|---|
| 133 | { |
|---|
| 134 | ImageData( osg::Vec3( 1, 0, 0), osg::Vec3( 0, -1, 0) ), |
|---|
| 135 | ImageData( osg::Vec3(-1, 0, 0), osg::Vec3( 0, -1, 0) ), |
|---|
| 136 | ImageData( osg::Vec3( 0, 1, 0), osg::Vec3( 0, 0, 1) ), |
|---|
| 137 | ImageData( osg::Vec3( 0, -1, 0), osg::Vec3( 0, 0, -1) ), |
|---|
| 138 | ImageData( osg::Vec3( 0, 0, 1), osg::Vec3( 0, -1, 0) ), |
|---|
| 139 | ImageData( osg::Vec3( 0, 0, -1), osg::Vec3( 0, -1, 0) ) |
|---|
| 140 | }; |
|---|
| 141 | |
|---|
| 142 | for(unsigned int i=0; |
|---|
| 143 | i<6 && i<_cameraNodes.size(); |
|---|
| 144 | ++i) |
|---|
| 145 | { |
|---|
| 146 | _cameraNodes[i]->setReferenceFrame(osg::CameraNode::ABSOLUTE_RF); |
|---|
| 147 | _cameraNodes[i]->setProjectionMatrixAsFrustum(-1.0,1.0,-1.0,1.0,1.0,10000.0); |
|---|
| 148 | _cameraNodes[i]->setViewMatrixAsLookAt(position,position+id[i].first,id[i].second); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | protected: |
|---|
| 154 | |
|---|
| 155 | virtual ~UpdateCameraAndTexGenCallback() {} |
|---|
| 156 | |
|---|
| 157 | osg::RefNodePath _reflectorNodePath; |
|---|
| 158 | CameraList _cameraNodes; |
|---|
| 159 | }; |
|---|
| 160 | |
|---|
| 161 | class TexMatCullCallback : public osg::NodeCallback |
|---|
| 162 | { |
|---|
| 163 | public: |
|---|
| 164 | |
|---|
| 165 | TexMatCullCallback(osg::TexMat* texmat): |
|---|
| 166 | _texmat(texmat) |
|---|
| 167 | { |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 171 | { |
|---|
| 172 | |
|---|
| 173 | traverse(node,nv); |
|---|
| 174 | |
|---|
| 175 | osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(nv); |
|---|
| 176 | if (cv) |
|---|
| 177 | { |
|---|
| 178 | osg::Quat quat; |
|---|
| 179 | cv->getModelViewMatrix().get(quat); |
|---|
| 180 | _texmat->setMatrix(osg::Matrix::rotate(quat.inverse())); |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | protected: |
|---|
| 185 | |
|---|
| 186 | osg::ref_ptr<TexMat> _texmat; |
|---|
| 187 | }; |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | osg::Group* createShadowedScene(osg::Node* reflectedSubgraph, osg::RefNodePath reflectorNodePath, unsigned int unit, const osg::Vec4& clearColor) |
|---|
| 191 | { |
|---|
| 192 | |
|---|
| 193 | osg::Group* group = new osg::Group; |
|---|
| 194 | |
|---|
| 195 | unsigned int tex_width = 512; |
|---|
| 196 | unsigned int tex_height = 512; |
|---|
| 197 | |
|---|
| 198 | osg::TextureCubeMap* texture = new osg::TextureCubeMap; |
|---|
| 199 | texture->setTextureSize(tex_width, tex_height); |
|---|
| 200 | |
|---|
| 201 | texture->setInternalFormat(GL_RGB); |
|---|
| 202 | texture->setFilter(osg::TextureCubeMap::MIN_FILTER,osg::TextureCubeMap::LINEAR); |
|---|
| 203 | texture->setFilter(osg::TextureCubeMap::MAG_FILTER,osg::TextureCubeMap::LINEAR); |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | UpdateCameraAndTexGenCallback::CameraList cameraNodes; |
|---|
| 207 | for(unsigned int i=0; i<6; ++i) |
|---|
| 208 | { |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | osg::CameraNode* camera = new osg::CameraNode; |
|---|
| 212 | |
|---|
| 213 | camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|---|
| 214 | camera->setClearColor(clearColor); |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | camera->setViewport(0,0,tex_width,tex_height); |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | camera->setRenderOrder(osg::CameraNode::PRE_RENDER); |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | camera->setRenderTargetImplmentation(osg::CameraNode::FRAME_BUFFER_OBJECT); |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | camera->attach(osg::CameraNode::COLOR_BUFFER, texture, 0, i); |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | camera->addChild(reflectedSubgraph); |
|---|
| 230 | |
|---|
| 231 | group->addChild(camera); |
|---|
| 232 | |
|---|
| 233 | cameraNodes.push_back(camera); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | osg::TexGenNode* texgenNode = new osg::TexGenNode; |
|---|
| 238 | texgenNode->getTexGen()->setMode(osg::TexGen::REFLECTION_MAP); |
|---|
| 239 | texgenNode->setTextureUnit(unit); |
|---|
| 240 | group->addChild(texgenNode); |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | { |
|---|
| 244 | osg::Node* reflectorNode = reflectorNodePath.front().get(); |
|---|
| 245 | group->addChild(reflectorNode); |
|---|
| 246 | |
|---|
| 247 | osg::StateSet* stateset = reflectorNode->getOrCreateStateSet(); |
|---|
| 248 | stateset->setTextureAttributeAndModes(unit,texture,osg::StateAttribute::ON); |
|---|
| 249 | stateset->setTextureMode(unit,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); |
|---|
| 250 | stateset->setTextureMode(unit,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); |
|---|
| 251 | stateset->setTextureMode(unit,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); |
|---|
| 252 | stateset->setTextureMode(unit,GL_TEXTURE_GEN_Q,osg::StateAttribute::ON); |
|---|
| 253 | |
|---|
| 254 | osg::TexMat* texmat = new osg::TexMat; |
|---|
| 255 | stateset->setTextureAttributeAndModes(unit,texmat,osg::StateAttribute::ON); |
|---|
| 256 | |
|---|
| 257 | reflectorNode->setCullCallback(new TexMatCullCallback(texmat)); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | group->addChild(reflectedSubgraph); |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | group->setUpdateCallback(new UpdateCameraAndTexGenCallback(reflectorNodePath, cameraNodes)); |
|---|
| 265 | |
|---|
| 266 | return group; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | int main(int argc, char** argv) |
|---|
| 271 | { |
|---|
| 272 | |
|---|
| 273 | ArgumentParser arguments(&argc, argv); |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName() + " is the example which demonstrates using of GL_ARB_shadow extension implemented in osg::Texture class"); |
|---|
| 277 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()); |
|---|
| 278 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help", "Display this information"); |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | osgProducer::Viewer viewer(arguments); |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | viewer.getUsage(*arguments. getApplicationUsage()); |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 291 | { |
|---|
| 292 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 293 | return 1; |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | if (arguments.errors()) |
|---|
| 301 | { |
|---|
| 302 | arguments.writeErrorMessages(std::cout); |
|---|
| 303 | return 1; |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | ref_ptr<MatrixTransform> scene = new MatrixTransform; |
|---|
| 307 | scene->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(125.0),1.0,0.0,0.0)); |
|---|
| 308 | |
|---|
| 309 | ref_ptr<Group> reflectedSubgraph = _create_scene(); |
|---|
| 310 | if (!reflectedSubgraph.valid()) return 1; |
|---|
| 311 | |
|---|
| 312 | ref_ptr<Group> reflectedScene = createShadowedScene(reflectedSubgraph.get(),createReflector(),0, viewer.getClearColor()); |
|---|
| 313 | |
|---|
| 314 | scene->addChild(reflectedScene.get()); |
|---|
| 315 | |
|---|
| 316 | viewer.setSceneData(scene.get()); |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | viewer.realize(); |
|---|
| 320 | |
|---|
| 321 | while (!viewer.done()) |
|---|
| 322 | { |
|---|
| 323 | |
|---|
| 324 | viewer.sync(); |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | viewer.update(); |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | viewer.frame(); |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | viewer.sync(); |
|---|
| 336 | |
|---|
| 337 | return 0; |
|---|
| 338 | } |
|---|