Show
Ignore:
Timestamp:
02/19/09 17:58:05 (5 years ago)
Author:
robert
Message:

From Christian Buchner, "I am hereby amending the osgforest sample with some code (functionally
equivalent to the GLSL shaders sample) that displays the forest with
shaders on Intel 945 GM hardware. This card supports OpenGL 1.4 and
ARB_fragment/vertex_program only.

I would be pleased if this change made it into the official set of
examples, as it illustrates the use of ARB shaders quite nicely. I did
not find any other example covering this topic."

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgforest/osgforest.cpp

    r9082 r9800  
    3333#include <osg/Texture2D> 
    3434#include <osg/TexEnv> 
     35#include <osg/VertexProgram> 
     36#include <osg/FragmentProgram> 
    3537 
    3638#include <osgDB/ReadFile> 
     
    4951 
    5052#include <iostream> 
     53#include <sstream> 
    5154 
    5255// for the grid data.. 
     
    328331void ForestTechniqueManager::Cell::bin() 
    329332{    
    330     // put trees in apprpriate cells. 
     333    // put trees in appropriate cells. 
    331334    TreeList treesNotAssigned; 
    332335    for(TreeList::iterator titr=_trees.begin(); 
     
    968971    {     
    969972        dstate->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON ); 
     973 
    970974        dstate->setTextureAttribute(0, new osg::TexEnv ); 
    971975 
     
    10061010        osg::Group* group = new osg::Group; 
    10071011        group->addChild(createTransformGraph(cell.get(),dstate)); 
    1008         group->addChild(createHUDWithText("Using osg::MatrixTransform's to create a forest\n\nPress left cursor key to select double quad based forest\nPress right cursor key to select OpenGL shader based forest")); 
     1012        group->addChild(createHUDWithText("Using osg::MatrixTransform's to create a forest\n\nPress left cursor key to select double quad based forest\nPress right cursor key to select osg::Vertex/FragmentProgram based forest")); 
    10091013        _techniqueSwitch->addChild(group); 
    10101014        std::cout<<"done."<<std::endl; 
     
    10121016 
    10131017    { 
     1018        std::cout<<"Creating osg::Vertex/FragmentProgram based forest..."; 
    10141019        osg::Group* group = new osg::Group; 
    1015          
    1016  
    1017         osg::StateSet* stateset = new osg::StateSet; 
    1018  
    1019         stateset->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON ); 
    1020         stateset->setRenderingHint( osg::StateSet::TRANSPARENT_BIN ); 
     1020 
     1021        osg::StateSet* stateset = new osg::StateSet(*dstate, osg::CopyOp::DEEP_COPY_ALL); 
     1022 
     1023        { 
     1024            // vertex program 
     1025            std::ostringstream vp_oss; 
     1026            vp_oss << 
     1027                "!!ARBvp1.0\n" 
     1028                 
     1029                "ATTRIB vpos = vertex.position;\n" 
     1030                "ATTRIB vcol = vertex.color;\n" 
     1031                "ATTRIB tc = vertex.texcoord[" << 0 << "];" 
     1032 
     1033                "PARAM mvp[4] = { state.matrix.mvp };\n" 
     1034                "PARAM one = { 1.0, 1.0, 1.0, 1.0 };" 
     1035 
     1036                "TEMP position;\n" 
     1037                 
     1038                 // vec3 position = gl_Vertex.xyz * gl_Color.w + gl_Color.xyz; 
     1039                "MAD position, vpos, vcol.w, vcol;\n" 
     1040 
     1041                // gl_Position     = gl_ModelViewProjectionMatrix * vec4(position,1.0); 
     1042                "MOV position.w, one;\n" 
     1043                "DP4 result.position.x, mvp[0], position;\n" 
     1044                "DP4 result.position.y, mvp[1], position;\n" 
     1045                "DP4 result.position.z, mvp[2], position;\n" 
     1046                "DP4 result.position.w, mvp[3], position;\n" 
     1047 
     1048                // gl_FrontColor = vec4(1.0,1.0,1.0,1.0); 
     1049                "MOV result.color.front.primary, one;\n" 
     1050 
     1051                 // texcoord = gl_MultiTexCoord0.st; 
     1052                "MOV result.texcoord, tc;\n" 
     1053                "END\n"; 
     1054 
     1055 
     1056            // fragment program 
     1057            std::ostringstream fp_oss; 
     1058            fp_oss << 
     1059                "!!ARBfp1.0\n" 
     1060                "TEX result.color, fragment.texcoord[" << 0 << "], texture[" << 0 << "], 2D;" 
     1061                "END\n"; 
     1062 
     1063            osg::ref_ptr<osg::VertexProgram> vp = new osg::VertexProgram; 
     1064            vp->setVertexProgram(vp_oss.str()); 
     1065            stateset->setAttributeAndModes(vp.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); 
     1066 
     1067            osg::ref_ptr<osg::FragmentProgram> fp = new osg::FragmentProgram; 
     1068            fp->setFragmentProgram(fp_oss.str()); 
     1069            stateset->setAttributeAndModes(fp.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); 
     1070        } 
     1071 
     1072        group->addChild(createShaderGraph(cell.get(),stateset)); 
     1073        group->addChild(createHUDWithText("Using osg::Vertex/FragmentProgram to create a forest\n\nPress left cursor key to select osg::MatrixTransform's based forest\nPress right cursor key to select OpenGL shader based forest")); 
     1074        _techniqueSwitch->addChild(group); 
     1075        std::cout<<"done."<<std::endl; 
     1076    } 
     1077 
     1078    { 
     1079        std::cout<<"Creating OpenGL shader based forest..."; 
     1080        osg::Group* group = new osg::Group; 
     1081 
     1082        osg::StateSet* stateset = new osg::StateSet(*dstate, osg::CopyOp::DEEP_COPY_ALL); 
    10211083 
    10221084        { 
     
    10701132        } 
    10711133 
    1072         std::cout<<"Creating OpenGL shader based forest..."; 
    10731134        group->addChild(createShaderGraph(cell.get(),stateset)); 
    1074         group->addChild(createHUDWithText("Using OpenGL Shader to create a forest\n\nPress left cursor key to select osg::MatrixTransform based forest\nPress right cursor key to select osg::Billboard based forest")); 
     1135        group->addChild(createHUDWithText("Using OpenGL Shader to create a forest\n\nPress left cursor key to select osg::Vertex/FragmentProgram based forest\nPress right cursor key to select osg::Billboard based forest")); 
    10751136        _techniqueSwitch->addChild(group); 
    10761137        std::cout<<"done."<<std::endl;