- Timestamp:
- 03/21/12 18:36:20 (14 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgParticle/PrecipitationEffect.cpp
r13022 r13041 1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 2 2 * 3 * This library is open source and may be redistributed and/or modified under 4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 3 * This library is open source and may be redistributed and/or modified under 4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 5 5 * (at your option) any later version. The full license is in LICENSE file 6 6 * included with this distribution, and on the openscenegraph.org website. 7 * 7 * 8 8 * This library is distributed in the hope that it will be useful, 9 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 11 * OpenSceneGraph Public License for more details. 12 12 */ … … 40 40 { 41 41 setNumChildrenRequiringUpdateTraversal(1); 42 42 43 43 setUpGeometries(1024); 44 44 … … 82 82 _nearTransition = 25.f; 83 83 _farTransition = 100.0f - 60.0f*sqrtf(intensity); 84 84 85 85 if (!_fog) _fog = new osg::Fog; 86 86 … … 90 90 91 91 _useFarLineSegments = false; 92 92 93 93 _dirty = true; 94 94 … … 106 106 _nearTransition = 25.f; 107 107 _farTransition = 100.0f - 60.0f*sqrtf(intensity); 108 108 109 109 if (!_fog) _fog = new osg::Fog; 110 110 … … 114 114 115 115 _useFarLineSegments = false; 116 116 117 117 _dirty = true; 118 118 … … 122 122 void PrecipitationEffect::compileGLObjects(osg::RenderInfo& renderInfo) const 123 123 { 124 if (_quadGeometry.valid()) 124 if (_quadGeometry.valid()) 125 125 { 126 126 _quadGeometry->compileGLObjects(renderInfo); … … 128 128 } 129 129 130 if (_lineGeometry.valid()) 130 if (_lineGeometry.valid()) 131 131 { 132 132 _lineGeometry->compileGLObjects(renderInfo); … … 134 134 } 135 135 136 if (_pointGeometry.valid()) 136 if (_pointGeometry.valid()) 137 137 { 138 138 _pointGeometry->compileGLObjects(renderInfo); … … 147 147 { 148 148 if (_dirty) update(); 149 149 150 150 if (nv.getFrameStamp()) 151 151 { … … 160 160 return; 161 161 } 162 162 163 163 if (nv.getVisitorType() == osg::NodeVisitor::NODE_VISITOR) 164 164 { … … 173 173 } 174 174 } 175 175 176 176 return; 177 177 } 178 178 179 179 180 180 if (nv.getVisitorType() != osg::NodeVisitor::CULL_VISITOR) … … 190 190 191 191 ViewIdentifier viewIndentifier(cv, nv.getNodePath()); 192 192 193 193 { 194 194 PrecipitationDrawableSet* precipitationDrawableSet = 0; 195 195 196 196 { 197 197 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); 198 198 precipitationDrawableSet = &(_viewDrawableMap[viewIndentifier]); 199 199 200 200 if (!precipitationDrawableSet->_quadPrecipitationDrawable) 201 201 { … … 219 219 } 220 220 } 221 221 222 222 cull(*precipitationDrawableSet, cv); 223 223 224 224 cv->pushStateSet(_stateset.get()); 225 225 float depth = 0.0f; … … 228 228 { 229 229 cv->pushStateSet(precipitationDrawableSet->_quadPrecipitationDrawable->getStateSet()); 230 cv->addDrawableAndDepth(precipitationDrawableSet->_quadPrecipitationDrawable.get(),cv->getModelViewMatrix(),depth); 230 cv->addDrawableAndDepth(precipitationDrawableSet->_quadPrecipitationDrawable.get(),cv->getModelViewMatrix(),depth); 231 231 cv->popStateSet(); 232 232 } … … 235 235 { 236 236 cv->pushStateSet(precipitationDrawableSet->_linePrecipitationDrawable->getStateSet()); 237 cv->addDrawableAndDepth(precipitationDrawableSet->_linePrecipitationDrawable.get(),cv->getModelViewMatrix(),depth); 237 cv->addDrawableAndDepth(precipitationDrawableSet->_linePrecipitationDrawable.get(),cv->getModelViewMatrix(),depth); 238 238 cv->popStateSet(); 239 239 } … … 242 242 { 243 243 cv->pushStateSet(precipitationDrawableSet->_pointPrecipitationDrawable->getStateSet()); 244 cv->addDrawableAndDepth(precipitationDrawableSet->_pointPrecipitationDrawable.get(),cv->getModelViewMatrix(),depth); 244 cv->addDrawableAndDepth(precipitationDrawableSet->_pointPrecipitationDrawable.get(),cv->getModelViewMatrix(),depth); 245 245 cv->popStateSet(); 246 246 } … … 271 271 _inverse_dv.set(0.0f, 1.0f/length_v, 0.0f); 272 272 _inverse_dw.set(0.0f, 0.0f, 1.0f/length_w); 273 273 274 274 OSG_INFO<<"Cell size X="<<length_u<<std::endl; 275 275 OSG_INFO<<"Cell size Y="<<length_v<<std::endl; 276 276 OSG_INFO<<"Cell size Z="<<length_w<<std::endl; 277 277 278 278 279 279 { 280 280 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); 281 281 _viewDrawableMap.clear(); 282 } 282 } 283 283 284 284 // set up state/ … … 309 309 } 310 310 else _particleColorUniform->set(_particleColor); 311 311 312 312 if (!_particleSizeUniform) 313 313 { … … 318 318 319 319 } 320 321 } 322 323 void PrecipitationEffect::createGeometry(unsigned int numParticles, 324 osg::Geometry* quad_geometry, 320 321 } 322 323 void PrecipitationEffect::createGeometry(unsigned int numParticles, 324 osg::Geometry* quad_geometry, 325 325 osg::Geometry* line_geometry, 326 326 osg::Geometry* point_geometry) … … 331 331 osg::Vec2 offset01(0.0f,1.0f); 332 332 osg::Vec2 offset11(1.0f,1.0f); 333 333 334 334 osg::Vec2 offset0(0.5f,0.0f); 335 335 osg::Vec2 offset1(0.5f,1.0f); … … 344 344 { 345 345 quad_geometry->setName("quad"); 346 346 347 347 quad_vertices = new osg::Vec3Array(numParticles*4); 348 348 quad_offsets = new osg::Vec2Array(numParticles*4); … … 384 384 { 385 385 osg::Vec3 pos( random(0.0f, 1.0f), random(0.0f, 1.0f), random(0.0f, 1.0f)); 386 386 387 387 // quad particles 388 388 if (quad_vertices) … … 397 397 (*quad_offsets)[i*4+3] = offset10; 398 398 } 399 399 400 400 // line particles 401 401 if (line_vertices) … … 406 406 (*line_offsets)[i*2+1] = offset1; 407 407 } 408 408 409 409 // point particles 410 410 if (point_vertices) … … 421 421 unsigned int lineRenderBin = 12; 422 422 unsigned int pointRenderBin = 11; 423 424 423 424 425 425 OSG_INFO<<"PrecipitationEffect::setUpGeometries("<<numParticles<<")"<<std::endl; 426 426 … … 440 440 needGeometryRebuild = true; 441 441 } 442 442 443 443 if (!_pointGeometry || _pointGeometry->getVertexArray()->getNumElements() != numParticles) 444 444 { … … 447 447 needGeometryRebuild = true; 448 448 } 449 450 if (needGeometryRebuild) 449 450 if (needGeometryRebuild) 451 451 { 452 452 createGeometry(numParticles, _quadGeometry.get(), _lineGeometry.get(), _pointGeometry.get()); … … 457 457 { 458 458 _quadStateSet = new osg::StateSet; 459 459 460 460 osg::Program* program = new osg::Program; 461 461 _quadStateSet->setAttribute(program); … … 463 463 464 464 #ifdef USE_LOCAL_SHADERS 465 char vertexShaderSource[] = 465 char vertexShaderSource[] = 466 466 "uniform float inversePeriod;\n" 467 467 "uniform vec4 particleColour;\n" … … 509 509 "}\n"; 510 510 511 char fragmentShaderSource[] = 511 char fragmentShaderSource[] = 512 512 "uniform sampler2D baseTexture;\n" 513 513 "varying vec2 texCoord;\n" … … 538 538 539 539 #ifdef USE_LOCAL_SHADERS 540 char vertexShaderSource[] = 540 char vertexShaderSource[] = 541 541 "uniform float inversePeriod;\n" 542 542 "uniform vec4 particleColour;\n" … … 581 581 "}\n"; 582 582 583 char fragmentShaderSource[] = 583 char fragmentShaderSource[] = 584 584 "uniform sampler2D baseTexture;\n" 585 585 "varying vec2 texCoord;\n" … … 609 609 610 610 #ifdef USE_LOCAL_SHADERS 611 char vertexShaderSource[] = 611 char vertexShaderSource[] = 612 612 "uniform float inversePeriod;\n" 613 613 "uniform vec4 particleColour;\n" … … 639 639 "}\n"; 640 640 641 char fragmentShaderSource[] = 641 char fragmentShaderSource[] = 642 642 "uniform sampler2D baseTexture;\n" 643 643 "varying vec4 colour;\n" … … 665 665 OSG_NOTICE<<"Warning: ParticleEffect::setUpGeometries(..) not fully implemented."<<std::endl; 666 666 #endif 667 667 668 668 _pointStateSet->setRenderBinDetails(pointRenderBin,"DepthSortedBin"); 669 669 } … … 693 693 osg::Matrix inverse_modelview; 694 694 inverse_modelview.invert(*(cv->getModelViewMatrix())); 695 695 696 696 osg::Vec3 eyeLocal = osg::Vec3(0.0f,0.0f,0.0f) * inverse_modelview; 697 697 //OSG_NOTICE<<" eyeLocal "<<eyeLocal<<std::endl; 698 698 699 699 float eye_k = (eyeLocal-_origin)*_inverse_dw; 700 700 osg::Vec3 eye_kPlane = eyeLocal-_dw*eye_k-_origin; 701 701 702 702 // OSG_NOTICE<<" eye_kPlane "<<eye_kPlane<<std::endl; 703 703 704 704 float eye_i = eye_kPlane*_inverse_du; 705 705 float eye_j = eye_kPlane*_inverse_dv; 706 706 707 707 osg::Polytope frustum; 708 708 frustum.setToUnitFrustum(false,false); … … 713 713 float j_delta = _farTransition * _inverse_dv.y(); 714 714 float k_delta = 1;//_nearTransition * _inverse_dw.z(); 715 715 716 716 int i_min = (int)floor(eye_i - i_delta); 717 717 int j_min = (int)floor(eye_j - j_delta); … … 721 721 int j_max = (int)ceil(eye_j + j_delta); 722 722 int k_max = (int)ceil(eye_k + k_delta); 723 723 724 724 //OSG_NOTICE<<"i_delta="<<i_delta<<" j_delta="<<j_delta<<" k_delta="<<k_delta<<std::endl; 725 725 … … 738 738 float startTime = (float)(i)*iCyle + (float)(j)*jCyle; 739 739 startTime = (startTime-floor(startTime))*_period; 740 740 741 741 if (build(eyeLocal, i,j,k, startTime, pds, frustum, cv)) ++numInFrustum; 742 742 ++numTested; … … 745 745 } 746 746 747 747 748 748 #ifdef DO_TIMING 749 749 osg::Timer_t endTick = osg::Timer::instance()->tick(); … … 761 761 osg::BoundingBox bb(position.x(), position.y(), position.z()+scale.z(), 762 762 position.x()+scale.x(), position.y()+scale.y(), position.z()); 763 763 764 764 if (!frustum.contains(bb)) return false; 765 765 … … 800 800 mymodelview->preMultTranslate(position); 801 801 mymodelview->preMultScale(scale); 802 802 803 803 cv->updateCalculatedNearFar(*(cv->getModelViewMatrix()),bb); 804 804 … … 840 840 841 841 const osg::Geometry::Extensions* extensions = osg::Geometry::getExtensions(renderInfo.getContextID(),true); 842 842 843 843 // save OpenGL matrices 844 844 glPushMatrix(); 845 845 846 846 if (_requiresPreviousMatrix) 847 847 { … … 850 850 glPushMatrix(); 851 851 } 852 852 853 853 typedef std::vector<const CellMatrixMap::value_type*> DepthMatrixStartTimeVector; 854 854 DepthMatrixStartTimeVector orderedEntries; … … 861 861 orderedEntries.push_back(&(*citr)); 862 862 } 863 863 864 864 std::sort(orderedEntries.begin(),orderedEntries.end(),LessFunctor()); 865 865 866 866 for(DepthMatrixStartTimeVector::reverse_iterator itr = orderedEntries.rbegin(); 867 867 itr != orderedEntries.rend(); … … 881 881 // load previous frame modelview matrix for motion blurr effect 882 882 glMatrixMode( GL_TEXTURE ); 883 glLoadMatrix(pitr->second.modelview.ptr()); 883 glLoadMatrix(pitr->second.modelview.ptr()); 884 884 } 885 885 else … … 887 887 // use current modelview matrix as "previous" frame value, cancelling motion blurr effect 888 888 glMatrixMode( GL_TEXTURE ); 889 glLoadMatrix((*itr)->second.modelview.ptr()); 889 glLoadMatrix((*itr)->second.modelview.ptr()); 890 890 } 891 891 } … … 896 896 897 897 _geometry->draw(renderInfo); 898 898 899 899 unsigned int numVertices = osg::minimum(_geometry->getVertexArray()->getNumElements(), _numberOfVertices); 900 900 glDrawArrays(_drawType, 0, numVertices); … … 907 907 glMatrixMode( GL_MODELVIEW ); 908 908 } 909 909 910 910 glPopMatrix(); 911 911 #else
