Resolve "Toggle Free Look with Hotkey"
[ryzomcore.git] / ryzom / client / src / prim_file.cpp
blobbb6b11b1071174ac84d29c78da8c92c99de988d6
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
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/>.
20 /////////////
21 // INCLUDE //
22 /////////////
23 #include "stdpch.h"
25 // game share
26 #include "game_share/brick_types.h"
27 // Client
28 #include "prim_file.h"
29 #include "client_cfg.h"
31 ///////////
32 // USING //
33 ///////////
34 using namespace NLLIGO;
35 using namespace NLMISC;
36 using namespace NL3D;
37 using namespace NLPACS;
38 using namespace NLNET;
39 using namespace std;
41 #define POINT_HEIGHT 1.f
42 #define POINT_SIZE 1.f
44 extern UGlobalRetriever *GR;
45 extern UScene *Scene;
46 extern UTextContext *TextContext;
48 CRGBA PrimColors[] =
50 CRGBA (255, 0, 0),
51 CRGBA (0, 255, 0),
52 CRGBA (0, 0, 255),
53 CRGBA (255, 255, 0),
54 CRGBA (0, 255, 255),
55 CRGBA (255, 0, 255),
56 CRGBA (127, 0, 0),
57 CRGBA (0, 127, 0),
58 CRGBA (0, 0, 127),
59 CRGBA (127, 127, 0),
60 CRGBA (0, 127, 127),
61 CRGBA (127, 0, 127),
64 /////////////
65 // GLOBALS //
66 /////////////
68 // Show prim file
69 CPrimFileMgr PrimFiles;
71 //---------------------------------------------------
73 CPrimFileMgr::CPrimFileMgr ()
75 _ShowPrim = false;
76 _Loaded = false;
77 _PrimFile = 0;
80 //---------------------------------------------------
82 void CPrimFileMgr::adjustPosition (NLMISC::CVector &pos)
84 UGlobalPosition globalPos = GR->retrievePosition (pos);
85 pos.z = GR->getMeanHeight (globalPos) + POINT_HEIGHT;
88 //---------------------------------------------------
90 void CPrimFileMgr::load (sint primFileIndex)
92 _Loaded = false;
93 // Prim file count
94 sint primFileCount = (sint)ClientCfg.PrimFiles.size ();
95 if (primFileCount)
97 // Get a valid index
98 while (primFileIndex >= primFileCount)
100 primFileIndex -= primFileCount;
102 while (primFileIndex < 0)
104 primFileIndex += primFileCount;
107 // Current prim index
108 _PrimFile = primFileIndex;
110 // Get the path name
111 string pathName = CPath::lookup (ClientCfg.PrimFiles[_PrimFile], false, true);
112 if (pathName.empty ())
114 pathName = ClientCfg.PrimFiles[_PrimFile];
117 // Reset the container
118 _PrimRegion = NLLIGO::CPrimRegion ();
120 // Open the file
121 CIFile file;
122 if (file.open (pathName))
126 // Serial XML
127 CIXml xml;
128 xml.init (file);
130 // Serial the region
131 _PrimRegion.serial (xml);
132 _Loaded = true;
134 // Put to the ground
136 // Adjust points
137 uint point;
138 for (point = 0; point < _PrimRegion.VPoints.size (); point++)
140 // Adjuste the position
141 adjustPosition (_PrimRegion.VPoints[point].Point);
144 // Adjust segments
145 uint seg;
146 for (seg = 0; seg < _PrimRegion.VPaths.size (); seg++)
148 // For each
149 uint pointCount = (uint)_PrimRegion.VPaths[seg].VPoints.size ();
151 for (point = 0; point < pointCount; point++)
153 adjustPosition (_PrimRegion.VPaths[seg].VPoints[point]);
157 // Adjust polygons
158 for (seg = 0; seg < _PrimRegion.VZones.size (); seg++)
160 // For each
161 uint pointCount = (uint)_PrimRegion.VZones[seg].VPoints.size ();
163 for (point = 0; point < pointCount; point ++)
165 adjustPosition (_PrimRegion.VZones[seg].VPoints[point]);
170 catch (const Exception &e)
172 // Error
173 nlwarning ("Error while reading the prim file (%s) : %s", pathName.c_str(), e.what ());
175 // Reset the container
176 _PrimRegion = NLLIGO::CPrimRegion ();
179 else
181 // Can't open
182 nlwarning ("Can't open the prim file %s", pathName.c_str ());
187 //---------------------------------------------------
189 void CPrimFileMgr::changeColor (uint &currentColor)
191 const uint colorCount = sizeof (PrimColors) / sizeof (CRGBA);
192 if (currentColor >= colorCount)
193 currentColor = 0;
194 _Material.setColor (PrimColors[currentColor]);
195 TextContext->setColor (PrimColors[currentColor]);
196 currentColor++;
199 //---------------------------------------------------
201 void CPrimFileMgr::draw3dText (const NLMISC::CVector &pos, NL3D::UCamera cam, const char *text)
203 // Create the matrix and set the orientation according to the camera.
204 CMatrix matrix;
205 matrix.identity();
206 matrix.setRot(cam.getRotQuat());
207 matrix.setPos (pos);
208 matrix.scale (60);
210 // Draw the name.
211 TextContext->render3D (matrix, text);
214 //---------------------------------------------------
216 void CPrimFileMgr::display (NL3D::UDriver &driver)
218 if (_ShowPrim)
220 // Material exist ?
221 if (!_Material.empty())
223 _Material = driver.createMaterial ();
224 _Material.initUnlit ();
225 _Material.setZFunc (UMaterial::always);
226 _Material.setZWrite (false);
229 // Remove fog
230 bool fogState = driver.fogEnabled ();
231 driver.enableFog (false);
233 // Load current
234 if (!_Loaded)
235 load (_PrimFile);
237 // Current color
238 uint currentColor = 0;
240 // Draw points
241 uint point;
242 for (point = 0; point < _PrimRegion.VPoints.size (); point++)
244 // Set the color for the next primitive
245 changeColor (currentColor);
247 // Ref on the vector
248 CVector &vect = _PrimRegion.VPoints[point].Point;
250 // Line
251 CLine line;
252 line.V0 = vect;
253 line.V1 = vect;
254 line.V0.x -= POINT_SIZE/2;
255 line.V1.x += POINT_SIZE/2;
256 driver.drawLine (line, _Material);
257 line.V0 = vect;
258 line.V1 = vect;
259 line.V0.y -= POINT_SIZE/2;
260 line.V1.y += POINT_SIZE/2;
261 driver.drawLine (line, _Material);
262 line.V0 = vect;
263 line.V1 = vect;
264 line.V0.z -= POINT_SIZE/2;
265 line.V1.z += POINT_SIZE/2;
266 driver.drawLine (line, _Material);
268 // Draw a text
269 string *name;
270 if (_PrimRegion.VPoints[point].getPropertyByName("name", name))
271 draw3dText (vect + CVector (0, 0, 10 * POINT_HEIGHT), Scene->getCam(), name->c_str ());
274 // Draw segments
275 uint seg;
276 for (seg = 0; seg < _PrimRegion.VPaths.size (); seg++)
278 // Set the color for the next primitive
279 changeColor (currentColor);
281 // Mean pos
282 CVector pos (0,0,0);
284 // For each
285 uint pointCount = (uint)_PrimRegion.VPaths[seg].VPoints.size ();
286 vector<CPrimVector> &points = _PrimRegion.VPaths[seg].VPoints;
288 // Some points ?
289 if (pointCount != 0)
291 pos = points[0];
292 for (point = 0; point < pointCount-1; point++)
294 // Draw the line
295 CLine line;
296 line.V0 = points[point];
297 line.V1 = points[point+1];
298 driver.drawLine (line, _Material);
299 pos += points[point+1];
302 pos /= (float) pointCount;
304 // Draw a text
305 string *name;
306 if (_PrimRegion.VPaths[seg].getPropertyByName("name", name))
307 draw3dText (pos + CVector (0, 0, 10 * POINT_HEIGHT), Scene->getCam(), name->c_str ());
311 // Draw polygons
312 for (seg = 0; seg < _PrimRegion.VZones.size (); seg++)
314 // Set the color for the next primitive
315 changeColor (currentColor);
317 // For each
318 uint pointCount = (uint)_PrimRegion.VZones[seg].VPoints.size ();
319 vector<CPrimVector> &points = _PrimRegion.VZones[seg].VPoints;
321 // Some points ?
322 if (pointCount != 0)
324 // Mean pos
325 CVector pos (0,0,0);
327 // Previous point
328 NLMISC::CVector *previous = &points[pointCount-1];
330 for (point = 0; point < pointCount; point ++)
332 // Next point
333 NLMISC::CVector *next = &points[point];
335 // Draw the line
336 CLine line;
337 line.V0 = *previous;
338 line.V1 = *next;
339 driver.drawLine (line, _Material);
341 pos += points[point];
343 previous = next;
346 pos /= (float) pointCount;
348 // Draw a text
349 string *name;
350 if (_PrimRegion.VZones[seg].getPropertyByName("name", name))
351 draw3dText (pos + CVector (0, 0, POINT_HEIGHT), Scene->getCam(), name->c_str ());
355 // Reset fog
356 driver.enableFog (fogState);
360 //---------------------------------------------------
362 void CPrimFileMgr::release (NL3D::UDriver &driver)
364 driver.deleteMaterial (_Material);
367 //---------------------------------------------------
369 string CPrimFileMgr::getCurrentPrimitive () const
371 if (_ShowPrim)
373 if ( (_PrimFile < (sint)ClientCfg.PrimFiles.size ()) && (_PrimFile >= 0) )
375 return ClientCfg.PrimFiles[_PrimFile];
378 return "";