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

Revision 11666, 2.8 kB (checked in by robert, 4 years ago)

Added basic code injection fields to osg::Shader,
creation of main shader to ShaderComposer? and
collection of ShaderComponent? to osg::State.
Also added very basic shader set up in osgshadecomposition example.

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
23osg::Node* createSceneGraph(osg::ArgumentParser& arguments)
24{
25    osg::Node* node = osgDB::readNodeFiles(arguments);
26    if (!node) return 0;
27
28    osg::StateSet* stateset = node->getOrCreateStateSet();
29    osg::ShaderAttribute* sa = new osg::ShaderAttribute;
30    stateset->setAttribute(sa);
31
32    {
33        const char shader_str[] =
34            "vec4 colour()\n"
35            "{\n"
36            "    return vec4(1.0,0.5,1.0,1.0);\n"
37           "}\n";
38
39        osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, shader_str);
40        vertex_shader->addCodeInjection(-1,"varying vec4 c;\n");
41        vertex_shader->addCodeInjection(-1,"vec4 colour();\n");
42        vertex_shader->addCodeInjection(0,"gl_Position = ftransform();\n");
43        vertex_shader->addCodeInjection(0,"c = colour();\n");
44
45        sa->addShader(vertex_shader);
46     }
47
48     {
49        osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT);
50        fragment_shader->addCodeInjection(-1,"varying vec4 c;\n");
51        fragment_shader->addCodeInjection(0,"gl_FragColor = c;\n");
52
53        sa->addShader(fragment_shader);
54     }
55
56    return node;
57}
58
59int main( int argc, char **argv )
60{
61    osg::ArgumentParser arguments(&argc,argv);
62
63    osgViewer::Viewer viewer(arguments);
64
65    osg::ref_ptr<osg::Node> scenegraph = createSceneGraph(arguments);
66    if (!scenegraph) return 1;
67
68    viewer.setSceneData(scenegraph.get());
69
70    viewer.realize();
71
72    // enable shader composition
73    osgViewer::Viewer::Windows windows;
74    viewer.getWindows(windows);
75    for(osgViewer::Viewer::Windows::iterator itr = windows.begin();
76        itr != windows.end();
77        ++itr)
78    {
79        (*itr)->getState()->setShaderCompositionEnabled(true);
80    }
81
82    return viewer.run();
83}
Note: See TracBrowser for help on using the browser.