Show
Ignore:
Timestamp:
07/06/10 12:55:54 (4 years ago)
Author:
robert
Message:

Added support for passing on uniforms from StateAttribute?

Files:
1 modified

Legend:

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

    r11666 r11667  
    2020#include <osgDB/ReadFile> 
    2121#include <osg/ShaderAttribute> 
     22#include <osg/PositionAttitudeTransform> 
    2223 
    2324osg::Node* createSceneGraph(osg::ArgumentParser& arguments) 
     
    2627    if (!node) return 0; 
    2728 
    28     osg::StateSet* stateset = node->getOrCreateStateSet(); 
    29     osg::ShaderAttribute* sa = new osg::ShaderAttribute; 
    30     stateset->setAttribute(sa); 
     29    osg::Group* group = new osg::Group; 
     30    double spacing = node->getBound().radius() * 2.0; 
     31 
     32    osg::Vec3d position(0.0,0.0,0.0); 
    3133 
    3234    { 
    33         const char shader_str[] = 
    34             "vec4 colour()\n" 
    35             "{\n" 
    36             "    return vec4(1.0,0.5,1.0,1.0);\n" 
    37            "}\n"; 
     35        osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform; 
     36        pat->setPosition(position); 
     37        pat->addChild(node); 
    3838 
    39         osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, shader_str); 
    40         vertex_shader->addCodeInjection(-1,"varying vec4 c;\n"); 
    41         vertex_shader->addCodeInjection(-1,"vec4 colour();\n"); 
    42         vertex_shader->addCodeInjection(0,"gl_Position = ftransform();\n"); 
    43         vertex_shader->addCodeInjection(0,"c = colour();\n"); 
     39        position.x() += spacing; 
    4440 
    45         sa->addShader(vertex_shader); 
    46      } 
     41        osg::StateSet* stateset = pat->getOrCreateStateSet(); 
     42        osg::ShaderAttribute* sa = new osg::ShaderAttribute; 
     43        stateset->setAttribute(sa); 
    4744 
    48      { 
    49         osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT); 
    50         fragment_shader->addCodeInjection(-1,"varying vec4 c;\n"); 
    51         fragment_shader->addCodeInjection(0,"gl_FragColor = c;\n"); 
     45        { 
     46            const char shader_str[] = 
     47                "uniform vec4 myColour;\n" 
     48                "vec4 colour()\n" 
     49                "{\n" 
     50                "    return myColour;\n" 
     51            "}\n"; 
    5252 
    53         sa->addShader(fragment_shader); 
    54      } 
     53            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, shader_str); 
     54            vertex_shader->addCodeInjection(-1,"varying vec4 c;\n"); 
     55            vertex_shader->addCodeInjection(-1,"vec4 colour();\n"); 
     56            vertex_shader->addCodeInjection(0,"gl_Position = ftransform();\n"); 
     57            vertex_shader->addCodeInjection(0,"c = colour();\n"); 
    5558 
    56     return node; 
     59            sa->addUniform(new osg::Uniform("myColour",osg::Vec4(1.0f,0.5f,0.0f,1.0f))); 
     60 
     61            sa->addShader(vertex_shader); 
     62        } 
     63 
     64        { 
     65            osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT); 
     66            fragment_shader->addCodeInjection(-1,"varying vec4 c;\n"); 
     67            fragment_shader->addCodeInjection(0,"gl_FragColor = c;\n"); 
     68 
     69            sa->addShader(fragment_shader); 
     70        } 
     71 
     72        group->addChild(pat); 
     73 
     74    } 
     75 
     76    return group; 
    5777} 
    5878