| 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 | #include <osg/TextureCubeMap> |
|---|
| 14 | #include <osg/io_utils> |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | #include <osgGA/TrackballManipulator> |
|---|
| 18 | #include <osgGA/FlightManipulator> |
|---|
| 19 | #include <osgGA/DriveManipulator> |
|---|
| 20 | #include <osgGA/KeySwitchMatrixManipulator> |
|---|
| 21 | #include <osgGA/StateSetManipulator> |
|---|
| 22 | #include <osgGA/AnimationPathManipulator> |
|---|
| 23 | #include <osgGA/TerrainManipulator> |
|---|
| 24 | |
|---|
| 25 | #include <osgUtil/SmoothingVisitor> |
|---|
| 26 | |
|---|
| 27 | #include <osgDB/Registry> |
|---|
| 28 | #include <osgDB/ReadFile> |
|---|
| 29 | |
|---|
| 30 | #include <osgViewer/Viewer> |
|---|
| 31 | #include <osgViewer/StatsHandler> |
|---|
| 32 | #include <osgViewer/HelpHandler> |
|---|
| 33 | |
|---|
| 34 | using namespace osg; |
|---|
| 35 | |
|---|
| 36 | osg::Node* createDistortionSubgraph(osg::Node* subgraph, const osg::Vec4& clearColour) |
|---|
| 37 | { |
|---|
| 38 | osg::Group* distortionNode = new osg::Group; |
|---|
| 39 | |
|---|
| 40 | unsigned int tex_width = 1024; |
|---|
| 41 | unsigned int tex_height = 1024; |
|---|
| 42 | |
|---|
| 43 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 44 | texture->setTextureSize(tex_width, tex_height); |
|---|
| 45 | texture->setInternalFormat(GL_RGBA); |
|---|
| 46 | texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); |
|---|
| 47 | texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | { |
|---|
| 51 | osg::Camera* camera = new osg::Camera; |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | camera->setClearColor(clearColour); |
|---|
| 55 | camera->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | camera->setReferenceFrame(osg::Transform::RELATIVE_RF); |
|---|
| 59 | camera->setProjectionMatrix(osg::Matrixd::identity()); |
|---|
| 60 | camera->setViewMatrix(osg::Matrixd::identity()); |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | camera->setViewport(0,0,tex_width,tex_height); |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | camera->setRenderOrder(osg::Camera::PRE_RENDER); |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | camera->attach(osg::Camera::COLOR_BUFFER, texture); |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | camera->addChild(subgraph); |
|---|
| 76 | |
|---|
| 77 | distortionNode->addChild(camera); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | { |
|---|
| 82 | |
|---|
| 83 | osg::Geometry* polyGeom = new osg::Geometry(); |
|---|
| 84 | |
|---|
| 85 | polyGeom->setSupportsDisplayList(false); |
|---|
| 86 | |
|---|
| 87 | osg::Vec3 origin(0.0f,0.0f,0.0f); |
|---|
| 88 | osg::Vec3 xAxis(1.0f,0.0f,0.0f); |
|---|
| 89 | osg::Vec3 yAxis(0.0f,1.0f,0.0f); |
|---|
| 90 | float height = 1024.0f; |
|---|
| 91 | float width = 1280.0f; |
|---|
| 92 | int noSteps = 50; |
|---|
| 93 | |
|---|
| 94 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 95 | osg::Vec2Array* texcoords = new osg::Vec2Array; |
|---|
| 96 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 97 | |
|---|
| 98 | osg::Vec3 bottom = origin; |
|---|
| 99 | osg::Vec3 dx = xAxis*(width/((float)(noSteps-1))); |
|---|
| 100 | osg::Vec3 dy = yAxis*(height/((float)(noSteps-1))); |
|---|
| 101 | |
|---|
| 102 | osg::Vec2 bottom_texcoord(0.0f,0.0f); |
|---|
| 103 | osg::Vec2 dx_texcoord(1.0f/(float)(noSteps-1),0.0f); |
|---|
| 104 | osg::Vec2 dy_texcoord(0.0f,1.0f/(float)(noSteps-1)); |
|---|
| 105 | |
|---|
| 106 | osg::Vec3 cursor = bottom; |
|---|
| 107 | osg::Vec2 texcoord = bottom_texcoord; |
|---|
| 108 | int i,j; |
|---|
| 109 | for(i=0;i<noSteps;++i) |
|---|
| 110 | { |
|---|
| 111 | osg::Vec3 cursor = bottom+dy*(float)i; |
|---|
| 112 | osg::Vec2 texcoord = bottom_texcoord+dy_texcoord*(float)i; |
|---|
| 113 | for(j=0;j<noSteps;++j) |
|---|
| 114 | { |
|---|
| 115 | vertices->push_back(cursor); |
|---|
| 116 | 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)); |
|---|
| 117 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 118 | |
|---|
| 119 | cursor += dx; |
|---|
| 120 | texcoord += dx_texcoord; |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | polyGeom->setVertexArray(vertices); |
|---|
| 126 | |
|---|
| 127 | polyGeom->setColorArray(colors); |
|---|
| 128 | polyGeom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 129 | |
|---|
| 130 | polyGeom->setTexCoordArray(0,texcoords); |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | for(i=0;i<noSteps-1;++i) |
|---|
| 134 | { |
|---|
| 135 | osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP); |
|---|
| 136 | for(j=0;j<noSteps;++j) |
|---|
| 137 | { |
|---|
| 138 | elements->push_back(j+(i+1)*noSteps); |
|---|
| 139 | elements->push_back(j+(i)*noSteps); |
|---|
| 140 | } |
|---|
| 141 | polyGeom->addPrimitiveSet(elements); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | osg::StateSet* stateset = polyGeom->getOrCreateStateSet(); |
|---|
| 148 | stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON); |
|---|
| 149 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 150 | |
|---|
| 151 | osg::Geode* geode = new osg::Geode(); |
|---|
| 152 | geode->addDrawable(polyGeom); |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | osg::Camera* camera = new osg::Camera; |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
|---|
| 159 | camera->setViewMatrix(osg::Matrix::identity()); |
|---|
| 160 | camera->setProjectionMatrixAsOrtho2D(0,1280,0,1024); |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | camera->setRenderOrder(osg::Camera::NESTED_RENDER); |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | camera->addChild(geode); |
|---|
| 167 | |
|---|
| 168 | distortionNode->addChild(camera); |
|---|
| 169 | } |
|---|
| 170 | return distortionNode; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | void setDomeFaces(osgViewer::Viewer& viewer, osg::ArgumentParser& ) |
|---|
| 174 | { |
|---|
| 175 | |
|---|
| 176 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
|---|
| 177 | if (!wsi) |
|---|
| 178 | { |
|---|
| 179 | osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl; |
|---|
| 180 | return; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | unsigned int width, height; |
|---|
| 184 | wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height); |
|---|
| 185 | |
|---|
| 186 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| 187 | traits->x = 0; |
|---|
| 188 | traits->y = 0; |
|---|
| 189 | traits->width = width; |
|---|
| 190 | traits->height = height; |
|---|
| 191 | traits->windowDecoration = true; |
|---|
| 192 | traits->doubleBuffer = true; |
|---|
| 193 | traits->sharedContext = 0; |
|---|
| 194 | |
|---|
| 195 | osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 196 | if (!gc) |
|---|
| 197 | { |
|---|
| 198 | osg::notify(osg::NOTICE)<<"GraphicsWindow has not been created successfully."<<std::endl; |
|---|
| 199 | return; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | int center_x = width/2; |
|---|
| 204 | int center_y = height/2; |
|---|
| 205 | int camera_width = 256; |
|---|
| 206 | int camera_height = 256; |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | { |
|---|
| 210 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 211 | camera->setGraphicsContext(gc.get()); |
|---|
| 212 | camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y, camera_width, camera_height)); |
|---|
| 213 | |
|---|
| 214 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 215 | camera->setDrawBuffer(buffer); |
|---|
| 216 | camera->setReadBuffer(buffer); |
|---|
| 217 | |
|---|
| 218 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd()); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | { |
|---|
| 223 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 224 | camera->setGraphicsContext(gc.get()); |
|---|
| 225 | camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y+camera_height, camera_width, camera_height)); |
|---|
| 226 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 227 | camera->setDrawBuffer(buffer); |
|---|
| 228 | camera->setReadBuffer(buffer); |
|---|
| 229 | |
|---|
| 230 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 1.0,0.0,0.0)); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | { |
|---|
| 235 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 236 | camera->setGraphicsContext(gc.get()); |
|---|
| 237 | camera->setViewport(new osg::Viewport(center_x-camera_width*3/2, center_y, camera_width, camera_height)); |
|---|
| 238 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 239 | camera->setDrawBuffer(buffer); |
|---|
| 240 | camera->setReadBuffer(buffer); |
|---|
| 241 | |
|---|
| 242 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 0.0,1.0,0.0)); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | { |
|---|
| 247 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 248 | camera->setGraphicsContext(gc.get()); |
|---|
| 249 | camera->setViewport(new osg::Viewport(center_x+camera_width/2, center_y, camera_width, camera_height)); |
|---|
| 250 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 251 | camera->setDrawBuffer(buffer); |
|---|
| 252 | camera->setReadBuffer(buffer); |
|---|
| 253 | |
|---|
| 254 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 0.0,1.0,0.0)); |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | { |
|---|
| 259 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 260 | camera->setGraphicsContext(gc.get()); |
|---|
| 261 | camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y-camera_height, camera_width, camera_height)); |
|---|
| 262 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 263 | camera->setDrawBuffer(buffer); |
|---|
| 264 | camera->setReadBuffer(buffer); |
|---|
| 265 | |
|---|
| 266 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 1.0,0.0,0.0)); |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | { |
|---|
| 271 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 272 | camera->setGraphicsContext(gc.get()); |
|---|
| 273 | camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y-2*camera_height, camera_width, camera_height)); |
|---|
| 274 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 275 | camera->setDrawBuffer(buffer); |
|---|
| 276 | camera->setReadBuffer(buffer); |
|---|
| 277 | |
|---|
| 278 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-180.0f), 1.0,0.0,0.0)); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | viewer.getCamera()->setProjectionMatrixAsPerspective(90.0f, 1.0, 1, 1000.0); |
|---|
| 282 | |
|---|
| 283 | viewer.assignSceneDataToCameras(); |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | osg::Geometry* createDomeDistortionMesh(const osg::Vec3& origin, const osg::Vec3& widthVector, const osg::Vec3& heightVector, |
|---|
| 287 | osg::ArgumentParser& arguments) |
|---|
| 288 | { |
|---|
| 289 | double sphere_radius = 1.0; |
|---|
| 290 | double collar_radius = 0.45; |
|---|
| 291 | osg::Vec3d center(0.0,0.0,0.0); |
|---|
| 292 | osg::Vec3d eye(0.0,0.0,0.0); |
|---|
| 293 | |
|---|
| 294 | double distance = sqrt(sphere_radius*sphere_radius - collar_radius*collar_radius); |
|---|
| 295 | osg::Vec3d projector = eye - osg::Vec3d(0.0,0.0, distance); |
|---|
| 296 | |
|---|
| 297 | osg::notify(osg::NOTICE)<<"Projector position = "<<projector<<std::endl; |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | osg::Geometry* geometry = new osg::Geometry(); |
|---|
| 302 | |
|---|
| 303 | geometry->setSupportsDisplayList(false); |
|---|
| 304 | |
|---|
| 305 | osg::Vec3 xAxis(widthVector); |
|---|
| 306 | float width = widthVector.length(); |
|---|
| 307 | xAxis /= width; |
|---|
| 308 | |
|---|
| 309 | osg::Vec3 yAxis(heightVector); |
|---|
| 310 | float height = heightVector.length(); |
|---|
| 311 | yAxis /= height; |
|---|
| 312 | |
|---|
| 313 | int noSteps = 100; |
|---|
| 314 | |
|---|
| 315 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 316 | osg::Vec3Array* texcoords = new osg::Vec3Array; |
|---|
| 317 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 318 | |
|---|
| 319 | osg::Vec3 bottom = origin; |
|---|
| 320 | osg::Vec3 dx = xAxis*(width/((float)(noSteps-1))); |
|---|
| 321 | osg::Vec3 dy = yAxis*(height/((float)(noSteps-1))); |
|---|
| 322 | |
|---|
| 323 | osg::Vec3d screenCenter = origin + widthVector*0.5f + heightVector*0.5f; |
|---|
| 324 | float screenRadius = heightVector.length() * 0.5f; |
|---|
| 325 | |
|---|
| 326 | osg::Vec3 cursor = bottom; |
|---|
| 327 | int i,j; |
|---|
| 328 | for(i=0;i<noSteps;++i) |
|---|
| 329 | { |
|---|
| 330 | osg::Vec3 cursor = bottom+dy*(float)i; |
|---|
| 331 | for(j=0;j<noSteps;++j) |
|---|
| 332 | { |
|---|
| 333 | osg::Vec2 delta(cursor.x() - screenCenter.x(), cursor.y() - screenCenter.y()); |
|---|
| 334 | double theta = atan2(-delta.y(), delta.x()); |
|---|
| 335 | double phi = osg::PI_2 * delta.length() / screenRadius; |
|---|
| 336 | if (phi > osg::PI_2) phi = osg::PI_2; |
|---|
| 337 | |
|---|
| 338 | phi *= 2.0; |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | osg::Vec3 texcoord(sin(phi) * cos(theta), |
|---|
| 343 | sin(phi) * sin(theta), |
|---|
| 344 | cos(phi)); |
|---|
| 345 | |
|---|
| 346 | vertices->push_back(cursor); |
|---|
| 347 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 348 | texcoords->push_back(texcoord); |
|---|
| 349 | |
|---|
| 350 | cursor += dx; |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | geometry->setVertexArray(vertices); |
|---|
| 357 | |
|---|
| 358 | geometry->setColorArray(colors); |
|---|
| 359 | geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 360 | |
|---|
| 361 | geometry->setTexCoordArray(0,texcoords); |
|---|
| 362 | |
|---|
| 363 | for(i=0;i<noSteps-1;++i) |
|---|
| 364 | { |
|---|
| 365 | osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP); |
|---|
| 366 | for(j=0;j<noSteps;++j) |
|---|
| 367 | { |
|---|
| 368 | elements->push_back(j+(i+1)*noSteps); |
|---|
| 369 | elements->push_back(j+(i)*noSteps); |
|---|
| 370 | } |
|---|
| 371 | geometry->addPrimitiveSet(elements); |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | return geometry; |
|---|
| 375 | }; |
|---|
| 376 | |
|---|
| 377 | void setDomeCorrection(osgViewer::Viewer& viewer, osg::ArgumentParser& arguments) |
|---|
| 378 | { |
|---|
| 379 | |
|---|
| 380 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
|---|
| 381 | if (!wsi) |
|---|
| 382 | { |
|---|
| 383 | osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl; |
|---|
| 384 | return; |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | unsigned int width, height; |
|---|
| 388 | wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height); |
|---|
| 389 | |
|---|
| 390 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| 391 | traits->x = 0; |
|---|
| 392 | traits->y = 0; |
|---|
| 393 | traits->width = width; |
|---|
| 394 | traits->height = height; |
|---|
| 395 | traits->windowDecoration = false; |
|---|
| 396 | traits->doubleBuffer = true; |
|---|
| 397 | traits->sharedContext = 0; |
|---|
| 398 | |
|---|
| 399 | osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 400 | if (!gc) |
|---|
| 401 | { |
|---|
| 402 | osg::notify(osg::NOTICE)<<"GraphicsWindow has not been created successfully."<<std::endl; |
|---|
| 403 | return; |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | int tex_width = 512; |
|---|
| 407 | int tex_height = 512; |
|---|
| 408 | |
|---|
| 409 | int camera_width = tex_width; |
|---|
| 410 | int camera_height = tex_height; |
|---|
| 411 | |
|---|
| 412 | osg::TextureCubeMap* texture = new osg::TextureCubeMap; |
|---|
| 413 | |
|---|
| 414 | texture->setTextureSize(tex_width, tex_height); |
|---|
| 415 | texture->setInternalFormat(GL_RGB); |
|---|
| 416 | texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); |
|---|
| 417 | texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); |
|---|
| 418 | |
|---|
| 419 | #if 0 |
|---|
| 420 | osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::SEPERATE_WINDOW; |
|---|
| 421 | GLenum buffer = GL_FRONT; |
|---|
| 422 | #else |
|---|
| 423 | osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::FRAME_BUFFER_OBJECT; |
|---|
| 424 | GLenum buffer = GL_FRONT; |
|---|
| 425 | #endif |
|---|
| 426 | |
|---|
| 427 | |
|---|
| 428 | { |
|---|
| 429 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 430 | camera->setName("Front face camera"); |
|---|
| 431 | camera->setGraphicsContext(gc.get()); |
|---|
| 432 | camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); |
|---|
| 433 | camera->setDrawBuffer(buffer); |
|---|
| 434 | camera->setReadBuffer(buffer); |
|---|
| 435 | camera->setAllowEventFocus(false); |
|---|
| 436 | |
|---|
| 437 | camera->setRenderTargetImplementation(renderTargetImplementation); |
|---|
| 438 | |
|---|
| 439 | |
|---|
| 440 | camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Y); |
|---|
| 441 | |
|---|
| 442 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd()); |
|---|
| 443 | } |
|---|
| 444 | |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | { |
|---|
| 448 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 449 | camera->setName("Top face camera"); |
|---|
| 450 | camera->setGraphicsContext(gc.get()); |
|---|
| 451 | camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); |
|---|
| 452 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 453 | camera->setDrawBuffer(buffer); |
|---|
| 454 | camera->setReadBuffer(buffer); |
|---|
| 455 | camera->setAllowEventFocus(false); |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | camera->setRenderTargetImplementation(renderTargetImplementation); |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Z); |
|---|
| 462 | |
|---|
| 463 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 1.0,0.0,0.0)); |
|---|
| 464 | } |
|---|
| 465 | |
|---|
| 466 | |
|---|
| 467 | { |
|---|
| 468 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 469 | camera->setName("Left face camera"); |
|---|
| 470 | camera->setGraphicsContext(gc.get()); |
|---|
| 471 | camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); |
|---|
| 472 | camera->setDrawBuffer(buffer); |
|---|
| 473 | camera->setReadBuffer(buffer); |
|---|
| 474 | camera->setAllowEventFocus(false); |
|---|
| 475 | |
|---|
| 476 | |
|---|
| 477 | camera->setRenderTargetImplementation(renderTargetImplementation); |
|---|
| 478 | |
|---|
| 479 | |
|---|
| 480 | camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_X); |
|---|
| 481 | |
|---|
| 482 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 0.0,1.0,0.0) * osg::Matrixd::rotate(osg::inDegrees(-90.0f), 0.0,0.0,1.0)); |
|---|
| 483 | } |
|---|
| 484 | |
|---|
| 485 | |
|---|
| 486 | { |
|---|
| 487 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 488 | camera->setName("Right face camera"); |
|---|
| 489 | camera->setGraphicsContext(gc.get()); |
|---|
| 490 | camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); |
|---|
| 491 | camera->setDrawBuffer(buffer); |
|---|
| 492 | camera->setReadBuffer(buffer); |
|---|
| 493 | camera->setAllowEventFocus(false); |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | camera->setRenderTargetImplementation(renderTargetImplementation); |
|---|
| 497 | |
|---|
| 498 | |
|---|
| 499 | camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_X); |
|---|
| 500 | |
|---|
| 501 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 0.0,1.0,0.0 ) * osg::Matrixd::rotate(osg::inDegrees(90.0f), 0.0,0.0,1.0)); |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | |
|---|
| 505 | { |
|---|
| 506 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 507 | camera->setGraphicsContext(gc.get()); |
|---|
| 508 | camera->setName("Bottom face camera"); |
|---|
| 509 | camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); |
|---|
| 510 | camera->setDrawBuffer(buffer); |
|---|
| 511 | camera->setReadBuffer(buffer); |
|---|
| 512 | camera->setAllowEventFocus(false); |
|---|
| 513 | |
|---|
| 514 | |
|---|
| 515 | camera->setRenderTargetImplementation(renderTargetImplementation); |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Z); |
|---|
| 519 | |
|---|
| 520 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 1.0,0.0,0.0) * osg::Matrixd::rotate(osg::inDegrees(180.0f), 0.0,0.0,1.0)); |
|---|
| 521 | } |
|---|
| 522 | |
|---|
| 523 | |
|---|
| 524 | { |
|---|
| 525 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 526 | camera->setName("Back face camera"); |
|---|
| 527 | camera->setGraphicsContext(gc.get()); |
|---|
| 528 | camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); |
|---|
| 529 | camera->setDrawBuffer(buffer); |
|---|
| 530 | camera->setReadBuffer(buffer); |
|---|
| 531 | camera->setAllowEventFocus(false); |
|---|
| 532 | |
|---|
| 533 | |
|---|
| 534 | camera->setRenderTargetImplementation(renderTargetImplementation); |
|---|
| 535 | |
|---|
| 536 | |
|---|
| 537 | camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Y); |
|---|
| 538 | |
|---|
| 539 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(180.0f), 1.0,0.0,0.0)); |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | viewer.getCamera()->setProjectionMatrixAsPerspective(90.0f, 1.0, 1, 1000.0); |
|---|
| 543 | |
|---|
| 544 | |
|---|
| 545 | viewer.assignSceneDataToCameras(); |
|---|
| 546 | |
|---|
| 547 | |
|---|
| 548 | |
|---|
| 549 | { |
|---|
| 550 | osg::Geode* geode = new osg::Geode(); |
|---|
| 551 | geode->addDrawable(createDomeDistortionMesh(osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(width,0.0f,0.0f), osg::Vec3(0.0f,height,0.0f), arguments)); |
|---|
| 552 | |
|---|
| 553 | |
|---|
| 554 | |
|---|
| 555 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| 556 | stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON); |
|---|
| 557 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 558 | |
|---|
| 559 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 560 | camera->setGraphicsContext(gc.get()); |
|---|
| 561 | camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); |
|---|
| 562 | camera->setClearColor( osg::Vec4(0.1,0.1,1.0,1.0) ); |
|---|
| 563 | camera->setViewport(new osg::Viewport(0, 0, width, height)); |
|---|
| 564 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 565 | camera->setDrawBuffer(buffer); |
|---|
| 566 | camera->setReadBuffer(buffer); |
|---|
| 567 | camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF); |
|---|
| 568 | camera->setAllowEventFocus(false); |
|---|
| 569 | |
|---|
| 570 | |
|---|
| 571 | |
|---|
| 572 | camera->setProjectionMatrixAsOrtho2D(0,width,0,height); |
|---|
| 573 | camera->setViewMatrix(osg::Matrix::identity()); |
|---|
| 574 | |
|---|
| 575 | |
|---|
| 576 | camera->addChild(geode); |
|---|
| 577 | |
|---|
| 578 | camera->setName("DistortionCorrectionCamera"); |
|---|
| 579 | |
|---|
| 580 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd()); |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | viewer.getCamera()->setNearFarRatio(0.0001f); |
|---|
| 584 | } |
|---|
| 585 | |
|---|
| 586 | |
|---|
| 587 | int main(int argc, char** argv) |
|---|
| 588 | { |
|---|
| 589 | |
|---|
| 590 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 591 | |
|---|
| 592 | |
|---|
| 593 | osgViewer::Viewer viewer; |
|---|
| 594 | |
|---|
| 595 | |
|---|
| 596 | osg::Node* loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 597 | if (!loadedModel) |
|---|
| 598 | { |
|---|
| 599 | osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<<std::endl; |
|---|
| 600 | return 1; |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | if (arguments.read("--dome")) |
|---|
| 604 | { |
|---|
| 605 | viewer.setSceneData( loadedModel ); |
|---|
| 606 | |
|---|
| 607 | setDomeCorrection(viewer, arguments); |
|---|
| 608 | |
|---|
| 609 | } |
|---|
| 610 | else if (arguments.read("--faces")) |
|---|
| 611 | { |
|---|
| 612 | viewer.setSceneData( loadedModel ); |
|---|
| 613 | |
|---|
| 614 | setDomeFaces(viewer, arguments); |
|---|
| 615 | |
|---|
| 616 | } |
|---|
| 617 | else |
|---|
| 618 | { |
|---|
| 619 | osg::Node* distortionNode = createDistortionSubgraph(loadedModel, viewer.getCamera()->getClearColor()); |
|---|
| 620 | |
|---|
| 621 | |
|---|
| 622 | viewer.setSceneData( distortionNode ); |
|---|
| 623 | } |
|---|
| 624 | |
|---|
| 625 | |
|---|
| 626 | { |
|---|
| 627 | osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; |
|---|
| 628 | |
|---|
| 629 | keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); |
|---|
| 630 | keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); |
|---|
| 631 | keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); |
|---|
| 632 | keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() ); |
|---|
| 633 | |
|---|
| 634 | std::string pathfile; |
|---|
| 635 | char keyForAnimationPath = '5'; |
|---|
| 636 | while (arguments.read("-p",pathfile)) |
|---|
| 637 | { |
|---|
| 638 | osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile); |
|---|
| 639 | if (apm || !apm->valid()) |
|---|
| 640 | { |
|---|
| 641 | unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 642 | keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm ); |
|---|
| 643 | keyswitchManipulator->selectMatrixManipulator(num); |
|---|
| 644 | ++keyForAnimationPath; |
|---|
| 645 | } |
|---|
| 646 | } |
|---|
| 647 | |
|---|
| 648 | viewer.setCameraManipulator( keyswitchManipulator.get() ); |
|---|
| 649 | } |
|---|
| 650 | |
|---|
| 651 | |
|---|
| 652 | |
|---|
| 653 | |
|---|
| 654 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 655 | |
|---|
| 656 | |
|---|
| 657 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 658 | |
|---|
| 659 | return viewer.run(); |
|---|
| 660 | } |
|---|
| 661 | ; |
|---|