-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
brfData.cpp
382 lines (319 loc) · 9.91 KB
/
brfData.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* OpenBRF -- by marco tarini. Provided under GNU General Public License */
#include "brfData.h"
#include "saveLoad.h"
const char * tokenBrfName[N_TOKEN] = {
"mesh",
"texture",
"shader",
"material",
"skeleton",
"skeleton_anim",
"body",
};
const char* BrfData::GetFirstObjectName() const{
if (mesh.size()!=0) return mesh[0].name;
if (texture.size()!=0) return texture[0].name;
if (shader.size()!=0) return shader[0].name;
if (material.size()!=0) return material[0].name;
if (skeleton.size()!=0) return skeleton[0].name;
if (animation.size()!=0) return animation[0].name;
if (body.size()!=0) return body[0].name;
return NULL;
}
template <class BrfType> void __AppendAll( const std::vector<BrfType> v, std::string& c){
for (unsigned int i=0; i<v.size(); i++ ) {
if (!c.empty()) c.append(", ");
c.append(v[i].name);
}
}
const char* BrfData::GetAllObjectNames() const{
static std::string c;
c.clear();
__AppendAll(mesh,c);
__AppendAll(texture,c);
__AppendAll(shader,c);
__AppendAll(material,c);
__AppendAll(skeleton,c);
__AppendAll(animation,c);
__AppendAll(body,c);
return c.c_str();
}
const char* BrfData::GetAllObjectNamesAsSceneProps(int *nFound, int *nFoundWithB) const{
static std::string c;
c.clear();
char lastName[1024]; lastName[0]=0;
*nFound = 0;
*nFoundWithB = 0;
for (unsigned int i=0; i<mesh.size(); i++) {
char bodyName[1024];
char baseName[1024];
sprintf(baseName,"\"%s\"",mesh[i].baseName);
char fullstr[1024*4];
if (strcmp(lastName,mesh[i].baseName)==0) continue;
sprintf(bodyName, "bo_%s",mesh[i].baseName );
bool hasBody = (Find(bodyName,BODY)!=-1);
sprintf(fullstr,"\t( %-45s,0,%s,\"%s\",[]),\n",
baseName,baseName,( (hasBody)?bodyName:"0")
);
(*nFound)++;
if (hasBody) (*nFoundWithB)++;
c.append(fullstr);
sprintf(lastName,mesh[i].baseName);
}
if (!*nFound) return NULL;
return c.c_str();
}
bool BrfData::IsOneSkelOneHitbox() const{
return 1
&& (skeleton.size()==1)
&& (body.size()==1)
&& (totSize() == 2 )
&& (body[0].part.size()==skeleton[0].bone.size())
&& (0==strcmp(body[0].name,skeleton[0].name))
;
}
BrfData::BrfData(){
version = 0;
//globalVersion = 0;
}
BrfData::BrfData(FILE*f,int verbose){
Load(f,verbose);
}
BrfData::BrfData(const char*f,int verbose){
Load(f,verbose);
}
int BrfData::GetFirstUnusedLetter() const{
vector<bool> used(255,false);
for (unsigned int i=0; i<mesh.size(); i++) {
char let = mesh[i].name[4];
used[let]=true;
}
for (char i='A'; i<'Z'; i++) if (!used[i]) return i-'A';
return -1;
}
BrfMesh BrfData::GetCompleteSkin(int pos) const{
BrfMesh res;
bool first = true;
for (unsigned int i=0; i<mesh.size(); i++) {
char let = mesh[i].name[4];
if (let==char(pos+'A')) {
BrfMesh a = mesh[i];
a.KeepOnlyFrame(0);
if (first) res=a; else res.Merge(a);
first=false;
}
}
return res;
}
template <class T>
static void mergeVec(vector<T> &a, const vector<T> &b){
for (unsigned int i=0; i<b.size(); i++) a.push_back(b[i]);
}
template <class T>
static int myfind(const vector<T> &b, const char* name) {
for (unsigned int i=0; i<b.size(); i++) if (strcmp(b[i].name,name)==0) return i;
return -1;
}
unsigned int BrfData::totSize() const{
return mesh.size() +
material.size() +
shader.size() +
texture.size() +
body.size() +
skeleton.size() +
animation.size();
}
unsigned int BrfData::size(int token) const{
switch (token) {
case MESH: return mesh.size();
case MATERIAL: return material.size();
case SHADER: return shader.size();
case TEXTURE: return texture.size();
case BODY: return body.size();
case SKELETON: return skeleton.size();
case ANIMATION: return animation.size();
}
return 0;
}
int BrfData::FindTextureWithExt(const char* name){
int k = myfind(texture,name);
if (k>=0) return k;
char full[1024];
sprintf(full,"%s.dds",name);
return myfind(texture, full);
}
bool BrfData::HasAnyTangentDirs() const{
for (unsigned int i=0; i<mesh.size(); i++) {
if (mesh[i].StoresTangentField()) return true;
}
return false;
}
int BrfData::Find(const char* name, int token) const{
switch (token) {
case MESH: return myfind(mesh,name);
case MATERIAL: return myfind(material,name);
case SHADER: return myfind(shader,name);
case TEXTURE: return myfind(texture,name);
case BODY: return myfind(body,name);
case SKELETON: return myfind(skeleton,name);
case ANIMATION: return myfind(animation,name);
}
return -1;
}
BrfBody* BrfData::FindBody(const char* name){
int i= myfind(body,name); if (i<0) return NULL; else return &(body[i]);
}
BrfMesh* BrfData::FindMesh(const char* name){
int i= myfind(mesh,name); if (i<0) return NULL; else return &(mesh[i]);
}
void BrfData::Merge(const BrfData& b){
mergeVec(mesh, b.mesh);
mergeVec(texture, b.texture);
mergeVec(shader, b.shader);
mergeVec(material, b.material);
mergeVec(skeleton, b.skeleton);
mergeVec(animation, b.animation);
mergeVec(body, b.body);
}
bool BrfData::Load(const char *filename,int verbose, int imposeVersion){
FILE *f = fopen(filename,"rb");
if (!f) return false;
return Load(f, verbose, imposeVersion);
}
template<class BrfType> void BrfData::SaveAll(FILE *f, const vector<BrfType> &v) const{
if (v.size()==0) return;
SaveString(f,tokenBrfName[ BrfType::tokenIndex() ]);
SaveInt(f, v.size());
for (unsigned int i=0; i<v.size(); i++) {
v[i].Save(f);
}
}
int BrfData::getOneSkeleton(int nbones, int after){
for (unsigned int i=0; i<skeleton.size(); i++){
int j = (i+after)%skeleton.size();
if ((int)skeleton[j].bone.size()>=nbones) { return j; }
}
return -1;
}
int globVersion;
bool BrfData::Save(FILE *f) const{
globVersion = version;
if (globVersion==1) { SaveString(f, "rfver "); SaveInt(f,1); }
SaveAll(f,texture);
SaveAll(f,shader);
SaveAll(f,material);
SaveAll(f,mesh);
SaveAll(f,skeleton);
SaveAll(f,animation);
SaveAll(f,body);
SaveString(f,"end");
return true;
}
bool BrfData::Save(const char*fn) const{
FILE *f = fopen(fn,"wb");
if (!f) return false;
Save(f);
fclose(f);
return true;
}
const char* BrfData::GetName(int i, int token) const{
switch (token) {
case MESH: return mesh[i].name;
case MATERIAL: return material[i].name;
case SHADER: return shader[i].name;
case TEXTURE: return texture[i].name;
case BODY: return body[i].name;
case SKELETON: return skeleton[i].name;
case ANIMATION: return animation[i].name;
}
assert(0);
return NULL;
}
int BrfData::FirstToken() const{
if(mesh.size()) return MESH;
if(material.size()) return MATERIAL;
if(shader.size()) return SHADER;
if(texture.size()) return TEXTURE;
if(body.size()) return BODY;
if(skeleton.size()) return SKELETON;
if(animation.size()) return ANIMATION;
return -1;
}
void BrfData::ForgetTextureLocations(){
for (unsigned int i=0; i<material.size(); i++) {
material[i].rgbLocation=BrfMaterial::UNKNOWN;
material[i].bumpLocation=BrfMaterial::UNKNOWN;
material[i].specLocation=BrfMaterial::UNKNOWN;
}
}
void BrfData::Clear(){
mesh.clear();
texture.clear();
shader.clear();
material.clear();
skeleton.clear();
animation.clear();
body.clear();
}
void BrfData::LoadVersion(FILE*f,int imposeVers){
LoadInt(f,version);
if (imposeVers>-1) version = imposeVers;
globVersion=version;
}
bool BrfData::Load(FILE*f,int verbose,int imposeVers){
Clear();
version = 0;
globVersion = version;
while (1) {
char str[255];
if (!LoadString(f, str)) return false;
if (verbose>1) printf("Read \"%s\"\n",str);
if (!strcmp(str,"end")) break;
else if (!strcmp(str,"rfver ")) LoadVersion(f,imposeVers);
else if (!strcmp(str,"mesh")) {
if (!LoadVector(f,mesh)) return false;
// int k; LoadInt(f,k); mesh.resize(1); mesh[0].Load(f); return true;
}
else if (!strcmp(str,"texture")) {if (!LoadVector(f,texture)) return false;}
else if (!strcmp(str,"shader")) {if (!LoadVector(f,shader)) return false;}
else if (!strcmp(str,"material")) {if (!LoadVector(f,material)) return false;}
else if (!strcmp(str,"skeleton")) {if (!LoadVector(f,skeleton)) return false;}
else if (!strcmp(str,"skeleton_anim")) {if (!LoadVector(f,animation)) return false;}
else if (!strcmp(str,"body")) {if (!LoadVector(f,body)) return false; }
else {
//printf("ERROR! Unknown token \"%s\"\n",str);
fflush(stdout);
fclose(f);
return false;
}
}
fclose(f);
return true;
}
bool BrfData::LoadFast(const char*filename, bool faster){
FILE *f = fopen(filename,"rb");
if (!f) return false;
version = 0;
globVersion = version;
while (1) {
char str[255];
if (!LoadString(f, str)) return false;
if (!strcmp(str,"end")) break;
else if (!strcmp(str,"rfver ")) LoadVersion(f,-1);
else if (!strcmp(str,"shader")) {if (!SkipVector(f,shader)) return false;}
else if (!strcmp(str,"texture")) {if (!LoadVector(f,texture)) return false; }
else if (!strcmp(str,"material")) {if (!SkipVector(f,material)) return false; }
else if (!strcmp(str,"mesh")) { if (faster) break; if (!SkipVector(f,mesh)) return false;}
else if (!strcmp(str,"skeleton")) {if (!SkipVector(f,skeleton)) return false;}
else if (!strcmp(str,"skeleton_anim")) { if (faster) break; if (!SkipVector(f,animation)) return false;}
else if (!strcmp(str,"body")) { if (faster) break; if (!SkipVector(f,body)) return false;}
else {
//printf("ERROR! Unknown token \"%s\"\n",str);
fflush(stdout);
fclose(f);
return false;
}
}
fclose(f);
return true;
}