forked from Alfwich/CubicEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaterialContainer.cpp
285 lines (232 loc) · 8.13 KB
/
MaterialContainer.cpp
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "MaterialContainer.h"
std::unordered_map<std::string,Material*> MaterialContainer::materials;
Material* MaterialContainer::lastMaterial = NULL;
bool MaterialContainer::loadMaterialFile( std::string path )
{
std::string line;
std::ifstream myfile ( path );
Material* currentMaterial = NULL;
std::string texture = "";
std::string textureName = "";
std::string textureFullPath = "";
if (myfile.is_open())
{
while ( myfile.good() )
{
getline ( myfile, line );
// Ignore empty lines
if( line.size() == 0 )
continue;
// Get rid of double spaces
StringFunctions::spliceOut( line, " ", " " );
// Ignore comments
if( StringFunctions::beginsWith( line, "#" ) )
continue;
// Create new materials
if( StringFunctions::beginsWith( line, "newmtl" ) )
{
currentMaterial = new Material;
currentMaterial->name = StringFunctions::getPram( line, 2, ' ');
materials[currentMaterial->name] = currentMaterial;
continue;
}
// Dont allow past this point unless there is a bound material
if( currentMaterial == NULL )
continue;
// Specular Exponent
if( StringFunctions::beginsWith( line, "Ns" ) )
{
currentMaterial->specularExponent = atoi( StringFunctions::getPram( line, 2, ' ' ).c_str() );
continue;
}
// Material Ambience
if( StringFunctions::beginsWith( line, "Ka" ) )
{
currentMaterial->ambience.r = static_cast<float>(atof( StringFunctions::getPram( line, 2, ' ' ).c_str() ));
currentMaterial->ambience.g = static_cast<float>(atof( StringFunctions::getPram( line, 3, ' ' ).c_str() ));
currentMaterial->ambience.b = static_cast<float>(atof( StringFunctions::getPram( line, 4, ' ' ).c_str() ));
continue;
}
// Material Diffuse
if( StringFunctions::beginsWith( line, "Kd" ) )
{
currentMaterial->diffuse.r = static_cast<float>(atof( StringFunctions::getPram( line, 2, ' ' ).c_str() ));
currentMaterial->diffuse.g = static_cast<float>(atof( StringFunctions::getPram( line, 3, ' ' ).c_str() ));
currentMaterial->diffuse.b = static_cast<float>(atof( StringFunctions::getPram( line, 4, ' ' ).c_str() ));
continue;
}
// Material Specular
if( StringFunctions::beginsWith( line, "Ks" ) )
{
currentMaterial->specularity.r = static_cast<float>(atof( StringFunctions::getPram( line, 2, ' ' ).c_str() ));
currentMaterial->specularity.g = static_cast<float>(atof( StringFunctions::getPram( line, 3, ' ' ).c_str() ));
currentMaterial->specularity.b = static_cast<float>(atof( StringFunctions::getPram( line, 4, ' ' ).c_str() ));
continue;
}
// Dissolve
if( StringFunctions::beginsWith( line, "d" ) )
{
currentMaterial->dissolve = static_cast<float>( atof( StringFunctions::getPram( line, 2, ' ' ).c_str() ) );
continue;
}
// Illumination Model
if( StringFunctions::beginsWith( line, "illum" ) )
{
currentMaterial->illuminationModel = atoi( StringFunctions::getPram( line, 2, ' ' ).c_str() );
continue;
}
// Texture
if( StringFunctions::beginsWith( line, "map_Kd" ) )
{
StringFunctions::removeFront( line, 7 );
textureFullPath = line;
StringFunctions::spliceOut( line, "\\", " " );
texture = StringFunctions::getPram( line, StringFunctions::getNumOfPrams( line, ' ' ), ' ' );
textureName = StringFunctions::getPram( texture, 1, '.' );
if( GLTextureContainer::hasTexture( textureName ) )
{
currentMaterial->texture = GLTextureContainer::getTexture( textureName )->textureID;
currentMaterial->textureName = textureName;
continue;
}
if( GLTextureContainer::addTexture( textureFullPath, textureName ) != NULL )
{
currentMaterial->texture = GLTextureContainer::getTexture( textureName )->textureID;
currentMaterial->textureName = textureName;
continue;
}
if( GLTextureContainer::addTexture( "obj/"+texture, textureName ) != NULL )
{
currentMaterial->texture = GLTextureContainer::getTexture( textureName )->textureID;
currentMaterial->textureName = textureName;
continue;
}
if( GLTextureContainer::addTexture( "image/"+texture, textureName ) != NULL )
{
currentMaterial->texture = GLTextureContainer::getTexture( textureName )->textureID;
currentMaterial->textureName = textureName;
continue;
}
currentMaterial->texture = -1;
currentMaterial->textureName = "";
continue;
}
}
}
return true;
}
Material* MaterialContainer::NewMaterial( std::string name )
{
if( hasMaterial( name ) )
return materials[name];
materials[name] = new Material;
materials[name]->name = name;
lastMaterial = materials[name];
return materials[name];
}
bool MaterialContainer::ExportMaterials( std::string path )
{
std::unordered_map<std::string, Material*>::iterator iter;
std::string newLine = "\n";
std::ofstream materialFile;
materialFile.open ( path );
materialFile << "#Created by Cubic Engine, ©AW-Games.com 2012-2013, All Rights Reserved" << newLine << newLine;
for( iter = materials.begin(); iter != materials.end(); iter++ )
{
materialFile << "newmtl " << iter->first << newLine;
materialFile << "Ns " << iter->second->specularExponent << newLine;
materialFile << "Ka " << iter->second->ambience.r << ' ' << iter->second->ambience.g << ' ' << iter->second->ambience.b << newLine;
materialFile << "Kd " << iter->second->diffuse.r << ' ' << iter->second->diffuse.g << ' ' << iter->second->diffuse.b << newLine;
materialFile << "Ks " << iter->second->specularity.r << ' ' << iter->second->specularity.g << ' ' << iter->second->specularity.b << newLine;
if( iter->second->textureName != "" )
materialFile << "map_Kd " << iter->second->textureName << newLine;
materialFile << newLine;
}
materialFile.close();
return true;
}
void MaterialContainer::addMaterial( Material* material, std::string name )
{
if( hasMaterial( name ) || material == NULL )
return;
lastMaterial = material;
materials[name] = material;
}
// Sets the diffuse of the last material
void MaterialContainer::SetDiffuse( Color4F diffuse )
{
if( lastMaterial == NULL )
return;
lastMaterial->diffuse.r = diffuse.r;
lastMaterial->diffuse.g = diffuse.g;
lastMaterial->diffuse.b = diffuse.b;
lastMaterial->diffuse.a = diffuse.a;
}
// Sets the ambience of the last material
void MaterialContainer::SetAmbience( Color4F ambience )
{
if( lastMaterial == NULL )
return;
lastMaterial->ambience.r = ambience.r;
lastMaterial->ambience.g = ambience.g;
lastMaterial->ambience.b = ambience.b;
lastMaterial->ambience.a = ambience.a;
}
// Sets the emissive of the last material
void MaterialContainer::SetEmissive( Color4F emissive )
{
if( lastMaterial == NULL )
return;
lastMaterial->emissive.r = emissive.r;
lastMaterial->emissive.g = emissive.g;
lastMaterial->emissive.b = emissive.b;
lastMaterial->emissive.a = emissive.a;
}
// Sets the specularity of the last material
void MaterialContainer::SetSpecularity( Color4F specularity )
{
if( lastMaterial == NULL )
return;
lastMaterial->specularity.r = specularity.r;
lastMaterial->specularity.g = specularity.g;
lastMaterial->specularity.b = specularity.b;
lastMaterial->specularity.a = specularity.a;
}
// Sets the specularity exponent
void MaterialContainer::SetSpecularityExponent( int specExponent )
{
if( lastMaterial == NULL )
return;
lastMaterial->specularExponent = specExponent;
}
// Sets the texture of the material
void MaterialContainer::SetTexture( Texture* texture, std::string name )
{
if( lastMaterial == NULL )
return;
lastMaterial->texture = texture->textureID;
lastMaterial->textureName = name;
}
// -Associated with GLTextureContainer
void MaterialContainer::SetTexture( std::string textureName )
{
if( lastMaterial == NULL || !GLTextureContainer::hasTexture( textureName ) )
return;
lastMaterial->textureName = textureName;
lastMaterial->texture = GLTextureContainer::getTexture( textureName )->textureID;
}
void MaterialContainer::clear()
{
}
Material* MaterialContainer::getMaterial( std::string name )
{
if( materials.find( name ) == materials.end() )
return NULL;
return materials[name];
}
bool MaterialContainer::hasMaterial( std::string name )
{
if( materials.find( name ) == materials.end() )
return false;
return true;
}