Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
IntermediateValues.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
7
8namespace MMAP
9{
11 {
12 rcFreeCompactHeightfield(compactHeightfield);
13 rcFreeHeightField(heightfield);
14 rcFreeContourSet(contours);
15 rcFreePolyMesh(polyMesh);
16 rcFreePolyMeshDetail(polyMeshDetail);
17 }
18
20 {
21 char fileName[255];
22 char tileString[25];
23 snprintf(tileString, sizeof(tileString), "[%02u,%02u]: ", tileX, tileY);
24
25 printf("%sWriting debug output... \r", tileString);
26
27 std::string name("meshes/%04u_%02i_%02i.");
28
29#define DEBUG_WRITE(fileExtension,data) \
30 do { \
31 snprintf(fileName, sizeof(fileName), (name + fileExtension).c_str(), mapID, tileY, tileX); \
32 FILE* file = fopen(fileName, "wb"); \
33 if (!file) \
34 { \
35 char message[1024]; \
36 snprintf(message, sizeof(message), "%sFailed to open %s for writing!\n", tileString, fileName); \
37 perror(message); \
38 } \
39 else \
40 debugWrite(file, data); \
41 if (file) fclose(file); \
42 printf("%sWriting debug output... \r", tileString); \
43 } while (false)
44
45 if (heightfield)
49 if (contours)
50 DEBUG_WRITE("cs", contours);
51 if (polyMesh)
52 DEBUG_WRITE("pmesh", polyMesh);
55
56#undef DEBUG_WRITE
57 }
58
59 void IntermediateValues::debugWrite(FILE* file, const rcHeightfield* mesh)
60 {
61 if (!file || !mesh)
62 return;
63
64 fwrite(&(mesh->cs), sizeof(float), 1, file);
65 fwrite(&(mesh->ch), sizeof(float), 1, file);
66 fwrite(&(mesh->width), sizeof(int), 1, file);
67 fwrite(&(mesh->height), sizeof(int), 1, file);
68 fwrite(mesh->bmin, sizeof(float), 3, file);
69 fwrite(mesh->bmax, sizeof(float), 3, file);
70
71 for (int y = 0; y < mesh->height; ++y)
72 for (int x = 0; x < mesh->width; ++x)
73 {
74 rcSpan* span = mesh->spans[x+y*mesh->width];
75
76 // first, count the number of spans
77 int spanCount = 0;
78 while (span)
79 {
80 spanCount++;
81 span = span->next;
82 }
83
84 // write the span count
85 fwrite(&spanCount, sizeof(int), 1, file);
86
87 // write the spans
88 span = mesh->spans[x+y*mesh->width];
89 while (span)
90 {
91 fwrite(span, sizeof(rcSpan), 1, file);
92 span = span->next;
93 }
94 }
95 }
96
97 void IntermediateValues::debugWrite(FILE* file, const rcCompactHeightfield* chf)
98 {
99 if (!file || !chf)
100 return;
101
102 fwrite(&(chf->width), sizeof(chf->width), 1, file);
103 fwrite(&(chf->height), sizeof(chf->height), 1, file);
104 fwrite(&(chf->spanCount), sizeof(chf->spanCount), 1, file);
105
106 fwrite(&(chf->walkableHeight), sizeof(chf->walkableHeight), 1, file);
107 fwrite(&(chf->walkableClimb), sizeof(chf->walkableClimb), 1, file);
108
109 fwrite(&(chf->maxDistance), sizeof(chf->maxDistance), 1, file);
110 fwrite(&(chf->maxRegions), sizeof(chf->maxRegions), 1, file);
111
112 fwrite(chf->bmin, sizeof(chf->bmin), 1, file);
113 fwrite(chf->bmax, sizeof(chf->bmax), 1, file);
114
115 fwrite(&(chf->cs), sizeof(chf->cs), 1, file);
116 fwrite(&(chf->ch), sizeof(chf->ch), 1, file);
117
118 int tmp = 0;
119 if (chf->cells) tmp |= 1;
120 if (chf->spans) tmp |= 2;
121 if (chf->dist) tmp |= 4;
122 if (chf->areas) tmp |= 8;
123
124 fwrite(&tmp, sizeof(tmp), 1, file);
125
126 if (chf->cells)
127 fwrite(chf->cells, sizeof(rcCompactCell), chf->width*chf->height, file);
128 if (chf->spans)
129 fwrite(chf->spans, sizeof(rcCompactSpan), chf->spanCount, file);
130 if (chf->dist)
131 fwrite(chf->dist, sizeof(unsigned short), chf->spanCount, file);
132 if (chf->areas)
133 fwrite(chf->areas, sizeof(unsigned char), chf->spanCount, file);
134 }
135
136 void IntermediateValues::debugWrite(FILE* file, const rcContourSet* cs)
137 {
138 if (!file || !cs)
139 return;
140
141 fwrite(&(cs->cs), sizeof(float), 1, file);
142 fwrite(&(cs->ch), sizeof(float), 1, file);
143 fwrite(cs->bmin, sizeof(float), 3, file);
144 fwrite(cs->bmax, sizeof(float), 3, file);
145 fwrite(&(cs->nconts), sizeof(int), 1, file);
146 for (int i = 0; i < cs->nconts; ++i)
147 {
148 fwrite(&cs->conts[i].area, sizeof(unsigned char), 1, file);
149 fwrite(&cs->conts[i].reg, sizeof(unsigned short), 1, file);
150 fwrite(&cs->conts[i].nverts, sizeof(int), 1, file);
151 fwrite(cs->conts[i].verts, sizeof(int), cs->conts[i].nverts*4, file);
152 fwrite(&cs->conts[i].nrverts, sizeof(int), 1, file);
153 fwrite(cs->conts[i].rverts, sizeof(int), cs->conts[i].nrverts*4, file);
154 }
155 }
156
157 void IntermediateValues::debugWrite(FILE* file, const rcPolyMesh* mesh)
158 {
159 if (!file || !mesh)
160 return;
161
162 fwrite(&(mesh->cs), sizeof(float), 1, file);
163 fwrite(&(mesh->ch), sizeof(float), 1, file);
164 fwrite(&(mesh->nvp), sizeof(int), 1, file);
165 fwrite(mesh->bmin, sizeof(float), 3, file);
166 fwrite(mesh->bmax, sizeof(float), 3, file);
167 fwrite(&(mesh->nverts), sizeof(int), 1, file);
168 fwrite(mesh->verts, sizeof(unsigned short), mesh->nverts*3, file);
169 fwrite(&(mesh->npolys), sizeof(int), 1, file);
170 fwrite(mesh->polys, sizeof(unsigned short), mesh->npolys*mesh->nvp*2, file);
171 fwrite(mesh->flags, sizeof(unsigned short), mesh->npolys, file);
172 fwrite(mesh->areas, sizeof(unsigned char), mesh->npolys, file);
173 fwrite(mesh->regs, sizeof(unsigned short), mesh->npolys, file);
174 }
175
176 void IntermediateValues::debugWrite(FILE* file, const rcPolyMeshDetail* mesh)
177 {
178 if (!file || !mesh)
179 return;
180
181 fwrite(&(mesh->nverts), sizeof(int), 1, file);
182 fwrite(mesh->verts, sizeof(float), mesh->nverts*3, file);
183 fwrite(&(mesh->ntris), sizeof(int), 1, file);
184 fwrite(mesh->tris, sizeof(char), mesh->ntris*4, file);
185 fwrite(&(mesh->nmeshes), sizeof(int), 1, file);
186 fwrite(mesh->meshes, sizeof(int), mesh->nmeshes*4, file);
187 }
188
190 {
191 char objFileName[255];
192 snprintf(objFileName, sizeof(objFileName), "meshes/map%04u_%02u_%02u.obj", mapID, tileY, tileX);
193
194 FILE* objFile = fopen(objFileName, "wb");
195 if (!objFile)
196 {
197 char message[1024];
198 snprintf(message, sizeof(message), "Failed to open %s for writing!\n", objFileName);
199 perror(message);
200 return;
201 }
202
203 G3D::Array<float> allVerts;
204 G3D::Array<int> allTris;
205
206 allTris.append(meshData.liquidTris);
207 allVerts.append(meshData.liquidVerts);
208 TerrainBuilder::copyIndices(meshData.solidTris, allTris, allVerts.size() / 3);
209 allVerts.append(meshData.solidVerts);
210
211 float* verts = allVerts.getCArray();
212 int vertCount = allVerts.size() / 3;
213 int* tris = allTris.getCArray();
214 int triCount = allTris.size() / 3;
215
216 for (int i = 0; i < allVerts.size() / 3; i++)
217 fprintf(objFile, "v %f %f %f\n", verts[i*3], verts[i*3 + 1], verts[i*3 + 2]);
218
219 for (int i = 0; i < allTris.size() / 3; i++)
220 fprintf(objFile, "f %i %i %i\n", tris[i*3] + 1, tris[i*3 + 1] + 1, tris[i*3 + 2] + 1);
221
222 fclose(objFile);
223
224
225 char tileString[25];
226 snprintf(tileString, sizeof(tileString), "[%02u,%02u]: ", tileY, tileX);
227 printf("%sWriting debug output... \r", tileString);
228
229 snprintf(objFileName, sizeof(objFileName), "meshes/%04u.map", mapID);
230
231 objFile = fopen(objFileName, "wb");
232 if (!objFile)
233 {
234 char message[1024];
235 snprintf(message, sizeof(message), "Failed to open %s for writing!\n", objFileName);
236 perror(message);
237 return;
238 }
239
240 char b = '\0';
241 fwrite(&b, sizeof(char), 1, objFile);
242 fclose(objFile);
243
244 snprintf(objFileName, sizeof(objFileName), "meshes/%04u_%02u_%02u.mesh", mapID, tileY, tileX);
245 objFile = fopen(objFileName, "wb");
246 if (!objFile)
247 {
248 char message[1024];
249 snprintf(message, sizeof(message), "Failed to open %s for writing!\n", objFileName);
250 perror(message);
251 return;
252 }
253
254 fwrite(&vertCount, sizeof(int), 1, objFile);
255 fwrite(verts, sizeof(float), vertCount*3, objFile);
256 fflush(objFile);
257
258 fwrite(&triCount, sizeof(int), 1, objFile);
259 fwrite(tris, sizeof(int), triCount*3, objFile);
260 fflush(objFile);
261
262 fclose(objFile);
263 }
264}
std::uint32_t uint32
Definition Define.h:77
#define DEBUG_WRITE(fileExtension, data)
static void copyIndices(std::vector< VMAP::MeshTriangle > &source, G3D::Array< int > &dest, int offest, bool flip)
rcCompactHeightfield * compactHeightfield
rcPolyMeshDetail * polyMeshDetail
void generateObjFile(uint32 mapID, uint32 tileX, uint32 tileY, MeshData &meshData)
void debugWrite(FILE *file, const rcHeightfield *mesh)
void writeIV(uint32 mapID, uint32 tileX, uint32 tileY)
G3D::Array< float > liquidVerts
G3D::Array< float > solidVerts
G3D::Array< int > liquidTris
G3D::Array< int > solidTris