1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
8 | Also see acknowledgements in Readme.html |
---|
9 | |
---|
10 | You may use this sample code for anything you like, it is not covered by the |
---|
11 | LGPL like the rest of the engine. |
---|
12 | ----------------------------------------------------------------------------- |
---|
13 | */ |
---|
14 | |
---|
15 | #include "MaterialControls.h" |
---|
16 | #include "OgreLogManager.h" |
---|
17 | #include "OgreStringVector.h" |
---|
18 | #include "OgreStringConverter.h" |
---|
19 | #include "OgreConfigFile.h" |
---|
20 | #include "OgreResourceGroupManager.h" |
---|
21 | #include "OgreException.h" |
---|
22 | |
---|
23 | /******************************************************************************** |
---|
24 | MaterialControls Methods |
---|
25 | *********************************************************************************/ |
---|
26 | void MaterialControls::addControl(const Ogre::String& params) |
---|
27 | { |
---|
28 | // params is a string containing using the following format: |
---|
29 | // "<Control Name>, <Shader parameter name>, <Parameter Type>, <Min Val>, <Max Val>, <Parameter Sub Index>" |
---|
30 | |
---|
31 | // break up long string into components |
---|
32 | Ogre::StringVector vecparams = Ogre::StringUtil::split(params, ","); |
---|
33 | |
---|
34 | // if there are not five elements then log error and move on |
---|
35 | if (vecparams.size() != 6) |
---|
36 | { |
---|
37 | Ogre::LogManager::getSingleton().logMessage( |
---|
38 | "Incorrect number of parameters passed in params string for MaterialControls::addControl()" ); |
---|
39 | |
---|
40 | return; |
---|
41 | } |
---|
42 | |
---|
43 | ShaderControl newControl; |
---|
44 | Ogre::StringUtil::trim(vecparams[0]); |
---|
45 | newControl.Name = vecparams[0]; |
---|
46 | |
---|
47 | Ogre::StringUtil::trim(vecparams[1]); |
---|
48 | newControl.ParamName = vecparams[1]; |
---|
49 | |
---|
50 | Ogre::StringUtil::trim(vecparams[2]); |
---|
51 | if (vecparams[2] == "GPU_VERTEX") |
---|
52 | newControl.ValType = GPU_VERTEX; |
---|
53 | else if (vecparams[2] == "GPU_FRAGMENT") |
---|
54 | newControl.ValType = GPU_FRAGMENT; |
---|
55 | |
---|
56 | newControl.MinVal = Ogre::StringConverter::parseReal(vecparams[3]); |
---|
57 | newControl.MaxVal = Ogre::StringConverter::parseReal(vecparams[4]); |
---|
58 | newControl.ElementIndex = Ogre::StringConverter::parseInt(vecparams[5]); |
---|
59 | |
---|
60 | mShaderControlsContainer.push_back(newControl); |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | void loadMaterialControlsFile(MaterialControlsContainer& controlsContainer, const Ogre::String& filename) |
---|
65 | { |
---|
66 | // Load material controls from config file |
---|
67 | Ogre::ConfigFile cf; |
---|
68 | |
---|
69 | try |
---|
70 | { |
---|
71 | |
---|
72 | cf.load(filename, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "\t;=", true); |
---|
73 | |
---|
74 | // Go through all sections & controls in the file |
---|
75 | Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
---|
76 | |
---|
77 | Ogre::String secName, typeName, materialName, dataString; |
---|
78 | |
---|
79 | while (seci.hasMoreElements()) |
---|
80 | { |
---|
81 | secName = seci.peekNextKey(); |
---|
82 | Ogre::ConfigFile::SettingsMultiMap* settings = seci.getNext(); |
---|
83 | if (!secName.empty() && settings) |
---|
84 | { |
---|
85 | materialName = cf.getSetting("material", secName); |
---|
86 | |
---|
87 | MaterialControls newMaaterialControls(secName, materialName); |
---|
88 | controlsContainer.push_back(newMaaterialControls); |
---|
89 | |
---|
90 | size_t idx = controlsContainer.size() - 1; |
---|
91 | |
---|
92 | Ogre::ConfigFile::SettingsMultiMap::iterator i; |
---|
93 | |
---|
94 | for (i = settings->begin(); i != settings->end(); ++i) |
---|
95 | { |
---|
96 | typeName = i->first; |
---|
97 | dataString = i->second; |
---|
98 | if (typeName == "control") |
---|
99 | controlsContainer[idx].addControl(dataString); |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | Ogre::LogManager::getSingleton().logMessage( "Material Controls setup" ); |
---|
105 | } |
---|
106 | catch (Ogre::Exception e) |
---|
107 | { |
---|
108 | // Guess the file didn't exist |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | void loadAllMaterialControlFiles(MaterialControlsContainer& controlsContainer) |
---|
114 | { |
---|
115 | Ogre::StringVectorPtr fileStringVector = Ogre::ResourceGroupManager::getSingleton().findResourceNames( Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "*.controls" ); |
---|
116 | std::vector<Ogre::String>::iterator controlsFileNameIterator = fileStringVector->begin(); |
---|
117 | |
---|
118 | while ( controlsFileNameIterator != fileStringVector->end() ) |
---|
119 | { |
---|
120 | loadMaterialControlsFile(controlsContainer, *controlsFileNameIterator); |
---|
121 | ++controlsFileNameIterator; |
---|
122 | } |
---|
123 | } |
---|