root/OpenSceneGraph/trunk/examples/osgshadercomposition/osgshadercomposition.cpp @ 11667

Revision 11667, 3.3 kB (checked in by robert, 4 years ago)

Added support for passing on uniforms from StateAttribute?

Line 
1/* OpenSceneGraph example, osganimate.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osgViewer/Viewer>
20#include <osgDB/ReadFile>
21#include <osg/ShaderAttribute>
22#include <osg/PositionAttitudeTransform>
23
24osg::Node* createSceneGraph(osg::ArgumentParser& arguments)
25{
26    osg::Node* node = osgDB::readNodeFiles(arguments);
27    if (!node) return 0;
28
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);
33
34    {
35        osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform;
36        pat->setPosition(position);
37        pat->addChild(node);
38
39        position.x() += spacing;
40
41        osg::StateSet* stateset = pat->getOrCreateStateSet();
42        osg::ShaderAttribute* sa = new osg::ShaderAttribute;
43        stateset->setAttribute(sa);
44
45        {
46            const char shader_str[] =
47                "uniform vec4 myColour;\n"
48                "vec4 colour()\n"
49                "{\n"
50                "    return myColour;\n"
51            "}\n";
52
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");
58
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;
77}
78
79int main( int argc, char **argv )
80{
81    osg::ArgumentParser arguments(&argc,argv);
82
83    osgViewer::Viewer viewer(arguments);
84
85    osg::ref_ptr<osg::Node> scenegraph = createSceneGraph(arguments);
86    if (!scenegraph) return 1;
87
88    viewer.setSceneData(scenegraph.get());
89
90    viewer.realize();
91
92    // enable shader composition
93    osgViewer::Viewer::Windows windows;
94    viewer.getWindows(windows);
95    for(osgViewer::Viewer::Windows::iterator itr = windows.begin();
96        itr != windows.end();
97        ++itr)
98    {
99        (*itr)->getState()->setShaderCompositionEnabled(true);
100    }
101
102    return viewer.run();
103}
Note: See TracBrowser for help on using the browser.