| 1 | /** |
|---|
| 2 | ******************************************************************************* |
|---|
| 3 | Copyright (c) W.J. van der Laan |
|---|
| 4 | |
|---|
| 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
|---|
| 6 | this software and associated documentation files (the "Software"), to deal in |
|---|
| 7 | the Software without restriction, including without limitation the rights to use, |
|---|
| 8 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
|---|
| 9 | Software, and to permit persons to whom the Software is furnished to do so, subject |
|---|
| 10 | to the following conditions: |
|---|
| 11 | |
|---|
| 12 | The above copyright notice and this permission notice shall be included in all copies |
|---|
| 13 | or substantial portions of the Software. |
|---|
| 14 | |
|---|
| 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
|---|
| 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
|---|
| 17 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|---|
| 18 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|---|
| 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE |
|---|
| 20 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|---|
| 21 | ******************************************************************************* |
|---|
| 22 | */ |
|---|
| 23 | #ifndef H_WJ_MaterialGenerator |
|---|
| 24 | #define H_WJ_MaterialGenerator |
|---|
| 25 | |
|---|
| 26 | #include "OgreMaterial.h" |
|---|
| 27 | #include "OgreStringVector.h" |
|---|
| 28 | |
|---|
| 29 | /** Caching, on-the-fly material generator. This is a class that automatically |
|---|
| 30 | generates and stores different permutations of a material, and its shaders. |
|---|
| 31 | It can be used if you have a material that has lots of slightly different |
|---|
| 32 | variations, like whether to use a specular light, skinning, normal mapping |
|---|
| 33 | and other options. Writing all these out is a tedioius job. Of course it is |
|---|
| 34 | possible to always use the material with all features, but that might result |
|---|
| 35 | in large, slow shader programs. This class provides an efficient solution |
|---|
| 36 | to that. |
|---|
| 37 | */ |
|---|
| 38 | class MaterialGenerator |
|---|
| 39 | { |
|---|
| 40 | public: |
|---|
| 41 | /** Bitfield used to signify a material permutations */ |
|---|
| 42 | typedef Ogre::uint32 Perm; |
|---|
| 43 | ~MaterialGenerator(); |
|---|
| 44 | |
|---|
| 45 | const Ogre::MaterialPtr &getMaterial(Perm permutation); |
|---|
| 46 | |
|---|
| 47 | /** Implementation class that takes care of actual generation or lookup |
|---|
| 48 | of the various constituent parts (template material, fragment shader |
|---|
| 49 | and vertex shader). These methods are only called once for every permutation, |
|---|
| 50 | after which the result is stored and re-used. |
|---|
| 51 | */ |
|---|
| 52 | class Impl |
|---|
| 53 | { |
|---|
| 54 | public: |
|---|
| 55 | virtual ~Impl(); |
|---|
| 56 | virtual Ogre::GpuProgramPtr generateVertexShader(Perm permutation) = 0; |
|---|
| 57 | virtual Ogre::GpuProgramPtr generateFragmentShader(Perm permutation) = 0; |
|---|
| 58 | virtual Ogre::MaterialPtr generateTemplateMaterial(Perm permutation) = 0; |
|---|
| 59 | }; |
|---|
| 60 | protected: |
|---|
| 61 | /** The constructor is protected as this base class should never be constructed |
|---|
| 62 | as-is. It is meant to be subclassed so that values can be assigned to |
|---|
| 63 | the various fields controlling material generator, and most importantly, the |
|---|
| 64 | mImpl field. |
|---|
| 65 | */ |
|---|
| 66 | MaterialGenerator(); |
|---|
| 67 | |
|---|
| 68 | const Ogre::GpuProgramPtr &getVertexShader(Perm permutation); |
|---|
| 69 | const Ogre::GpuProgramPtr &getFragmentShader(Perm permutation); |
|---|
| 70 | const Ogre::MaterialPtr &getTemplateMaterial(Perm permutation); |
|---|
| 71 | |
|---|
| 72 | /// Base name of materials generated by this |
|---|
| 73 | Ogre::String materialBaseName; |
|---|
| 74 | /// Name of every bit |
|---|
| 75 | Ogre::StringVector bitNames; |
|---|
| 76 | /// Mask of permutation bits that influence vertex shader choice |
|---|
| 77 | Perm vsMask; |
|---|
| 78 | /// Mask of permutation bits that influence fragment shader choice |
|---|
| 79 | Perm fsMask; |
|---|
| 80 | /// Mask of permutation bits that influence template material choice |
|---|
| 81 | Perm matMask; |
|---|
| 82 | /// Generator |
|---|
| 83 | Impl *mImpl; |
|---|
| 84 | |
|---|
| 85 | typedef std::map<Perm, Ogre::GpuProgramPtr> ProgramMap; |
|---|
| 86 | typedef std::map<Perm, Ogre::MaterialPtr> MaterialMap; |
|---|
| 87 | |
|---|
| 88 | ProgramMap mVs, mFs; |
|---|
| 89 | MaterialMap mTemplateMat, mMaterials; |
|---|
| 90 | }; |
|---|
| 91 | |
|---|
| 92 | #endif |
|---|