csplugincommon/rendermanager/renderlayers.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

csplugincommon/rendermanager/renderlayers.h
Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 2007-2008 by Marten Svanfeldt
    00003
    00004     This library is free software; you can redistribute it and/or
    00005     modify it under the terms of the GNU Library General Public
    00006     License as published by the Free Software Foundation; either
    00007     version 2 of the License, or (at your option) any later version.
    00008
    00009     This library is distributed in the hope that it will be useful,
    00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012     Library General Public License for more details.
    00013
    00014     You should have received a copy of the GNU Library General Public
    00015     License along with this library; if not, write to the Free
    00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00017 */
    00018
    00019 #ifndef __CS_CSPLUGINCOMMON_RENDERMANAGER_RENDERLAYERS_H__
    00020 #define __CS_CSPLUGINCOMMON_RENDERMANAGER_RENDERLAYERS_H__
    00021
    00026 #include "csutil/dirtyaccessarray.h"
    00027 #include "csutil/strset.h"
    00028
    00029 struct iShader;
    00030
    00031 namespace CS
    00032 {
    00033 namespace RenderManager
    00034 {
    00035   /*\file
    00036    *
    00037    * Provide render layer implementations.
    00038    * All implementations must at least supply:
    00039    *  - size_t GetLayerCount () const
    00040    *  - const csStringID* GetShaderTypes (size_t layer, size_t& num) const
    00041    *  - iShader* GetDefaultShader (size_t layer) const
    00042    */
    00043
    00045   struct StaticLightsSettings
    00046   {
    00048     bool nodraw;
    00050     bool specularOnly;
    00051
    00052     StaticLightsSettings() : nodraw (false), specularOnly (false) {}
    00053   };
    00054
    00058   class SingleRenderLayer
    00059   {
    00060   public:
    00062     SingleRenderLayer (iShader* defaultShader = 0,
    00063       size_t maxLightPasses = 3, size_t maxLights = ~0)
    00064       : defaultShader (defaultShader), maxLightPasses (maxLightPasses),
    00065         maxLights (maxLights), isAmbient (false)
    00066     { }
    00068     SingleRenderLayer (const csStringID shaderType, iShader* defaultShader = 0,
    00069         size_t maxLightPasses = 3, size_t maxLights = ~0)
    00070       : defaultShader (defaultShader), maxLightPasses (maxLightPasses),
    00071         maxLights (maxLights), isAmbient (false)
    00072     {
    00073       shaderTypes.Push (shaderType);
    00074     }
    00076     SingleRenderLayer (const csStringID* shaderTypes, size_t numTypes,
    00077       iShader* defaultShader = 0,
    00078       size_t maxLightPasses = 3, size_t maxLights = ~0)
    00079       : defaultShader (defaultShader), maxLightPasses (maxLightPasses),
    00080         maxLights (maxLights), isAmbient (false)
    00081     {
    00082       this->shaderTypes.SetSize (numTypes);
    00083       for (size_t i = 0; i < numTypes; ++i)
    00084         this->shaderTypes[i] = shaderTypes[i];
    00085     }
    00086
    00088     void AddShaderType (csStringID shaderType)
    00089     {
    00090       shaderTypes.Push (static_cast<StringIDValue> (shaderType));
    00091     }
    00092
    00094     size_t GetLayerCount () const
    00095     {
    00096       return 1;
    00097     }
    00098
    00100     const csStringID* GetShaderTypes (size_t layer, size_t& num) const
    00101     {
    00102       CS_ASSERT(layer == 0);
    00103
    00104       num = shaderTypes.GetSize ();
    00105       return reinterpret_cast<const csStringID*> (shaderTypes.GetArray());
    00106     }
    00107
    00109     iShader* GetDefaultShader (size_t layer) const
    00110     {
    00111       CS_ASSERT(layer == 0);
    00112
    00113       return defaultShader;
    00114     }
    00115
    00122     size_t GetMaxLightNum (size_t layer) const
    00123     {
    00124       CS_ASSERT(layer == 0);
    00125
    00126       return maxLights;
    00127     }
    00128
    00133     size_t GetMaxLightPasses (size_t layer) const
    00134     {
    00135       CS_ASSERT(layer == 0);
    00136
    00137       return maxLightPasses;
    00138     }
    00143     bool IsAmbientLayer (size_t layer) const
    00144     {
    00145       CS_ASSERT(layer == 0);
    00146       return isAmbient;
    00147     }
    00149     iShaderVariableContext* GetSVContext (size_t layer) const
    00150     {
    00151       CS_ASSERT(layer == 0);
    00152       return svContext;
    00153     }
    00155
    00156     StaticLightsSettings& GetStaticLightsSettings ()
    00157     {
    00158       return staticLights;
    00159     }
    00160     const StaticLightsSettings& GetStaticLightsSettings () const
    00161     {
    00162       return staticLights;
    00163     }
    00165
    00167     void SetShaderTypes (const csStringID* shaderTypes, size_t numTypes)
    00168     {
    00169       this->shaderTypes.SetSize (numTypes);
    00170       for (size_t i = 0; i < numTypes; ++i)
    00171         this->shaderTypes[i] = shaderTypes[i];
    00172     }
    00174     void SetDefaultShader (iShader* defaultShader)
    00175     {
    00176       this->defaultShader = defaultShader;
    00177     }
    00182     void SetMaxLightPasses (size_t maxLightPasses)
    00183     {
    00184       this->maxLightPasses = maxLightPasses;
    00185     }
    00190     void SetMaxLights (size_t maxLights)
    00191     {
    00192       this->maxLights = maxLights;
    00193     }
    00198     void SetAmbient (bool isAmbient)
    00199     {
    00200       this->isAmbient = isAmbient;
    00201     }
    00203     void SetSVContext (iShaderVariableContext* svContext)
    00204     {
    00205       this->svContext = svContext;
    00206     }
    00207   private:
    00208     /*csDirtyAccessArray<StringIDValue,
    00209       csArrayElementHandler<StringIDValue>,
    00210       CS::Memory::AllocatorMalloc,
    00211       csArrayCapacityFixedGrow<1> > shaderTypes;*/
    00212     csDirtyAccessArray<StringIDValue> shaderTypes;
    00213     csRef<iShader> defaultShader;
    00214     size_t maxLightPasses;
    00215     size_t maxLights;
    00216     bool isAmbient;
    00217     csRef<iShaderVariableContext> svContext;
    00218     StaticLightsSettings staticLights;
    00219   };
    00220
    00224   class MultipleRenderLayer
    00225   {
    00226   public:
    00228     MultipleRenderLayer () {}
    00234     MultipleRenderLayer (size_t numLayers, const csStringID* shaderTypes,
    00235       iShader** defaultShader,
    00236       size_t* maxLightPasses = 0, size_t* maxLights = 0)
    00237     {
    00238       layerTypes.SetSize (numLayers);
    00239       for (size_t l = 0; l < numLayers; l++)
    00240       {
    00241         layerTypes[l] = shaderTypes[l];
    00242
    00243         Layer newLayer;
    00244         newLayer.defaultShader = defaultShader[l];
    00245         newLayer.maxLightPasses = maxLightPasses ? maxLightPasses[l] : 1;
    00246         newLayer.maxLights = maxLights ? maxLights[l] : ~0;
    00247         newLayer.firstType = l;
    00248         newLayer.numTypes = 1;
    00249         newLayer.isAmbient = false;
    00250         layers.Push (newLayer);
    00251       }
    00252       layers.ShrinkBestFit ();
    00253       layerTypes.ShrinkBestFit ();
    00254     }
    00255
    00256     ~MultipleRenderLayer ()
    00257     {
    00258     }
    00259
    00261     template<typename LayerType>
    00262     void AddLayers (const LayerType& layers)
    00263     {
    00264       for (size_t l = 0; l < layers.GetLayerCount(); l++)
    00265       {
    00266         Layer newLayer;
    00267         newLayer.defaultShader = layers.GetDefaultShader (l);
    00268         newLayer.maxLightPasses = layers.GetMaxLightPasses (l);
    00269         newLayer.maxLights = layers.GetMaxLightNum (l);
    00270         newLayer.isAmbient = layers.IsAmbientLayer (l);
    00271         newLayer.svContext = layers.GetSVContext (l);
    00272         newLayer.firstType = layerTypes.GetSize ();
    00273         const csStringID* copyTypes = layers.GetShaderTypes (l,
    00274           newLayer.numTypes);
    00275         layerTypes.SetSize (newLayer.firstType + newLayer.numTypes);
    00276
    00277         for (size_t i = 0; i < newLayer.numTypes; ++i)
    00278           layerTypes[i + newLayer.firstType] = copyTypes[i];
    00279
    00280         this->layers.Push (newLayer);
    00281       }
    00282       this->layers.ShrinkBestFit ();
    00283       layerTypes.ShrinkBestFit ();
    00284     }
    00285
    00287     size_t GetLayerCount () const
    00288     {
    00289       return layers.GetSize();
    00290     }
    00291
    00293     const csStringID* GetShaderTypes (size_t layer, size_t& num) const
    00294     {
    00295       num = layers[layer].numTypes;
    00296       return reinterpret_cast<const csStringID*> (
    00297         layerTypes.GetArray() + layers[layer].firstType);
    00298     }
    00299
    00301     iShader* GetDefaultShader (size_t layer) const
    00302     {
    00303       return layers[layer].defaultShader;
    00304     }
    00305
    00312     size_t GetMaxLightNum (size_t layer) const
    00313     {
    00314       return layers[layer].maxLights;
    00315     }
    00316
    00321     size_t GetMaxLightPasses (size_t layer) const
    00322     {
    00323       return layers[layer].maxLightPasses;
    00324     }
    00329     bool IsAmbientLayer (size_t layer) const
    00330     {
    00331       return layers[layer].isAmbient;
    00332     }
    00334     iShaderVariableContext* GetSVContext (size_t layer) const
    00335     {
    00336       return layers[layer].svContext;
    00337     }
    00339
    00340     StaticLightsSettings& GetStaticLightsSettings ()
    00341     {
    00342       return staticLights;
    00343     }
    00344     const StaticLightsSettings& GetStaticLightsSettings () const
    00345     {
    00346       return staticLights;
    00347     }
    00349
    00351     void Clear()
    00352     {
    00353       layers.DeleteAll ();
    00354       layerTypes.DeleteAll ();
    00355     }
    00356   private:
    00357     StaticLightsSettings staticLights;
    00358     struct Layer
    00359     {
    00360       csRef<iShader> defaultShader;
    00361       size_t maxLightPasses;
    00362       size_t maxLights;
    00363       size_t firstType;
    00364       size_t numTypes;
    00365       bool isAmbient;
    00366       csRef<iShaderVariableContext> svContext;
    00367     };
    00368     csArray<Layer> layers;
    00369     csDirtyAccessArray<StringIDValue> layerTypes;
    00370   };
    00371
    00372   enum
    00373   {
    00374     defaultlayerNoTerrain = 1,
    00375     defaultlayerNoLighting = 2
    00376   };
    00377
    00378   /* @@@ TODO: Perhaps revisit naming after light support was added,
    00379       see how many seperate layer objects needed for base vs. lighting
    00380       (I[res] could imagine that light iteration needs another layer
    00381       object). */
    00382   void CS_CRYSTALSPACE_EXPORT AddDefaultBaseLayers (iObjectRegistry* objectReg,
    00383     MultipleRenderLayer& layers, uint flags = 0, iShader* defaultShader = 0);
    00384
    00388   bool CS_CRYSTALSPACE_EXPORT AddLayersFromDocument (
    00389     iObjectRegistry* objectReg, iDocumentNode* node,
    00390     MultipleRenderLayer& layers);
    00391
    00395   bool CS_CRYSTALSPACE_EXPORT AddLayersFromFile (
    00396     iObjectRegistry* objectReg, const char* fileName,
    00397     MultipleRenderLayer& layers);
    00398 }
    00399 }
    00400
    00401 #endif