-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeshSerializer.cpp
More file actions
110 lines (87 loc) · 3.37 KB
/
MeshSerializer.cpp
File metadata and controls
110 lines (87 loc) · 3.37 KB
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
#include "MeshSerializer.h"
using namespace std;
void MeshSerializer::serialize(const char* fileName, const vector<glm::vec3>& vertices, const vector<unsigned int>& indices, const vector<glm::vec3>& normals){
rapidjson::Document jsonDoc;
jsonDoc.SetObject();
rapidjson::Document::AllocatorType& allocator = jsonDoc.GetAllocator();
jsonDoc.AddMember("type", "dcmesh0", allocator);
// vertex array
rapidjson::Value vertexArray(rapidjson::kArrayType);
for (auto iter = vertices.begin(); iter != vertices.end(); ++iter){
rapidjson::Value objValue;
objValue.SetObject();
char temp[64];
sprintf_s(temp, sizeof(temp), "%f,%f,%f", iter->x, iter->y, iter->z);
rapidjson::Value strObj(rapidjson::kStringType);
strObj.SetString(temp, allocator);
objValue.AddMember("p", strObj, allocator);
vertexArray.PushBack(objValue, allocator);
}
jsonDoc.AddMember("vertexArray", vertexArray, allocator);
// index array
{
rapidjson::Value indexArray(rapidjson::kStringType);
std::string str;
for (auto iter = indices.begin(); iter != indices.end(); ++iter){
char temp[12];
if (iter != indices.begin())
str.append(",");
sprintf_s(temp, sizeof(temp), "%d", *iter);
str.append(temp);
}
indexArray.SetString(str.c_str(), allocator);
jsonDoc.AddMember("indexArray", indexArray, allocator);
}
// normal array
rapidjson::Value normalArray(rapidjson::kArrayType);
for (auto iter = normals.begin(); iter != normals.end(); ++iter){
rapidjson::Value objValue;
objValue.SetObject();
char temp[64];
sprintf_s(temp, sizeof(temp), "%f,%f,%f", iter->x, iter->y, iter->z);
rapidjson::Value strObj(rapidjson::kStringType);
strObj.SetString(temp, allocator);
objValue.AddMember("n", strObj, allocator);
normalArray.PushBack(objValue, allocator);
}
jsonDoc.AddMember("normalArray", normalArray, allocator);
// end of data
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
jsonDoc.Accept(writer);
CharBuffer outBuffer;
outBuffer.buffer = const_cast<char *>(strbuf.GetString());
outBuffer.length = strbuf.GetSize();
CharHelper::writeTextFile(fileName, outBuffer);
}
void MeshSerializer::serializeIndexFile(const char* indexPath, const vector<string> fileNames, const vector<glm::ivec4>& coords){
rapidjson::Document jsonDoc;
jsonDoc.SetObject();
rapidjson::Document::AllocatorType& allocator = jsonDoc.GetAllocator();
jsonDoc.AddMember("type", "dcindex", allocator);
// vertex array
rapidjson::Value vertexArray(rapidjson::kArrayType);
for (int i = 0; i < fileNames.size(); i++){
rapidjson::Value objValue;
objValue.SetObject();
{
rapidjson::Value strObj(rapidjson::kStringType);
strObj.SetString(fileNames[i].c_str(), allocator);
objValue.AddMember("file_name", strObj, allocator);
}
objValue.AddMember("coord_X", coords[i].x, allocator);
objValue.AddMember("coord_Y", coords[i].y, allocator);
objValue.AddMember("coord_Z", coords[i].z, allocator);
objValue.AddMember("coord_W", coords[i].w, allocator);
vertexArray.PushBack(objValue, allocator);
}
jsonDoc.AddMember("files", vertexArray, allocator);
// end of data
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
jsonDoc.Accept(writer);
CharBuffer outBuffer;
outBuffer.buffer = const_cast<char *>(strbuf.GetString());
outBuffer.length = strbuf.GetSize();
CharHelper::writeTextFile(indexPath, outBuffer);
}