1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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/>.
26 #include "game_share/brick_types.h"
28 #include "prim_file.h"
29 #include "client_cfg.h"
34 using namespace NLLIGO
;
35 using namespace NLMISC
;
37 using namespace NLPACS
;
38 using namespace NLNET
;
41 #define POINT_HEIGHT 1.f
42 #define POINT_SIZE 1.f
44 extern UGlobalRetriever
*GR
;
46 extern UTextContext
*TextContext
;
69 CPrimFileMgr PrimFiles
;
71 //---------------------------------------------------
73 CPrimFileMgr::CPrimFileMgr ()
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
)
94 sint primFileCount
= (sint
)ClientCfg
.PrimFiles
.size ();
98 while (primFileIndex
>= primFileCount
)
100 primFileIndex
-= primFileCount
;
102 while (primFileIndex
< 0)
104 primFileIndex
+= primFileCount
;
107 // Current prim index
108 _PrimFile
= primFileIndex
;
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 ();
122 if (file
.open (pathName
))
131 _PrimRegion
.serial (xml
);
138 for (point
= 0; point
< _PrimRegion
.VPoints
.size (); point
++)
140 // Adjuste the position
141 adjustPosition (_PrimRegion
.VPoints
[point
].Point
);
146 for (seg
= 0; seg
< _PrimRegion
.VPaths
.size (); seg
++)
149 uint pointCount
= (uint
)_PrimRegion
.VPaths
[seg
].VPoints
.size ();
151 for (point
= 0; point
< pointCount
; point
++)
153 adjustPosition (_PrimRegion
.VPaths
[seg
].VPoints
[point
]);
158 for (seg
= 0; seg
< _PrimRegion
.VZones
.size (); seg
++)
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
)
173 nlwarning ("Error while reading the prim file (%s) : %s", pathName
.c_str(), e
.what ());
175 // Reset the container
176 _PrimRegion
= NLLIGO::CPrimRegion ();
182 nlwarning ("Can't open the prim file %s", pathName
.c_str ());
187 //---------------------------------------------------
189 void CPrimFileMgr::changeColor (uint
¤tColor
)
191 const uint colorCount
= sizeof (PrimColors
) / sizeof (CRGBA
);
192 if (currentColor
>= colorCount
)
194 _Material
.setColor (PrimColors
[currentColor
]);
195 TextContext
->setColor (PrimColors
[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.
206 matrix
.setRot(cam
.getRotQuat());
211 TextContext
->render3D (matrix
, text
);
214 //---------------------------------------------------
216 void CPrimFileMgr::display (NL3D::UDriver
&driver
)
221 if (!_Material
.empty())
223 _Material
= driver
.createMaterial ();
224 _Material
.initUnlit ();
225 _Material
.setZFunc (UMaterial::always
);
226 _Material
.setZWrite (false);
230 bool fogState
= driver
.fogEnabled ();
231 driver
.enableFog (false);
238 uint currentColor
= 0;
242 for (point
= 0; point
< _PrimRegion
.VPoints
.size (); point
++)
244 // Set the color for the next primitive
245 changeColor (currentColor
);
248 CVector
&vect
= _PrimRegion
.VPoints
[point
].Point
;
254 line
.V0
.x
-= POINT_SIZE
/2;
255 line
.V1
.x
+= POINT_SIZE
/2;
256 driver
.drawLine (line
, _Material
);
259 line
.V0
.y
-= POINT_SIZE
/2;
260 line
.V1
.y
+= POINT_SIZE
/2;
261 driver
.drawLine (line
, _Material
);
264 line
.V0
.z
-= POINT_SIZE
/2;
265 line
.V1
.z
+= POINT_SIZE
/2;
266 driver
.drawLine (line
, _Material
);
270 if (_PrimRegion
.VPoints
[point
].getPropertyByName("name", name
))
271 draw3dText (vect
+ CVector (0, 0, 10 * POINT_HEIGHT
), Scene
->getCam(), name
->c_str ());
276 for (seg
= 0; seg
< _PrimRegion
.VPaths
.size (); seg
++)
278 // Set the color for the next primitive
279 changeColor (currentColor
);
285 uint pointCount
= (uint
)_PrimRegion
.VPaths
[seg
].VPoints
.size ();
286 vector
<CPrimVector
> &points
= _PrimRegion
.VPaths
[seg
].VPoints
;
292 for (point
= 0; point
< pointCount
-1; point
++)
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
;
306 if (_PrimRegion
.VPaths
[seg
].getPropertyByName("name", name
))
307 draw3dText (pos
+ CVector (0, 0, 10 * POINT_HEIGHT
), Scene
->getCam(), name
->c_str ());
312 for (seg
= 0; seg
< _PrimRegion
.VZones
.size (); seg
++)
314 // Set the color for the next primitive
315 changeColor (currentColor
);
318 uint pointCount
= (uint
)_PrimRegion
.VZones
[seg
].VPoints
.size ();
319 vector
<CPrimVector
> &points
= _PrimRegion
.VZones
[seg
].VPoints
;
328 NLMISC::CVector
*previous
= &points
[pointCount
-1];
330 for (point
= 0; point
< pointCount
; point
++)
333 NLMISC::CVector
*next
= &points
[point
];
339 driver
.drawLine (line
, _Material
);
341 pos
+= points
[point
];
346 pos
/= (float) pointCount
;
350 if (_PrimRegion
.VZones
[seg
].getPropertyByName("name", name
))
351 draw3dText (pos
+ CVector (0, 0, POINT_HEIGHT
), Scene
->getCam(), name
->c_str ());
356 driver
.enableFog (fogState
);
360 //---------------------------------------------------
362 void CPrimFileMgr::release (NL3D::UDriver
&driver
)
364 driver
.deleteMaterial (_Material
);
367 //---------------------------------------------------
369 string
CPrimFileMgr::getCurrentPrimitive () const
373 if ( (_PrimFile
< (sint
)ClientCfg
.PrimFiles
.size ()) && (_PrimFile
>= 0) )
375 return ClientCfg
.PrimFiles
[_PrimFile
];