# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlGraphics/ShaderNode.hpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
    00007 /*  All rights reserved.                                                              */
    00008 /*                                                                                    */
    00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
    00010 /*  are permitted provided that the following conditions are met:                     */
    00011 /*                                                                                    */
    00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
    00013 /*  list of conditions and the following disclaimer.                                  */
    00014 /*                                                                                    */
    00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
    00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
    00017 /*  other materials provided with the distribution.                                   */
    00018 /*                                                                                    */
    00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
    00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
    00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
    00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
    00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
    00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
    00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
    00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
    00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
    00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
    00029 /*                                                                                    */
    00030 /**************************************************************************************/
    00031
    00032 #ifndef ShaderNode_INCLUDE_ONCE
    00033 #define ShaderNode_INCLUDE_ONCE
    00034
    00035 #include <vlGraphics/Shader.hpp>
    00036 #include <vlCore/vlnamespace.hpp>
    00037 #include <vector>
    00038 #include <map>
    00039
    00040 namespace vl
    00041 {
    00045   class ShaderNode: public Object
    00046   {
    00047   public:
    00048     // --- --- ---
    00049     struct EnableInfo
    00050     {
    00051       EnableInfo(EEnable en=EN_UnknownEnable, bool on=false, EInheritance inheritance=IN_Propagate): mEnable(en), mOn(on), mInheritance(inheritance) {}
    00052
    00053       EEnable mEnable;
    00054       bool mOn;
    00055       EInheritance mInheritance;
    00056     };
    00057     // --- --- ---
    00058     struct RenderStateInfo
    00059     {
    00060       RenderStateInfo(RenderState* rs=NULL, EInheritance inheritance=IN_Propagate): mRenderState(rs), mInheritance(inheritance) {}
    00061
    00062       ref<RenderState> mRenderState;
    00063       EInheritance mInheritance;
    00064     };
    00065     // --- --- ---
    00066     struct UniformInfo
    00067     {
    00068       UniformInfo(Uniform* unif=NULL, EInheritance inheritance=IN_Propagate): mUniform(unif), mInheritance(inheritance) {}
    00069
    00070       ref<Uniform> mUniform;
    00071       EInheritance mInheritance;
    00072     };
    00073     // --- --- ---
    00074     typedef std::map< ERenderState, RenderStateInfo > RenderStatesMap;
    00075     typedef std::map< EEnable, EnableInfo > EnablesMap;
    00076     typedef std::map< std::string, UniformInfo > UniformsMap;
    00077
    00078   public:
    00079     virtual const char* className() { return "vl::ShaderNode"; }
    00080
    00081     ShaderNode(): mParent(NULL) {}
    00082
    00083     // shader-related functions
    00084
    00085     void setShader(Shader* shader) { mShader = shader; }
    00086
    00087     const Shader* shader() const { return mShader.get(); }
    00088
    00089     Shader* shader() { return mShader.get(); }
    00090
    00091     // tree-related functions
    00092
    00093     void addChild(ShaderNode* node)
    00094     {
    00095       VL_CHECK(node->parent() == NULL);
    00096       mNodes.push_back(node);
    00097       node->mParent = this;
    00098     }
    00099
    00100     void eraseChild(ShaderNode* node)
    00101     {
    00102       VL_CHECK(node->parent() == this);
    00103       for(unsigned i=0; i<childrenCount(); ++i)
    00104         if (child(i) == node)
    00105         {
    00106           mNodes.erase(mNodes.begin()+i);
    00107           node->mParent = NULL;
    00108           return;
    00109         }
    00110     }
    00111
    00112     void eraseChild(unsigned index)
    00113     {
    00114       VL_CHECK(index<childrenCount())
    00115       mNodes[index]->mParent = NULL;
    00116       mNodes.erase(mNodes.begin()+index);
    00117     }
    00118
    00119     void eraseChildren(unsigned start, unsigned count)
    00120     {
    00121       VL_CHECK(start < childrenCount())
    00122       VL_CHECK(start+count-1 < childrenCount())
    00123       for(unsigned i=start; i<start+count; ++i)
    00124         mNodes[i]->mParent = NULL;
    00125       mNodes.erase(mNodes.begin()+start, mNodes.begin()+start+count);
    00126     }
    00127
    00128     int findChild(const ShaderNode* node) const
    00129     {
    00130       for(unsigned i=0; i<childrenCount(); ++i)
    00131         if(child(i) == node)
    00132           return i;
    00133       return -1;
    00134     }
    00135
    00136     size_t childrenCount() const { return mNodes.size(); }
    00137
    00138     ShaderNode* child(unsigned i) { return mNodes[i].get(); }
    00139
    00140     const ShaderNode* child(unsigned i) const { return mNodes[i].get(); }
    00141
    00142     ShaderNode* parent() { return mParent; }
    00143
    00144     const ShaderNode* parent() const { return mParent; }
    00145
    00146     // inheritance-related functions
    00147
    00148     void updateHierarchy()
    00149     {
    00150       inherit(parent());
    00151       for(unsigned i=0;i <childrenCount(); ++i)
    00152         child(i)->updateHierarchy();
    00153     }
    00154
    00155     void inherit(ShaderNode* parent)
    00156     {
    00157       // init
    00158       mRenderStates_Final.clear();
    00159       mEnables_Final.clear();
    00160       mUniforms_Final.clear();
    00161
    00162       // propagate
    00163       for(RenderStatesMap::const_iterator rs_it = mRenderStates.begin(); rs_it != mRenderStates.end(); ++rs_it)
    00164           mRenderStates_Final[rs_it->first] = rs_it->second;
    00165
    00166       for(std::map< EEnable, EnableInfo>::const_iterator en_it = mEnables.begin(); en_it != mEnables.end(); ++en_it)
    00167         if(en_it->second.mOn)
    00168           mEnables_Final[en_it->first] = en_it->second;
    00169
    00170       for(UniformsMap::const_iterator unif_it = mUniforms.begin(); unif_it != mUniforms.end(); ++unif_it)
    00171           mUniforms_Final[unif_it->first] = unif_it->second;
    00172
    00173       // iterate parent final values
    00174
    00175       if (parent)
    00176       {
    00177
    00178         /* ALGORITHM:
    00179          *
    00180          * 1 - iterate over parent's values
    00181          * 2 - if it doesn't propagate -> end
    00182          * 3 - find current values
    00183          *   3a - if there is not -> insert parent's -> end
    00184          *   3b - if there is
    00185          *      3ba - if parent's do not overrides -> end
    00186          *      3bb - if child's do not overridden -> end
    00187          *      3bc - else -> use parent's
    00188          */
    00189
    00190         // render states
    00191         for(RenderStatesMap::const_iterator par_it = parent->mRenderStates_Final.begin(); par_it != parent->mRenderStates_Final.end(); ++par_it)
    00192         {
    00193           if (!(par_it->second.mInheritance & IN_Propagate))
    00194             continue;
    00195           RenderStatesMap::const_iterator cur_it = mRenderStates_Final.find(par_it->first);
    00196           if (cur_it == mRenderStates_Final.end())
    00197             mRenderStates_Final[par_it->first] = par_it->second;
    00198           else
    00199           if (!(par_it->second.mInheritance & 0x02/*IN_Overrides*/) || (cur_it->second.mInheritance & IN_Sticky))
    00200             continue;
    00201           else
    00202             mRenderStates_Final[par_it->first] = par_it->second;
    00203         }
    00204
    00205         // enables
    00206         for(EnablesMap::const_iterator par_it = parent->mEnables_Final.begin(); par_it != parent->mEnables_Final.end(); ++par_it)
    00207         {
    00208           if (!par_it->second.mOn)
    00209             continue;
    00210           if (!(par_it->second.mInheritance & IN_Propagate))
    00211             continue;
    00212           EnablesMap::const_iterator cur_it = mEnables_Final.find(par_it->first);
    00213           if (cur_it == mEnables_Final.end())
    00214             mEnables_Final[par_it->first] = par_it->second;
    00215           else
    00216           if (!(par_it->second.mInheritance & 0x02/*IN_Overrides*/) || (cur_it->second.mInheritance & IN_Sticky))
    00217             continue;
    00218           else
    00219             mEnables_Final[par_it->first] = par_it->second;
    00220         }
    00221
    00222         // uniforms
    00223         for(UniformsMap::const_iterator par_it = parent->mUniforms_Final.begin(); par_it != parent->mUniforms_Final.end(); ++par_it)
    00224         {
    00225           if (!(par_it->second.mInheritance & IN_Propagate))
    00226             continue;
    00227           UniformsMap::const_iterator cur_it = mUniforms_Final.find(par_it->first);
    00228           if (cur_it == mUniforms_Final.end())
    00229             mUniforms_Final[par_it->first] = par_it->second;
    00230           else
    00231           if (!(par_it->second.mInheritance & 0x02/*IN_Overrides*/) || (cur_it->second.mInheritance & IN_Sticky))
    00232             continue;
    00233           else
    00234             mUniforms_Final[par_it->first] = par_it->second;
    00235         }
    00236       }
    00237
    00238       // apply it to the Shader
    00239
    00240       if (mShader)
    00241       {
    00242         mShader->eraseAllRenderStates();
    00243         mShader->eraseAllUniforms();
    00244         mShader->disableAll();
    00245
    00246         // we can speed this up even more by removing the duplication check
    00247
    00248         for(RenderStatesMap::const_iterator rs_it = mRenderStates_Final.begin(); rs_it != mRenderStates_Final.end(); ++rs_it)
    00249           mShader->setRenderState(rs_it->second.mRenderState.get());
    00250         for(EnablesMap::const_iterator en_it = mEnables_Final.begin(); en_it != mEnables_Final.end(); ++en_it)
    00251           mShader->enable(en_it->second.mEnable);
    00252         for(UniformsMap::const_iterator rs_it = mUniforms_Final.begin(); rs_it != mUniforms_Final.end(); ++rs_it)
    00253           mShader->setUniform(rs_it->second.mUniform.get());
    00254       }
    00255     }
    00256
    00257     // states setters
    00258
    00259     void setRenderState(RenderState* rs, EInheritance inheritance=IN_Propagate)
    00260     {
    00261       RenderStateInfo info(rs, inheritance);
    00262       mRenderStates[rs->type()] = info;
    00263     }
    00264
    00265     void eraseRenderState(RenderState* rs)
    00266     {
    00267       mRenderStates.erase(rs->type());
    00268     }
    00269
    00270     void setEnable(EEnable en, bool on, EInheritance inheritance=IN_Propagate)
    00271     {
    00272       EnableInfo info(en,on,inheritance);
    00273       mEnables[en] = info;
    00274     }
    00275
    00276     void eraseEnable(EEnable en)
    00277     {
    00278       mEnables.erase(en);
    00279     }
    00280
    00281     void setUniform(Uniform* unif, EInheritance inheritance=IN_Propagate)
    00282     {
    00283       UniformInfo info(unif, inheritance);
    00284       mUniforms[unif->name()] = info;
    00285     }
    00286
    00287     void eraseUniform(Uniform* unif)
    00288     {
    00289       mUniforms.erase(unif->name());
    00290     }
    00291
    00292     // states getters
    00293
    00294     const RenderStatesMap& renderStates() const { return mRenderStates; }
    00295
    00296     RenderStatesMap& renderStates() { return mRenderStates; }
    00297
    00298     const EnablesMap& enables() const { return mEnables; }
    00299
    00300     EnablesMap& enables() { return mEnables; }
    00301
    00302     const UniformsMap& uniforms() const { return mUniforms; }
    00303
    00304     UniformsMap& uniforms() { return mUniforms; }
    00305
    00306   protected:
    00307     std::vector< ref< ShaderNode > > mNodes;
    00308     ShaderNode* mParent;
    00309
    00310     RenderStatesMap mRenderStates;
    00311     EnablesMap mEnables;
    00312     UniformsMap mUniforms;
    00313
    00314     RenderStatesMap mRenderStates_Final;
    00315     EnablesMap mEnables_Final;
    00316     UniformsMap mUniforms_Final;
    00317
    00318     ref<Shader> mShader;
    00319   };
    00320 }
    00321
    00322 #endif

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:25.
Permission is granted to use this page to write and publish articles regarding Visualization Library.