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

Revision 11669, 6.8 kB (checked in by robert, 4 years ago)

Added some debugging to investigate issue of global default ShaderAttribute? not being automatically assigned.

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#if 1
66        osg::ShaderAttribute* sa_dummy = new osg::ShaderAttribute;
67        sa_dummy->setType(osg::StateAttribute::Type(10001));
68        stateset->setAttribute(sa_dummy);
69#endif
70
71    }
72
73    // inherit the ShaderComponents entirely from above
74    {
75        osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform;
76        pat->setPosition(position);
77        pat->addChild(node);
78
79        position.x() += spacing;
80
81        group->addChild(pat);
82
83    }
84
85    {
86        osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform;
87        pat->setPosition(position);
88        pat->addChild(node);
89
90        position.x() += spacing;
91
92        osg::StateSet* stateset = pat->getOrCreateStateSet();
93        stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
94
95#if 1
96        osg::ShaderAttribute* sa = new osg::ShaderAttribute;
97        sa->setType(osg::StateAttribute::Type(10001));
98        stateset->setAttribute(sa);
99
100        {
101            osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT);
102            fragment_shader->addCodeInjection(0.9f,"gl_FragColor.a = gl_FragColor.a*0.5;\n");
103
104            sa->addShader(fragment_shader);
105        }
106#endif
107
108        group->addChild(pat);
109    }
110
111    // resuse the first ShaderAttribute's type and ShaderComponent, just use new uniform
112    {
113        osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform;
114        pat->setPosition(position);
115        pat->addChild(node);
116
117        position.x() += spacing;
118
119        osg::StateSet* stateset = pat->getOrCreateStateSet();
120        osg::ShaderAttribute* sa = new osg::ShaderAttribute(*sa1);
121        stateset->setAttribute(sa);
122
123        // reuse the same ShaderComponent as the first branch
124        sa->addUniform(new osg::Uniform("myColour",osg::Vec4(1.0f,1.0f,0.0f,1.0f)));
125
126        group->addChild(pat);
127
128    }
129
130
131    // resuse the first ShaderAttribute's type and ShaderComponent, just use new uniform
132    {
133        osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform;
134        pat->setPosition(position);
135        pat->addChild(node);
136
137        position.x() += spacing;
138
139        osg::StateSet* stateset = pat->getOrCreateStateSet();
140        osg::ShaderAttribute* sa = new osg::ShaderAttribute;
141        sa->setType(osg::StateAttribute::Type(10000));
142        stateset->setAttribute(sa);
143        stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
144        stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
145
146        {
147            osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX);
148            vertex_shader->addCodeInjection(0,"gl_Position = ftransform();\n");
149
150            sa->addShader(vertex_shader);
151        }
152
153        {
154            osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT);
155            fragment_shader->addCodeInjection(-1,"uniform vec4 newColour;\n");
156            fragment_shader->addCodeInjection(-1,"uniform float osg_FrameTime;\n");
157            fragment_shader->addCodeInjection(0,"gl_FragColor = vec4(newColour.r,newColour.g,newColour.b, 0.5+sin(osg_FrameTime*2.0)*0.5);\n");
158
159            sa->addShader(fragment_shader);
160            sa->addUniform(new osg::Uniform("newColour",osg::Vec4(1.0f,1.0f,1.0f,0.5f)));
161        }
162
163        group->addChild(pat);
164
165        group->addChild(pat);
166
167    }
168
169    return group;
170}
171
172int main( int argc, char **argv )
173{
174    osg::ArgumentParser arguments(&argc,argv);
175
176    osgViewer::Viewer viewer(arguments);
177
178    OSG_NOTICE<<"********** Constructing scene graph ************ "<<std::endl<<std::endl;
179
180    osg::ref_ptr<osg::Node> scenegraph = createSceneGraph(arguments);
181    if (!scenegraph) return 1;
182
183    viewer.setSceneData(scenegraph.get());
184
185    OSG_NOTICE<<std::endl<<"********** Finished constructing scene graph ************ "<<std::endl<<std::endl;
186
187    viewer.realize();
188
189    // enable shader composition
190    osgViewer::Viewer::Windows windows;
191    viewer.getWindows(windows);
192    for(osgViewer::Viewer::Windows::iterator itr = windows.begin();
193        itr != windows.end();
194        ++itr)
195    {
196        (*itr)->getState()->setShaderCompositionEnabled(true);
197    }
198
199    return viewer.run();
200}
Note: See TracBrowser for help on using the browser.