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