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

Revision 12292, 6.3 kB (checked in by robert, 3 years ago)

Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

  • Property svn:eol-style set to native
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::StateSet* stateset = group->getOrCreateStateSet();
38        osg::ShaderAttribute* sa = new osg::ShaderAttribute;
39        sa->setType(osg::StateAttribute::Type(10000));
40        sa1 = sa;
41        stateset->setAttribute(sa);
42
43        {
44            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX);
45            vertex_shader->addCodeInjection(-1,"varying vec4 color;\n");
46            vertex_shader->addCodeInjection(-1,"varying vec4 texcoord;\n");
47            vertex_shader->addCodeInjection(0,"color = gl_Color;\n");
48            vertex_shader->addCodeInjection(0,"texcoord = gl_MultiTexCoord0;\n");
49            vertex_shader->addCodeInjection(0,"gl_Position = ftransform();\n");
50            sa->addShader(vertex_shader);
51        }
52
53        {
54            osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT);
55            fragment_shader->addCodeInjection(-1,"varying vec4 color;\n");
56            fragment_shader->addCodeInjection(-1,"varying vec4 texcoord;\n");
57            fragment_shader->addCodeInjection(-1,"uniform sampler2D baseTexture; \n");
58            fragment_shader->addCodeInjection(0,"gl_FragColor = color * texture2DProj( baseTexture, texcoord );\n");
59
60            sa->addShader(fragment_shader);
61        }
62
63        sa->addUniform(new osg::Uniform("baseTexture",0));
64
65    }
66
67    // inherit the ShaderComponents entirely from above
68    {
69        osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform;
70        pat->setPosition(position);
71        pat->addChild(node);
72
73        position.x() += spacing;
74
75        group->addChild(pat);
76
77    }
78
79    {
80        osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform;
81        pat->setPosition(position);
82        pat->addChild(node);
83
84        position.x() += spacing;
85
86        osg::StateSet* stateset = pat->getOrCreateStateSet();
87        stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
88
89        osg::ShaderAttribute* sa = new osg::ShaderAttribute;
90        sa->setType(osg::StateAttribute::Type(10001));
91        stateset->setAttribute(sa);
92
93        {
94            osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT);
95            fragment_shader->addCodeInjection(0.9f,"gl_FragColor.a = gl_FragColor.a*0.5;\n");
96
97            sa->addShader(fragment_shader);
98        }
99
100        group->addChild(pat);
101    }
102
103    // resuse the first ShaderAttribute's type and ShaderComponent, just use new uniform
104    {
105        osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform;
106        pat->setPosition(position);
107        pat->addChild(node);
108
109        position.x() += spacing;
110
111        osg::StateSet* stateset = pat->getOrCreateStateSet();
112        osg::ShaderAttribute* sa = new osg::ShaderAttribute(*sa1);
113        stateset->setAttribute(sa);
114
115        // reuse the same ShaderComponent as the first branch
116        sa->addUniform(new osg::Uniform("myColour",osg::Vec4(1.0f,1.0f,0.0f,1.0f)));
117
118        group->addChild(pat);
119
120    }
121
122
123    // resuse the first ShaderAttribute's type and ShaderComponent, just use new uniform
124    {
125        osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform;
126        pat->setPosition(position);
127        pat->addChild(node);
128
129        position.x() += spacing;
130
131        osg::StateSet* stateset = pat->getOrCreateStateSet();
132        osg::ShaderAttribute* sa = new osg::ShaderAttribute;
133        sa->setType(osg::StateAttribute::Type(10000));
134        stateset->setAttribute(sa);
135        stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
136        stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
137
138        {
139            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX);
140            vertex_shader->addCodeInjection(0,"gl_Position = ftransform();\n");
141
142            sa->addShader(vertex_shader);
143        }
144
145        {
146            osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT);
147            fragment_shader->addCodeInjection(-1,"uniform vec4 newColour;\n");
148            fragment_shader->addCodeInjection(-1,"uniform float osg_FrameTime;\n");
149            fragment_shader->addCodeInjection(0,"gl_FragColor = vec4(newColour.r,newColour.g,newColour.b, 0.5+sin(osg_FrameTime*2.0)*0.5);\n");
150
151            sa->addShader(fragment_shader);
152            sa->addUniform(new osg::Uniform("newColour",osg::Vec4(1.0f,1.0f,1.0f,0.5f)));
153        }
154
155        group->addChild(pat);
156
157    }
158
159    return group;
160}
161
162int main( int argc, char **argv )
163{
164    osg::ArgumentParser arguments(&argc,argv);
165
166    osgViewer::Viewer viewer(arguments);
167
168    osg::ref_ptr<osg::Node> scenegraph = createSceneGraph(arguments);
169    if (!scenegraph) return 1;
170
171    viewer.setSceneData(scenegraph.get());
172
173    viewer.realize();
174
175    // enable shader composition
176    osgViewer::Viewer::Windows windows;
177    viewer.getWindows(windows);
178    for(osgViewer::Viewer::Windows::iterator itr = windows.begin();
179        itr != windows.end();
180        ++itr)
181    {
182        (*itr)->getState()->setShaderCompositionEnabled(true);
183    }
184
185    return viewer.run();
186}
Note: See TracBrowser for help on using the browser.