From 4d0a3444bee409d14a6c5a740e6a77aa72011700 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 19 Feb 2009 16:58:05 +0000 Subject: [PATCH] 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." --- examples/osgforest/osgforest.cpp | 77 ++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 8 deletions(-) diff --git a/examples/osgforest/osgforest.cpp b/examples/osgforest/osgforest.cpp index 42b088df1..87253e4ad 100644 --- a/examples/osgforest/osgforest.cpp +++ b/examples/osgforest/osgforest.cpp @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include #include @@ -48,6 +50,7 @@ #include #include +#include // for the grid data.. #include "../osghangglide/terrain_coords.h" @@ -327,7 +330,7 @@ bool ForestTechniqueManager::Cell::divide(bool xAxis, bool yAxis, bool zAxis) void ForestTechniqueManager::Cell::bin() { - // put trees in apprpriate cells. + // put trees in appropriate cells. TreeList treesNotAssigned; for(TreeList::iterator titr=_trees.begin(); titr!=_trees.end(); @@ -967,6 +970,7 @@ osg::Node* ForestTechniqueManager::createScene(unsigned int numTreesToCreates) osg::StateSet *dstate = new osg::StateSet; { dstate->setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON ); + dstate->setTextureAttribute(0, new osg::TexEnv ); dstate->setAttributeAndModes( new osg::BlendFunc, osg::StateAttribute::ON ); @@ -1005,19 +1009,77 @@ osg::Node* ForestTechniqueManager::createScene(unsigned int numTreesToCreates) std::cout<<"Creating osg::MatrixTransform based forest..."; osg::Group* group = new osg::Group; group->addChild(createTransformGraph(cell.get(),dstate)); - 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")); + 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")); _techniqueSwitch->addChild(group); std::cout<<"done."<setTextureAttributeAndModes(0, tex, osg::StateAttribute::ON ); - stateset->setRenderingHint( osg::StateSet::TRANSPARENT_BIN ); + { + // vertex program + std::ostringstream vp_oss; + vp_oss << + "!!ARBvp1.0\n" + + "ATTRIB vpos = vertex.position;\n" + "ATTRIB vcol = vertex.color;\n" + "ATTRIB tc = vertex.texcoord[" << 0 << "];" + + "PARAM mvp[4] = { state.matrix.mvp };\n" + "PARAM one = { 1.0, 1.0, 1.0, 1.0 };" + + "TEMP position;\n" + + // vec3 position = gl_Vertex.xyz * gl_Color.w + gl_Color.xyz; + "MAD position, vpos, vcol.w, vcol;\n" + + // gl_Position = gl_ModelViewProjectionMatrix * vec4(position,1.0); + "MOV position.w, one;\n" + "DP4 result.position.x, mvp[0], position;\n" + "DP4 result.position.y, mvp[1], position;\n" + "DP4 result.position.z, mvp[2], position;\n" + "DP4 result.position.w, mvp[3], position;\n" + + // gl_FrontColor = vec4(1.0,1.0,1.0,1.0); + "MOV result.color.front.primary, one;\n" + + // texcoord = gl_MultiTexCoord0.st; + "MOV result.texcoord, tc;\n" + "END\n"; + + + // fragment program + std::ostringstream fp_oss; + fp_oss << + "!!ARBfp1.0\n" + "TEX result.color, fragment.texcoord[" << 0 << "], texture[" << 0 << "], 2D;" + "END\n"; + + osg::ref_ptr vp = new osg::VertexProgram; + vp->setVertexProgram(vp_oss.str()); + stateset->setAttributeAndModes(vp.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); + + osg::ref_ptr fp = new osg::FragmentProgram; + fp->setFragmentProgram(fp_oss.str()); + stateset->setAttributeAndModes(fp.get(), osg::StateAttribute::OVERRIDE|osg::StateAttribute::ON); + } + + group->addChild(createShaderGraph(cell.get(),stateset)); + 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")); + _techniqueSwitch->addChild(group); + std::cout<<"done."<addUniform(baseTextureSampler); } - std::cout<<"Creating OpenGL shader based forest..."; group->addChild(createShaderGraph(cell.get(),stateset)); - 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")); + 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")); _techniqueSwitch->addChild(group); std::cout<<"done."<