/
StaticMesh.cpp
128 lines (103 loc) · 2.96 KB
/
StaticMesh.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
#include "StaticMesh.h"
#include "ObjLoader\objLoader.h"
StaticMesh::StaticMesh()
: SubMeshes(subMeshes)
{
}
StaticMesh::~StaticMesh(void)
{
}
void StaticMesh::LoadObj(std::string filename, bool smoothNormals, bool generateNormals, bool loadMaterial)
{
std::string cachedName = filename + ".cache";
FILE* cached = fopen(cachedName.c_str(), "rb");
bool readFromCache = false;
MeshCacheInfo mci;
if (cached)
{
size_t fileSize;
fseek(cached, 0L, SEEK_END);
fileSize = ftell(cached);
fseek(cached, 0L, SEEK_SET);
if (fileSize > sizeof(int))
{
fread(&mci.numSubMeshes, sizeof(int), 1, cached);
if (fileSize > sizeof(int) + mci.numSubMeshes * sizeof(size_t))
{
mci.subMeshSize = (size_t*)malloc(sizeof(size_t) * mci.numSubMeshes);
fread(mci.subMeshSize, sizeof(size_t), mci.numSubMeshes, cached);
size_t subMeshDataSize = 0;
for (int i = 0; i < mci.numSubMeshes; ++i)
{
subMeshDataSize += mci.subMeshSize[i];
}
subMeshDataSize += sizeof(int) + mci.numSubMeshes * sizeof(size_t);
if (fileSize == subMeshDataSize)
readFromCache = true;
}
}
}
if (!readFromCache)
{
if (cached)
{
fclose(cached);
cached = 0;
}
objLoader* obj = new objLoader();
printf("Parsing file: %s...", filename.c_str());
obj->load(filename.c_str());
printf(" done.\n");
if (loadMaterial == false || obj->materialCount <= 1)
{
SubMesh subMesh;
subMesh.LoadObj(obj, smoothNormals, generateNormals);
subMeshes.insert(meshEntry("submesh0", subMesh));
}
else
return; //need support for multiple materials etc.
printf("Caching...");
cached = fopen(cachedName.c_str(), "wb");
MeshCacheInfo mci;
mci.numSubMeshes = subMeshes.size();
mci.subMeshSize = (size_t*)malloc(sizeof(size_t) * mci.numSubMeshes);
std::vector<SubMeshBlock> smbs;
std::map<std::string, SubMesh>::iterator iter;
int counter = 0;
for (iter = subMeshes.begin(); iter != subMeshes.end(); ++iter)
{
smbs.push_back(iter->second.GenerateCache());
mci.subMeshSize[counter] = smbs[counter].size;
++counter;
}
fwrite(&mci.numSubMeshes, sizeof(int), 1, cached);
fwrite(mci.subMeshSize, sizeof(size_t), mci.numSubMeshes, cached);
long bytePosition = ftell(cached);
for (int i = 0; i < mci.numSubMeshes; ++i)
{
fwrite(smbs[i].data, smbs[i].size, 1, cached);
}
SubMeshBlock* smb = &smbs[0];
bytePosition = ftell(cached);
fclose(cached);
printf(" done.\n");
}
else
{
printf("Loading from cached file: %s...\n", cachedName.c_str());
for (int i = 0; i < mci.numSubMeshes; ++i)
{
unsigned char* data = (unsigned char*)malloc(mci.subMeshSize[i]);
long bytePosition = ftell(cached);
size_t bytesRead = fread(data, sizeof(unsigned char), mci.subMeshSize[i], cached);
bytePosition = ftell(cached);
SubMesh subMesh;
subMesh.LoadCached(data);
char name[16];
sprintf(name, "submesh%d", i);
subMeshes.insert(meshEntry(name, subMesh));
free(data);
}
fclose(cached);
}
}