1 //-----------------------------------------------------------------------------
2 // BSP Structures
3 //-----------------------------------------------------------------------------
4 
5 #ifndef __Q3BSP_H__
6 #define __Q3BSP_H__
7 
8 #include "cake.h"
9 #include "types.h"
10 #include "skybox.h"
11 #include "surfaceflags.h"
12 #include "entity.h"
13 #include "files.h"
14 #include "bezierpatch.h"
15 #include "world.h"
16 
17 #define BSPHEADERID  (*(int*)"IBSP")
18 #define BSPVERSION 46
19 
20 // lightmaps
21 #define LIGHTMAP_BYTES      3
22 #define LIGHTMAP_WIDTH      128
23 #define LIGHTMAP_HEIGHT     128
24 #define LIGHTMAP_SIZE     (LIGHTMAP_WIDTH*LIGHTMAP_HEIGHT*LIGHTMAP_BYTES)
25 
26 #define MAX_MAP_LIGHTMAPS   (MAX_MAP_LIGHTING / LIGHTMAP_SIZE)
27 
28 // there shouldn't be any problem with increasing these values at the
29 // expense of more memory allocation in the utilities
30 #define MAX_MAP_MODELS    0x400
31 #define MAX_MAP_BRUSHES   0x8000
32 #define MAX_MAP_ENTITIES  0x800
33 #define MAX_MAP_ENTSTRING 0x40000
34 #define MAX_MAP_SHADERS   0x400
35 
36 #define MAX_MAP_AREAS   0x100
37 #define MAX_MAP_FOGS    0x100
38 #define MAX_MAP_PLANES    0x20000
39 #define MAX_MAP_NODES   0x20000
40 #define MAX_MAP_BRUSHSIDES  0x30000
41 #define MAX_MAP_LEAFS   0x20000
42 #define MAX_MAP_VERTEXES  0x80000
43 #define MAX_MAP_FACES   0x20000
44 #define MAX_MAP_LEAFFACES 0x20000
45 #define MAX_MAP_LEAFBRUSHES 0x40000
46 #define MAX_MAP_PORTALS   0x20000
47 #define MAX_MAP_INDICES   0x80000
48 #define MAX_MAP_LIGHTING  0x800000
49 #define MAX_MAP_VISIBILITY  0x200000
50 
51 #define MAX_CM_AREAS      (MAX_MAP_AREAS)
52 #define MAX_CM_BRUSHSIDES   (MAX_MAP_BRUSHSIDES << 1)
53 #define MAX_CM_SHADERS      (MAX_MAP_SHADERS)
54 #define MAX_CM_PLANES     (MAX_MAP_PLANES << 2)
55 #define MAX_CM_NODES      (MAX_MAP_NODES)
56 #define MAX_CM_LEAFS      (MAX_MAP_LEAFS)
57 #define MAX_CM_LEAFBRUSHES    (MAX_MAP_LEAFBRUSHES)
58 #define MAX_CM_MODELS     (MAX_MAP_MODELS)
59 #define MAX_CM_BRUSHES      (MAX_MAP_BRUSHES << 1)
60 #define MAX_CM_VISIBILITY   (MAX_MAP_VISIBILITY)
61 #define MAX_CM_FACES      (MAX_MAP_FACES)
62 #define MAX_CM_LEAFFACES    (MAX_MAP_LEAFFACES)
63 #define MAX_CM_VERTEXES     (MAX_MAP_VERTEXES)
64 #define MAX_CM_PATCHES      (0x10000)
65 #define MAX_CM_PATCH_VERTS    (4096)
66 #define MAX_CM_ENTSTRING    (MAX_MAP_ENTSTRING)
67 
68 //-----------------------------------------------------------------------------
69 
70 /**
71  * Q3BSP class.
72  * The q3bsp is the main bsp manager.
73  * @bug The map loading bugs sometimes. For example sky in q3dm10 isn't
74  *      correctly loaded in some cases. The reload of the map often shows
75  *      other loading bugs. Try to see for eventual memory overfilling.
76  */
77 class Q3BSP : public LumpFile
78 {
79   public:
80     World*      world;
81 
82     /**
83      * Constructor will load data from a file and store it in internal
84      * variables.
85      * @param name the name of file to read
86      * @param w a pointer to the parent world object
87      * @param res a pointer to an integer that will receive the operation
88      *        result (1 if success, 0 if error)
89      */
90     Q3BSP(char *name, World* w, int *res);
91     ~Q3BSP(void);
92 
93     /**
94      * More initialization.
95      * The function must treat data before being capable to draw it. It
96      * is also responsible to generate sky.
97      */
98     void Init(void);
99 
100     void Update(void);    /**< Update all the BSP data for a new frame. */
101 
102     void Report(void);    /**< Write a report of BSP elements in console. */
103 
104     /**
105      * Renders the map.
106      */
107     void Render(void);
108 
109     /**
110      * Get the entity manager.
111      * The entity manager is used for entities support, loading, update, etc.
112      * It contains all entities. See EntityManager class for more info.
113      * @return A pointer to entities manager structure.
114      */
115     EntityManager* GetEntities(void);
116 
117     /**
118      * Check a movement for collisions.
119      */
120     trace_t CheckMove(vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int head_node = 0, int brushmask = MASK_PLAYERSOLID);
121 
122     /**
123      * Link an entity in the BSP tree.
124      * Needs to be called any time an entity changes origin, mins, maxs,
125      * or solid. Automatically unlinks if needed.
126      * @todo This is not supported yet
127      * @param ent The entity that will be added to BSP tree.
128      */
129     void LinkEntity(Entity* ent);
130 
131     /**
132      * Unling an entity from the BSP tree.
133      * Call before removing an entity, and before trying to move one,
134      * so it doesn't clip agains itself.
135      * @todo This is not supported yet
136      * @param ent The entity that will be removed from BSP tree.
137      */
138     void UnlinkEntity(Entity *ent);
139 
140     /**
141      * Return a pointer to an unused model in BSP models
142      * This function can be used to add new models into BSP models structure.
143      * This is used by md3 loader to directly put the loaded models in the
144      * main models table.
145      * @param modelnum The index of returned model in BSP models table
146      * @return The pointer to allocated model.
147      */
148     cmodel_t* CreateNewModel(int *modelnum);
149 
150     /**
151      * Return a pointer to an unused surface in BSP surfaces.
152      * This function works on the same manner than the CreateNewModel() function.
153      * @param facetype The surface type
154      * @param facenum The index of returned surface in BSP surfaces table
155      * @return The pointer to allocated surface.
156      */
157     Surface* CreateNewSurface(int facetype, int *facenum);
158 
159     /**
160      * Generates a bounding box using BSP faces
161      * @param destbox The destination bounding box to build
162      * @param firstface The index of first faces used for bbox building
163      * @param numfaces The number of faces used for bbox building
164      */
165     void GenerateBoundingBox(bboxf_t destbox, int firstface, int numfaces);
166 
167     /**
168      * Display the bounding box of a specified face.
169      * This function is only for debug.
170      * @param bbox The bounding box to render.
171      * @param colorline The color of bounding box lines.
172      * @param colorface The color of bounding box faces.
173      */
174     static void DrawBoundingBox(bboxf_t bbox, colour_t colorline, colour_t colorface);
175 
176     /**
177      * Display a message on screen.
178      * This function is only usable when bsp state is not RUNNING. This is mainly
179      * used to display informations on the loading process. The function displays
180      * the levelshot and put a message in forground. The message is centered on
181      * the line.
182      * @param msg The message to display
183      * @param line The line to write the message on (-1 => centered on screen height)
184      */
185     void DisplayLoadingMessage(const char *msg, int line = -1);
186 
187     void SetWorldName(const char *name);
188 
189   private:
190 
191     int FindLeaf(vec3_t p, int headnode = 0);
192     void MarkLeafs(int vis_cluster);
193     void WalkTree(int idx, bool checkFrustum);
194     void UpdateFaces(void);     /**< Update the faces. */
195     void UpdateSky(void);     /**< Update the skybox. */
196 
197     void DrawFaces(void);     /**< Draws the faces. */
198     void DrawSky(void);       /**< Draws the sky box. */
199     void DrawModels(void);      /**< Draws the models. */
200     void DrawBoundingBoxes(void); /**< Display the bounding box of patches and meshes. */
201     void DrawNormals(void);     /**< Draw the vertice normal */
202 
203     void ClearLink(link_t *l);
204     void RemoveLink(link_t *l);
205     void InsertLinkBefore(link_t *l, link_t *before);
206 
207 
208     /**
209      * Generates the skybox.
210      * The function will find sky shader and associate it with the sky
211      * box. It will then store a reference to all surfaces that have
212      * sky shader to prepare sky surface list for sky rendering
213      * optimization.
214      * @see SkyBox
215      */
216     void GenerateSkyBox(void);
217 
218     /**
219      * Initialize the surfaces.
220      * The function creates the patches and the patch grounps.
221      */
222     void InitSurfaces(void);
223 
224     /**
225      * Creates the patches groups.
226      * Patches are organized in groups. Every patch of a group is near to
227      * the others, so tesselation changes are made together for each patch
228      * of the group. This is done for progressive tesselation.
229      * @see PatchesGroup
230      */
231     void CreatePatchesGroups(void);
232 
233     /**
234      * Callback to sort the faces.
235      */
236     static int FaceCmp(const void *a, const void *b);
237 
238     void LoadEntities(void);      /**< Load entities from BSP file */
239     void LoadShaders(void);       /**< Load shaders from BSP file */
240     void LoadPlanes(void);        /**< Load planes from BSP file */
241     void LoadNodes(void);       /**< Load nodes from BSP file */
242     void LoadLeafs(void);       /**< Load leafs from BSP file */
243     void LoadLFaces(void);        /**< Load lfaces from BSP file */
244     void LoadLBrushes(void);      /**< Load lbrushes from BSP file */
245     void LoadModels(void);        /**< Load models from BSP file */
246     void LoadBrushes(void);       /**< Load brushes from BSP file */
247     void LoadBrushSides(void);      /**< Load brushsides from BSP file */
248     void LoadVerts(void);       /**< Load verts from BSP file */
249     void LoadElems(void);       /**< Load elems from BSP file */
250     void LoadFaces(void);       /**< Load faces from BSP file */
251     void LoadLightmaps(void);     /**< Load lightmaps from BSP file */
252     void LoadVisibility(void);      /**< Load visibility from BSP file */
253     void LoadEffects(void);       /**< Load visibility from BSP file
254                        *   @todo Finish effect management. */
255     void LoadLightVols(void);     /**< Load visibility from BSP file
256                        *   @todo Finish lightvols management */
257 
258     int levelshotdetail, levelshot;   /**< Levelshot and levelshotdetails shader index */
259 
260     /**
261      * Load the levelshot in the framework shaders table.
262      * @param map_name The map that is currently being loaded.
263      */
264     void LoadLevelshot(const char* map_name);
265 
266     /**
267      * Display the current defined levelshot.
268      * The function only map the levelshot on render surface but does not
269      * execute buffers swap.
270      */
271     void DisplayLevelshot(void);
272 
273     void SetParent(int node, cnode_t *parent);
274 
275     /**
276      * Resets the surface rotation.
277      * The function resets the surface rotation so the surface has a
278      * default orientation. The surface must have normal colliding with
279      * Oy axis and logically be in the Oxz plane.
280      * <pre>
281      *   Note on the Quake 3 axis system
282      *
283      *  Z /|\     _  Y
284      *     |      /|
285      *     |  ___/___
286      *     | |       |
287      *     | |       |  The surface must finally be in the Oxz plane,
288      *     | |       |  with normal pointing in Oy direction.
289      *     | |_______|
290      *     |/              \
291      *   --+---------------- X
292      *    /|               /
293      *
294      * </pre>
295      * The function is used with surfaces that have Autosprite or
296      * Autosprite2 deformation vertex. The goal is to reorient the face
297      * to be then able to rotate it correctly. The function also update
298      * surface axis for autosprite2 rotation.
299      * @param surf the surface to analyse
300      * @param type the autosprite type
301      */
302     void InitAutosprite(Surface *surf, int type);
303 
304     /**
305      * Check if a point is inside a brush.
306      * @param brushnum The index of tested brush.
307      * @param p The point position
308      * @return A boolean value that is true if the point is in the brush, false if not
309      */
310     bool PointInsideBrush(int brushnum, vec3_t p);
311 
312     /**
313      * Generate a bezier patch.
314      * The function creates several bezier patches (depending on subdivision levels).
315      * @param surf The destination surface (surface that will become bezier patch)
316      * @param curvefactor Factor used to perform flatness test.
317      */
318     void BuildBezierPatch(Surface* surf, int curvefactor);
319 
320     // Collision detection stuff
321     // @todo Organize this when coldet is finished
322     int   leaf_count, leaf_maxcount;
323     int   *leaf_list;
324     float *leaf_mins, *leaf_maxs;
325     int   leaf_topnode;
326     void RecursiveHullCheck(float sf, float ef, const vec3_t sp, const vec3_t ep, int node, trace_t *trace_trace);
327     void TraceToLeaf(int leafnum, trace_t *trace_trace);
328     void TestInLeaf(int leafnum, trace_t *trace_trace);
329     void ClipBoxToBrush(cbrush_t *brush, trace_t *trace);
330     void ClipBoxToPatch(cbrush_t *brush, trace_t *trace);
331     void TestBoxInBrush(cbrush_t *brush, trace_t *trace);
332     void TestBoxInPatch(cbrush_t *brush, trace_t *trace);
333     int BoxLeafnums_headnode(vec3_t mins, vec3_t maxs, int *list, int listsize, int headnode, int *topnode);
334     void BoxLeafnums_r(int nodenum);
335 
336     void CreateBrush(cbrush_t *brush, vec3_t *verts, int surface_shader);
337     void CreatePatch(cpatch_t *patch, int numverts, vec4_t *verts, int *patch_cp);
338     void CreatePatchesForLeafs(void);
339 
340     // sizes of the loaded data
341     // The sizes are defined by 2 int. The first value is the currently number
342     // of data and the second value is the size of allocated memory. The allocated
343     // memory can be bigger than what is required.
344     int       r_nummodels[2];
345     int       r_numverts[2];
346     int       r_numplanes[2];
347     int       r_numleafs[2];
348     int       r_numnodes[2];
349     int       r_numshaders[2];
350     int       r_numsurfaces[2];   /**< total number of surfaces */
351     int       r_numleaffaces[2];
352     int       r_numelems[2];
353     int       r_numlightmaps[2];
354     int       r_numleafbrushes[2];
355     int       r_numbrushes[2];
356     int       r_numbrushsides[2];
357     int       r_numlightvols[2];
358     int       r_numeffects[2];
359     int       r_numvisibility[2];
360     int       r_numleafpatches[2];
361     int       r_numfaces[2];
362     int       r_numpatches[2];
363     int       r_numcpatches[2];
364     int       r_nummeshes[2];
365     int       r_numflares[2];
366 
367     // pointers to loaded data
368     vec3_t *    v_xyz;
369     texcoord_t *  v_tex_st;
370     texcoord_t *  v_lm_st;
371     vec3_t *    v_norm;
372     colour_t *    v_colour;
373 
374     vertex_t *    r_verts;
375     cmodel_t *    r_models;
376     cleaf_t *   r_leafs;
377     cnode_t *   r_nodes;
378     shaderref_t * r_shaderrefs;
379     int *     r_leaffaces;
380     int *     r_elems;
381     visibility_t *  r_visibility;
382     texinfo *   r_lightmapinfos;
383     int *     r_leafbrushes;
384     cbrush_t *    r_brushes;
385     cbrushside_t *  r_brushsides;
386     cplane_t *    r_planes;
387     ceffect_t *   r_effects;
388     lightvols_t * r_lightvols;
389     int *     r_leafpatches;
390     cpatch_t *    r_cpatches;     /**< structure used for collision detection with patches */
391 
392     render_face_s * face_list;
393 
394     Surface *   r_surfaces;     /**< table conntaining all surfaces */
395     Surface **    r_patches;      /**< table countaining patches surfaces pointers */
396     Surface **    r_faces;      /**< table countaining faces surfaces pointers */
397     Surface **    r_meshes;     /**< table countaining meshes surfaces pointers */
398     Surface **    r_flares;     /**< table countaining flares surfaces pointers */
399     int       r_numpatchesgroups[2];
400     PatchesGroup *  r_patchesgroups;
401     SkyBox *    r_skybox;
402     EntityManager * r_entities;
403 
404     bboxf_t     bbox;       /**< bsp bounding box */
405 
406     int       emptyleaf;
407 
408     char*     worldname;
409 
410     /**
411      * Visibility counter.
412      * The visibility counter is updated each frame. It is used to know
413      * if a node or a leag must be walked or drawn.
414      */
415     int viscount;
416 
417     /**
418      * Node counter.
419      * The node counter is different because nodes only are updated when
420      * the camera leaf changes
421      */
422     int nodecount;
423 
424     /**
425      * Check counter.
426      * This is used to avoid repeated testings in collision tests.
427      */
428     int checkcount;
429 };
430 
431 #endif  /* __Q3BSP_H__ */
432