Fix game:addSpawnShapesByZone
[ryzomcore.git] / nel / tools / 3d / zone_dump / zone_dump.cpp
blobde6872463e0faba8d0c43a21e6382806e2b677cb
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "nel/misc/stream.h"
18 #include "nel/misc/file.h"
19 #include "nel/misc/vector.h"
20 #include "nel/misc/time_nl.h"
21 #include "nel/3d/zone.h"
22 #include "nel/3d/landscape.h"
23 #include "nel/misc/triangle.h"
24 #include "../zone_lib/zone_utility.h" // load a header file from zone_welder project
26 #include <stdio.h>
27 #include <float.h>
29 using namespace std;
30 using namespace NLMISC;
31 using namespace NL3D;
33 void buildFaces(CLandscape& landscape, sint zoneId, sint patch, std::vector<CTriangle> &faces)
35 faces.clear();
37 CZone* pZone=landscape.getZone (zoneId);
39 // Then trace all patch.
40 sint N= pZone->getNumPatchs();
41 nlassert(patch>=0);
42 nlassert(patch<N);
43 const CPatch *pa= const_cast<const CZone*>(pZone)->getPatch(patch);
45 // Build the faces.
46 //=================
47 sint ordS= 4*pa->getOrderS();
48 sint ordT= 4*pa->getOrderT();
49 sint x,y;
50 float OOS= 1.0f/ordS;
51 float OOT= 1.0f/ordT;
52 for(y=0;y<ordT;y++)
54 for(x=0;x<ordS;x++)
56 CTriangle f;
57 f.V0= pa->computeVertex(x*OOS, y*OOT);
58 f.V1= pa->computeVertex(x*OOS, (y+1)*OOT);
59 f.V2= pa->computeVertex((x+1)*OOS, (y+1)*OOT);
60 faces.push_back(f);
61 f.V0= pa->computeVertex(x*OOS, y*OOT);
62 f.V1= pa->computeVertex((x+1)*OOS, (y+1)*OOT);
63 f.V2= pa->computeVertex((x+1)*OOS, y*OOT);
64 faces.push_back(f);
69 int main(int argc, char* argv[])
71 try
73 // Good number of args ?
74 if (argc!=4)
76 // Help message
77 printf ("zone_dump [first_zone.zone] [last_zone.zone] [output.dump]\n");
78 printf ("Dump file format:\n");
79 printf ("\t4 bytes: number of triangles\n");
80 printf ("\tfor each triangles:\n");
81 printf ("\t\t3 floats, X, Y, Z for Vertex 0\n");
82 printf ("\t\t3 floats, X, Y, Z for Vertex 1\n");
83 printf ("\t\t3 floats, X, Y, Z for Vertex 2\n");
84 printf ("\t\tVertices are CCW, in a right hand basis with Z axis to the top\n");
86 else
88 // Get zones coordinates
89 uint16 xMin;
90 uint16 yMin;
91 if (!getZoneCoordByName(getName (argv[1]).c_str(), xMin, yMin))
92 fprintf (stderr, "Invalid zone name: %s\n", argv[1]);
93 else
95 // Get zones coordinates
96 uint16 xMax;
97 uint16 yMax;
98 if (!getZoneCoordByName(getName (argv[2]).c_str(), xMax, yMax))
99 fprintf (stderr, "Invalid zone name: %s\n", argv[2]);
100 else
102 // Reorder coordinates
103 uint16 tmp;
104 if (xMax<xMin)
106 tmp=xMin;
107 xMin=xMax;
108 xMax=tmp;
110 if (yMax<yMin)
112 tmp=yMin;
113 yMin=yMax;
114 yMax=tmp;
117 // Open the output file
118 COFile output;
119 if (output.open (argv[3]))
121 // Serial a tmp size
122 uint32 zero=0;
123 output.serial (zero);
125 // Triangles counter
126 uint32 triangles=0;
128 // Get zones path name
129 string path=getDir (argv[1]);
130 string ext=getExt (argv[1]);
132 // For all the zones
133 for (int y=yMin; y<=yMax; y++)
134 for (int x=xMin; x<=xMax; x++)
136 // Name of the zone
137 string name;
139 // Generate the zone name
140 getZoneNameByCoord(x, y, name);
142 // Open the zone
143 CIFile input;
144 if (input.open(path+name+ext))
146 // Warning message
147 printf ("Dump %s\n", name.c_str());
149 // Landscape
150 CLandscape landscape;
152 // Create a zone
153 CZone zone;
155 // Serial the zone
156 zone.serial (input);
158 // Add the zone
159 landscape.addZone (zone);
161 // Add triangle of this zone in the quadtree
162 for (uint patch=0; patch<(uint)zone.getNumPatchs(); patch++)
164 // vector of triangle
165 std::vector<CTriangle> faces;
167 // Build a list of triangles at 50 cm
168 buildFaces (landscape, zone.getZoneId(), patch, faces);
170 // Add to the file
171 for (uint tri=0; tri<faces.size(); tri++)
173 // Serial the triangle
174 faces[tri].V0.serial (output);
175 faces[tri].V1.serial (output);
176 faces[tri].V2.serial (output);
179 // Triangle count
180 triangles+=(uint32)faces.size();
183 else
184 // Warning message
185 printf ("Dump %s (missing)\n", name.c_str());
188 // Get the current pos
189 sint32 curPos=output.getPos ();
191 // File at the beginning
192 output.seek (0, NLMISC::IStream::begin);
194 // Write the triangle count
195 output.serial (triangles);
197 // Go to the end of the file
198 output.seek (curPos, NLMISC::IStream::begin);
200 // Close the file
201 output.close ();
203 else
205 fprintf (stderr, "Can't open %s for writing.", argv[3]);
208 // Export the zones
214 catch (const Exception& e)
216 fprintf (stderr, "FATAL: %s", e.what());
219 // exit.
220 return 0;