1 /*
2 Copyright (C) 2003-2006 Andrey Nazarov
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the 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.
12 
13 See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 */
20 
21 #include "qgl_local.h"
22 #include "q_shared.h"
23 #include "qfiles.h"
24 #include "com_public.h"
25 #include "ref_public.h"
26 #include "in_public.h"
27 #include "vid_public.h"
28 #include "q_list.h"
29 #include "r_shared.h"
30 #include "qgl_api.h"
31 
32 /*
33  * gl_main.c
34  *
35  */
36 typedef struct {
37 	int width;
38     int height;
39 } viddef_t;
40 
41 extern viddef_t	vid;
42 
43 extern vidGLAPI_t video;
44 
45 #define MAX_TMUS        3
46 
47 typedef struct {
48     int flags;
49     int prev_mode;
50     int numTextureUnits;
51     int maxTextureSize;
52 	qboolean registering;
53 	uint32 palette[256]; /* cinematic palette */
54 } glStatic_t;
55 
56 typedef struct {
57     refdef_t fd;
58     vec3_t viewaxis[3];
59     int visframe;
60     int drawframe;
61     int viewcluster1;
62 	int viewcluster2;
63     cplane_t frustumPlanes[4];
64 	entity_t	*ent;
65 	vec3_t		entaxis[3];
66 	qboolean	entrotated;
67 	float	scroll;
68 	int		num_beams;
69 } glRefdef_t;
70 
71 extern glStatic_t gl_static;
72 extern glconfig_t gl_config;
73 extern glRefdef_t glr;
74 
75 typedef struct {
76     int nodesVisible;
77     int nodesDrawn;
78     int facesMarked;
79     int facesDrawn;
80     int texSwitches;
81     int trisDrawn;
82     int batchesDrawn;
83     int nodesCulled;
84     int facesCulled;
85 	int boxesCulled;
86 	int spheresCulled;
87 	int rotatedBoxesCulled;
88 } statCounters_t;
89 
90 extern statCounters_t c;
91 
92 extern cvar_t *gl_znear;
93 extern cvar_t *gl_zfar;
94 extern cvar_t *gl_modulate;
95 extern cvar_t *gl_showtris;
96 extern cvar_t *gl_cull_nodes;
97 extern cvar_t *gl_clear;
98 extern cvar_t *gl_novis;
99 extern cvar_t *gl_lockpvs;
100 extern cvar_t *gl_primitives;
101 extern cvar_t *gl_sort;
102 extern cvar_t *gl_subdivide;
103 extern cvar_t *gl_fastsky;
104 extern cvar_t *gl_dynamic;
105 extern cvar_t *gl_fullbright;
106 extern cvar_t *gl_mode;
107 extern cvar_t *gl_hwgamma;
108 extern cvar_t *gl_fullscreen;
109 
110 typedef enum {
111 	CULL_OUT,
112 	CULL_IN,
113 	CULL_CLIP
114 } glCullResult_t;
115 
116 glCullResult_t GL_CullBox( vec3_t bounds[2] );
117 glCullResult_t GL_CullSphere( const vec3_t origin, float radius );
118 glCullResult_t GL_CullLocalBox( const vec3_t origin, vec3_t bounds[2] );
119 
120 void GL_DrawBox( const vec3_t origin, vec3_t bounds[2] );
121 
122 /*
123  * gl_model.c
124  *
125  */
126 
127 typedef struct tcoord_s {
128     float st[2];
129 } tcoord_t;
130 
131 typedef struct aliasVert_s {
132 	short pos[3];
133 	byte normalIndex;
134 	byte pad;
135 } aliasVert_t;
136 
137 typedef struct aliasFrame_s {
138 	vec3_t scale;
139 	vec3_t translate;
140 	vec3_t bounds[2];
141 	vec_t radius;
142 } aliasFrame_t;
143 
144 typedef struct aliasMesh_s {
145 	int numVerts;
146 	int numTris;
147 	int numIndices;
148 	uint32 *indices;
149 	aliasVert_t *verts;
150 	tcoord_t *tcoords;
151 	image_t *skins[MAX_MD2SKINS];
152 	int numSkins;
153 } aliasMesh_t;
154 
155 typedef struct spriteFrame_s {
156 	int width, height;
157 	int x, y;
158 	image_t *image;
159 } spriteFrame_t;
160 
161 typedef struct model_s {
162 	modelType_t type;
163 
164 	char name[MAX_QPATH];
165 	int registration_sequence;
166 	mempool_t pool;
167 
168 	/* alias models */
169 	int numFrames;
170 	int numMeshes;
171 	aliasFrame_t *frames;
172 	aliasMesh_t *meshes;
173 
174 	/* sprite models */
175 	spriteFrame_t *sframes;
176 } model_t;
177 
178 /* xyz[3] + st[2] + lmst[2] */
179 #define VERTEX_SIZE 7
180 
181 typedef struct bspPoly_s {
182     struct bspPoly_s *next;
183     int numVerts;
184     int numIndices;
185     vec_t vertices[VERTEX_SIZE];
186 } bspPoly_t;
187 
188 void GL_InitModels( void );
189 void GL_ShutdownModels( void );
190 void GL_GetModelSize( qhandle_t hModel, vec3_t mins, vec3_t maxs );
191 qhandle_t GL_RegisterModel( const char *name );
192 modelType_t *GL_ModelForHandle( qhandle_t hModel );
193 
194 void Model_FreeUnused( void );
195 void Model_FreeAll( void );
196 
197 /*
198  * gl_surf.c
199  *
200  */
201 #define LM_BITS 5
202 #define LM_MAX_LIGHTMAPS    ( 1 << LM_BITS )
203 #define LM_BLOCK_WIDTH  256
204 #define LM_BLOCK_HEIGHT 256
205 
206 #define NOLIGHT_MASK \
207     (SURF_SKY|SURF_WARP|SURF_TRANS33|SURF_TRANS66)
208 
209 typedef struct {
210     int inuse[LM_BLOCK_WIDTH];
211     byte buffer[LM_BLOCK_WIDTH * LM_BLOCK_HEIGHT * 4];
212     image_t *lightmaps[LM_MAX_LIGHTMAPS];
213     int numMaps;
214 } lightmapBuilder_t;
215 
216 extern lightmapBuilder_t lm;
217 
218 int GL_PostProcessSurface( bspSurface_t *surf );
219 void GL_BeginPostProcessing( void );
220 void GL_EndPostProcessing( void );
221 
222 /*
223  * gl_state.c
224  *
225  */
226 typedef enum {
227     GLS_CULL_DISABLE,
228     GLS_CULL_FRONT,
229     GLS_CULL_BACK
230 } glCullFace_t;
231 
232 typedef enum {
233     GLS_DEFAULT             = 0,
234     GLS_DEPTHMASK_FALSE     = ( 1 << 0 ),
235     GLS_DEPTHTEST_DISABLE   = ( 1 << 1 ),
236 	GLS_BLEND_BLEND         = ( 1 << 2 ),
237 	GLS_BLEND_ADD           = ( 1 << 3 ),
238 	GLS_BLEND_MODULATE      = ( 1 << 4 ),
239 	GLS_ALPHATEST_ENABLE    = ( 1 << 5 )
240 } glStateBits_t;
241 
242 #define GLS_BLEND_MASK	(GLS_BLEND_BLEND|GLS_BLEND_ADD|GLS_BLEND_MODULATE)
243 
244 typedef struct {
245     int tmu;
246     int texnum[MAX_TMUS];
247     GLenum texenv[MAX_TMUS];
248     glStateBits_t bits;
249     glCullFace_t cull;
250 } glState_t;
251 
252 extern glState_t gls;
253 
254 void GL_BindTexture( int texnum );
255 void GL_SelectTMU( int tmu );
256 void GL_TexEnv( GLenum texenv );
257 void GL_CullFace( glCullFace_t cull );
258 void GL_Bits( glStateBits_t bits );
259 void GL_Setup2D( void );
260 void GL_Setup3D( void );
261 
262 
263 /*
264  * gl_draw.c
265  *
266  */
267 typedef struct {
268     color_t color;
269 	uint32 flags;
270 	float scale;
271 } drawStatic_t;
272 
273 extern drawStatic_t	draw;
274 
275 qhandle_t GL_RegisterFont( const char *name );
276 
277 void Draw_SetColor( uint32 flags, const color_t color );
278 void Draw_SetClipRect( uint32 flags, const clipRect_t *clip );
279 void Draw_SetScale( float *scale );
280 void Draw_GetPicSize( int *w, int *h, qhandle_t hPic );
281 void Draw_GetFontSize( int *w, int *h, qhandle_t hFont );
282 void Draw_StretchPicST( int x, int y, int w, int h, float s1, float t1,
283         float s2, float t2, qhandle_t hPic );
284 void Draw_StretchPic( int x, int y, int w, int h, qhandle_t hPic );
285 void Draw_Pic( int x, int y, qhandle_t hPic );
286 void Draw_StretchRaw( int x, int y, int w, int h, int cols,
287         int rows, const byte *data );
288 void Draw_TileClear( int x, int y, int w, int h, qhandle_t hPic );
289 void Draw_Fill( int x, int y, int w, int h, int c );
290 void Draw_FillEx( int x, int y, int w, int h, const color_t color );
291 void Draw_FadeScreen( void );
292 void Draw_Char( int x, int y, uint32 flags, int ch, qhandle_t hFont );
293 void Draw_String( int x, int y, uint32 flags, int maxChars,
294         const char *string, qhandle_t hFont );
295 void Draw_Stringf( int x, int y, const char *fmt, ... );
296 void Draw_Stats( void );
297 
298 
299 /*
300  * gl_images.c
301  *
302  */
303 extern image_t *r_notexture;
304 extern image_t *r_particletexture;
305 extern image_t *r_beamtexture;
306 extern image_t *r_dlightTex;
307 extern image_t *r_whiteimage;
308 
309 extern int gl_filter_min;
310 extern int gl_filter_max;
311 extern float gl_filter_anisotropy;
312 extern int gl_tex_alpha_format;
313 extern int gl_tex_solid_format;
314 
315 extern qboolean scrap_dirty;
316 
317 void Scrap_Upload( void );
318 
319 void GL_InitImages( void );
320 void GL_ShutdownImages( void );
321 
322 void GL_UpdateGammaTable( qboolean realTime );
323 
324 image_t *R_ImageForHandle( qhandle_t hPic );
325 qhandle_t R_RegisterSkin( const char *name );
326 qhandle_t R_RegisterPic( const char *name );
327 
328 
329 /*
330  * gl_tess.c
331  *
332  */
333 #define TESS_MAX_FACES      256
334 #define TESS_MAX_VERTICES   ( 16 * TESS_MAX_FACES )
335 #define TESS_MAX_INDICES    ( 3 * TESS_MAX_VERTICES )
336 
337 typedef struct {
338 	int dlightbits;
339     int numVertices;
340     int numIndices;
341     int numFaces;
342     vec_t vertices[4*TESS_MAX_VERTICES];
343     byte colors[4*TESS_MAX_VERTICES];
344     tcoord_t tcoords[TESS_MAX_VERTICES];
345     tcoord_t lmtcoords[TESS_MAX_VERTICES];
346     int indices[TESS_MAX_INDICES];
347     bspSurface_t *faces[TESS_MAX_FACES];
348     int texnum;
349     int lightmapnum;
350 	qboolean istrans;
351 } tesselator_t;
352 
353 extern tesselator_t tess;
354 
355 void EndSurface_Multitextured( void );
356 void EndSurface_Single( void );
357 
358 void Tess_DrawSurfaceTriangles( int *indices, int numIndices );
359 
360 void GL_AddBspSurface( bspSurface_t *surf );
361 void GL_DrawSurfPoly( bspSurface_t *surf );
362 void GL_SortAndDrawSurfs( qboolean doSort );
363 void GL_StretchPic( float x, float y, float w, float h,
364         float s1, float t1, float s2, float t2, const byte *color, image_t *image );
365 void GL_Flush2D( void );
366 void GL_DrawParticles( void );
367 void GL_DrawBeams( void );
368 
369 /*
370  * gl_world.c
371  *
372  */
373 extern vec3_t modelViewOrigin;
374 
375 void GL_MarkLeaves( void );
376 void GL_MarkLights( void );
377 void GL_DrawBspModel( bspSubmodel_t *model );
378 void GL_DrawWorld( void );
379 void GL_DrawAlphaFaces( void );
380 void GL_LightPoint( vec3_t origin, vec3_t dest );
381 
382 /*
383  * gl_sky.c
384  *
385  */
386 void R_AddSkySurface( bspSurface_t *surf );
387 void R_ClearSkyBox( void );
388 void R_DrawSkyBox( void );
389 void R_SetSky( const char *name, float rotate, vec3_t axis );
390 
391 /*
392  * gl_mesh.c
393  *
394  */
395 void GL_DrawAliasModel( model_t *model );
396 
397