-
Notifications
You must be signed in to change notification settings - Fork 59
/
OgreMaterialParser.cc
211 lines (190 loc) · 6.2 KB
/
OgreMaterialParser.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <iostream>
#include <sstream>
#include "ConfigLoader.hh"
#include "OgreMaterialParser.hh"
using namespace gzweb;
/////////////////////////////////////////////////
OgreMaterialParser::OgreMaterialParser()
{
this->configLoader = new ConfigLoader(".material");
}
/////////////////////////////////////////////////
OgreMaterialParser::~OgreMaterialParser()
{
delete this->configLoader;
}
/////////////////////////////////////////////////
void OgreMaterialParser::Load(const std::string &_path)
{
ConfigLoader::loadAllFiles(this->configLoader, _path);
}
/////////////////////////////////////////////////
std::string OgreMaterialParser::GetMaterialAsJson() const
{
std::string jsonStr = "{";
std::map<std::string, ConfigNode *> scripts =
this->configLoader->getAllConfigScripts();
std::map<std::string, ConfigNode *>::iterator it;
bool first = true;
for (it = scripts.begin(); it != scripts.end(); ++it)
{
std::string name = it->first;
ConfigNode *node = it->second;
ConfigNode *techniqueNode = node->findChild("technique");
if (techniqueNode)
{
ConfigNode *passNode = techniqueNode->findChild("pass");
if (passNode)
{
if (!first)
jsonStr += ", ";
else
first = false;
std::size_t index = name.rfind(" ");
if (index != std::string::npos)
{
name = name.substr(index+1);
}
jsonStr += "\"" + name + "\":{";
ConfigNode *ambientNode = passNode->findChild("ambient");
if (ambientNode)
{
std::stringstream ss;
std::vector<std::string> values = ambientNode->getValues();
if (values.size() == 1)
ss << "\"";
for (unsigned int i = 0; i < values.size(); ++i)
{
std::string value = ambientNode->getValue(i);
if (value[0] == '.')
value = '0' + value;
ss << value;
if (i != values.size() - 1)
ss << ",";
}
if (values.size() == 1)
ss << "\"";
jsonStr += "\"ambient\":[" + ss.str() + "],";
}
ConfigNode *diffuseNode = passNode->findChild("diffuse");
if (diffuseNode)
{
std::stringstream ss;
std::vector<std::string> values = diffuseNode->getValues();
if (values.size() == 1)
ss << "\"";
for (unsigned int i = 0; i < values.size(); ++i)
{
std::string value = diffuseNode->getValue(i);
if (value[0] == '.')
value = '0' + value;
ss << value;
if (i != values.size() - 1)
ss << ",";
}
if (values.size() == 1)
ss << "\"";
jsonStr += "\"diffuse\":[" + ss.str() + "],";
}
ConfigNode *specularNode = passNode->findChild("specular");
if (specularNode)
{
std::stringstream ss;
std::vector<std::string> values = specularNode->getValues();
if (values.size() == 1)
ss << "\"";
for (unsigned int i = 0; i < values.size(); ++i)
{
std::string value = specularNode->getValue(i);
if (value[0] == '.')
value = '0' + value;
ss << value;
if (i != values.size() - 1)
ss << ",";
}
if (values.size() == 1)
ss << "\"";
jsonStr += "\"specular\":[" + ss.str() + "],";
}
ConfigNode *depthWriteNode = passNode->findChild("depth_write");
if (depthWriteNode)
{
std::stringstream ss;
std::string depthWriteStr = depthWriteNode->getValue(0);
if (depthWriteStr == "off")
ss << "false";
else
ss << "true";
jsonStr += "\"depth_write\":" + ss.str() + ",";
}
ConfigNode *depthCheckNode = passNode->findChild("depth_check");
if (depthCheckNode)
{
std::stringstream ss;
std::string depthCheckStr = depthCheckNode->getValue(0);
if (depthCheckStr == "off")
ss << "false";
else
ss << "true";
jsonStr += "\"depth_check\":" + ss.str() + ",";
}
ConfigNode *textureUnitNode = passNode->findChild("texture_unit");
if (textureUnitNode)
{
ConfigNode *textureNode = textureUnitNode->findChild("texture");
if (textureNode)
{
std::string textureStr = textureNode->getValue(0);
index = textureStr.rfind(".");
if (index != std::string::npos)
{
textureStr = textureStr.substr(0, index+1) + "png";
}
jsonStr += "\"texture\":\"" + textureStr + "\",";
}
ConfigNode *scaleNode = textureUnitNode->findChild("scale");
if (scaleNode)
{
std::stringstream ss;
std::vector<std::string> values = scaleNode->getValues();
if (values.size() == 1)
ss << "\"";
for (unsigned int i = 0; i < values.size(); ++i)
{
std::string value = scaleNode->getValue(i);
if (value[0] == '.')
value = '0' + value;
ss << value;
if (i != values.size() - 1)
ss << ",";
}
if (values.size() == 1)
ss << "\"";
jsonStr += "\"scale\":[" + ss.str() + "],";
}
ConfigNode *alphaOpNode = textureUnitNode->findChild("alpha_op_ex");
if (alphaOpNode)
{
std::stringstream ss;
std::vector<std::string> values = alphaOpNode->getValues();
// a bit hacky, just assuming there is an alpha value to use
// fix this to support more ogre alpha operations.
if (values[1] == "src_manual")
{
ss << values[3];
}
jsonStr += "\"opacity\":" + ss.str() + ",";
}
}
if (jsonStr[jsonStr.size()-1] == ',')
{
jsonStr = jsonStr.substr(0, jsonStr.size()-1);
}
jsonStr += "}";
}
}
}
jsonStr += "}";
// std::cout << jsonStr << std::endl;
return jsonStr;
}