| 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 | |
|---|
| 26 | namespace osgUtil |
|---|
| 27 | { |
|---|
| 28 | |
|---|
| 29 | class OSGUTIL_EXPORT ShaderGenCache : public osg::Referenced |
|---|
| 30 | { |
|---|
| 31 | public: |
|---|
| 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<int, osg::ref_ptr<osg::StateSet> > StateSetMap; |
|---|
| 42 | |
|---|
| 43 | ShaderGenCache() {}; |
|---|
| 44 | |
|---|
| 45 | void setStateSet(int stateMask, osg::StateSet *program); |
|---|
| 46 | osg::StateSet *getStateSet(int stateMask) const; |
|---|
| 47 | osg::StateSet *getOrCreateStateSet(int stateMask); |
|---|
| 48 | |
|---|
| 49 | protected: |
|---|
| 50 | osg::StateSet *createStateSet(int stateMask) const; |
|---|
| 51 | mutable OpenThreads::Mutex _mutex; |
|---|
| 52 | StateSetMap _stateSetMap; |
|---|
| 53 | |
|---|
| 54 | }; |
|---|
| 55 | |
|---|
| 56 | class OSGUTIL_EXPORT ShaderGenVisitor : public osg::NodeVisitor |
|---|
| 57 | { |
|---|
| 58 | public: |
|---|
| 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 | |
|---|
| 74 | protected: |
|---|
| 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 |
|---|