root/OpenSceneGraph/trunk/include/osgUtil/ShaderGen @ 9940

Revision 9940, 2.3 kB (checked in by robert, 6 years ago)

From Maciej Krol, "As promised to Roland I assembled simple shader generator. ShaderGenVisitor? converts accumulated fixed function pipeline state sets to ones with shader programs. Generated state sets are attached to geometries and stored in ShaderGenCache? for reuse.

Very simple cases of state configuration are supported (all the ones I really need):
- single per pixel not attenuated non spot light source ON/OFF
- exp2 fog ON/OFF
- diffuse texture in rgb + optional specular gloss in alpha (Texture unit 0) ON/OFF
- normal map texture (Texture unit 1 and Tangent in VertexAttribArray? 6) ON/OFF
- blending and alpha testing (not in shader pipeline)

To view fixed function pipeline files and paged databases simply run >osgshadergen myfile.osg"

Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under 
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14/**
15 * \brief    Shader generator framework.
16 * \author   Maciej Krol
17 */
18
19#ifndef OSGUTIL_SHADER_STATE_
20#define OSGUTIL_SHADER_STATE_ 1
21
22#include <osgUtil/Export>
23#include <osg/NodeVisitor>
24#include <osg/State>
25
26namespace osgUtil
27{
28
29class OSGUTIL_EXPORT ShaderGenCache : public osg::Referenced
30{
31public:
32    enum StateMask
33    {
34        BLEND = 1,
35        LIGHTING = 2,
36        FOG = 4,
37        DIFFUSE_MAP = 8, //< Texture in unit 0
38        NORMAL_MAP = 16  //< Texture in unit 1 and vertex attribute array 6
39    };
40
41    typedef std::map<unsigned int, osg::ref_ptr<osg::StateSet> > StateSetMap;
42
43    ShaderGenCache() {};
44
45    void setStateSet(unsigned int stateMask, osg::StateSet *program);
46    osg::StateSet *getStateSet(unsigned int stateMask) const;
47    osg::StateSet *getOrCreateStateSet(unsigned int stateMask);
48
49protected:
50    osg::StateSet *createStateSet(unsigned int stateMask) const;
51    mutable OpenThreads::Mutex _mutex;
52    StateSetMap _stateSetMap;
53
54};
55
56class OSGUTIL_EXPORT ShaderGenVisitor : public osg::NodeVisitor
57{
58public:
59    ShaderGenVisitor();
60    ShaderGenVisitor(ShaderGenCache *stateCache);
61
62    void setStateCache(ShaderGenCache *stateCache) { _stateCache = stateCache; }
63    ShaderGenCache *getStateCache() const { return _stateCache.get(); }
64
65    /// Top level state set applied as the first one.
66    void setRootStateSet(osg::StateSet *stateSet);
67    osg::StateSet *getRootStateSet() const { return _rootStateSet.get(); }
68
69    void apply(osg::Node &node);
70    void apply(osg::Geode &geode);
71
72    void reset();
73
74protected:
75    void update(osg::Drawable *drawable);
76
77    osg::ref_ptr<ShaderGenCache> _stateCache;
78    osg::ref_ptr<osg::State> _state;
79    osg::ref_ptr<osg::StateSet> _rootStateSet;
80};
81
82}
83
84#endif
Note: See TracBrowser for help on using the browser.