[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 | } |
---|
[247] | 99 | if( attr_name == "renderimplementation" ) |
---|
[149] | 100 | { |
---|
| 101 | if(attr_value=="fbo") |
---|
| 102 | renderImplementation = osg::Camera::FRAME_BUFFER_OBJECT; |
---|
| 103 | if(attr_value=="pbuffer") |
---|
| 104 | renderImplementation = osg::Camera::PIXEL_BUFFER; |
---|
| 105 | if(attr_value=="pbuffer-rtt") |
---|
| 106 | renderImplementation = osg::Camera::PIXEL_BUFFER_RTT; |
---|
| 107 | if(attr_value=="fb") |
---|
| 108 | renderImplementation = osg::Camera::FRAME_BUFFER; |
---|
| 109 | if(attr_value=="window") |
---|
| 110 | renderImplementation = osg::Camera::SEPERATE_WINDOW; |
---|
| 111 | } |
---|
| 112 | if( attr_name == "width" ) |
---|
| 113 | { |
---|
| 114 | std::stringstream sstr(attr_value); |
---|
| 115 | sstr >> tex_width; |
---|
| 116 | } |
---|
| 117 | if( attr_name == "height" ) |
---|
| 118 | { |
---|
| 119 | std::stringstream sstr(attr_value); |
---|
| 120 | sstr >> tex_height; |
---|
| 121 | } |
---|
| 122 | if( attr_name == "useshader" ) |
---|
| 123 | useShaderDistortion = (attr_value == "yes") ? true : false; |
---|
| 124 | if( attr_name == "hdr" ) |
---|
| 125 | useHDR = (attr_value == "yes") ? true : false; |
---|
| 126 | if( attr_name == "usetexturerectangle" ) |
---|
| 127 | useTextureRectangle = (attr_value == "yes") ? true : false; |
---|
[148] | 128 | |
---|
| 129 | |
---|
[149] | 130 | attr = attr->next; |
---|
| 131 | } // WHILE attrib END |
---|
| 132 | } // IF Node == distortion END |
---|
| 133 | |
---|
| 134 | // Check for distortionmap node |
---|
| 135 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "distortionmap") |
---|
| 136 | { |
---|
[150] | 137 | xmlAttr *attr = cur_node->properties; |
---|
| 138 | while ( attr ) |
---|
| 139 | { |
---|
| 140 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 141 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
| 142 | if( attr_name == "filename" ) |
---|
| 143 | { |
---|
| 144 | distortMapFileName = attr_value; |
---|
| 145 | } |
---|
| 146 | attr = attr->next; |
---|
| 147 | } |
---|
[149] | 148 | } |
---|
| 149 | |
---|
| 150 | // Check for distortionmap node |
---|
| 151 | if(cur_node->type == XML_ELEMENT_NODE && node_name == "blendmap") |
---|
| 152 | { |
---|
[150] | 153 | xmlAttr *attr = cur_node->properties; |
---|
| 154 | while ( attr ) |
---|
| 155 | { |
---|
| 156 | std::string attr_name=reinterpret_cast<const char*>(attr->name); |
---|
| 157 | std::string attr_value=reinterpret_cast<const char*>(attr->children->content); |
---|
| 158 | if( attr_name == "filename" ) |
---|
| 159 | { |
---|
| 160 | blendMapFileName = attr_value; |
---|
| 161 | } |
---|
| 162 | attr = attr->next; |
---|
| 163 | } |
---|
[149] | 164 | } |
---|
| 165 | } // FOR all nodes END |
---|
| 166 | |
---|
[151] | 167 | // clean up |
---|
| 168 | xmlFreeDoc(tmpDoc); xmlCleanupParser(); |
---|
| 169 | return true; |
---|
[149] | 170 | } // IF Config valid END |
---|
| 171 | else |
---|
[151] | 172 | { |
---|
[152] | 173 | OSG_WARN << "ERROR: visual_distortion::processXMLConfiguration() - Module configuration not found" << std::endl; |
---|
[151] | 174 | return false; |
---|
| 175 | } |
---|
[149] | 176 | |
---|
[148] | 177 | return true; |
---|
| 178 | } |
---|
| 179 | |
---|
[31] | 180 | osg::Group* visual_distortion::initialize(osg::Group* subgraph, const osg::Vec4& clearColor ) |
---|
| 181 | { |
---|
[152] | 182 | OSG_NOTIFY (osg::ALWAYS ) << "visual_distortion initialize.."; // The sentence is finished by the init result... |
---|
[31] | 183 | |
---|
[147] | 184 | // Initialize variables: |
---|
| 185 | tex_width = 2048; |
---|
| 186 | tex_height = 2048; |
---|
| 187 | renderImplementation = osg::Camera::FRAME_BUFFER_OBJECT; |
---|
| 188 | useTextureRectangle = false; |
---|
| 189 | useShaderDistortion = false; |
---|
| 190 | useHDR = false; |
---|
| 191 | distortMapFileName = "..\\resources\\distortion\\distort_distDisabled.bmp"; |
---|
| 192 | blendMapFileName = "..\\resources\\distortion\\blend_distDisabled.bmp"; |
---|
| 193 | |
---|
[149] | 194 | // Process XML configuration |
---|
[151] | 195 | if(!processXMLConfiguration()) |
---|
| 196 | return NULL; // Abort distortion initialization. |
---|
[147] | 197 | |
---|
[31] | 198 | // Add this node to the scenegraph to get updated during update traversal. |
---|
| 199 | subgraph->addChild( this ); |
---|
| 200 | |
---|
| 201 | // add the keyboard handler for toggle distortion |
---|
[147] | 202 | arguments.getApplicationUsage()->addKeyboardMouseBinding("d", "Toggle Distortion."); |
---|
[31] | 203 | kBEventHandler = new ToggleDistortionKBEventHandler( this ); |
---|
| 204 | viewer->addEventHandler( kBEventHandler ); |
---|
| 205 | |
---|
| 206 | cleanGraph = subgraph; |
---|
| 207 | distortedGraph = createPreRenderSubGraph( subgraph, clearColor ); |
---|
| 208 | |
---|
| 209 | // Create and install updateCallback (to get called for copying the main cameras view matrixes to the PRE_RENDER camera) |
---|
| 210 | // -- must be called _AFTER_ createPreRenderSubGraph() (necessary because sceneCamera is set by createPreRenderSubGraph()) |
---|
| 211 | updateCallback = new distortionUpdateCallback( viewer, sceneCamera ); |
---|
| 212 | this->setUpdateCallback( updateCallback ); |
---|
| 213 | |
---|
| 214 | // Note down state flags.. |
---|
| 215 | initialized = true; |
---|
| 216 | distortionEnabled = true; |
---|
| 217 | |
---|
| 218 | distortionEnabled = true; |
---|
| 219 | viewer->setSceneData( distortedGraph ); |
---|
| 220 | |
---|
| 221 | return distortedGraph; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | void visual_distortion::shutdown() |
---|
| 225 | { |
---|
| 226 | if(initialized) |
---|
| 227 | { |
---|
| 228 | // Remove this Node from scenegraph |
---|
| 229 | cleanGraph->removeChild( this ); |
---|
| 230 | |
---|
| 231 | // Remove update callback |
---|
| 232 | this->removeUpdateCallback( updateCallback ); |
---|
| 233 | updateCallback = NULL; |
---|
| 234 | |
---|
| 235 | OSG_NOTIFY (osg::ALWAYS ) << "visual_distortion shutdown complete." << std::endl; |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | void visual_distortion::distortionUpdateCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) |
---|
| 240 | { |
---|
| 241 | // Copy Main Camera's matrixes to the PRE_RENDER Camera. |
---|
[145] | 242 | //std::cout << "distortion updatecallback" << std::endl; |
---|
[31] | 243 | sceneCamera->setViewMatrix( viewer->getCamera()->getViewMatrix() ); |
---|
| 244 | sceneCamera->setProjectionMatrix( viewer->getCamera()->getProjectionMatrix() ); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | void visual_distortion::toggleDistortion() |
---|
| 248 | { |
---|
| 249 | // Toggle |
---|
| 250 | //// If not Distorted: enabled distortion |
---|
| 251 | if( !distortionEnabled ) |
---|
| 252 | { |
---|
| 253 | distortionEnabled = true; |
---|
| 254 | viewer->setSceneData( distortedGraph ); |
---|
| 255 | } |
---|
| 256 | else // .. otherwise disable distortion |
---|
| 257 | { |
---|
| 258 | distortionEnabled = false; |
---|
| 259 | viewer->setSceneData( cleanGraph ); |
---|
| 260 | } |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | |
---|
| 264 | osg::Group* visual_distortion::createPreRenderSubGraph(osg::Group* subgraph, const osg::Vec4& clearColor ) |
---|
| 265 | { |
---|
| 266 | if (!subgraph) return 0; |
---|
| 267 | |
---|
| 268 | // create a group to contain the flag and the pre rendering camera. |
---|
| 269 | osg::Group* parent = new osg::Group; |
---|
| 270 | |
---|
| 271 | // texture to render to and to use for rendering of flag. |
---|
| 272 | osg::Texture* texture = 0; |
---|
| 273 | if (useTextureRectangle) |
---|
| 274 | { |
---|
| 275 | osg::TextureRectangle* textureRect = new osg::TextureRectangle; |
---|
| 276 | textureRect->setTextureSize(tex_width, tex_height); |
---|
| 277 | textureRect->setInternalFormat(GL_RGBA); |
---|
| 278 | textureRect->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); |
---|
| 279 | textureRect->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); |
---|
| 280 | |
---|
| 281 | texture = textureRect; |
---|
| 282 | } |
---|
| 283 | else |
---|
| 284 | { |
---|
| 285 | osg::Texture2D* texture2D = new osg::Texture2D; |
---|
| 286 | texture2D->setTextureSize(tex_width, tex_height); |
---|
| 287 | texture2D->setInternalFormat(GL_RGBA); |
---|
| 288 | texture2D->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); |
---|
| 289 | texture2D->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); |
---|
| 290 | |
---|
| 291 | texture = texture2D; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | if (useHDR) |
---|
| 295 | { |
---|
| 296 | texture->setInternalFormat(GL_RGBA16F_ARB); |
---|
| 297 | texture->setSourceFormat(GL_RGBA); |
---|
| 298 | texture->setSourceType(GL_FLOAT); |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | // load a distortion map texture file |
---|
| 302 | if ( osgDB::fileExists( distortMapFileName ) ) |
---|
| 303 | distortMapTexture = loadTexture(distortMapFileName); |
---|
| 304 | else |
---|
| 305 | { |
---|
| 306 | 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; |
---|
| 307 | exit(-1); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | // load a blend map texture file |
---|
| 311 | if ( osgDB::fileExists( blendMapFileName ) ) |
---|
| 312 | blendMapTexture = loadTexture(blendMapFileName); |
---|
| 313 | else |
---|
| 314 | { |
---|
| 315 | 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; |
---|
| 316 | exit(-1); |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | // set up the plane to render the rendered view. |
---|
| 320 | { |
---|
| 321 | // create the quad to visualize. |
---|
| 322 | osg::Geometry* polyGeom = new osg::Geometry(); |
---|
| 323 | |
---|
| 324 | polyGeom->setSupportsDisplayList(false); |
---|
| 325 | |
---|
| 326 | osg::Vec3 origin(0.0f,0.0f,0.0f); |
---|
| 327 | osg::Vec3 xAxis(1.0f,0.0f,0.0f); |
---|
| 328 | osg::Vec3 yAxis(0.0f,1.0f,0.0f); |
---|
| 329 | osg::Vec3 zAxis(0.0f,0.0f,1.0f); |
---|
| 330 | float height = 1024.0f; |
---|
| 331 | float width = 1280.0f; |
---|
| 332 | int noSteps = 128; |
---|
| 333 | if (useShaderDistortion) |
---|
| 334 | noSteps = 2; |
---|
| 335 | |
---|
| 336 | osg::Vec3Array* vertices = new osg::Vec3Array; |
---|
| 337 | osg::Vec2Array* texcoords = new osg::Vec2Array; |
---|
| 338 | osg::Vec2Array* texcoords2 = useShaderDistortion?NULL:(new osg::Vec2Array); |
---|
| 339 | osg::Vec4Array* colors = new osg::Vec4Array; |
---|
| 340 | |
---|
| 341 | osg::Vec3 bottom = origin; |
---|
| 342 | osg::Vec3 dx = xAxis*(width/((float)(noSteps-1))); |
---|
| 343 | osg::Vec3 dy = yAxis*(height/((float)(noSteps-1))); |
---|
| 344 | |
---|
| 345 | osg::Vec2 bottom_texcoord(0.0f,0.0f); |
---|
| 346 | osg::Vec2 dx_texcoord(1.0f/(float)(noSteps-1),0.0f); |
---|
| 347 | osg::Vec2 dy_texcoord(0.0f,1.0f/(float)(noSteps-1)); |
---|
| 348 | |
---|
| 349 | osg::Vec3 cursor = bottom; |
---|
| 350 | osg::Vec2 texcoord = bottom_texcoord; |
---|
| 351 | int i,j; |
---|
| 352 | for(i=0;i<noSteps;++i) |
---|
| 353 | { |
---|
| 354 | osg::Vec3 cursor = bottom+dy*(float)i; |
---|
| 355 | osg::Vec2 texcoord = bottom_texcoord+dy_texcoord*(float)i; |
---|
| 356 | for(j=0;j<noSteps;++j) |
---|
| 357 | { |
---|
| 358 | vertices->push_back(cursor); |
---|
| 359 | if (distortMapTexture && !useShaderDistortion) |
---|
| 360 | { |
---|
| 361 | osg::Vec2 imgcoord = osg::Vec2((distortMapTexture->getImage(0)->s()-1) * texcoord.x(), |
---|
| 362 | (distortMapTexture->getImage(0)->t()-1) * texcoord.y()); |
---|
| 363 | unsigned char* pPixel = &distortMapTexture->getImage(0)->data()[(int)imgcoord.y()*distortMapTexture->getImage(0)->getRowSizeInBytes()+ |
---|
| 364 | (int)imgcoord.x()*distortMapTexture->getImage(0)->getPixelSizeInBits()/8]; |
---|
| 365 | texcoords->push_back(osg::Vec2(((float)pPixel[2] / 255.0f + (pPixel[0] % 16)) / 16.0f, |
---|
| 366 | 1.0f - ((float)pPixel[1] / 255.0f + (pPixel[0] / 16)) / 16.0f)); |
---|
| 367 | texcoords2->push_back(osg::Vec2(texcoord.x(), texcoord.y())); |
---|
| 368 | } |
---|
| 369 | else |
---|
| 370 | texcoords->push_back(osg::Vec2(texcoord.x(), texcoord.y())); |
---|
| 371 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
---|
| 372 | |
---|
| 373 | cursor += dx; |
---|
| 374 | texcoord += dx_texcoord; |
---|
| 375 | } |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | // pass the created vertex array to the points geometry object. |
---|
| 379 | polyGeom->setVertexArray(vertices); |
---|
| 380 | |
---|
| 381 | polyGeom->setColorArray(colors); |
---|
| 382 | polyGeom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
---|
| 383 | polyGeom->setTexCoordArray(0,texcoords); |
---|
| 384 | if (!useShaderDistortion) |
---|
| 385 | polyGeom->setTexCoordArray(2,texcoords2); |
---|
| 386 | |
---|
| 387 | for(i=0;i<noSteps-1;++i) |
---|
| 388 | { |
---|
| 389 | osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP); |
---|
| 390 | for(j=0;j<noSteps;++j) |
---|
| 391 | { |
---|
| 392 | elements->push_back(j+(i+1)*noSteps); |
---|
| 393 | elements->push_back(j+(i)*noSteps); |
---|
| 394 | } |
---|
| 395 | polyGeom->addPrimitiveSet(elements); |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | // new we need to add the texture to the Drawable, we do so by creating a |
---|
| 399 | // StateSet to contain the Texture StateAttribute. |
---|
| 400 | osg::StateSet* stateset = polyGeom->getOrCreateStateSet(); |
---|
| 401 | stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON); |
---|
| 402 | stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF); |
---|
| 403 | stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); |
---|
| 404 | |
---|
| 405 | osg::Geode* geode = new osg::Geode(); |
---|
| 406 | geode->addDrawable(polyGeom); |
---|
| 407 | |
---|
| 408 | if (useShaderDistortion) |
---|
| 409 | { |
---|
| 410 | // apply the distortion map texture |
---|
| 411 | if (distortMapTexture) |
---|
| 412 | geode->getOrCreateStateSet()->setTextureAttributeAndModes(1, distortMapTexture, osg::StateAttribute::ON); |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | // apply the blend map texture |
---|
| 416 | if (blendMapTexture) |
---|
| 417 | geode->getOrCreateStateSet()->setTextureAttributeAndModes(2, blendMapTexture, osg::StateAttribute::ON); |
---|
| 418 | |
---|
| 419 | // set up the camera to render the textured quad |
---|
| 420 | osg::Camera* camera = new osg::Camera; |
---|
| 421 | |
---|
| 422 | // just inherit the main cameras view |
---|
| 423 | camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
---|
| 424 | camera->setViewMatrix(osg::Matrix::identity()); |
---|
[145] | 425 | //sceneCamera->setProjectionMatrixAsOrtho2D(0,viewer->getCamera()->getViewport()->width(),0,viewer->getCamera()->getViewport()->height()); |
---|
[31] | 426 | camera->setProjectionMatrixAsOrtho2D(0,1280,0,1024); |
---|
[145] | 427 | /** \todo: use screen size dynamically */ |
---|
[31] | 428 | |
---|
| 429 | // set the camera to render before the main camera. |
---|
| 430 | camera->setRenderOrder(osg::Camera::POST_RENDER, 200); |
---|
| 431 | |
---|
| 432 | // only clear the depth buffer |
---|
| 433 | camera->setClearMask(0); |
---|
| 434 | |
---|
| 435 | // add subgraph to render |
---|
| 436 | camera->addChild(geode); |
---|
| 437 | |
---|
| 438 | if (useShaderDistortion) |
---|
| 439 | { |
---|
| 440 | // create shaders for distortion |
---|
| 441 | osg::StateSet* ss = geode->getOrCreateStateSet(); |
---|
| 442 | |
---|
| 443 | osg::Program* distortProgram = new osg::Program; |
---|
| 444 | distortProgram->setName( "distortion" ); |
---|
| 445 | osg::Shader* distortVertObj = new osg::Shader( osg::Shader::VERTEX ); |
---|
| 446 | osg::Shader* distortFragObj = new osg::Shader( osg::Shader::FRAGMENT ); |
---|
| 447 | |
---|
[147] | 448 | if (loadShaderSource( distortVertObj, "../resources/distortion/shader.vert" ) && |
---|
| 449 | loadShaderSource( distortFragObj, "../resources/distortion/shader.frag" ) && |
---|
[31] | 450 | distortMapTexture) |
---|
| 451 | { |
---|
| 452 | distortProgram->addShader( distortFragObj ); |
---|
| 453 | distortProgram->addShader( distortVertObj ); |
---|
| 454 | ss->addUniform( new osg::Uniform("textureImage", 0) ); |
---|
| 455 | ss->addUniform( new osg::Uniform("textureDistort", 1) ); |
---|
| 456 | ss->addUniform( new osg::Uniform("textureBlend", 2) ); |
---|
| 457 | ss->setAttributeAndModes(distortProgram, osg::StateAttribute::ON); |
---|
| 458 | } |
---|
| 459 | else |
---|
| 460 | { |
---|
| 461 | OSG_NOTIFY(osg::FATAL) << "##################################" << std::endl << "## Unable to activate ShaderDistortion!" << std::endl << "## ABORT" << std::endl << "##################################" << std::endl; |
---|
| 462 | exit(0); |
---|
| 463 | } |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | parent->addChild(camera); |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | // then create the camera node to do the render to texture |
---|
| 470 | { |
---|
| 471 | osg::Camera* camera = new osg::Camera; |
---|
| 472 | |
---|
| 473 | // set up the background color and clear mask. |
---|
| 474 | camera->setClearColor(clearColor); |
---|
| 475 | camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 476 | |
---|
| 477 | |
---|
| 478 | // just inherit the main cameras view |
---|
| 479 | /* ABSOLUTE_RF required to make intersections possible. |
---|
| 480 | Disadvantage of ABOLUTE_RF : the maincameras view matrix and projection |
---|
| 481 | matrix has to copied to the PRE_RENDER camera by our self. |
---|
| 482 | Therefore an update callback is installed. |
---|
| 483 | */ |
---|
| 484 | camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
---|
| 485 | camera->setProjectionMatrix(osg::Matrixd::identity()); |
---|
| 486 | camera->setViewMatrix(osg::Matrixd::identity()); |
---|
| 487 | |
---|
| 488 | // set viewport |
---|
| 489 | camera->setViewport(0,0,tex_width,tex_height); |
---|
| 490 | |
---|
| 491 | // set the camera to render before the main camera. |
---|
| 492 | camera->setRenderOrder(osg::Camera::PRE_RENDER, 0); |
---|
| 493 | |
---|
| 494 | sceneCamera = camera; |
---|
| 495 | |
---|
| 496 | // tell the camera to use OpenGL frame buffer object where supported. |
---|
| 497 | camera->setRenderTargetImplementation(renderImplementation); |
---|
| 498 | |
---|
| 499 | // attach the texture and use it as the color buffer. |
---|
| 500 | camera->attach(osg::Camera::COLOR_BUFFER, texture); // No Multisampling/Antialiasing |
---|
| 501 | //camera->attach(osg::Camera::COLOR_BUFFER, texture, 0, 0, false, 4, 4); // 4x Multisampling/Antialiasing |
---|
| 502 | |
---|
| 503 | // add subgraph to render |
---|
| 504 | camera->addChild(subgraph); |
---|
| 505 | |
---|
| 506 | parent->addChild(camera); |
---|
| 507 | } |
---|
| 508 | return parent; |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | |
---|
| 512 | bool visual_distortion::loadShaderSource( osg::Shader* shader, const std::string& fileName ) |
---|
| 513 | { |
---|
| 514 | std::string foundFileName = osgDB::findDataFile(fileName); |
---|
| 515 | if (foundFileName.length() != 0 ) |
---|
| 516 | { |
---|
| 517 | return shader->loadShaderSourceFromFile( foundFileName.c_str() ); |
---|
| 518 | } |
---|
| 519 | |
---|
| 520 | OSG_NOTIFY(osg::WARN) << "File \"" << fileName << "\" not found." << std::endl; |
---|
| 521 | |
---|
| 522 | return false; |
---|
| 523 | } |
---|
| 524 | |
---|
| 525 | osg::Texture* visual_distortion::loadTexture( const std::string& fileName ) |
---|
| 526 | { |
---|
| 527 | std::string foundFileName = osgDB::findDataFile(fileName); |
---|
| 528 | if (foundFileName.length() != 0 ) |
---|
| 529 | { |
---|
| 530 | // load distortion map texture file |
---|
| 531 | osg::Image* image = osgDB::readImageFile(foundFileName); |
---|
| 532 | if (image) |
---|
| 533 | { |
---|
| 534 | osg::Texture2D* texture = new osg::Texture2D; |
---|
| 535 | texture->setImage(image); |
---|
| 536 | texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST); |
---|
| 537 | texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST); |
---|
| 538 | texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::CLAMP_TO_EDGE); |
---|
| 539 | texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::CLAMP_TO_EDGE); |
---|
| 540 | return texture; |
---|
| 541 | } |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | OSG_NOTIFY(osg::WARN) << "File \"" << fileName << "\" not found." << std::endl; |
---|
| 545 | |
---|
| 546 | return NULL; |
---|
| 547 | } |
---|