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

Revision 11668, 4.1 kB (checked in by robert, 4 years ago)

Added compile/release and resize of GL objects to ShaderAttribute?.

Removed the StateAttribute::compose() method.

Fixed the default type value in ShaderAttribute?

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    osg::ShaderAttribute* sa1 = NULL;
35
36    {
37        osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform;
38        pat->setPosition(position);
39        pat->addChild(node);
40
41        position.x() += spacing;
42
43        osg::StateSet* stateset = pat->getOrCreateStateSet();
44        osg::ShaderAttribute* sa = new osg::ShaderAttribute;
45        //sa->setType(osg::StateAttribute::Type(10000));
46        sa1 = sa;
47        stateset->setAttribute(sa);
48
49        {
50            const char shader_str[] =
51                "uniform vec4 myColour;\n"
52                "vec4 colour()\n"
53                "{\n"
54                "    return myColour;\n"
55            "}\n";
56
57            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, shader_str);
58            vertex_shader->addCodeInjection(-1,"varying vec4 c;\n");
59            vertex_shader->addCodeInjection(-1,"vec4 colour();\n");
60            vertex_shader->addCodeInjection(0,"gl_Position = ftransform();\n");
61            vertex_shader->addCodeInjection(0,"c = colour();\n");
62
63            sa->addUniform(new osg::Uniform("myColour",osg::Vec4(1.0f,0.5f,0.0f,1.0f)));
64
65            sa->addShader(vertex_shader);
66        }
67
68        {
69            osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT);
70            fragment_shader->addCodeInjection(-1,"varying vec4 c;\n");
71            fragment_shader->addCodeInjection(0,"gl_FragColor = c;\n");
72
73            sa->addShader(fragment_shader);
74        }
75
76        group->addChild(pat);
77
78    }
79#if 1
80    {
81        osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform;
82        pat->setPosition(position);
83        pat->addChild(node);
84
85        position.x() += spacing;
86
87        osg::StateSet* stateset = pat->getOrCreateStateSet();
88        osg::ShaderAttribute* sa = new osg::ShaderAttribute;
89        //sa->setType(osg::StateAttribute::Type(10000));
90        stateset->setAttribute(sa);
91
92        // reuse the same ShaderComponent as the first branch
93        sa->setShaderComponent(sa1->getShaderComponent());
94        sa->addUniform(new osg::Uniform("myColour",osg::Vec4(1.0f,1.0f,0.0f,1.0f)));
95
96        group->addChild(pat);
97
98    }
99#endif
100    return group;
101}
102
103int main( int argc, char **argv )
104{
105    osg::ArgumentParser arguments(&argc,argv);
106
107    osgViewer::Viewer viewer(arguments);
108
109    osg::ref_ptr<osg::Node> scenegraph = createSceneGraph(arguments);
110    if (!scenegraph) return 1;
111
112    viewer.setSceneData(scenegraph.get());
113
114    viewer.realize();
115
116    // enable shader composition
117    osgViewer::Viewer::Windows windows;
118    viewer.getWindows(windows);
119    for(osgViewer::Viewer::Windows::iterator itr = windows.begin();
120        itr != windows.end();
121        ++itr)
122    {
123        (*itr)->getState()->setShaderCompositionEnabled(true);
124    }
125
126    return viewer.run();
127}
Note: See TracBrowser for help on using the browser.