TrinityCore
IntermediateValues.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "IntermediateValues.h"
19#include "StringFormat.h"
20
21namespace MMAP
22{
24 {
25 rcFreeCompactHeightfield(compactHeightfield);
26 rcFreeHeightField(heightfield);
27 rcFreeContourSet(contours);
28 rcFreePolyMesh(polyMesh);
29 rcFreePolyMeshDetail(polyMeshDetail);
30 }
31
33 {
34 std::string tileString = Trinity::StringFormat("[{:02},{:02}]: ", tileX, tileY);
35
36 printf("%sWriting debug output... \r", tileString.c_str());
37
38 auto debugWrite = [&](char const* extension, auto const* data)
39 {
40 std::string fileName = Trinity::StringFormat("meshes/{:04}{:02}{:02}.{}", mapID, tileY, tileX, extension);
41 if (FILE* file = fopen(fileName.c_str(), "wb"))
42 {
43 this->debugWrite(file, data);
44 fclose(file);
45 }
46 else
47 perror(Trinity::StringFormat("{}Failed to open {} for writing!\n", tileString, fileName).c_str());
48 };
49
50 if (heightfield)
54 if (contours)
55 debugWrite("cs", contours);
56 if (polyMesh)
57 debugWrite("pmesh", polyMesh);
59 debugWrite("dmesh", polyMeshDetail);
60 }
61
62 void IntermediateValues::debugWrite(FILE* file, rcHeightfield const* mesh)
63 {
64 if (!file || !mesh)
65 return;
66
67 fwrite(&(mesh->cs), sizeof(float), 1, file);
68 fwrite(&(mesh->ch), sizeof(float), 1, file);
69 fwrite(&(mesh->width), sizeof(int), 1, file);
70 fwrite(&(mesh->height), sizeof(int), 1, file);
71 fwrite(mesh->bmin, sizeof(float), 3, file);
72 fwrite(mesh->bmax, sizeof(float), 3, file);
73
74 for (int y = 0; y < mesh->height; ++y)
75 for (int x = 0; x < mesh->width; ++x)
76 {
77 rcSpan* span = mesh->spans[x+y*mesh->width];
78
79 // first, count the number of spans
80 int spanCount = 0;
81 while (span)
82 {
83 spanCount++;
84 span = span->next;
85 }
86
87 // write the span count
88 fwrite(&spanCount, sizeof(int), 1, file);
89
90 // write the spans
91 span = mesh->spans[x+y*mesh->width];
92 while (span)
93 {
94 fwrite(span, sizeof(rcSpan), 1, file);
95 span = span->next;
96 }
97 }
98 }
99
100 void IntermediateValues::debugWrite(FILE* file, rcCompactHeightfield const* chf)
101 {
102 if (!file | !chf)
103 return;
104
105 fwrite(&(chf->width), sizeof(chf->width), 1, file);
106 fwrite(&(chf->height), sizeof(chf->height), 1, file);
107 fwrite(&(chf->spanCount), sizeof(chf->spanCount), 1, file);
108
109 fwrite(&(chf->walkableHeight), sizeof(chf->walkableHeight), 1, file);
110 fwrite(&(chf->walkableClimb), sizeof(chf->walkableClimb), 1, file);
111
112 fwrite(&(chf->maxDistance), sizeof(chf->maxDistance), 1, file);
113 fwrite(&(chf->maxRegions), sizeof(chf->maxRegions), 1, file);
114
115 fwrite(chf->bmin, sizeof(chf->bmin), 1, file);
116 fwrite(chf->bmax, sizeof(chf->bmax), 1, file);
117
118 fwrite(&(chf->cs), sizeof(chf->cs), 1, file);
119 fwrite(&(chf->ch), sizeof(chf->ch), 1, file);
120
121 int tmp = 0;
122 if (chf->cells) tmp |= 1;
123 if (chf->spans) tmp |= 2;
124 if (chf->dist) tmp |= 4;
125 if (chf->areas) tmp |= 8;
126
127 fwrite(&tmp, sizeof(tmp), 1, file);
128
129 if (chf->cells)
130 fwrite(chf->cells, sizeof(rcCompactCell), chf->width*chf->height, file);
131 if (chf->spans)
132 fwrite(chf->spans, sizeof(rcCompactSpan), chf->spanCount, file);
133 if (chf->dist)
134 fwrite(chf->dist, sizeof(unsigned short), chf->spanCount, file);
135 if (chf->areas)
136 fwrite(chf->areas, sizeof(unsigned char), chf->spanCount, file);
137 }
138
139 void IntermediateValues::debugWrite(FILE* file, rcContourSet const* cs)
140 {
141 if (!file || !cs)
142 return;
143
144 fwrite(&(cs->cs), sizeof(float), 1, file);
145 fwrite(&(cs->ch), sizeof(float), 1, file);
146 fwrite(cs->bmin, sizeof(float), 3, file);
147 fwrite(cs->bmax, sizeof(float), 3, file);
148 fwrite(&(cs->nconts), sizeof(int), 1, file);
149 for (int i = 0; i < cs->nconts; ++i)
150 {
151 fwrite(&cs->conts[i].area, sizeof(unsigned char), 1, file);
152 fwrite(&cs->conts[i].reg, sizeof(unsigned short), 1, file);
153 fwrite(&cs->conts[i].nverts, sizeof(int), 1, file);
154 fwrite(cs->conts[i].verts, sizeof(int), cs->conts[i].nverts*4, file);
155 fwrite(&cs->conts[i].nrverts, sizeof(int), 1, file);
156 fwrite(cs->conts[i].rverts, sizeof(int), cs->conts[i].nrverts*4, file);
157 }
158 }
159
160 void IntermediateValues::debugWrite(FILE* file, rcPolyMesh const* mesh)
161 {
162 if (!file || !mesh)
163 return;
164
165 fwrite(&(mesh->cs), sizeof(float), 1, file);
166 fwrite(&(mesh->ch), sizeof(float), 1, file);
167 fwrite(&(mesh->nvp), sizeof(int), 1, file);
168 fwrite(mesh->bmin, sizeof(float), 3, file);
169 fwrite(mesh->bmax, sizeof(float), 3, file);
170 fwrite(&(mesh->nverts), sizeof(int), 1, file);
171 fwrite(mesh->verts, sizeof(unsigned short), mesh->nverts*3, file);
172 fwrite(&(mesh->npolys), sizeof(int), 1, file);
173 fwrite(mesh->polys, sizeof(unsigned short), mesh->npolys*mesh->nvp*2, file);
174 fwrite(mesh->flags, sizeof(unsigned short), mesh->npolys, file);
175 fwrite(mesh->areas, sizeof(unsigned char), mesh->npolys, file);
176 fwrite(mesh->regs, sizeof(unsigned short), mesh->npolys, file);
177 }
178
179 void IntermediateValues::debugWrite(FILE* file, rcPolyMeshDetail const* mesh)
180 {
181 if (!file || !mesh)
182 return;
183
184 fwrite(&(mesh->nverts), sizeof(int), 1, file);
185 fwrite(mesh->verts, sizeof(float), mesh->nverts*3, file);
186 fwrite(&(mesh->ntris), sizeof(int), 1, file);
187 fwrite(mesh->tris, sizeof(char), mesh->ntris*4, file);
188 fwrite(&(mesh->nmeshes), sizeof(int), 1, file);
189 fwrite(mesh->meshes, sizeof(int), mesh->nmeshes*4, file);
190 }
191
193 {
194 std::string objFileName;
195 objFileName = Trinity::StringFormat("meshes/map{:04}{:02}{:02}.obj", mapID, tileY, tileX);
196
197 FILE* objFile = fopen(objFileName.c_str(), "wb");
198 if (!objFile)
199 {
200 perror(Trinity::StringFormat("Failed to open {} for writing!\n", objFileName).c_str());
201 return;
202 }
203
204 G3D::Array<float> allVerts;
205 G3D::Array<int> allTris;
206
207 allTris.append(meshData.liquidTris);
208 allVerts.append(meshData.liquidVerts);
209 TerrainBuilder::copyIndices(meshData.solidTris, allTris, allVerts.size() / 3);
210 allVerts.append(meshData.solidVerts);
211
212 float* verts = allVerts.getCArray();
213 int vertCount = allVerts.size() / 3;
214 int* tris = allTris.getCArray();
215 int triCount = allTris.size() / 3;
216
217 for (int i = 0; i < allVerts.size() / 3; i++)
218 fprintf(objFile, "v %f %f %f\n", verts[i*3], verts[i*3 + 1], verts[i*3 + 2]);
219
220 for (int i = 0; i < allTris.size() / 3; i++)
221 fprintf(objFile, "f %i %i %i\n", tris[i*3] + 1, tris[i*3 + 1] + 1, tris[i*3 + 2] + 1);
222
223 fclose(objFile);
224
225 printf("[%02u,%02u]: Writing debug output... \r", tileY, tileX);
226
227 objFileName = Trinity::StringFormat("meshes/map{:04}.map", mapID);
228
229 objFile = fopen(objFileName.c_str(), "wb");
230 if (!objFile)
231 {
232 perror(Trinity::StringFormat("Failed to open {} for writing!\n", objFileName).c_str());
233 return;
234 }
235
236 char b = '\0';
237 fwrite(&b, sizeof(char), 1, objFile);
238 fclose(objFile);
239
240 objFileName = Trinity::StringFormat("meshes/map{:04}{:02}{:02}.mesh", mapID, tileY, tileX);
241 objFile = fopen(objFileName.c_str(), "wb");
242 if (!objFile)
243 {
244 perror(Trinity::StringFormat("Failed to open {} for writing!\n", objFileName).c_str());
245 return;
246 }
247
248 fwrite(&vertCount, sizeof(int), 1, objFile);
249 fwrite(verts, sizeof(float), vertCount*3, objFile);
250 fflush(objFile);
251
252 fwrite(&triCount, sizeof(int), 1, objFile);
253 fwrite(tris, sizeof(int), triCount*3, objFile);
254 fflush(objFile);
255
256 fclose(objFile);
257 }
258}
uint32_t uint32
Definition: Define.h:142
static void copyIndices(std::vector< VMAP::MeshTriangle > const &source, G3D::Array< int > &dest, int offset, bool flip)
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
Definition: StringFormat.h:38
rcCompactHeightfield * compactHeightfield
void debugWrite(FILE *file, rcHeightfield const *mesh)
rcPolyMeshDetail * polyMeshDetail
void generateObjFile(uint32 mapID, uint32 tileX, uint32 tileY, MeshData &meshData)
void writeIV(uint32 mapID, uint32 tileX, uint32 tileY)
G3D::Array< float > liquidVerts
G3D::Array< float > solidVerts
G3D::Array< int > liquidTris
G3D::Array< int > solidTris