[221] | 1 | /* -*-c++-*- osgVisual - Copyright (C) 2009-2011 Torben Dannhauer |
---|
[31] | 2 | * |
---|
| 3 | * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under |
---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
---|
| 7 | * |
---|
| 8 | * osgVisual requires for some proprietary modules a license from the correspondig manufacturer. |
---|
| 9 | * You have to aquire licenses for all used proprietary modules. |
---|
| 10 | * |
---|
| 11 | * This library is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | * OpenSceneGraph Public License for more details. |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | #include <visual_distortion.h> |
---|
| 18 | |
---|
| 19 | using namespace osgVisual; |
---|
| 20 | |
---|
[145] | 21 | visual_distortion::visual_distortion(osgViewer::Viewer* viewer_, osg::ArgumentParser& arguments_, std::string configFileName) : viewer(viewer_), arguments(arguments_) |
---|
[31] | 22 | { |
---|
| 23 | OSG_NOTIFY (osg::ALWAYS ) << "visual_distortion instantiated." << std::endl; |
---|
| 24 | |
---|
[148] | 25 | this->configFileName = configFileName; |
---|
[31] | 26 | initialized = false; |
---|
| 27 | distortionEnabled = false; |
---|
| 28 | parser = new CameraConfigParser(); |
---|
| 29 | |
---|
| 30 | distortedGraph = NULL; |
---|
| 31 | cleanGraph = NULL; |
---|
| 32 | |
---|
| 33 | distortMapTexture = NULL; |
---|
| 34 | blendMapTexture = NULL; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | visual_distortion::~visual_distortion(void) |
---|
| 38 | { |
---|
| 39 | OSG_NOTIFY (osg::ALWAYS ) << "visual_distortion destroyed." << std::endl; |
---|
| 40 | } |
---|
| 41 | |
---|
[148] | 42 | bool visual_distortion::processXMLConfiguration() |
---|
| 43 | { |
---|
| 44 | // Init XML |
---|
| 45 | xmlDoc* tmpDoc; |
---|
[152] | 46 | bool disabled; |
---|
| 47 | xmlNode* config = util::getModuleXMLConfig( configFileName, "distortion", tmpDoc, disabled ); |
---|
| 48 | |
---|
| 49 | if( disabled) |
---|
| 50 | { |
---|
| 51 | OSG_NOTIFY( osg::ALWAYS ) << "..disabled by XML configuration file." << std::endl; |
---|
| 52 | return false; |
---|
| 53 | } |
---|
[149] | 54 | |
---|
| 55 | // extract configuration values |
---|
[148] | 56 | if(config) |
---|
| 57 | { |
---|
[152] | 58 | OSG_NOTIFY( osg::ALWAYS ) << "..using distortion." << std::endl; |
---|
[151] | 59 | |
---|
[149] | 60 | xmlNode* a_node = config->children; |
---|
[148] | 61 | |
---|
[149] | 62 | for (xmlNode *cur_node = a_node; cur_node; cur_node = cur_node->next) |
---|
| 63 | { |
---|
| 64 | std::string node_name=reinterpret_cast<const char*>(cur_node->name); |
---|
[151] | 65 | //OSG_ALWAYS << "----visual_distortion::processXMLConfiguration() - node type="<< cur_node->type <<", name=" << cur_node->name << std::endl; |
---|
[148] | 66 | |
---|
[149] | 67 | // Check for distortion node |
---|
| 68 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "distortion") |
---|
| 69 | { |
---|
| 70 | xmlAttr *attr = cur_node->properties; |
---|
| 71 | while ( attr ) |
---|
| 72 | { |
---|
| 73 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 74 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
| 75 | if( attr_name == "channelname" ) |
---|
[150] | 76 | { |
---|
| 77 | std::string channelname = attr_value; |
---|
[151] | 78 | OSG_NOTIFY(osg::ALWAYS) << "visual_distortion: Channelname:" << channelname << std::endl; |
---|
[150] | 79 | |
---|
| 80 | std::string pre_cfg("..\\resources\\distortion\\view_"); |
---|
| 81 | std::string pre_distortion("..\\resources\\distortion\\distort_"); |
---|
| 82 | std::string pre_blend("..\\resources\\distortion\\blend_"); |
---|
| 83 | std::string post_cfg(".cfg"); |
---|
| 84 | std::string post_distortion(".bmp"); |
---|
| 85 | std::string post_blend(".bmp"); |
---|
| 86 | |
---|
| 87 | // load channel config |
---|
| 88 | parser->parseConfigFile((pre_cfg+channelname+post_cfg).data()); |
---|
| 89 | // load channel blendmap |
---|
| 90 | blendMapFileName = (pre_blend+channelname+post_blend).data(); |
---|
| 91 | // load channel distortionmap |
---|
| 92 | distortMapFileName = (pre_distortion+channelname+post_distortion).data(); |
---|
| 93 | // set channel frustum |
---|
| 94 | if( parser->isConfigParsed()) |
---|
| 95 | viewer->getCamera()->setProjectionMatrixAsFrustum((parser->getFrustumDataset())[0], (parser->getFrustumDataset())[1], (parser->getFrustumDataset())[2], (parser->getFrustumDataset())[3], (parser->getFrustumDataset())[4], (parser->getFrustumDataset())[5]); |
---|
| 96 | else |
---|
| 97 | OSG_NOTIFY(osg::WARN) << "WARNING: Unable to parse Frustum values from '" << pre_cfg<<channelname<<post_cfg << "' -- continue without valid frustum values." << std::endl; |
---|
| 98 | } |
---|
[305] | 99 | if( attr_name == "width" ) |
---|
[149] | 100 | { |
---|
| 101 | std::stringstream sstr(attr_value); |
---|
| 102 | sstr >> tex_width; |
---|
| 103 | } |
---|
| 104 | if( attr_name == "height" ) |
---|
| 105 | { |
---|
| 106 | std::stringstream sstr(attr_value); |
---|
| 107 | sstr >> tex_height; |
---|
| 108 | } |
---|
| 109 | if( attr_name == "useshader" ) |
---|
| 110 | useShaderDistortion = (attr_value == "yes") ? true : false; |
---|
| 111 | if( attr_name == "hdr" ) |
---|
| 112 | useHDR = (attr_value == "yes") ? true : false; |
---|
| 113 | if( attr_name == "usetexturerectangle" ) |
---|
| 114 | useTextureRectangle = (attr_value == "yes") ? true : false; |
---|
[148] | 115 | |
---|
| 116 | |
---|
[149] | 117 | attr = attr->next; |
---|
| 118 | } // WHILE attrib END |
---|
| 119 | } // IF Node == distortion END |
---|
| 120 | |
---|
| 121 | // Check for distortionmap node |
---|
| 122 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "distortionmap") |
---|
| 123 | { |
---|
[150] | 124 | xmlAttr *attr = cur_node->properties; |
---|
| 125 | while ( attr ) |
---|
| 126 | { |
---|
| 127 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 128 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
| 129 | if( attr_name == "filename" ) |
---|
| 130 | { |
---|
| 131 | distortMapFileName = attr_value; |
---|
| 132 | } |
---|
| 133 | attr = attr->next; |
---|
| 134 | } |
---|
[149] | 135 | } |
---|
| 136 | |
---|
| 137 | // Check for distortionmap node |
---|
| 138 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "blendmap") |
---|
| 139 | { |
---|
[150] | 140 | xmlAttr *attr = cur_node->properties; |
---|
| 141 | while ( attr ) |
---|
| 142 | { |
---|
| 143 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 144 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
| 145 | if( attr_name == "filename" ) |
---|
| 146 | { |
---|
| 147 | blendMapFileName = attr_value; |
---|
| 148 | } |
---|
| 149 | attr = attr->next; |
---|
| 150 | } |
---|
[149] | 151 | } |
---|
| 152 | } // FOR all nodes END |
---|
| 153 | |
---|
[151] | 154 | // clean up |
---|
| 155 | xmlFreeDoc(tmpDoc); xmlCleanupParser(); |
---|
| 156 | return true; |
---|
[149] | 157 | } // IF Config valid END |
---|
| 158 | else |
---|
[151] | 159 | { |
---|
[152] | 160 | OSG_WARN << "ERROR: visual_distortion::processXMLConfiguration() - Module configuration not found" << std::endl; |
---|
[151] | 161 | return false; |
---|
| 162 | } |
---|
[149] | 163 | |
---|
[148] | 164 | return true; |
---|
| 165 | } |
---|
| 166 | |
---|
[31] | 167 | osg::Group* visual_distortion::initialize(osg::Group* subgraph, const osg::Vec4& clearColor ) |
---|
| 168 | { |
---|
[152] | 169 | OSG_NOTIFY (osg::ALWAYS ) << "visual_distortion initialize.."; // The sentence is finished by the init result... |
---|
[31] | 170 | |
---|
[147] | 171 | // Initialize variables: |
---|
| 172 | tex_width = 2048; |
---|
| 173 | tex_height = 2048; |
---|
| 174 | useShaderDistortion = false; |
---|
| 175 | useHDR = false; |
---|
| 176 | distortMapFileName = "..\\resources\\distortion\\distort_distDisabled.bmp"; |
---|
| 177 | blendMapFileName = "..\\resources\\distortion\\blend_distDisabled.bmp"; |
---|
| 178 | |
---|
[149] | 179 | // Process XML configuration |
---|
[151] | 180 | if(!processXMLConfiguration()) |
---|
| 181 | return NULL; // Abort distortion initialization. |
---|
[147] | 182 | |
---|
[31] | 183 | // Add this node to the scenegraph to get updated during update traversal. |
---|
| 184 | subgraph->addChild( this ); |
---|
| 185 | |
---|
| 186 | // add the keyboard handler for toggle distortion |
---|
[147] | 187 | arguments.getApplicationUsage()->addKeyboardMouseBinding("d", "Toggle Distortion."); |
---|
[31] | 188 | kBEventHandler = new ToggleDistortionKBEventHandler( this ); |
---|
| 189 | viewer->addEventHandler( kBEventHandler ); |
---|
| 190 | |
---|
| 191 | cleanGraph = subgraph; |
---|
| 192 | distortedGraph = createPreRenderSubGraph( subgraph, clearColor ); |
---|
| 193 | |
---|
| 194 | // Create and install updateCallback (to get called for copying the main cameras view matrixes to the PRE_RENDER camera) |
---|
| 195 | // -- must be called _AFTER_ createPreRenderSubGraph() (necessary because sceneCamera is set by createPreRenderSubGraph()) |
---|
| 196 | updateCallback = new distortionUpdateCallback( viewer, sceneCamera ); |
---|
| 197 | this->setUpdateCallback( updateCallback ); |
---|
| 198 | |
---|
| 199 | // Note down state flags.. |
---|
| 200 | initialized = true; |
---|
| 201 | distortionEnabled = true; |
---|
| 202 | |
---|
| 203 | distortionEnabled = true; |
---|
| 204 | viewer->setSceneData( distortedGraph ); |
---|
| 205 | |
---|
| 206 | return distortedGraph; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | void visual_distortion::shutdown() |
---|
| 210 | { |
---|
| 211 | if(initialized) |
---|
| 212 | { |
---|
| 213 | // Remove this Node from scenegraph |
---|
| 214 | cleanGraph->removeChild( this ); |
---|
| 215 | |
---|
| 216 | // Remove update callback |
---|
| 217 | this->removeUpdateCallback( updateCallback ); |
---|
| 218 | updateCallback = NULL; |
---|
| 219 | |
---|
| 220 | OSG_NOTIFY (osg::ALWAYS ) << "visual_distortion shutdown complete." << std::endl; |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | void visual_distortion::distortionUpdateCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) |
---|
| 225 | { |
---|
| 226 | // Copy Main Camera's matrixes to the PRE_RENDER Camera. |
---|
[145] | 227 | //std::cout << "distortion updatecallback" << std::endl; |
---|
[31] | 228 | sceneCamera->setViewMatrix( viewer->getCamera()->getViewMatrix() ); |
---|
| 229 | sceneCamera->setProjectionMatrix( viewer->getCamera()->getProjectionMatrix() ); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | void visual_distortion::toggleDistortion() |
---|
| 233 | { |
---|
| 234 | // Toggle |
---|
| 235 | //// If not Distorted: enabled distortion |
---|
| 236 | if( !distortionEnabled ) |
---|
| 237 | { |
---|
| 238 | distortionEnabled = true; |
---|
| 239 | viewer->setSceneData( distortedGraph ); |
---|
| 240 | } |
---|
| 241 | else // .. otherwise disable distortion |
---|
| 242 | { |
---|
| 243 | distortionEnabled = false; |
---|
| 244 | viewer->setSceneData( cleanGraph ); |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | osg::Group* visual_distortion::createPreRenderSubGraph(osg::Group* subgraph, const osg::Vec4& clearColor ) |
---|
| 250 | { |
---|
| 251 | if (!subgraph) return 0; |
---|
| 252 | |
---|
| 253 | // create a group to contain the flag and the pre rendering camera. |
---|
| 254 | osg::Group* parent = new osg::Group; |
---|
| 255 | |
---|
| 256 | // texture to render to and to use for rendering of flag. |
---|
[305] | 257 | //osg::Texture* texture = 0; |
---|
| 258 | osg::Texture2D* texture = new osg::Texture2D; |
---|
| 259 | texture->setTextureSize(tex_width, tex_height); |
---|
| 260 | texture->setInternalFormat(GL_RGBA); |
---|
| 261 | texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); |
---|
| 262 | texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); |
---|
| 263 | |
---|
| 264 | //texture = texture2D; |
---|
[31] | 265 | |
---|
| 266 | if (useHDR) |
---|
| 267 | { |
---|
| 268 | texture->setInternalFormat(GL_RGBA16F_ARB); |
---|
| 269 | texture->setSourceFormat(GL_RGBA); |
---|
| 270 | texture->setSourceType(GL_FLOAT); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | // load a distortion map texture file |
---|
| 274 | if ( osgDB::fileExists( distortMapFileName ) ) |
---|
| 275 | distortMapTexture = loadTexture(distortMapFileName); |
---|
| 276 | else |
---|
| 277 | { |
---|
| 278 | OSG_NOTIFY(osg::FATAL) << "ERROR: Distortionmap file'" << distortMapFileName << "' not found! Please change the channelname, filename or copy the desired file to the applications root folder!" << std::endl; |
---|
| 279 | exit(-1); |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | // load a blend map texture file |
---|
| 283 | if ( osgDB::fileExists( blendMapFileName ) ) |
---|
| 284 | blendMapTexture = loadTexture(blendMapFileName); |
---|
| 285 | else |
---|
| 286 | { |
---|
| 287 | OSG_NOTIFY(osg::FATAL) << "ERROR: Blendmap file'" << blendMapFileName << "' not found! Please change the channelname, filename or copy the desired file to the applications root folder!" << std::endl; |
---|
| 288 | exit(-1); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | // set up the plane to render the rendered view. |
---|
| 292 | { |
---|
| 293 | // create the quad to visualize. |
---|
| 294 | osg::Geometry* polyGeom = new osg::Geometry(); |
---|
| 295 | |
---|
| 296 | polyGeom->setSupportsDisplayList(false); |
---|
| 297 | |
---|
| 298 | osg::Vec3 origin(0.0f,0.0f,0.0f); |
---|
| 299 | osg::Vec3 xAxis(1.0f,0.0f,0.0f); |
---|
| 300 | osg::Vec3 yAxis(0.0f,1.0f,0.0f); |
---|
| 301 | osg::Vec3 zAxis(0.0f,0.0f,1.0f); |
---|
[305] | 302 | float width = 1600.0f; |
---|
| 303 | float height = 900.0f; |
---|
[31] | 304 | int noSteps = 128; |
---|
| 305 | if (useShaderDistortion) |
---|
| 306 | noSteps = 2; |
---|
| 307 | |
---|
| 308 | osg::Vec3Array* vertices = new osg::Vec3Array; |
---|
| 309 | osg::Vec2Array* texcoords = new osg::Vec2Array; |
---|
| 310 | osg::Vec2Array* texcoords2 = useShaderDistortion?NULL:(new osg::Vec2Array); |
---|
| 311 | osg::Vec4Array* colors = new osg::Vec4Array; |
---|
| 312 | |
---|
| 313 | osg::Vec3 bottom = origin; |
---|
| 314 | osg::Vec3 dx = xAxis*(width/((float)(noSteps-1))); |
---|
| 315 | osg::Vec3 dy = yAxis*(height/((float)(noSteps-1))); |
---|
| 316 | |
---|
| 317 | osg::Vec2 bottom_texcoord(0.0f,0.0f); |
---|
| 318 | osg::Vec2 dx_texcoord(1.0f/(float)(noSteps-1),0.0f); |
---|
| 319 | osg::Vec2 dy_texcoord(0.0f,1.0f/(float)(noSteps-1)); |
---|
| 320 | |
---|
| 321 | osg::Vec3 cursor = bottom; |
---|
| 322 | osg::Vec2 texcoord = bottom_texcoord; |
---|
| 323 | int i,j; |
---|
| 324 | for(i=0;i<noSteps;++i) |
---|
| 325 | { |
---|
| 326 | osg::Vec3 cursor = bottom+dy*(float)i; |
---|
| 327 | osg::Vec2 texcoord = bottom_texcoord+dy_texcoord*(float)i; |
---|
| 328 | for(j=0;j<noSteps;++j) |
---|
| 329 | { |
---|
| 330 | vertices->push_back(cursor); |
---|
| 331 | if (distortMapTexture && !useShaderDistortion) |
---|
| 332 | { |
---|
| 333 | osg::Vec2 imgcoord = osg::Vec2((distortMapTexture->getImage(0)->s()-1) * texcoord.x(), |
---|
| 334 | (distortMapTexture->getImage(0)->t()-1) * texcoord.y()); |
---|
| 335 | unsigned char* pPixel = &distortMapTexture->getImage(0)->data()[(int)imgcoord.y()*distortMapTexture->getImage(0)->getRowSizeInBytes()+ |
---|
| 336 | (int)imgcoord.x()*distortMapTexture->getImage(0)->getPixelSizeInBits()/8]; |
---|
| 337 | texcoords->push_back(osg::Vec2(((float)pPixel[2] / 255.0f + (pPixel[0] % 16)) / 16.0f, |
---|
| 338 | 1.0f - ((float)pPixel[1] / 255.0f + (pPixel[0] / 16)) / 16.0f)); |
---|
| 339 | texcoords2->push_back(osg::Vec2(texcoord.x(), texcoord.y())); |
---|
| 340 | } |
---|
| 341 | else |
---|
| 342 | texcoords->push_back(osg::Vec2(texcoord.x(), texcoord.y())); |
---|
| 343 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
---|
| 344 | |
---|
| 345 | cursor += dx; |
---|
| 346 | texcoord += dx_texcoord; |
---|
| 347 | } |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | // pass the created vertex array to the points geometry object. |
---|
| 351 | polyGeom->setVertexArray(vertices); |
---|
| 352 | |
---|
| 353 | polyGeom->setColorArray(colors); |
---|
| 354 | polyGeom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
---|
| 355 | polyGeom->setTexCoordArray(0,texcoords); |
---|
| 356 | if (!useShaderDistortion) |
---|
| 357 | polyGeom->setTexCoordArray(2,texcoords2); |
---|
| 358 | |
---|
| 359 | for(i=0;i<noSteps-1;++i) |
---|
| 360 | { |
---|
| 361 | osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP); |
---|
| 362 | for(j=0;j<noSteps;++j) |
---|
| 363 | { |
---|
| 364 | elements->push_back(j+(i+1)*noSteps); |
---|
| 365 | elements->push_back(j+(i)*noSteps); |
---|
| 366 | } |
---|
| 367 | polyGeom->addPrimitiveSet(elements); |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | // new we need to add the texture to the Drawable, we do so by creating a |
---|
| 371 | // StateSet to contain the Texture StateAttribute. |
---|
| 372 | osg::StateSet* stateset = polyGeom->getOrCreateStateSet(); |
---|
| 373 | stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON); |
---|
| 374 | stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF); |
---|
| 375 | stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); |
---|
| 376 | |
---|
| 377 | osg::Geode* geode = new osg::Geode(); |
---|
| 378 | geode->addDrawable(polyGeom); |
---|
| 379 | |
---|
| 380 | if (useShaderDistortion) |
---|
| 381 | { |
---|
| 382 | // apply the distortion map texture |
---|
| 383 | if (distortMapTexture) |
---|
| 384 | geode->getOrCreateStateSet()->setTextureAttributeAndModes(1, distortMapTexture, osg::StateAttribute::ON); |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | // apply the blend map texture |
---|
| 388 | if (blendMapTexture) |
---|
| 389 | geode->getOrCreateStateSet()->setTextureAttributeAndModes(2, blendMapTexture, osg::StateAttribute::ON); |
---|
| 390 | |
---|
| 391 | // set up the camera to render the textured quad |
---|
| 392 | osg::Camera* camera = new osg::Camera; |
---|
| 393 | |
---|
| 394 | // just inherit the main cameras view |
---|
| 395 | camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
---|
| 396 | camera->setViewMatrix(osg::Matrix::identity()); |
---|
[305] | 397 | camera->setProjectionMatrixAsOrtho2D(0,1600,0,900); /** \todo: use screen size dynamically */ |
---|
| 398 | |
---|
[31] | 399 | // set the camera to render before the main camera. |
---|
| 400 | camera->setRenderOrder(osg::Camera::POST_RENDER, 200); |
---|
[305] | 401 | |
---|
[31] | 402 | // only clear the depth buffer |
---|
| 403 | camera->setClearMask(0); |
---|
| 404 | |
---|
| 405 | // add subgraph to render |
---|
| 406 | camera->addChild(geode); |
---|
| 407 | |
---|
| 408 | if (useShaderDistortion) |
---|
| 409 | { |
---|
| 410 | // create shaders for distortion |
---|
| 411 | osg::StateSet* ss = geode->getOrCreateStateSet(); |
---|
| 412 | |
---|
| 413 | osg::Program* distortProgram = new osg::Program; |
---|
| 414 | distortProgram->setName( "distortion" ); |
---|
| 415 | osg::Shader* distortVertObj = new osg::Shader( osg::Shader::VERTEX ); |
---|
| 416 | osg::Shader* distortFragObj = new osg::Shader( osg::Shader::FRAGMENT ); |
---|
| 417 | |
---|
[147] | 418 | if (loadShaderSource( distortVertObj, "../resources/distortion/shader.vert" ) && |
---|
| 419 | loadShaderSource( distortFragObj, "../resources/distortion/shader.frag" ) && |
---|
[31] | 420 | distortMapTexture) |
---|
| 421 | { |
---|
| 422 | distortProgram->addShader( distortFragObj ); |
---|
| 423 | distortProgram->addShader( distortVertObj ); |
---|
| 424 | ss->addUniform( new osg::Uniform("textureImage", 0) ); |
---|
| 425 | ss->addUniform( new osg::Uniform("textureDistort", 1) ); |
---|
| 426 | ss->addUniform( new osg::Uniform("textureBlend", 2) ); |
---|
| 427 | ss->setAttributeAndModes(distortProgram, osg::StateAttribute::ON); |
---|
| 428 | } |
---|
| 429 | else |
---|
| 430 | { |
---|
| 431 | OSG_NOTIFY(osg::FATAL) << "##################################" << std::endl << "## Unable to activate ShaderDistortion!" << std::endl << "## ABORT" << std::endl << "##################################" << std::endl; |
---|
| 432 | exit(0); |
---|
| 433 | } |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | parent->addChild(camera); |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | // then create the camera node to do the render to texture |
---|
| 440 | { |
---|
| 441 | osg::Camera* camera = new osg::Camera; |
---|
| 442 | |
---|
| 443 | // set up the background color and clear mask. |
---|
| 444 | camera->setClearColor(clearColor); |
---|
| 445 | camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 446 | |
---|
| 447 | |
---|
| 448 | // just inherit the main cameras view |
---|
| 449 | /* ABSOLUTE_RF required to make intersections possible. |
---|
| 450 | Disadvantage of ABOLUTE_RF : the maincameras view matrix and projection |
---|
| 451 | matrix has to copied to the PRE_RENDER camera by our self. |
---|
| 452 | Therefore an update callback is installed. |
---|
| 453 | */ |
---|
| 454 | camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
---|
| 455 | camera->setProjectionMatrix(osg::Matrixd::identity()); |
---|
| 456 | camera->setViewMatrix(osg::Matrixd::identity()); |
---|
| 457 | |
---|
| 458 | // set viewport |
---|
| 459 | camera->setViewport(0,0,tex_width,tex_height); |
---|
| 460 | |
---|
| 461 | // set the camera to render before the main camera. |
---|
| 462 | camera->setRenderOrder(osg::Camera::PRE_RENDER, 0); |
---|
| 463 | |
---|
| 464 | sceneCamera = camera; |
---|
| 465 | |
---|
| 466 | // tell the camera to use OpenGL frame buffer object where supported. |
---|
[305] | 467 | camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); |
---|
[31] | 468 | |
---|
| 469 | // attach the texture and use it as the color buffer. |
---|
[305] | 470 | //camera->attach(osg::Camera::COLOR_BUFFER, texture); // No Multisampling/Antialiasing |
---|
| 471 | camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, 0, false, 4, 4); // 4x Multisampling/Antialiasing |
---|
[31] | 472 | |
---|
| 473 | // add subgraph to render |
---|
| 474 | camera->addChild(subgraph); |
---|
| 475 | |
---|
| 476 | parent->addChild(camera); |
---|
| 477 | } |
---|
| 478 | return parent; |
---|
| 479 | } |
---|
| 480 | |
---|
| 481 | |
---|
| 482 | bool visual_distortion::loadShaderSource( osg::Shader* shader, const std::string& fileName ) |
---|
| 483 | { |
---|
| 484 | std::string foundFileName = osgDB::findDataFile(fileName); |
---|
| 485 | if (foundFileName.length() != 0 ) |
---|
| 486 | { |
---|
| 487 | return shader->loadShaderSourceFromFile( foundFileName.c_str() ); |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | OSG_NOTIFY(osg::WARN) << "File \"" << fileName << "\" not found." << std::endl; |
---|
| 491 | |
---|
| 492 | return false; |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | osg::Texture* visual_distortion::loadTexture( const std::string& fileName ) |
---|
| 496 | { |
---|
| 497 | std::string foundFileName = osgDB::findDataFile(fileName); |
---|
| 498 | if (foundFileName.length() != 0 ) |
---|
| 499 | { |
---|
| 500 | // load distortion map texture file |
---|
| 501 | osg::Image* image = osgDB::readImageFile(foundFileName); |
---|
| 502 | if (image) |
---|
| 503 | { |
---|
| 504 | osg::Texture2D* texture = new osg::Texture2D; |
---|
| 505 | texture->setImage(image); |
---|
| 506 | texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST); |
---|
| 507 | texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST); |
---|
| 508 | texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP_TO_EDGE); |
---|
| 509 | texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP_TO_EDGE); |
---|
| 510 | return texture; |
---|
| 511 | } |
---|
| 512 | } |
---|
| 513 | |
---|
| 514 | OSG_NOTIFY(osg::WARN) << "File \"" << fileName << "\" not found." << std::endl; |
---|
| 515 | |
---|
| 516 | return NULL; |
---|
| 517 | } |
---|