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