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

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
RevLine 
[11664]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>
[11667]22#include <osg/PositionAttitudeTransform>
[11664]23
24osg::Node* createSceneGraph(osg::ArgumentParser& arguments)
25{
26    osg::Node* node = osgDB::readNodeFiles(arguments);
27    if (!node) return 0;
28
[11667]29    osg::Group* group = new osg::Group;
30    double spacing = node->getBound().radius() * 2.0;
[11664]31
[11667]32    osg::Vec3d position(0.0,0.0,0.0);
33
[11668]34    osg::ShaderAttribute* sa1 = NULL;
35
[11666]36    {
[11669]37        osg::StateSet* stateset = group->getOrCreateStateSet();
[11667]38        osg::ShaderAttribute* sa = new osg::ShaderAttribute;
[11669]39        sa->setType(osg::StateAttribute::Type(10000));
[11668]40        sa1 = sa;
[11667]41        stateset->setAttribute(sa);
[11666]42
[11667]43        {
[11669]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");
[11667]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);
[11669]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");
[11667]59
60            sa->addShader(fragment_shader);
61        }
62
[11669]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
[11667]75        group->addChild(pat);
76
77    }
[11669]78
[11668]79    {
80        osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform;
81        pat->setPosition(position);
82        pat->addChild(node);
[11667]83
[11668]84        position.x() += spacing;
85
86        osg::StateSet* stateset = pat->getOrCreateStateSet();
[11669]87        stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
88
[11668]89        osg::ShaderAttribute* sa = new osg::ShaderAttribute;
[11669]90        sa->setType(osg::StateAttribute::Type(10001));
[11668]91        stateset->setAttribute(sa);
92
[11669]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
[11668]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    }
[11669]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
[11667]159    return group;
[11664]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
[11666]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
[11664]185    return viewer.run();
186}
Note: See TracBrowser for help on using the browser.