| 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& arguments) |
|---|
| 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 | while (arguments.read("--width",width)) {} |
|---|
| 187 | while (arguments.read("--height",height)) {} |
|---|
| 188 | |
|---|
| 189 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| 190 | traits->x = 0; |
|---|
| 191 | traits->y = 0; |
|---|
| 192 | traits->width = width; |
|---|
| 193 | traits->height = height; |
|---|
| 194 | traits->windowDecoration = true; |
|---|
| 195 | traits->doubleBuffer = true; |
|---|
| 196 | traits->sharedContext = 0; |
|---|
| 197 | |
|---|
| 198 | osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 199 | if (!gc) |
|---|
| 200 | { |
|---|
| 201 | osg::notify(osg::NOTICE)<<"GraphicsWindow has not been created successfully."<<std::endl; |
|---|
| 202 | return; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | int center_x = width/2; |
|---|
| 207 | int center_y = height/2; |
|---|
| 208 | int camera_width = 256; |
|---|
| 209 | int camera_height = 256; |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | { |
|---|
| 213 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 214 | camera->setGraphicsContext(gc.get()); |
|---|
| 215 | camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y, camera_width, camera_height)); |
|---|
| 216 | |
|---|
| 217 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 218 | camera->setDrawBuffer(buffer); |
|---|
| 219 | camera->setReadBuffer(buffer); |
|---|
| 220 | |
|---|
| 221 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd()); |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | { |
|---|
| 226 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 227 | camera->setGraphicsContext(gc.get()); |
|---|
| 228 | camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y+camera_height, camera_width, camera_height)); |
|---|
| 229 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 230 | camera->setDrawBuffer(buffer); |
|---|
| 231 | camera->setReadBuffer(buffer); |
|---|
| 232 | |
|---|
| 233 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 1.0,0.0,0.0)); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | { |
|---|
| 238 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 239 | camera->setGraphicsContext(gc.get()); |
|---|
| 240 | camera->setViewport(new osg::Viewport(center_x-camera_width*3/2, center_y, camera_width, camera_height)); |
|---|
| 241 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 242 | camera->setDrawBuffer(buffer); |
|---|
| 243 | camera->setReadBuffer(buffer); |
|---|
| 244 | |
|---|
| 245 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 0.0,1.0,0.0)); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | { |
|---|
| 250 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 251 | camera->setGraphicsContext(gc.get()); |
|---|
| 252 | camera->setViewport(new osg::Viewport(center_x+camera_width/2, center_y, camera_width, camera_height)); |
|---|
| 253 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 254 | camera->setDrawBuffer(buffer); |
|---|
| 255 | camera->setReadBuffer(buffer); |
|---|
| 256 | |
|---|
| 257 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 0.0,1.0,0.0)); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | { |
|---|
| 262 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 263 | camera->setGraphicsContext(gc.get()); |
|---|
| 264 | camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y-camera_height, camera_width, camera_height)); |
|---|
| 265 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 266 | camera->setDrawBuffer(buffer); |
|---|
| 267 | camera->setReadBuffer(buffer); |
|---|
| 268 | |
|---|
| 269 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 1.0,0.0,0.0)); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | { |
|---|
| 274 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 275 | camera->setGraphicsContext(gc.get()); |
|---|
| 276 | camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y-2*camera_height, camera_width, camera_height)); |
|---|
| 277 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 278 | camera->setDrawBuffer(buffer); |
|---|
| 279 | camera->setReadBuffer(buffer); |
|---|
| 280 | |
|---|
| 281 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-180.0f), 1.0,0.0,0.0)); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | viewer.getCamera()->setProjectionMatrixAsPerspective(90.0f, 1.0, 1, 1000.0); |
|---|
| 285 | |
|---|
| 286 | viewer.assignSceneDataToCameras(); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | osg::Geometry* createDomeDistortionMesh(const osg::Vec3& origin, const osg::Vec3& widthVector, const osg::Vec3& heightVector, |
|---|
| 290 | osg::ArgumentParser& arguments) |
|---|
| 291 | { |
|---|
| 292 | double sphere_radius = 1.0; |
|---|
| 293 | if (arguments.read("--radius", sphere_radius)) {} |
|---|
| 294 | |
|---|
| 295 | double collar_radius = 0.45; |
|---|
| 296 | if (arguments.read("--collar", collar_radius)) {} |
|---|
| 297 | |
|---|
| 298 | osg::Vec3d center(0.0,0.0,0.0); |
|---|
| 299 | osg::Vec3d eye(0.0,0.0,0.0); |
|---|
| 300 | |
|---|
| 301 | double distance = sqrt(sphere_radius*sphere_radius - collar_radius*collar_radius); |
|---|
| 302 | if (arguments.read("--distance", distance)) {} |
|---|
| 303 | |
|---|
| 304 | bool centerProjection = false; |
|---|
| 305 | |
|---|
| 306 | osg::Vec3d projector = eye - osg::Vec3d(0.0,0.0, distance); |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | osg::notify(osg::NOTICE)<<"Projector position = "<<projector<<std::endl; |
|---|
| 310 | osg::notify(osg::NOTICE)<<"distance = "<<distance<<std::endl; |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | osg::Geometry* geometry = new osg::Geometry(); |
|---|
| 315 | |
|---|
| 316 | geometry->setSupportsDisplayList(false); |
|---|
| 317 | |
|---|
| 318 | osg::Vec3 xAxis(widthVector); |
|---|
| 319 | float width = widthVector.length(); |
|---|
| 320 | xAxis /= width; |
|---|
| 321 | |
|---|
| 322 | osg::Vec3 yAxis(heightVector); |
|---|
| 323 | float height = heightVector.length(); |
|---|
| 324 | yAxis /= height; |
|---|
| 325 | |
|---|
| 326 | int noSteps = 50; |
|---|
| 327 | |
|---|
| 328 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 329 | osg::Vec3Array* texcoords = new osg::Vec3Array; |
|---|
| 330 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 331 | |
|---|
| 332 | osg::Vec3 bottom = origin; |
|---|
| 333 | osg::Vec3 dx = xAxis*(width/((float)(noSteps-1))); |
|---|
| 334 | osg::Vec3 dy = yAxis*(height/((float)(noSteps-1))); |
|---|
| 335 | |
|---|
| 336 | osg::Vec3d screenCenter = origin + widthVector*0.5f + heightVector*0.5f; |
|---|
| 337 | float screenRadius = heightVector.length() * 0.5f; |
|---|
| 338 | |
|---|
| 339 | osg::Vec3 cursor = bottom; |
|---|
| 340 | int i,j; |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | if (centerProjection) |
|---|
| 344 | { |
|---|
| 345 | for(i=0;i<noSteps;++i) |
|---|
| 346 | { |
|---|
| 347 | osg::Vec3 cursor = bottom+dy*(float)i; |
|---|
| 348 | for(j=0;j<noSteps;++j) |
|---|
| 349 | { |
|---|
| 350 | osg::Vec2 delta(cursor.x() - screenCenter.x(), cursor.y() - screenCenter.y()); |
|---|
| 351 | double theta = atan2(-delta.y(), delta.x()); |
|---|
| 352 | double phi = osg::PI_2 * delta.length() / screenRadius; |
|---|
| 353 | if (phi > osg::PI_2) phi = osg::PI_2; |
|---|
| 354 | |
|---|
| 355 | phi *= 2.0; |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | osg::Vec3 texcoord(sin(phi) * cos(theta), |
|---|
| 360 | sin(phi) * sin(theta), |
|---|
| 361 | cos(phi)); |
|---|
| 362 | |
|---|
| 363 | vertices->push_back(cursor); |
|---|
| 364 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 365 | texcoords->push_back(texcoord); |
|---|
| 366 | |
|---|
| 367 | cursor += dx; |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | } |
|---|
| 371 | } |
|---|
| 372 | else |
|---|
| 373 | { |
|---|
| 374 | for(i=0;i<noSteps;++i) |
|---|
| 375 | { |
|---|
| 376 | osg::Vec3 cursor = bottom+dy*(float)i; |
|---|
| 377 | for(j=0;j<noSteps;++j) |
|---|
| 378 | { |
|---|
| 379 | osg::Vec2 delta(cursor.x() - screenCenter.x(), cursor.y() - screenCenter.y()); |
|---|
| 380 | double theta = atan2(-delta.y(), delta.x()); |
|---|
| 381 | double phi = osg::PI_2 * delta.length() / screenRadius; |
|---|
| 382 | if (phi > osg::PI_2) phi = osg::PI_2; |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | double f = distance * sin(phi); |
|---|
| 387 | double e = distance * cos(phi) + sqrt( sphere_radius*sphere_radius - f*f); |
|---|
| 388 | double l = e * cos(phi); |
|---|
| 389 | double h = e * sin(phi); |
|---|
| 390 | double z = l - distance; |
|---|
| 391 | |
|---|
| 392 | osg::Vec3 texcoord(h * cos(theta) / sphere_radius, |
|---|
| 393 | h * sin(theta) / sphere_radius, |
|---|
| 394 | z / sphere_radius); |
|---|
| 395 | |
|---|
| 396 | vertices->push_back(cursor); |
|---|
| 397 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 398 | texcoords->push_back(texcoord); |
|---|
| 399 | |
|---|
| 400 | cursor += dx; |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | } |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | |
|---|
| 407 | geometry->setVertexArray(vertices); |
|---|
| 408 | |
|---|
| 409 | geometry->setColorArray(colors); |
|---|
| 410 | geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 411 | |
|---|
| 412 | geometry->setTexCoordArray(0,texcoords); |
|---|
| 413 | |
|---|
| 414 | for(i=0;i<noSteps-1;++i) |
|---|
| 415 | { |
|---|
| 416 | osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP); |
|---|
| 417 | for(j=0;j<noSteps;++j) |
|---|
| 418 | { |
|---|
| 419 | elements->push_back(j+(i+1)*noSteps); |
|---|
| 420 | elements->push_back(j+(i)*noSteps); |
|---|
| 421 | } |
|---|
| 422 | geometry->addPrimitiveSet(elements); |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | return geometry; |
|---|
| 426 | } |
|---|
| 427 | |
|---|
| 428 | void setDomeCorrection(osgViewer::Viewer& viewer, osg::ArgumentParser& arguments) |
|---|
| 429 | { |
|---|
| 430 | |
|---|
| 431 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
|---|
| 432 | if (!wsi) |
|---|
| 433 | { |
|---|
| 434 | osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl; |
|---|
| 435 | return; |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | unsigned int width, height; |
|---|
| 439 | wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height); |
|---|
| 440 | |
|---|
| 441 | while (arguments.read("--width",width)) {} |
|---|
| 442 | while (arguments.read("--height",height)) {} |
|---|
| 443 | |
|---|
| 444 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| 445 | traits->x = 0; |
|---|
| 446 | traits->y = 0; |
|---|
| 447 | traits->width = width; |
|---|
| 448 | traits->height = height; |
|---|
| 449 | traits->windowDecoration = false; |
|---|
| 450 | traits->doubleBuffer = true; |
|---|
| 451 | traits->sharedContext = 0; |
|---|
| 452 | |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 456 | if (!gc) |
|---|
| 457 | { |
|---|
| 458 | osg::notify(osg::NOTICE)<<"GraphicsWindow has not been created successfully."<<std::endl; |
|---|
| 459 | return; |
|---|
| 460 | } |
|---|
| 461 | |
|---|
| 462 | int tex_width = 512; |
|---|
| 463 | int tex_height = 512; |
|---|
| 464 | |
|---|
| 465 | int camera_width = tex_width; |
|---|
| 466 | int camera_height = tex_height; |
|---|
| 467 | |
|---|
| 468 | osg::TextureCubeMap* texture = new osg::TextureCubeMap; |
|---|
| 469 | |
|---|
| 470 | texture->setTextureSize(tex_width, tex_height); |
|---|
| 471 | texture->setInternalFormat(GL_RGB); |
|---|
| 472 | texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); |
|---|
| 473 | texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); |
|---|
| 474 | |
|---|
| 475 | #if 0 |
|---|
| 476 | osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::SEPERATE_WINDOW; |
|---|
| 477 | GLenum buffer = GL_FRONT; |
|---|
| 478 | #else |
|---|
| 479 | osg::Camera::RenderTargetImplementation renderTargetImplementation = osg::Camera::FRAME_BUFFER_OBJECT; |
|---|
| 480 | GLenum buffer = GL_FRONT; |
|---|
| 481 | #endif |
|---|
| 482 | |
|---|
| 483 | |
|---|
| 484 | { |
|---|
| 485 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 486 | camera->setName("Front face camera"); |
|---|
| 487 | camera->setGraphicsContext(gc.get()); |
|---|
| 488 | camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); |
|---|
| 489 | camera->setDrawBuffer(buffer); |
|---|
| 490 | camera->setReadBuffer(buffer); |
|---|
| 491 | camera->setAllowEventFocus(false); |
|---|
| 492 | |
|---|
| 493 | camera->setRenderTargetImplementation(renderTargetImplementation); |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Y); |
|---|
| 497 | |
|---|
| 498 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd()); |
|---|
| 499 | } |
|---|
| 500 | |
|---|
| 501 | |
|---|
| 502 | |
|---|
| 503 | { |
|---|
| 504 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 505 | camera->setName("Top face camera"); |
|---|
| 506 | camera->setGraphicsContext(gc.get()); |
|---|
| 507 | camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); |
|---|
| 508 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 509 | camera->setDrawBuffer(buffer); |
|---|
| 510 | camera->setReadBuffer(buffer); |
|---|
| 511 | camera->setAllowEventFocus(false); |
|---|
| 512 | |
|---|
| 513 | |
|---|
| 514 | camera->setRenderTargetImplementation(renderTargetImplementation); |
|---|
| 515 | |
|---|
| 516 | |
|---|
| 517 | camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_Z); |
|---|
| 518 | |
|---|
| 519 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 1.0,0.0,0.0)); |
|---|
| 520 | } |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | { |
|---|
| 524 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 525 | camera->setName("Left face camera"); |
|---|
| 526 | camera->setGraphicsContext(gc.get()); |
|---|
| 527 | camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); |
|---|
| 528 | camera->setDrawBuffer(buffer); |
|---|
| 529 | camera->setReadBuffer(buffer); |
|---|
| 530 | camera->setAllowEventFocus(false); |
|---|
| 531 | |
|---|
| 532 | |
|---|
| 533 | camera->setRenderTargetImplementation(renderTargetImplementation); |
|---|
| 534 | |
|---|
| 535 | |
|---|
| 536 | camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_X); |
|---|
| 537 | |
|---|
| 538 | 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)); |
|---|
| 539 | } |
|---|
| 540 | |
|---|
| 541 | |
|---|
| 542 | { |
|---|
| 543 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 544 | camera->setName("Right face camera"); |
|---|
| 545 | camera->setGraphicsContext(gc.get()); |
|---|
| 546 | camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); |
|---|
| 547 | camera->setDrawBuffer(buffer); |
|---|
| 548 | camera->setReadBuffer(buffer); |
|---|
| 549 | camera->setAllowEventFocus(false); |
|---|
| 550 | |
|---|
| 551 | |
|---|
| 552 | camera->setRenderTargetImplementation(renderTargetImplementation); |
|---|
| 553 | |
|---|
| 554 | |
|---|
| 555 | camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::POSITIVE_X); |
|---|
| 556 | |
|---|
| 557 | 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)); |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | |
|---|
| 561 | { |
|---|
| 562 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 563 | camera->setGraphicsContext(gc.get()); |
|---|
| 564 | camera->setName("Bottom face camera"); |
|---|
| 565 | camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); |
|---|
| 566 | camera->setDrawBuffer(buffer); |
|---|
| 567 | camera->setReadBuffer(buffer); |
|---|
| 568 | camera->setAllowEventFocus(false); |
|---|
| 569 | |
|---|
| 570 | |
|---|
| 571 | camera->setRenderTargetImplementation(renderTargetImplementation); |
|---|
| 572 | |
|---|
| 573 | |
|---|
| 574 | camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Z); |
|---|
| 575 | |
|---|
| 576 | 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)); |
|---|
| 577 | } |
|---|
| 578 | |
|---|
| 579 | |
|---|
| 580 | { |
|---|
| 581 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 582 | camera->setName("Back face camera"); |
|---|
| 583 | camera->setGraphicsContext(gc.get()); |
|---|
| 584 | camera->setViewport(new osg::Viewport(0,0,camera_width, camera_height)); |
|---|
| 585 | camera->setDrawBuffer(buffer); |
|---|
| 586 | camera->setReadBuffer(buffer); |
|---|
| 587 | camera->setAllowEventFocus(false); |
|---|
| 588 | |
|---|
| 589 | |
|---|
| 590 | camera->setRenderTargetImplementation(renderTargetImplementation); |
|---|
| 591 | |
|---|
| 592 | |
|---|
| 593 | camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, osg::TextureCubeMap::NEGATIVE_Y); |
|---|
| 594 | |
|---|
| 595 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(180.0f), 1.0,0.0,0.0)); |
|---|
| 596 | } |
|---|
| 597 | |
|---|
| 598 | viewer.getCamera()->setProjectionMatrixAsPerspective(90.0f, 1.0, 1, 1000.0); |
|---|
| 599 | |
|---|
| 600 | |
|---|
| 601 | |
|---|
| 602 | |
|---|
| 603 | { |
|---|
| 604 | osg::Geode* geode = new osg::Geode(); |
|---|
| 605 | 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)); |
|---|
| 606 | |
|---|
| 607 | |
|---|
| 608 | |
|---|
| 609 | osg::StateSet* stateset = geode->getOrCreateStateSet(); |
|---|
| 610 | stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON); |
|---|
| 611 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 612 | |
|---|
| 613 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 614 | camera->setGraphicsContext(gc.get()); |
|---|
| 615 | camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); |
|---|
| 616 | camera->setClearColor( osg::Vec4(0.1,0.1,1.0,1.0) ); |
|---|
| 617 | camera->setViewport(new osg::Viewport(0, 0, width, height)); |
|---|
| 618 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 619 | camera->setDrawBuffer(buffer); |
|---|
| 620 | camera->setReadBuffer(buffer); |
|---|
| 621 | camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF); |
|---|
| 622 | camera->setAllowEventFocus(false); |
|---|
| 623 | |
|---|
| 624 | |
|---|
| 625 | |
|---|
| 626 | camera->setProjectionMatrixAsOrtho2D(0,width,0,height); |
|---|
| 627 | camera->setViewMatrix(osg::Matrix::identity()); |
|---|
| 628 | |
|---|
| 629 | |
|---|
| 630 | camera->addChild(geode); |
|---|
| 631 | |
|---|
| 632 | camera->setName("DistortionCorrectionCamera"); |
|---|
| 633 | |
|---|
| 634 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd(), false); |
|---|
| 635 | } |
|---|
| 636 | |
|---|
| 637 | viewer.getCamera()->setNearFarRatio(0.0001f); |
|---|
| 638 | } |
|---|
| 639 | |
|---|
| 640 | |
|---|
| 641 | int main(int argc, char** argv) |
|---|
| 642 | { |
|---|
| 643 | |
|---|
| 644 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 645 | |
|---|
| 646 | |
|---|
| 647 | osgViewer::Viewer viewer; |
|---|
| 648 | |
|---|
| 649 | |
|---|
| 650 | if (arguments.read("--dome") || arguments.read("--puffer") ) |
|---|
| 651 | { |
|---|
| 652 | |
|---|
| 653 | setDomeCorrection(viewer, arguments); |
|---|
| 654 | |
|---|
| 655 | viewer.setSceneData( osgDB::readNodeFiles(arguments) ); |
|---|
| 656 | } |
|---|
| 657 | else if (arguments.read("--faces")) |
|---|
| 658 | { |
|---|
| 659 | |
|---|
| 660 | setDomeFaces(viewer, arguments); |
|---|
| 661 | |
|---|
| 662 | viewer.setSceneData( osgDB::readNodeFiles(arguments) ); |
|---|
| 663 | } |
|---|
| 664 | else |
|---|
| 665 | { |
|---|
| 666 | osg::Node* distortionNode = createDistortionSubgraph( osgDB::readNodeFiles(arguments), viewer.getCamera()->getClearColor()); |
|---|
| 667 | viewer.setSceneData( distortionNode ); |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | if (!viewer.getSceneData()) |
|---|
| 673 | { |
|---|
| 674 | osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<<std::endl; |
|---|
| 675 | return 1; |
|---|
| 676 | } |
|---|
| 677 | |
|---|
| 678 | |
|---|
| 679 | |
|---|
| 680 | { |
|---|
| 681 | osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; |
|---|
| 682 | |
|---|
| 683 | keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); |
|---|
| 684 | keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); |
|---|
| 685 | keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); |
|---|
| 686 | keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() ); |
|---|
| 687 | |
|---|
| 688 | std::string pathfile; |
|---|
| 689 | char keyForAnimationPath = '5'; |
|---|
| 690 | while (arguments.read("-p",pathfile)) |
|---|
| 691 | { |
|---|
| 692 | osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile); |
|---|
| 693 | if (apm || !apm->valid()) |
|---|
| 694 | { |
|---|
| 695 | unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); |
|---|
| 696 | keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm ); |
|---|
| 697 | keyswitchManipulator->selectMatrixManipulator(num); |
|---|
| 698 | ++keyForAnimationPath; |
|---|
| 699 | } |
|---|
| 700 | } |
|---|
| 701 | |
|---|
| 702 | viewer.setCameraManipulator( keyswitchManipulator.get() ); |
|---|
| 703 | } |
|---|
| 704 | |
|---|
| 705 | viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); |
|---|
| 706 | |
|---|
| 707 | |
|---|
| 708 | viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); |
|---|
| 709 | |
|---|
| 710 | |
|---|
| 711 | viewer.addEventHandler(new osgViewer::StatsHandler); |
|---|
| 712 | |
|---|
| 713 | return viewer.run(); |
|---|
| 714 | } |
|---|