SO3Engine
SO3Technique.cpp
Go to the documentation of this file.
1
8#include <boost/format.hpp>
9
10#include "SO3Renderer/SO3Root.h"
15#include "SO3Material/SO3Pass.h"
16
17namespace SO3
18{
19
20STechnique::STechnique(SMaterial* material, Ogre::Technique* ogreTechniqueRef) : SData(ogreTechniqueRef->getName())
21{
22 ogreTechnique = ogreTechniqueRef;
23 parentMaterial = material;
24 if (ogreTechniqueRef->getSchemeName() == "" || ogreTechniqueRef->getSchemeName() == "Default" || ogreTechniqueRef->getSchemeName() == Ogre::Root::getSingleton().getRenderSystem()->_getDefaultViewportMaterialScheme())
25 ogreTechniqueRef->setSchemeName("SO3_LEGACY"); //Ogre::MSN_SHADERGEN
26
27 originalScheme = ogreTechniqueRef->getSchemeName();
28
29 //$BB manage techniques with empty names
30 if(name.empty())
31 {
32 Ogre::Material::Techniques techs = ogreTechniqueRef->getParent()->getTechniques();
33 for (unsigned int techid = 0; techid < techs.size() && name.empty(); techid++)
34 {
35 if (techs[techid] == ogreTechniqueRef)
36 name = boost::str(boost::format("%1%") % techid);
37 }
38 }
39
40 // Generate passes objects
41 Ogre::Technique::Passes passes = ogreTechnique->getPasses();
42 for (unsigned int passid = 0; passid < passes.size(); passid++)
43 {
44 Ogre::Pass* ogrePass = passes[passid];
45 SPass* pass = new SPass(this, ogrePass);
46 ogrePass->getUserObjectBindings().setUserAny("SPass", Ogre::Any(pass));
47 //ogrePass->setDepthBias(0.1f, 0.1f);
48 }
49
50 //generate shadow pass
53}
54
55STechnique::STechnique() : SData("")
56{
57 // Forbiden (private)
58}
59
61{
62 // Destroy passes
63 Ogre::Technique::Passes passes = ogreTechnique->getPasses();
64 for (unsigned int passid = 0; passid < passes.size(); passid++)
65 {
66 Ogre::Pass* ogrePass = passes[passid];
67 Ogre::Any bindedSPass = ogrePass->getUserObjectBindings().getUserAny("SPass");
68 if (bindedSPass.has_value())
69 {
70 SPass* pass = Ogre::any_cast<SPass*> (bindedSPass);
71 SO3_SAFE_DELETE(pass);
72 }
73 }
74
75 Ogre::Any bindedSTechnique = ogreTechnique->getUserObjectBindings().getUserAny("STechnique");
76 if(bindedSTechnique.has_value())
77 ogreTechnique->getUserObjectBindings().eraseUserAny("STechnique");
78}
79
81{
82 return ogreTechnique;
83}
84
89
91{
92 assert(ogreTechnique != 0);
93 return ogreTechnique->getNumPasses();
94}
95
96SPass* STechnique::GetPass(const unsigned int& pass)
97{
98 assert(ogreTechnique != 0);
99 if ((pass >= 0) && (pass < ogreTechnique->getNumPasses()))
100 {
101 Ogre::Pass* ogrePass = ogreTechnique->getPass(pass);
102 Ogre::Any bindedSPass = ogrePass->getUserObjectBindings().getUserAny("SPass");
103 if (!bindedSPass.has_value())
104 return 0;
105
106 SPass* matPass = Ogre::any_cast<SPass*> (bindedSPass);
107 return matPass;
108 }
109 else
110 return 0;
111}
112
113int STechnique::GetPassIndexByName(const std::string& passName)
114{
115 assert(ogreTechnique != 0);
116
117 bool found = false;
118 int index = -1;
119
120 int numPass = ogreTechnique->getNumPasses();
121 int p = 0;
122
123 while(!found && p<numPass)
124 {
125 Ogre::Pass* myPass = ogreTechnique->getPass(p);
126 if (myPass != 0)
127 {
128 if(myPass->getName() == passName)
129 {
130 index = p;
131 found = true;
132 }
133 }
134 p++;
135 }
136
137 return index;
138}
139
140void STechnique::SetSchemeName(std::string scheme)
141{
142 if (scheme == "" || scheme == "Default" || scheme == Ogre::Root::getSingleton().getRenderSystem()->_getDefaultViewportMaterialScheme())
143 ogreTechnique->setSchemeName("SO3_LEGACY");
144 else
145 ogreTechnique->setSchemeName(scheme);
146}
147
148
150{
151 // Generate pass
152 Ogre::Pass* ogrePass = ogreTechnique->createPass();
153 SPass* pass = new SPass(this, ogrePass);
154 ogrePass->getUserObjectBindings().setUserAny("SPass", Ogre::Any(pass));
155 //ogrePass->setDepthBias(0.1f, 0.1f);
156}
157
158void STechnique::RemovePass(const unsigned int& pass)
159{
160 if ((pass >= 0) && (pass < ogreTechnique->getNumPasses()))
161 {
162 Ogre::Pass* ogrePass = ogreTechnique->getPass(pass);
163 Ogre::Any bindedSPass = ogrePass->getUserObjectBindings().getUserAny("SPass");
164 assert(bindedSPass.has_value());
165 SPass* matPass = Ogre::any_cast<SPass*> (bindedSPass);
166 SO3_SAFE_DELETE(matPass);
167 }
168}
169
171{
172 return originalScheme;
173}
174
175void STechnique::SetPointSize(Ogre::Real size)
176{
177 Ogre::Technique::Passes ogrePasses = ogreTechnique->getPasses();
178 for (unsigned int i = 0; i < ogrePasses.size(); i++)
179 {
180 if (ogrePasses[i]->getPolygonMode() == Ogre::PM_POINTS)
181 ogrePasses[i]->setPointSize(size);
182 }
183
185}
186
188{
189 if (full)
191 else
192 SRoot::getSingletonPtr()->RemoveGeneratedMaterial(GetParentMaterial()->getOgreMaterialPointer().get());
193
194 Ogre::Technique::Passes passes = ogreTechnique->getPasses();
195 for (unsigned int passid = 0; passid < passes.size(); passid++)
196 {
197 Ogre::Pass* ogrePass = passes[passid];
198 Ogre::Any bindedSPass = ogrePass->getUserObjectBindings().getUserAny("SPass");
199 if (bindedSPass.has_value())
200 {
201 SPass* matPass = Ogre::any_cast<SPass*> (bindedSPass);
202
203 if (matPass->IsShaderGenerated())
204 matPass->BuildShader(full);
205 }
206 }
207
208 //generate shadow pass
209 if (GetParentMaterial()->GetScene()->GetShadowManager())
211}
212
213}
std::string name
Definition SO3DataScol.h:44
static void CleanUpGeneratedTechniques(Ogre::Material *mat)
SScene * GetScene()
bool IsShaderGenerated()
Definition SO3Pass.cpp:151
void BuildShader(bool force=false)
Definition SO3Pass.cpp:165
void RemoveGeneratedMaterial(Ogre::Material *mat)
Definition SO3Root.cpp:2343
static SRoot * getSingletonPtr()
Definition SO3Root.cpp:111
SShadowManager * GetShadowManager()
void UpdateShadowMaterial(Ogre::Technique *tech)
void UpdateGeneratedTech(bool full=false)
Ogre::String originalScheme
SMaterial * GetParentMaterial()
int GetPassIndexByName(const std::string &passName)
void RemovePass(const unsigned int &pass)
void SetPointSize(Ogre::Real size)
SPass * GetPass(const unsigned int &pass)
const Ogre::String GetOriginalScheme()
SMaterial * parentMaterial
Ogre::Technique * GetOgreTechniquePointer()
void SetSchemeName(std::string scheme)
Ogre::Technique * ogreTechnique