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 extern int gl_maxdrawrangeelementsvertices;
362 extern int gl_maxdrawrangeelementsindices;
364 void R_DrawQueue(void)
366 int pos, num, chartexnum, overbright;
367 float x, y, w, h, s, t, u, v;
370 char *str, *currentpic;
371 int batch, batchcount, additive;
373 drawqueuemesh_t *mesh;
375 if (!r_render.integer)
378 qglViewport(vid.realx, vid.realy, vid.realwidth, vid.realheight);
380 qglMatrixMode(GL_PROJECTION);
382 qglOrtho(0, vid.conwidth, vid.conheight, 0, -99999, 99999);
384 qglMatrixMode(GL_MODELVIEW);
387 qglDisable(GL_DEPTH_TEST);
388 qglDisable(GL_CULL_FACE);
390 qglEnable(GL_TEXTURE_2D);
391 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
393 chartexnum = R_GetTexture(char_texture);
396 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
399 qglBindTexture(GL_TEXTURE_2D, 0);
401 qglColor4ub(0,0,0,0);
403 overbright = v_overbrightbits.integer;
406 for (pos = 0;pos < r_refdef.drawqueuesize;pos += ((drawqueue_t *)(r_refdef.drawqueue + pos))->size)
408 dq = (drawqueue_t *)(r_refdef.drawqueue + pos);
409 if (dq->flags & DRAWFLAG_ADDITIVE)
419 qglBlendFunc(GL_SRC_ALPHA, GL_ONE);
432 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
435 if (color != dq->color)
438 qglColor4ub((qbyte)(((color >> 24) & 0xFF) >> overbright), (qbyte)(((color >> 16) & 0xFF) >> overbright), (qbyte)(((color >> 8) & 0xFF) >> overbright), (qbyte)(color & 0xFF));
440 if (batch && batchcount > 128)
452 str = (char *)(dq + 1);
455 if (strcmp(str, currentpic))
463 pic = Draw_CachePic(str);
464 qglBindTexture(GL_TEXTURE_2D, R_GetTexture(pic->tex));
473 qglBegin(GL_TRIANGLES);
476 qglTexCoord2f (0, 0);qglVertex2f (x , y );
477 qglTexCoord2f (1, 0);qglVertex2f (x+w, y );
478 qglTexCoord2f (1, 1);qglVertex2f (x+w, y+h);
479 qglTexCoord2f (0, 0);qglVertex2f (x , y );
480 qglTexCoord2f (1, 1);qglVertex2f (x+w, y+h);
481 qglTexCoord2f (0, 1);qglVertex2f (x , y+h);
494 qglBindTexture(GL_TEXTURE_2D, 0);
499 qglBegin(GL_TRIANGLES);
502 qglTexCoord2f (0, 0);qglVertex2f (x , y );
503 qglTexCoord2f (1, 0);qglVertex2f (x+w, y );
504 qglTexCoord2f (1, 1);qglVertex2f (x+w, y+h);
505 qglTexCoord2f (0, 0);qglVertex2f (x , y );
506 qglTexCoord2f (1, 1);qglVertex2f (x+w, y+h);
507 qglTexCoord2f (0, 1);qglVertex2f (x , y+h);
511 case DRAWQUEUE_STRING:
512 str = (char *)(dq + 1);
513 if (strcmp("conchars", currentpic))
520 currentpic = "conchars";
521 qglBindTexture(GL_TEXTURE_2D, chartexnum);
526 qglBegin(GL_TRIANGLES);
529 while ((num = *str++) && x < vid.conwidth)
533 s = (num & 15)*0.0625f + (0.5f / 256.0f);
534 t = (num >> 4)*0.0625f + (0.5f / 256.0f);
535 u = 0.0625f - (1.0f / 256.0f);
536 v = 0.0625f - (1.0f / 256.0f);
537 qglTexCoord2f (s , t );qglVertex2f (x , y );
538 qglTexCoord2f (s+u, t );qglVertex2f (x+w, y );
539 qglTexCoord2f (s+u, t+v);qglVertex2f (x+w, y+h);
540 qglTexCoord2f (s , t );qglVertex2f (x , y );
541 qglTexCoord2f (s+u, t+v);qglVertex2f (x+w, y+h);
542 qglTexCoord2f (s , t+v);qglVertex2f (x , y+h);
554 mesh = (void *)(dq + 1);
555 qglBindTexture(GL_TEXTURE_2D, R_GetTexture(mesh->texture));
557 if (gl_mesh_drawmode.integer < 0)
558 Cvar_SetValueQuick(&gl_mesh_drawmode, 0);
559 if (gl_mesh_drawmode.integer > 3)
560 Cvar_SetValueQuick(&gl_mesh_drawmode, 3);
561 if (gl_mesh_drawmode.integer >= 3 && qglDrawRangeElements == NULL)
562 Cvar_SetValueQuick(&gl_mesh_drawmode, 2);
564 if (gl_mesh_drawmode.integer > 0)
566 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), mesh->vertices);CHECKGLERROR
567 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), mesh->texcoords);CHECKGLERROR
568 qglColorPointer(4, GL_UNSIGNED_BYTE, sizeof(qbyte[4]), mesh->colors);CHECKGLERROR
569 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
570 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
571 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
574 if (gl_mesh_drawmode.integer >= 3/* && (mesh->numvertices) <= gl_maxdrawrangeelementsvertices && (mesh->numindices) <= gl_maxdrawrangeelementsindices*/)
576 // GL 1.2 or GL 1.1 with extension
577 GL_LockArray(0, mesh->numvertices);
578 qglDrawRangeElements(GL_TRIANGLES, 0, mesh->numvertices, mesh->numindices, GL_UNSIGNED_INT, mesh->indices);
582 else if (gl_mesh_drawmode.integer >= 2)
585 GL_LockArray(0, mesh->numvertices);
586 qglDrawElements(GL_TRIANGLES, mesh->numindices, GL_UNSIGNED_INT, mesh->indices);
590 else if (gl_mesh_drawmode.integer >= 1)
594 // feed it manually using glArrayElement
595 GL_LockArray(0, mesh->numvertices);
596 qglBegin(GL_TRIANGLES);
597 for (i = 0;i < mesh->numindices;i++)
598 qglArrayElement(mesh->indices[i]);
606 // GL 1.1 but not using vertex arrays - 3dfx glquake minigl driver
608 qglBegin(GL_TRIANGLES);
609 for (i = 0;i < mesh->numindices;i++)
611 in = mesh->indices[i];
612 qglColor4ub(mesh->colors[in * 4], mesh->colors[in * 4 + 1], mesh->colors[in * 4 + 2], mesh->colors[in * 4 + 3]);
613 qglTexCoord2f(mesh->texcoords[in * 2], mesh->texcoords[in * 2 + 1]);
614 qglVertex3f(mesh->vertices[in * 3], mesh->vertices[in * 3 + 1], mesh->vertices[in * 3 + 2]);
619 if (gl_mesh_drawmode.integer > 0)
621 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
622 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
623 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
625 // restore color, since it got trashed by using color array
626 qglColor4ub((qbyte)(((color >> 24) & 0xFF) >> overbright), (qbyte)(((color >> 16) & 0xFF) >> overbright), (qbyte)(((color >> 8) & 0xFF) >> overbright), (qbyte)(color & 0xFF));
636 if (!v_hwgamma.integer)
638 qglDisable(GL_TEXTURE_2D);
640 t = v_contrast.value * (float) (1 << v_overbrightbits.integer);
643 qglBlendFunc (GL_DST_COLOR, GL_ONE);
645 qglBegin (GL_TRIANGLES);
648 num = (int) ((t - 1.0f) * 255.0f);
651 qglColor4ub ((qbyte) num, (qbyte) num, (qbyte) num, 255);
652 qglVertex2f (-5000, -5000);
653 qglVertex2f (10000, -5000);
654 qglVertex2f (-5000, 10000);
662 qglBlendFunc(GL_ZERO, GL_SRC_COLOR);
664 qglBegin(GL_TRIANGLES);
665 num = (int) (t * 255.0f);
666 qglColor4ub ((qbyte) num, (qbyte) num, (qbyte) num, 255);
667 qglVertex2f (-5000, -5000);
668 qglVertex2f (10000, -5000);
669 qglVertex2f (-5000, 10000);
673 if (v_brightness.value >= 0.01f)
675 qglBlendFunc (GL_ONE, GL_ONE);
677 num = (int) (v_brightness.value * 255.0f);
678 qglColor4ub ((qbyte) num, (qbyte) num, (qbyte) num, 255);
680 qglBegin (GL_TRIANGLES);
681 qglVertex2f (-5000, -5000);
682 qglVertex2f (10000, -5000);
683 qglVertex2f (-5000, 10000);
687 qglEnable(GL_TEXTURE_2D);
691 qglBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
693 qglEnable (GL_CULL_FACE);
695 qglEnable (GL_DEPTH_TEST);
697 qglDisable (GL_BLEND);
699 qglColor4ub (255, 255, 255, 255);