[5] | 1 | /****************************************************************************** |
---|
| 2 | Copyright (c) W.J. van der Laan |
---|
| 3 | |
---|
| 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
---|
| 5 | this software and associated documentation files (the "Software"), to deal in |
---|
| 6 | the Software without restriction, including without limitation the rights to use, |
---|
| 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
---|
| 8 | Software, and to permit persons to whom the Software is furnished to do so, subject |
---|
| 9 | to the following conditions: |
---|
| 10 | |
---|
| 11 | The above copyright notice and this permission notice shall be included in all copies |
---|
| 12 | or substantial portions of the Software. |
---|
| 13 | |
---|
| 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
| 15 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
---|
| 16 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
---|
| 17 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
---|
| 18 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE |
---|
| 19 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
| 20 | ******************************************************************************/ |
---|
| 21 | |
---|
| 22 | #include "MaterialGenerator.h" |
---|
| 23 | |
---|
| 24 | #include "OgreStringConverter.h" |
---|
| 25 | #include "OgreException.h" |
---|
| 26 | |
---|
| 27 | #include "OgrePass.h" |
---|
| 28 | #include "OgreTechnique.h" |
---|
| 29 | |
---|
| 30 | #include "OgreHighLevelGpuProgram.h" |
---|
| 31 | #include "OgreHighLevelGpuProgramManager.h" |
---|
| 32 | |
---|
| 33 | using namespace Ogre; |
---|
| 34 | |
---|
| 35 | MaterialGenerator::MaterialGenerator(): |
---|
| 36 | vsMask(0), fsMask(0), matMask(0), mImpl(0) |
---|
| 37 | { |
---|
| 38 | } |
---|
| 39 | MaterialGenerator::~MaterialGenerator() |
---|
| 40 | { |
---|
| 41 | delete mImpl; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | const Ogre::MaterialPtr &MaterialGenerator::getMaterial(Perm permutation) |
---|
| 45 | { |
---|
| 46 | /// Check input validity |
---|
| 47 | size_t totalBits = bitNames.size(); |
---|
| 48 | size_t totalPerms = 1<<totalBits; |
---|
| 49 | assert(permutation < totalPerms); |
---|
| 50 | |
---|
| 51 | /// Check if material/shader permutation already was generated |
---|
| 52 | MaterialMap::iterator i = mMaterials.find(permutation); |
---|
| 53 | if(i != mMaterials.end()) |
---|
| 54 | { |
---|
| 55 | return i->second; |
---|
| 56 | } |
---|
| 57 | else |
---|
| 58 | { |
---|
| 59 | /// Create it |
---|
| 60 | Ogre::MaterialPtr templ = getTemplateMaterial(permutation & matMask); |
---|
| 61 | Ogre::GpuProgramPtr vs = getVertexShader(permutation & vsMask); |
---|
| 62 | Ogre::GpuProgramPtr fs = getFragmentShader(permutation & fsMask); |
---|
| 63 | |
---|
| 64 | /// Create material name |
---|
| 65 | String name=materialBaseName; |
---|
| 66 | for(size_t bit=0; bit<totalBits; ++bit) |
---|
| 67 | if(permutation & (1<<bit)) |
---|
| 68 | name += bitNames[bit]; |
---|
| 69 | |
---|
| 70 | std::cerr << name << " " << vs->getName() << " " << fs->getName() << std::endl; |
---|
| 71 | /// Create material from template, and set shaders |
---|
| 72 | MaterialPtr mat = templ->clone(name); |
---|
| 73 | Technique *tech = mat->getTechnique(0); |
---|
| 74 | Pass *pass = tech->getPass(0); |
---|
| 75 | pass->setFragmentProgram(fs->getName()); |
---|
| 76 | pass->setVertexProgram(vs->getName()); |
---|
| 77 | |
---|
| 78 | /// And store it |
---|
| 79 | mMaterials[permutation] = mat; |
---|
| 80 | return mMaterials[permutation]; |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | const Ogre::GpuProgramPtr &MaterialGenerator::getVertexShader(Perm permutation) |
---|
| 85 | { |
---|
| 86 | ProgramMap::iterator i = mVs.find(permutation); |
---|
| 87 | if(i != mVs.end()) |
---|
| 88 | { |
---|
| 89 | return i->second; |
---|
| 90 | } |
---|
| 91 | else |
---|
| 92 | { |
---|
| 93 | /// Create it |
---|
| 94 | mVs[permutation] = mImpl->generateVertexShader(permutation); |
---|
| 95 | return mVs[permutation]; |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | const Ogre::GpuProgramPtr &MaterialGenerator::getFragmentShader(Perm permutation) |
---|
| 100 | { |
---|
| 101 | ProgramMap::iterator i = mFs.find(permutation); |
---|
| 102 | if(i != mFs.end()) |
---|
| 103 | { |
---|
| 104 | return i->second; |
---|
| 105 | } |
---|
| 106 | else |
---|
| 107 | { |
---|
| 108 | /// Create it |
---|
| 109 | mFs[permutation] = mImpl->generateFragmentShader(permutation); |
---|
| 110 | return mFs[permutation]; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | const Ogre::MaterialPtr &MaterialGenerator::getTemplateMaterial(Perm permutation) |
---|
| 115 | { |
---|
| 116 | MaterialMap::iterator i = mTemplateMat.find(permutation); |
---|
| 117 | if(i != mTemplateMat.end()) |
---|
| 118 | { |
---|
| 119 | return i->second; |
---|
| 120 | } |
---|
| 121 | else |
---|
| 122 | { |
---|
| 123 | /// Create it |
---|
| 124 | mTemplateMat[permutation] = mImpl->generateTemplateMaterial(permutation); |
---|
| 125 | return mTemplateMat[permutation]; |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | MaterialGenerator::Impl::~Impl() |
---|
| 130 | { |
---|
| 131 | } |
---|
| 132 | |
---|