 Timestamp:
 04/19/06 15:04:21 (8 years ago)
 Files:

 1 modified
Legend:
 Unmodified
 Added
 Removed

OpenSceneGraph/trunk/examples/osgprecipitation/osgprecipitation.cpp
r5095 r5096 46 46 fogEnd(1000.0), 47 47 fogColour(0.5, 0.5, 0.5, 1.0), 48 clearColour(0.5, 0.5, 0.5, 1.0) 48 clearColour(0.5, 0.5, 0.5, 1.0), 49 useFarLineSegments(false) 49 50 { 50 51 rain(0.5); … … 65 66 fogColour.set(0.5, 0.5, 0.5, 1.0); 66 67 clearColour.set(0.5, 0.5, 0.5, 1.0); 68 useFarLineSegments = false; 67 69 } 68 70 … … 81 83 fogColour.set(0.6, 0.6, 0.6, 1.0); 82 84 clearColour.set(0.6, 0.6, 0.6, 1.0); 85 useFarLineSegments = false; 83 86 } 84 87 … … 98 101 osg::Vec4 fogColour; 99 102 osg::Vec4 clearColour; 103 bool useFarLineSegments; 100 104 }; 101 105 … … 139 143 void setPosition(const osg::Vec3& position) { _position = position; } 140 144 const osg::Vec3& getPosition() const { return _position; } 145 146 void setScale(const osg::Vec3& scale) { _scale = scale; } 147 const osg::Vec3& getScale() const { return _scale; } 141 148 142 149 void setStartTime(float time) { _startTime = time; } … … 164 171 glNormal3fv(_position.ptr()); 165 172 extensions>glMultiTexCoord1f(GL_TEXTURE0+1, _startTime); 166 173 174 glPushMatrix(); 175 176 osg::Matrix modelview = state.getModelViewMatrix(); 177 modelview.preMult(osg::Matrix::translate(_position)); 178 modelview.preMult(osg::Matrix::scale(_scale)); 179 180 bool isPoint = (_internalGeometry>getName()=="point"); 181 182 glLoadMatrix(modelview.ptr()); 183 184 if (!isPoint) 185 { 186 state.setActiveTextureUnit(0); 187 glMatrixMode( GL_TEXTURE ); 188 189 glPushMatrix(); 190 191 glLoadMatrix(_previousModelView.ptr()); 192 193 _previousModelView = modelview; 194 } 195 #if 0 196 else 197 { 198 state.setActiveTextureUnit(0); 199 glMatrixMode( GL_TEXTURE ); 200 201 glPushMatrix(); 202 203 glLoadIdentity(); 204 205 _previousModelView = modelview; 206 } 207 #endif 167 208 _internalGeometry>draw(state); 168 209 … … 206 247 s_NumberPointsVertices+= _internalGeometry>getVertexArray()>getNumElements(); 207 248 } 249 250 if (!isPoint) 251 { 252 glPopMatrix(); 253 glMatrixMode( GL_MODELVIEW ); 254 } 255 256 glPopMatrix(); 208 257 } 209 258 … … 216 265 217 266 osg::Vec3 _position; 267 osg::Vec3 _scale; 218 268 float _startTime; 219 269 osg::ref_ptr<osg::Geometry> _internalGeometry; 270 mutable osg::Matrix _previousModelView; 220 271 221 272 }; … … 452 503 #ifdef USE_LOCAL_SHADERS 453 504 char vertexShaderSource[] = 454 "uniform vec3 dv_i;\n"455 "uniform vec3 dv_j;\n"456 "uniform vec3 dv_k;\n"457 "\n"458 505 "uniform float inversePeriod;\n" 459 506 "uniform vec4 particleColour;\n" … … 462 509 "uniform float osg_FrameTime;\n" 463 510 "uniform float osg_DeltaFrameTime;\n" 464 "uniform mat4 previousModelViewMatrix;\n"465 511 "\n" 466 512 "varying vec4 colour;\n" … … 469 515 "void main(void)\n" 470 516 "{\n" 471 " vec3 pos = gl_Normal.xyz + (gl_Vertex.x*dv_i) + (dv_j * gl_Vertex.y);\n"472 " \n"473 517 " float offset = gl_Vertex.z;\n" 518 " float startTime = gl_MultiTexCoord1.x;\n" 474 519 " texCoord = gl_MultiTexCoord0.xy;\n" 475 " float startTime = gl_MultiTexCoord1.x;\n" 476 "\n" 477 " vec3 v_previous = pos + dv_k * fract( (osg_FrameTime  startTime)*inversePeriod  offset);\n" 478 " vec3 v_current = v_previous + dv_k * (osg_DeltaFrameTime*inversePeriod);\n" 479 " \n" 520 "\n" 521 " vec4 v_previous = gl_Vertex;\n" 522 " v_previous.z = fract( (osg_FrameTime  startTime)*inversePeriod  offset);\n" 523 " \n" 524 " vec4 v_current = v_previous;\n" 525 " v_current.z += (osg_DeltaFrameTime*inversePeriod);\n" 526 " \n" 527 "\n" 480 528 " colour = particleColour;\n" 481 529 " \n" 482 " vec4 v1 = gl_ModelViewMatrix * v ec4(v_current,1.0);\n"483 " vec4 v2 = previousModelViewMatrix * vec4(v_previous,1.0);\n"530 " vec4 v1 = gl_ModelViewMatrix * v_current;\n" 531 " vec4 v2 = gl_TextureMatrix[0] * v_previous;\n" 484 532 " \n" 485 533 " vec3 dv = v2.xyz  v1.xyz;\n" … … 531 579 #ifdef USE_LOCAL_SHADERS 532 580 char vertexShaderSource[] = 533 "uniform vec3 dv_i;\n"534 "uniform vec3 dv_j;\n"535 "uniform vec3 dv_k;\n"536 "\n"537 581 "uniform float inversePeriod;\n" 538 582 "uniform vec4 particleColour;\n" … … 548 592 "void main(void)\n" 549 593 "{\n" 550 " vec3 pos = gl_Normal.xyz + (gl_Vertex.x*dv_i) + (dv_j * gl_Vertex.y);\n"551 " \n"552 "\n"553 594 " float offset = gl_Vertex.z;\n" 595 " float startTime = gl_MultiTexCoord1.x;\n" 554 596 " texCoord = gl_MultiTexCoord0.xy;\n" 555 " float startTime = gl_MultiTexCoord1.x;\n" 556 "\n" 557 " vec3 v_previous = pos + dv_k * fract( (osg_FrameTime  startTime)*inversePeriod  offset);\n" 558 " vec3 v_current = v_previous + dv_k * (osg_DeltaFrameTime*inversePeriod);\n" 597 "\n" 598 " vec4 v_previous = gl_Vertex;\n" 599 " v_previous.z = fract( (osg_FrameTime  startTime)*inversePeriod  offset);\n" 600 " \n" 601 " vec4 v_current = v_previous;\n" 602 " v_current.z += (osg_DeltaFrameTime*inversePeriod);\n" 559 603 " \n" 560 604 " colour = particleColour;\n" 561 605 " \n" 562 " vec4 v1 = gl_ModelViewMatrix * v ec4(v_current,1.0);\n"563 " vec4 v2 = previousModelViewMatrix * vec4(v_previous,1.0);\n"606 " vec4 v1 = gl_ModelViewMatrix * v_current;\n" 607 " vec4 v2 = gl_TextureMatrix[0] * v_previous;\n" 564 608 " \n" 565 609 " vec3 dv = v2.xyz  v1.xyz;\n" … … 569 613 " \n" 570 614 " float area = length(dv.xy);\n" 571 " colour.a = 0.1+(particleSize)/area;\n"615 " colour.a = (particleSize)/area;\n" 572 616 " \n" 573 617 " v1.xyz += dv*texCoord.y;\n" 574 618 " \n" 575 619 " gl_Position = gl_ProjectionMatrix * v1;\n" 576 "} ";620 "}\n"; 577 621 578 622 char fragmentShaderSource[] = … … 607 651 #ifdef USE_LOCAL_SHADERS 608 652 char vertexShaderSource[] = 609 "uniform vec3 dv_i;\n"610 "uniform vec3 dv_j;\n"611 "uniform vec3 dv_k;\n"612 "\n"613 653 "uniform float inversePeriod;\n" 614 654 "uniform vec4 particleColour;\n" … … 618 658 "\n" 619 659 "varying vec4 colour;\n" 620 "varying vec2 texCoord;\n"621 660 "\n" 622 661 "void main(void)\n" 623 662 "{\n" 624 " vec3 pos = gl_Normal.xyz + (gl_Vertex.x*dv_i) + (dv_j * gl_Vertex.y);\n" 625 " texCoord = gl_MultiTexCoord0.xy;\n" 626 "\n" 663 " float offset = gl_Vertex.z;\n" 627 664 " float startTime = gl_MultiTexCoord1.x;\n" 628 665 "\n" 629 " vec3 v_current = pos + dv_k * fract( (osg_FrameTime  startTime)*inversePeriod  gl_Vertex.z);\n" 666 " vec4 v_current = gl_Vertex;\n" 667 " v_current.z = fract( (osg_FrameTime  startTime)*inversePeriod  offset);\n" 630 668 " \n" 631 669 " colour = particleColour;\n" 632 670 "\n" 633 " gl_Position = gl_ModelViewProjectionMatrix * v ec4(v_current,1.0);\n"671 " gl_Position = gl_ModelViewProjectionMatrix * v_current;\n" 634 672 "\n" 635 673 " float pointSize = abs(1280.0*particleSize / gl_Position.w);\n" 674 "\n" 636 675 " //gl_PointSize = max(ceil(pointSize),2);\n" 637 676 " gl_PointSize = ceil(pointSize);\n" … … 642 681 char fragmentShaderSource[] = 643 682 "uniform sampler2D baseTexture;\n" 644 "varying vec2 texCoord;\n"645 683 "varying vec4 colour;\n" 646 684 "\n" 647 685 "void main (void)\n" 648 686 "{\n" 649 " gl_FragColor = colour * texture2D( baseTexture, texCoord);\n"687 " gl_FragColor = colour * texture2D( baseTexture, gl_TexCoord[0]);\n" 650 688 "}\n"; 651 689 … … 655 693 // get shaders from source 656 694 program>addShader(osg::Shader::readShaderFile(osg::Shader::VERTEX, osgDB::findDataFile("point_rain.vert"))); 657 program>addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, osgDB::findDataFile(" rain.frag")));695 program>addShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT, osgDB::findDataFile("point_rain.frag"))); 658 696 #endif 659 697 … … 671 709 672 710 673 osg::Node* createRainEffect(const osg::BoundingBox& bb, const osg::Vec3& velocity, float nearTransition, float farTransition)711 osg::Node* createRainEffect(const osg::BoundingBox& bb, const PrecipatationParameters& parameters) 674 712 { 675 713 osg::LOD* lod = new osg::LOD; … … 680 718 681 719 // time taken to get from start to the end of cycle 682 float period = fabs((bb.zMax()bb.zMin()) / velocity.z());720 float period = fabs((bb.zMax()bb.zMin()) / parameters.particleVelocity.z()); 683 721 osg::Vec3 dv_i( bb.xMax()bb.xMin(), 0.0f, 0.0f ); 684 722 osg::Vec3 dv_j( 0.0f, bb.yMax()bb.yMin(), 0.0f ); 685 osg::Vec3 dv_k( velocity * period ); 723 osg::Vec3 dv_k( parameters.particleVelocity * period ); 724 725 osg::Vec3 scale( bb.xMax()bb.xMin(), bb.yMax()bb.yMin(), parameters.particleVelocity.z() * period); 686 726 687 727 float startTime = random(0, period); … … 697 737 geometry>setName("highres"); 698 738 geometry>setPosition(position); 739 geometry>setScale(scale); 699 740 geometry>setStartTime(startTime); 700 741 geometry>setInitialBound(bb); … … 702 743 geometry>setStateSet(quad_stateset.get()); 703 744 704 lod>addChild( highres_geode, 0.0f, nearTransition ); 705 } 706 707 // low res LOD 745 lod>addChild( highres_geode, 0.0f, parameters.nearTransition ); 746 } 747 748 749 if (parameters.useFarLineSegments) 708 750 { 709 751 osg::Geode* lowres_geode = new osg::Geode; … … 716 758 geometry>setName("lowres"); 717 759 geometry>setPosition(position); 760 geometry>setScale(scale); 761 geometry>setStartTime(startTime); 762 geometry>setInitialBound(bb); 763 geometry>setInternalGeometry(line_geometry.get()); 764 geometry>setStateSet(line_stateset.get()); 765 766 lod>addChild( lowres_geode, parameters.nearTransition, parameters.farTransition ); 767 } 768 else 769 { 770 osg::Geode* lowres_geode = new osg::Geode; 771 772 773 PrecipitationGeometry* geometry = new PrecipitationGeometry; 774 775 lowres_geode>addDrawable(geometry); 776 777 geometry>setName("lowres"); 778 geometry>setPosition(position); 779 geometry>setScale(scale); 718 780 geometry>setStartTime(startTime); 719 781 geometry>setInitialBound(bb); … … 721 783 geometry>setStateSet(point_stateset.get()); 722 784 723 lod>addChild( lowres_geode, nearTransition,farTransition );785 lod>addChild( lowres_geode, parameters.nearTransition, parameters.farTransition ); 724 786 } 725 787 … … 759 821 bb.zMax()); 760 822 761 group>addChild(createRainEffect(bbLocal, parameters .particleVelocity, parameters.nearTransition, parameters.farTransition));823 group>addChild(createRainEffect(bbLocal, parameters)); 762 824 } 763 825 } … … 779 841 osg::Vec3 dv_k( parameters.particleVelocity * period ); 780 842 781 // set up uniforms782 // osg::Uniform* position_Uniform = new osg::Uniform("position",position);783 osg::Uniform* dv_i_Uniform = new osg::Uniform("dv_i",dv_i);784 osg::Uniform* dv_j_Uniform = new osg::Uniform("dv_j",dv_j);785 osg::Uniform* dv_k_Uniform = new osg::Uniform("dv_k",dv_k);786 843 787 844 osg::Uniform* inversePeriodUniform = new osg::Uniform("inversePeriod",1.0f/period); 788 845 //osg::Uniform* startTime = new osg::Uniform("startTime",0.0f); 789 846 790 //stateset>addUniform(position_Uniform); // vec3791 stateset>addUniform(dv_i_Uniform); // vec3 could be float792 stateset>addUniform(dv_j_Uniform); // vec3 could be float793 stateset>addUniform(dv_k_Uniform); // vec3794 847 stateset>addUniform(inversePeriodUniform); // float 795 848 //stateset>addUniform(startTime); // float … … 807 860 stateset>addUniform(new osg::Uniform("particleSize", parameters.particleSize)); 808 861 809 osg::Uniform* previousModelViewUniform = new osg::Uniform("previousModelViewMatrix",osg::Matrix());810 stateset>addUniform(previousModelViewUniform);811 812 group>setCullCallback(new CullCallback(previousModelViewUniform));862 // osg::Uniform* previousModelViewUniform = new osg::Uniform("previousModelViewMatrix",osg::Matrix()); 863 // stateset>addUniform(previousModelViewUniform); 864 865 // group>setCullCallback(new CullCallback(previousModelViewUniform)); 813 866 814 867 … … 940 993 while (arguments.read("fogColour", parameters.fogColour.r(), parameters.fogColour.g(), parameters.fogColour.b(), parameters.fogColour.a())) {} 941 994 995 while (arguments.read("useFarLineSegments")) { parameters.useFarLineSegments = true; } 942 996 943 997 viewer.setClearColor(parameters.clearColour);