| 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 | |
|---|
| 14 | #include <osgGA/TrackballManipulator> |
|---|
| 15 | #include <osgGA/FlightManipulator> |
|---|
| 16 | #include <osgGA/DriveManipulator> |
|---|
| 17 | |
|---|
| 18 | #include <osgUtil/SmoothingVisitor> |
|---|
| 19 | |
|---|
| 20 | #include <osgDB/Registry> |
|---|
| 21 | #include <osgDB/ReadFile> |
|---|
| 22 | |
|---|
| 23 | #include <osgViewer/Viewer> |
|---|
| 24 | |
|---|
| 25 | using namespace osg; |
|---|
| 26 | |
|---|
| 27 | osg::Node* createDistortionSubgraph(osg::Node* subgraph, const osg::Vec4& clearColour) |
|---|
| 28 | { |
|---|
| 29 | osg::Group* distortionNode = new osg::Group; |
|---|
| 30 | |
|---|
| 31 | unsigned int tex_width = 1024; |
|---|
| 32 | unsigned int tex_height = 1024; |
|---|
| 33 | |
|---|
| 34 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 35 | texture->setTextureSize(tex_width, tex_height); |
|---|
| 36 | texture->setInternalFormat(GL_RGBA); |
|---|
| 37 | texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); |
|---|
| 38 | texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | { |
|---|
| 42 | osg::Camera* camera = new osg::Camera; |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | camera->setClearColor(clearColour); |
|---|
| 46 | camera->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | camera->setReferenceFrame(osg::Transform::RELATIVE_RF); |
|---|
| 50 | camera->setProjectionMatrix(osg::Matrixd::identity()); |
|---|
| 51 | camera->setViewMatrix(osg::Matrixd::identity()); |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | camera->setViewport(0,0,tex_width,tex_height); |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | camera->setRenderOrder(osg::Camera::PRE_RENDER); |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | camera->attach(osg::Camera::COLOR_BUFFER, texture); |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | camera->addChild(subgraph); |
|---|
| 67 | |
|---|
| 68 | distortionNode->addChild(camera); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | { |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | osg::Geometry* polyGeom = new osg::Geometry(); |
|---|
| 76 | |
|---|
| 77 | polyGeom->setSupportsDisplayList(false); |
|---|
| 78 | |
|---|
| 79 | osg::Vec3 origin(0.0f,0.0f,0.0f); |
|---|
| 80 | osg::Vec3 xAxis(1.0f,0.0f,0.0f); |
|---|
| 81 | osg::Vec3 yAxis(0.0f,1.0f,0.0f); |
|---|
| 82 | osg::Vec3 zAxis(0.0f,0.0f,1.0f); |
|---|
| 83 | float height = 1024.0f; |
|---|
| 84 | float width = 1280.0f; |
|---|
| 85 | int noSteps = 50; |
|---|
| 86 | |
|---|
| 87 | osg::Vec3Array* vertices = new osg::Vec3Array; |
|---|
| 88 | osg::Vec2Array* texcoords = new osg::Vec2Array; |
|---|
| 89 | osg::Vec4Array* colors = new osg::Vec4Array; |
|---|
| 90 | |
|---|
| 91 | osg::Vec3 bottom = origin; |
|---|
| 92 | osg::Vec3 dx = xAxis*(width/((float)(noSteps-1))); |
|---|
| 93 | osg::Vec3 dy = yAxis*(height/((float)(noSteps-1))); |
|---|
| 94 | |
|---|
| 95 | osg::Vec2 bottom_texcoord(0.0f,0.0f); |
|---|
| 96 | osg::Vec2 dx_texcoord(1.0f/(float)(noSteps-1),0.0f); |
|---|
| 97 | osg::Vec2 dy_texcoord(0.0f,1.0f/(float)(noSteps-1)); |
|---|
| 98 | |
|---|
| 99 | osg::Vec3 cursor = bottom; |
|---|
| 100 | osg::Vec2 texcoord = bottom_texcoord; |
|---|
| 101 | int i,j; |
|---|
| 102 | for(i=0;i<noSteps;++i) |
|---|
| 103 | { |
|---|
| 104 | osg::Vec3 cursor = bottom+dy*(float)i; |
|---|
| 105 | osg::Vec2 texcoord = bottom_texcoord+dy_texcoord*(float)i; |
|---|
| 106 | for(j=0;j<noSteps;++j) |
|---|
| 107 | { |
|---|
| 108 | vertices->push_back(cursor); |
|---|
| 109 | 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)); |
|---|
| 110 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 111 | |
|---|
| 112 | cursor += dx; |
|---|
| 113 | texcoord += dx_texcoord; |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | polyGeom->setVertexArray(vertices); |
|---|
| 119 | |
|---|
| 120 | polyGeom->setColorArray(colors); |
|---|
| 121 | polyGeom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
|---|
| 122 | |
|---|
| 123 | polyGeom->setTexCoordArray(0,texcoords); |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | for(i=0;i<noSteps-1;++i) |
|---|
| 127 | { |
|---|
| 128 | osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP); |
|---|
| 129 | for(j=0;j<noSteps;++j) |
|---|
| 130 | { |
|---|
| 131 | elements->push_back(j+(i+1)*noSteps); |
|---|
| 132 | elements->push_back(j+(i)*noSteps); |
|---|
| 133 | } |
|---|
| 134 | polyGeom->addPrimitiveSet(elements); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | osg::StateSet* stateset = polyGeom->getOrCreateStateSet(); |
|---|
| 141 | stateset->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON); |
|---|
| 142 | stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); |
|---|
| 143 | |
|---|
| 144 | osg::Geode* geode = new osg::Geode(); |
|---|
| 145 | geode->addDrawable(polyGeom); |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | osg::Camera* camera = new osg::Camera; |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
|---|
| 152 | camera->setViewMatrix(osg::Matrix::identity()); |
|---|
| 153 | camera->setProjectionMatrixAsOrtho2D(0,1280,0,1024); |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | camera->setRenderOrder(osg::Camera::NESTED_RENDER); |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | camera->attach(osg::Camera::COLOR_BUFFER, texture); |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | camera->addChild(geode); |
|---|
| 166 | |
|---|
| 167 | distortionNode->addChild(camera); |
|---|
| 168 | } |
|---|
| 169 | return distortionNode; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | void setDomeCorrection(osgViewer::Viewer& viewer, osg::ArgumentParser& arguments) |
|---|
| 173 | { |
|---|
| 174 | |
|---|
| 175 | osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); |
|---|
| 176 | if (!wsi) |
|---|
| 177 | { |
|---|
| 178 | osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl; |
|---|
| 179 | return; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | unsigned int width, height; |
|---|
| 183 | wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height); |
|---|
| 184 | |
|---|
| 185 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| 186 | traits->x = 0; |
|---|
| 187 | traits->y = 0; |
|---|
| 188 | traits->width = width; |
|---|
| 189 | traits->height = height; |
|---|
| 190 | traits->windowDecoration = true; |
|---|
| 191 | traits->doubleBuffer = true; |
|---|
| 192 | traits->sharedContext = 0; |
|---|
| 193 | |
|---|
| 194 | osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 195 | if (!gc) |
|---|
| 196 | { |
|---|
| 197 | osg::notify(osg::NOTICE)<<"GraphicsWindow has not been created successfully."<<std::endl; |
|---|
| 198 | return; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | int center_x = width/2; |
|---|
| 203 | int center_y = height/2; |
|---|
| 204 | int camera_width = 256; |
|---|
| 205 | int camera_height = 256; |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | { |
|---|
| 209 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 210 | camera->setGraphicsContext(gc.get()); |
|---|
| 211 | camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y, camera_width, camera_height)); |
|---|
| 212 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 213 | camera->setDrawBuffer(buffer); |
|---|
| 214 | camera->setReadBuffer(buffer); |
|---|
| 215 | |
|---|
| 216 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd()); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | { |
|---|
| 221 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 222 | camera->setGraphicsContext(gc.get()); |
|---|
| 223 | camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y+camera_height, camera_width, camera_height)); |
|---|
| 224 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 225 | camera->setDrawBuffer(buffer); |
|---|
| 226 | camera->setReadBuffer(buffer); |
|---|
| 227 | |
|---|
| 228 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 1.0,0.0,0.0)); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | { |
|---|
| 233 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 234 | camera->setGraphicsContext(gc.get()); |
|---|
| 235 | camera->setViewport(new osg::Viewport(center_x-camera_width*3/2, center_y, camera_width, camera_height)); |
|---|
| 236 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 237 | camera->setDrawBuffer(buffer); |
|---|
| 238 | camera->setReadBuffer(buffer); |
|---|
| 239 | |
|---|
| 240 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-90.0f), 0.0,1.0,0.0)); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | { |
|---|
| 245 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 246 | camera->setGraphicsContext(gc.get()); |
|---|
| 247 | camera->setViewport(new osg::Viewport(center_x+camera_width/2, center_y, camera_width, camera_height)); |
|---|
| 248 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 249 | camera->setDrawBuffer(buffer); |
|---|
| 250 | camera->setReadBuffer(buffer); |
|---|
| 251 | |
|---|
| 252 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 0.0,1.0,0.0)); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | { |
|---|
| 257 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 258 | camera->setGraphicsContext(gc.get()); |
|---|
| 259 | camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y-camera_height, camera_width, camera_height)); |
|---|
| 260 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 261 | camera->setDrawBuffer(buffer); |
|---|
| 262 | camera->setReadBuffer(buffer); |
|---|
| 263 | |
|---|
| 264 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(90.0f), 1.0,0.0,0.0)); |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | { |
|---|
| 269 | osg::ref_ptr<osg::Camera> camera = new osg::Camera; |
|---|
| 270 | camera->setGraphicsContext(gc.get()); |
|---|
| 271 | camera->setViewport(new osg::Viewport(center_x-camera_width/2, center_y-2*camera_height, camera_width, camera_height)); |
|---|
| 272 | GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT; |
|---|
| 273 | camera->setDrawBuffer(buffer); |
|---|
| 274 | camera->setReadBuffer(buffer); |
|---|
| 275 | |
|---|
| 276 | viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::rotate(osg::inDegrees(-180.0f), 1.0,0.0,0.0)); |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | viewer.getCamera()->setProjectionMatrixAsPerspective(90.0f, 1.0, 1, 1000.0); |
|---|
| 280 | |
|---|
| 281 | viewer.assignSceneDataToCameras(); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | int main(int argc, char** argv) |
|---|
| 285 | { |
|---|
| 286 | |
|---|
| 287 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | osgViewer::Viewer viewer; |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | osg::Node* loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 294 | if (!loadedModel) |
|---|
| 295 | { |
|---|
| 296 | osg::notify(osg::NOTICE)<<"Please specify a model filename on the command line."<<std::endl; |
|---|
| 297 | return 1; |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | if (arguments.read("--dome")) |
|---|
| 301 | { |
|---|
| 302 | viewer.setSceneData( loadedModel ); |
|---|
| 303 | |
|---|
| 304 | setDomeCorrection(viewer, arguments); |
|---|
| 305 | |
|---|
| 306 | } |
|---|
| 307 | else |
|---|
| 308 | { |
|---|
| 309 | osg::Node* distortionNode = createDistortionSubgraph(loadedModel, viewer.getCamera()->getClearColor()); |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | viewer.setSceneData( distortionNode ); |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | return viewer.run(); |
|---|
| 316 | } |
|---|
| 317 | ; |
|---|