2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
23 cvar_t scr_conalpha = {CVAR_SAVE, "scr_conalpha", "1"};
25 static rtexture_t *char_texture;
27 //=============================================================================
28 /* Support Routines */
30 #define MAX_CACHED_PICS 256
31 #define CACHEPICHASHSIZE 256
32 static cachepic_t *cachepichash[CACHEPICHASHSIZE];
33 static cachepic_t cachepics[MAX_CACHED_PICS];
34 static int numcachepics;
36 static rtexturepool_t *drawtexturepool;
38 static qbyte pointerimage[256] =
58 static rtexture_t *draw_generatemousepointer(void)
62 for (i = 0;i < 256;i++)
64 if (pointerimage[i] == '.')
73 buffer[i][0] = (pointerimage[i] - '0') * 16;
74 buffer[i][1] = (pointerimage[i] - '0') * 16;
75 buffer[i][2] = (pointerimage[i] - '0') * 16;
79 return R_LoadTexture(drawtexturepool, "mousepointer", 16, 16, &buffer[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE);
82 // must match NUMCROSSHAIRS in r_crosshairs.c
83 #define NUMCROSSHAIRS 5
85 static qbyte *crosshairtexdata[NUMCROSSHAIRS] =
173 static rtexture_t *draw_generatecrosshair(int num)
177 qbyte data[16*16][4];
178 in = crosshairtexdata[num];
179 for (i = 0;i < 16*16;i++)
193 data[i][3] = (qbyte) ((int) (in[i] - '0') * 255 / 7);
196 return R_LoadTexture(drawtexturepool, va("crosshair%i", num), 16, 16, &data[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE);
204 // FIXME: move this to client somehow
205 cachepic_t *Draw_CachePic (char *path)
211 crc = CRC_Block(path, strlen(path));
212 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
213 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
214 if (!strcmp (path, pic->name))
217 if (numcachepics == MAX_CACHED_PICS)
218 Sys_Error ("numcachepics == MAX_CACHED_PICS");
219 pic = cachepics + (numcachepics++);
220 strcpy (pic->name, path);
222 pic->chain = cachepichash[hashkey];
223 cachepichash[hashkey] = pic;
225 // load the pic from disk
226 pic->tex = loadtextureimage(drawtexturepool, path, 0, 0, false, false, true);
227 if (pic->tex == NULL && (p = W_GetLumpName (path)))
229 if (!strcmp(path, "conchars"))
232 // conchars is a raw image and with the wrong transparent color
234 for (i = 0;i < 128 * 128;i++)
237 pic->tex = R_LoadTexture (drawtexturepool, path, 128, 128, pix, TEXTYPE_QPALETTE, TEXF_ALPHA | TEXF_PRECACHE);
240 pic->tex = R_LoadTexture (drawtexturepool, path, p->width, p->height, p->data, TEXTYPE_QPALETTE, TEXF_ALPHA | TEXF_PRECACHE);
242 if (pic->tex == NULL && !strcmp(path, "ui/mousepointer.tga"))
243 pic->tex = draw_generatemousepointer();
244 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair1.tga"))
245 pic->tex = draw_generatecrosshair(0);
246 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair2.tga"))
247 pic->tex = draw_generatecrosshair(1);
248 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair3.tga"))
249 pic->tex = draw_generatecrosshair(2);
250 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair4.tga"))
251 pic->tex = draw_generatecrosshair(3);
252 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair5.tga"))
253 pic->tex = draw_generatecrosshair(4);
254 if (pic->tex == NULL)
255 Sys_Error ("Draw_CachePic: failed to load %s", path);
257 pic->width = R_TextureWidth(pic->tex);
258 pic->height = R_TextureHeight(pic->tex);
262 cachepic_t *Draw_NewPic(char *picname, int width, int height, int alpha, qbyte *pixels)
267 crc = CRC_Block(picname, strlen(picname));
268 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
269 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
270 if (!strcmp (picname, pic->name))
275 if (pic->tex && R_TextureWidth(pic->tex) == width && R_TextureHeight(pic->tex) == height && (R_TextureHasAlpha(pic->tex) != 0) == (alpha != 0))
277 R_UpdateTexture(pic->tex, pixels);
285 if (numcachepics == MAX_CACHED_PICS)
286 Sys_Error ("numcachepics == MAX_CACHED_PICS");
287 pic = cachepics + (numcachepics++);
288 strcpy (pic->name, picname);
290 pic->chain = cachepichash[hashkey];
291 cachepichash[hashkey] = pic;
296 pic->height = height;
298 R_FreeTexture(pic->tex);
299 pic->tex = R_LoadTexture (drawtexturepool, picname, width, height, pixels, TEXTYPE_RGBA, alpha ? TEXF_ALPHA : 0);
303 void Draw_FreePic(char *picname)
308 // this doesn't really free the pic, but does free it's texture
309 crc = CRC_Block(picname, strlen(picname));
310 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
311 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
313 if (!strcmp (picname, pic->name))
315 R_FreeTexture(pic->tex);
328 static void gl_draw_start(void)
330 drawtexturepool = R_AllocTexturePool();
333 memset(cachepichash, 0, sizeof(cachepichash));
335 char_texture = Draw_CachePic("conchars")->tex;
338 static void gl_draw_shutdown(void)
340 R_FreeTexturePool(&drawtexturepool);
343 memset(cachepichash, 0, sizeof(cachepichash));
346 static void gl_draw_newmap(void)
350 void GL_Draw_Init (void)
352 Cvar_RegisterVariable (&scr_conalpha);
355 memset(cachepichash, 0, sizeof(cachepichash));
357 R_RegisterModule("GL_Draw", gl_draw_start, gl_draw_shutdown, gl_draw_newmap);
360 extern cvar_t gl_mesh_drawmode;
361 void R_DrawQueue(void)
363 int pos, num, chartexnum, overbright;
364 float x, y, w, h, s, t, u, v;
367 char *str, *currentpic;
368 int batch, batchcount, additive;
370 drawqueuemesh_t *mesh;
372 if (!r_render.integer)
375 qglViewport(vid.realx, vid.realy, vid.realwidth, vid.realheight);
377 qglMatrixMode(GL_PROJECTION);
379 qglOrtho(0, vid.conwidth, vid.conheight, 0, -99999, 99999);
381 qglMatrixMode(GL_MODELVIEW);
384 qglDisable(GL_DEPTH_TEST);
385 qglDisable(GL_CULL_FACE);
387 qglEnable(GL_TEXTURE_2D);
388 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
390 chartexnum = R_GetTexture(char_texture);
393 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
396 qglBindTexture(GL_TEXTURE_2D, 0);
398 qglColor4ub(0,0,0,0);
400 overbright = v_overbrightbits.integer;
403 for (pos = 0;pos < r_refdef.drawqueuesize;pos += ((drawqueue_t *)(r_refdef.drawqueue + pos))->size)
405 dq = (drawqueue_t *)(r_refdef.drawqueue + pos);
406 if (dq->flags & DRAWFLAG_ADDITIVE)
416 qglBlendFunc(GL_SRC_ALPHA, GL_ONE);
429 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
432 if (color != dq->color)
435 qglColor4ub((qbyte)(((color >> 24) & 0xFF) >> overbright), (qbyte)(((color >> 16) & 0xFF) >> overbright), (qbyte)(((color >> 8) & 0xFF) >> overbright), (qbyte)(color & 0xFF));
437 if (batch && batchcount > 128)
449 str = (char *)(dq + 1);
452 if (strcmp(str, currentpic))
460 pic = Draw_CachePic(str);
461 qglBindTexture(GL_TEXTURE_2D, R_GetTexture(pic->tex));
470 qglBegin(GL_TRIANGLES);
473 qglTexCoord2f (0, 0);qglVertex2f (x , y );
474 qglTexCoord2f (1, 0);qglVertex2f (x+w, y );
475 qglTexCoord2f (1, 1);qglVertex2f (x+w, y+h);
476 qglTexCoord2f (0, 0);qglVertex2f (x , y );
477 qglTexCoord2f (1, 1);qglVertex2f (x+w, y+h);
478 qglTexCoord2f (0, 1);qglVertex2f (x , y+h);
491 qglBindTexture(GL_TEXTURE_2D, 0);
496 qglBegin(GL_TRIANGLES);
499 qglTexCoord2f (0, 0);qglVertex2f (x , y );
500 qglTexCoord2f (1, 0);qglVertex2f (x+w, y );
501 qglTexCoord2f (1, 1);qglVertex2f (x+w, y+h);
502 qglTexCoord2f (0, 0);qglVertex2f (x , y );
503 qglTexCoord2f (1, 1);qglVertex2f (x+w, y+h);
504 qglTexCoord2f (0, 1);qglVertex2f (x , y+h);
508 case DRAWQUEUE_STRING:
509 str = (char *)(dq + 1);
510 if (strcmp("conchars", currentpic))
517 currentpic = "conchars";
518 qglBindTexture(GL_TEXTURE_2D, chartexnum);
523 qglBegin(GL_TRIANGLES);
526 while ((num = *str++) && x < vid.conwidth)
530 s = (num & 15)*0.0625f + (0.5f / 256.0f);
531 t = (num >> 4)*0.0625f + (0.5f / 256.0f);
532 u = 0.0625f - (1.0f / 256.0f);
533 v = 0.0625f - (1.0f / 256.0f);
534 qglTexCoord2f (s , t );qglVertex2f (x , y );
535 qglTexCoord2f (s+u, t );qglVertex2f (x+w, y );
536 qglTexCoord2f (s+u, t+v);qglVertex2f (x+w, y+h);
537 qglTexCoord2f (s , t );qglVertex2f (x , y );
538 qglTexCoord2f (s+u, t+v);qglVertex2f (x+w, y+h);
539 qglTexCoord2f (s , t+v);qglVertex2f (x , y+h);
551 mesh = (void *)(dq + 1);
552 qglBindTexture(GL_TEXTURE_2D, R_GetTexture(mesh->texture));
553 if (gl_mesh_drawmode.integer > 0)
555 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), mesh->vertices);CHECKGLERROR
556 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), mesh->texcoords);CHECKGLERROR
557 qglColorPointer(4, GL_UNSIGNED_BYTE, sizeof(qbyte[4]), mesh->colors);CHECKGLERROR
558 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
559 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
560 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
562 if (gl_mesh_drawmode.integer == 3 && qglDrawRangeElements == NULL)
563 Cvar_SetValueQuick(&gl_mesh_drawmode, 2);
565 if (gl_mesh_drawmode.integer == 3)
567 // GL 1.2 or GL 1.1 with extension
568 qglDrawRangeElements(GL_TRIANGLES, 0, mesh->numvertices, mesh->numindices, GL_UNSIGNED_INT, mesh->indices);
571 else if (gl_mesh_drawmode.integer == 2)
574 qglDrawElements(GL_TRIANGLES, mesh->numindices, GL_UNSIGNED_INT, mesh->indices);
577 else if (gl_mesh_drawmode.integer == 1)
581 // feed it manually using glArrayElement
582 qglBegin(GL_TRIANGLES);
583 for (i = 0;i < mesh->numindices;i++)
584 qglArrayElement(mesh->indices[i]);
591 // GL 1.1 but not using vertex arrays - 3dfx glquake minigl driver
593 if (gl_mesh_drawmode.integer != 0)
594 Cvar_SetValueQuick(&gl_mesh_drawmode, 0);
595 qglBegin(GL_TRIANGLES);
596 for (i = 0;i < mesh->numindices;i++)
598 in = mesh->indices[i];
599 qglColor4ub(mesh->colors[in * 4], mesh->colors[in * 4 + 1], mesh->colors[in * 4 + 2], mesh->colors[in * 4 + 3]);
600 qglTexCoord2f(mesh->texcoords[in * 2], mesh->texcoords[in * 2 + 1]);
601 qglVertex3f(mesh->vertices[in * 3], mesh->vertices[in * 3 + 1], mesh->vertices[in * 3 + 2]);
606 if (gl_mesh_drawmode.integer > 0)
608 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
609 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
610 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
612 // restore color, since it got trashed by using color array
613 qglColor4ub((qbyte)(((color >> 24) & 0xFF) >> overbright), (qbyte)(((color >> 16) & 0xFF) >> overbright), (qbyte)(((color >> 8) & 0xFF) >> overbright), (qbyte)(color & 0xFF));
623 if (!v_hwgamma.integer)
625 qglDisable(GL_TEXTURE_2D);
627 t = v_contrast.value * (float) (1 << v_overbrightbits.integer);
630 qglBlendFunc (GL_DST_COLOR, GL_ONE);
632 qglBegin (GL_TRIANGLES);
635 num = (int) ((t - 1.0f) * 255.0f);
638 qglColor4ub ((qbyte) num, (qbyte) num, (qbyte) num, 255);
639 qglVertex2f (-5000, -5000);
640 qglVertex2f (10000, -5000);
641 qglVertex2f (-5000, 10000);
649 qglBlendFunc(GL_ZERO, GL_SRC_COLOR);
651 qglBegin(GL_TRIANGLES);
652 num = (int) (t * 255.0f);
653 qglColor4ub ((qbyte) num, (qbyte) num, (qbyte) num, 255);
654 qglVertex2f (-5000, -5000);
655 qglVertex2f (10000, -5000);
656 qglVertex2f (-5000, 10000);
660 if (v_brightness.value >= 0.01f)
662 qglBlendFunc (GL_ONE, GL_ONE);
664 num = (int) (v_brightness.value * 255.0f);
665 qglColor4ub ((qbyte) num, (qbyte) num, (qbyte) num, 255);
667 qglBegin (GL_TRIANGLES);
668 qglVertex2f (-5000, -5000);
669 qglVertex2f (10000, -5000);
670 qglVertex2f (-5000, 10000);
674 qglEnable(GL_TEXTURE_2D);
678 qglBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
680 qglEnable (GL_CULL_FACE);
682 qglEnable (GL_DEPTH_TEST);
684 qglDisable (GL_BLEND);
686 qglColor4ub (255, 255, 255, 255);