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 && pic->width == width && pic->height == height)
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;
374 int arraylocked = false;
376 if (!r_render.integer)
379 qglViewport(vid.realx, vid.realy, vid.realwidth, vid.realheight);
381 qglMatrixMode(GL_PROJECTION);
383 qglOrtho(0, vid.conwidth, vid.conheight, 0, -99999, 99999);
385 qglMatrixMode(GL_MODELVIEW);
388 qglDisable(GL_DEPTH_TEST);
389 qglDisable(GL_CULL_FACE);
391 qglEnable(GL_TEXTURE_2D);
392 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
394 chartexnum = R_GetTexture(char_texture);
397 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
400 qglBindTexture(GL_TEXTURE_2D, 0);
402 qglColor4ub(0,0,0,0);
404 overbright = v_overbrightbits.integer;
407 for (pos = 0;pos < r_refdef.drawqueuesize;pos += ((drawqueue_t *)(r_refdef.drawqueue + pos))->size)
409 dq = (drawqueue_t *)(r_refdef.drawqueue + pos);
410 if (dq->flags & DRAWFLAG_ADDITIVE)
420 qglBlendFunc(GL_SRC_ALPHA, GL_ONE);
433 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
436 if (color != dq->color)
439 qglColor4ub((qbyte)(((color >> 24) & 0xFF) >> overbright), (qbyte)(((color >> 16) & 0xFF) >> overbright), (qbyte)(((color >> 8) & 0xFF) >> overbright), (qbyte)(color & 0xFF));
441 if (batch && batchcount > 128)
453 str = (char *)(dq + 1);
456 if (strcmp(str, currentpic))
464 pic = Draw_CachePic(str);
465 qglBindTexture(GL_TEXTURE_2D, R_GetTexture(pic->tex));
474 qglBegin(GL_TRIANGLES);
477 qglTexCoord2f (0, 0);qglVertex2f (x , y );
478 qglTexCoord2f (1, 0);qglVertex2f (x+w, y );
479 qglTexCoord2f (1, 1);qglVertex2f (x+w, y+h);
480 qglTexCoord2f (0, 0);qglVertex2f (x , y );
481 qglTexCoord2f (1, 1);qglVertex2f (x+w, y+h);
482 qglTexCoord2f (0, 1);qglVertex2f (x , y+h);
495 qglBindTexture(GL_TEXTURE_2D, 0);
500 qglBegin(GL_TRIANGLES);
503 qglTexCoord2f (0, 0);qglVertex2f (x , y );
504 qglTexCoord2f (1, 0);qglVertex2f (x+w, y );
505 qglTexCoord2f (1, 1);qglVertex2f (x+w, y+h);
506 qglTexCoord2f (0, 0);qglVertex2f (x , y );
507 qglTexCoord2f (1, 1);qglVertex2f (x+w, y+h);
508 qglTexCoord2f (0, 1);qglVertex2f (x , y+h);
512 case DRAWQUEUE_STRING:
513 str = (char *)(dq + 1);
514 if (strcmp("conchars", currentpic))
521 currentpic = "conchars";
522 qglBindTexture(GL_TEXTURE_2D, chartexnum);
527 qglBegin(GL_TRIANGLES);
530 while ((num = *str++) && x < vid.conwidth)
534 s = (num & 15)*0.0625f + (0.5f / 256.0f);
535 t = (num >> 4)*0.0625f + (0.5f / 256.0f);
536 u = 0.0625f - (1.0f / 256.0f);
537 v = 0.0625f - (1.0f / 256.0f);
538 qglTexCoord2f (s , t );qglVertex2f (x , y );
539 qglTexCoord2f (s+u, t );qglVertex2f (x+w, y );
540 qglTexCoord2f (s+u, t+v);qglVertex2f (x+w, y+h);
541 qglTexCoord2f (s , t );qglVertex2f (x , y );
542 qglTexCoord2f (s+u, t+v);qglVertex2f (x+w, y+h);
543 qglTexCoord2f (s , t+v);qglVertex2f (x , y+h);
555 mesh = (void *)(dq + 1);
556 qglBindTexture(GL_TEXTURE_2D, R_GetTexture(mesh->texture));
558 if (gl_mesh_drawmode.integer < 0)
559 Cvar_SetValueQuick(&gl_mesh_drawmode, 0);
560 if (gl_mesh_drawmode.integer > 3)
561 Cvar_SetValueQuick(&gl_mesh_drawmode, 3);
562 if (gl_mesh_drawmode.integer >= 3 && qglDrawRangeElements == NULL)
563 Cvar_SetValueQuick(&gl_mesh_drawmode, 2);
565 if (gl_mesh_drawmode.integer > 0)
567 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), mesh->vertices);CHECKGLERROR
568 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), mesh->texcoords);CHECKGLERROR
569 qglColorPointer(4, GL_UNSIGNED_BYTE, sizeof(qbyte[4]), mesh->colors);CHECKGLERROR
570 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
571 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
572 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
575 if (gl_supportslockarrays && gl_lockarrays.integer && gl_mesh_drawmode.integer > 0)
577 qglLockArraysEXT(0, mesh->numvertices);
581 if (gl_mesh_drawmode.integer >= 3/* && (mesh->numvertices) <= gl_maxdrawrangeelementsvertices && (mesh->numindices) <= gl_maxdrawrangeelementsindices*/)
583 // GL 1.2 or GL 1.1 with extension
584 qglDrawRangeElements(GL_TRIANGLES, 0, mesh->numvertices, mesh->numindices, GL_UNSIGNED_INT, mesh->indices);
587 else if (gl_mesh_drawmode.integer >= 2)
590 qglDrawElements(GL_TRIANGLES, mesh->numindices, GL_UNSIGNED_INT, mesh->indices);
593 else if (gl_mesh_drawmode.integer >= 1)
597 // feed it manually using glArrayElement
598 qglBegin(GL_TRIANGLES);
599 for (i = 0;i < mesh->numindices;i++)
600 qglArrayElement(mesh->indices[i]);
607 // GL 1.1 but not using vertex arrays - 3dfx glquake minigl driver
609 qglBegin(GL_TRIANGLES);
610 for (i = 0;i < mesh->numindices;i++)
612 in = mesh->indices[i];
613 qglColor4ub(mesh->colors[in * 4], mesh->colors[in * 4 + 1], mesh->colors[in * 4 + 2], mesh->colors[in * 4 + 3]);
614 qglTexCoord2f(mesh->texcoords[in * 2], mesh->texcoords[in * 2 + 1]);
615 qglVertex3f(mesh->vertices[in * 3], mesh->vertices[in * 3 + 1], mesh->vertices[in * 3 + 2]);
622 qglUnlockArraysEXT();
626 if (gl_mesh_drawmode.integer > 0)
628 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
629 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
630 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
632 // restore color, since it got trashed by using color array
633 qglColor4ub((qbyte)(((color >> 24) & 0xFF) >> overbright), (qbyte)(((color >> 16) & 0xFF) >> overbright), (qbyte)(((color >> 8) & 0xFF) >> overbright), (qbyte)(color & 0xFF));
643 if (!v_hwgamma.integer)
645 qglDisable(GL_TEXTURE_2D);
647 t = v_contrast.value * (float) (1 << v_overbrightbits.integer);
650 qglBlendFunc (GL_DST_COLOR, GL_ONE);
652 qglBegin (GL_TRIANGLES);
655 num = (int) ((t - 1.0f) * 255.0f);
658 qglColor4ub ((qbyte) num, (qbyte) num, (qbyte) num, 255);
659 qglVertex2f (-5000, -5000);
660 qglVertex2f (10000, -5000);
661 qglVertex2f (-5000, 10000);
669 qglBlendFunc(GL_ZERO, GL_SRC_COLOR);
671 qglBegin(GL_TRIANGLES);
672 num = (int) (t * 255.0f);
673 qglColor4ub ((qbyte) num, (qbyte) num, (qbyte) num, 255);
674 qglVertex2f (-5000, -5000);
675 qglVertex2f (10000, -5000);
676 qglVertex2f (-5000, 10000);
680 if (v_brightness.value >= 0.01f)
682 qglBlendFunc (GL_ONE, GL_ONE);
684 num = (int) (v_brightness.value * 255.0f);
685 qglColor4ub ((qbyte) num, (qbyte) num, (qbyte) num, 255);
687 qglBegin (GL_TRIANGLES);
688 qglVertex2f (-5000, -5000);
689 qglVertex2f (10000, -5000);
690 qglVertex2f (-5000, 10000);
694 qglEnable(GL_TEXTURE_2D);
698 qglBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
700 qglEnable (GL_CULL_FACE);
702 qglEnable (GL_DEPTH_TEST);
704 qglDisable (GL_BLEND);
706 qglColor4ub (255, 255, 255, 255);