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 && !strncmp(path, "gfx/", 4))
229 // compatibility with older versions
230 pic->tex = loadtextureimage(drawtexturepool, path + 4, 0, 0, false, false, true);
231 // failed to find gfx/whatever.tga or similar, try the wad
232 if (pic->tex == NULL && (p = W_GetLumpName (path + 4)))
234 if (!strcmp(path, "gfx/conchars"))
237 // conchars is a raw image and with the wrong transparent color
239 for (i = 0;i < 128 * 128;i++)
242 pic->tex = R_LoadTexture (drawtexturepool, path, 128, 128, pix, TEXTYPE_QPALETTE, TEXF_ALPHA | TEXF_PRECACHE);
245 pic->tex = R_LoadTexture (drawtexturepool, path, p->width, p->height, p->data, TEXTYPE_QPALETTE, TEXF_ALPHA | TEXF_PRECACHE);
248 if (pic->tex == NULL && !strcmp(path, "ui/mousepointer.tga"))
249 pic->tex = draw_generatemousepointer();
250 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair1.tga"))
251 pic->tex = draw_generatecrosshair(0);
252 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair2.tga"))
253 pic->tex = draw_generatecrosshair(1);
254 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair3.tga"))
255 pic->tex = draw_generatecrosshair(2);
256 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair4.tga"))
257 pic->tex = draw_generatecrosshair(3);
258 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair5.tga"))
259 pic->tex = draw_generatecrosshair(4);
260 if (pic->tex == NULL)
262 Con_Printf ("Draw_CachePic: failed to load %s", path);
263 pic->tex = r_notexture;
266 pic->width = R_TextureWidth(pic->tex);
267 pic->height = R_TextureHeight(pic->tex);
271 cachepic_t *Draw_NewPic(char *picname, int width, int height, int alpha, qbyte *pixels)
276 crc = CRC_Block(picname, strlen(picname));
277 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
278 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
279 if (!strcmp (picname, pic->name))
284 if (pic->tex && pic->width == width && pic->height == height)
286 R_UpdateTexture(pic->tex, pixels);
294 if (numcachepics == MAX_CACHED_PICS)
295 Sys_Error ("numcachepics == MAX_CACHED_PICS");
296 pic = cachepics + (numcachepics++);
297 strcpy (pic->name, picname);
299 pic->chain = cachepichash[hashkey];
300 cachepichash[hashkey] = pic;
305 pic->height = height;
307 R_FreeTexture(pic->tex);
308 pic->tex = R_LoadTexture (drawtexturepool, picname, width, height, pixels, TEXTYPE_RGBA, alpha ? TEXF_ALPHA : 0);
312 void Draw_FreePic(char *picname)
317 // this doesn't really free the pic, but does free it's texture
318 crc = CRC_Block(picname, strlen(picname));
319 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
320 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
322 if (!strcmp (picname, pic->name))
324 R_FreeTexture(pic->tex);
337 static void gl_draw_start(void)
339 drawtexturepool = R_AllocTexturePool();
342 memset(cachepichash, 0, sizeof(cachepichash));
344 char_texture = Draw_CachePic("gfx/conchars")->tex;
347 static void gl_draw_shutdown(void)
349 R_FreeTexturePool(&drawtexturepool);
352 memset(cachepichash, 0, sizeof(cachepichash));
355 static void gl_draw_newmap(void)
359 void GL_Draw_Init (void)
361 Cvar_RegisterVariable (&scr_conalpha);
364 memset(cachepichash, 0, sizeof(cachepichash));
366 R_RegisterModule("GL_Draw", gl_draw_start, gl_draw_shutdown, gl_draw_newmap);
369 extern cvar_t gl_mesh_drawrangeelements;
370 extern int gl_maxdrawrangeelementsvertices;
371 extern int gl_maxdrawrangeelementsindices;
374 void R_DrawQueue(void)
376 int pos, num, chartexnum, overbright;
377 float x, y, w, h, s, t, u, v;
380 char *str, *currentpic;
383 drawqueuemesh_t *mesh;
385 if (!r_render.integer)
388 GL_SetupView_ViewPort(vid.realx, vid.realy, vid.realwidth, vid.realheight);
389 GL_SetupView_Mode_Ortho(0, 0, vid.conwidth, vid.conheight, -10, 100);
390 GL_SetupView_Orientation_Identity();
391 GL_DepthFunc(GL_LEQUAL);
394 chartexnum = R_GetTexture(char_texture);
401 overbright = v_overbrightbits.integer;
404 for (pos = 0;pos < r_refdef.drawqueuesize;pos += ((drawqueue_t *)(r_refdef.drawqueue + pos))->size)
406 dq = (drawqueue_t *)(r_refdef.drawqueue + pos);
407 additive = (dq->flags & DRAWFLAG_ADDITIVE) != 0;
409 m.blendfunc1 = GL_SRC_ALPHA;
411 m.blendfunc2 = GL_ONE;
413 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
414 m.depthdisable = true;
415 R_Mesh_MainState(&m);
417 cr = (float) ((color >> 24) & 0xFF) * (1.0f / 255.0f) * r_colorscale;
418 cg = (float) ((color >> 16) & 0xFF) * (1.0f / 255.0f) * r_colorscale;
419 cb = (float) ((color >> 8) & 0xFF) * (1.0f / 255.0f) * r_colorscale;
420 ca = (float) ( color & 0xFF) * (1.0f / 255.0f);
429 str = (char *)(dq + 1);
430 if (strcmp(str, currentpic))
438 pic = Draw_CachePic(str);
439 qglBindTexture(GL_TEXTURE_2D, R_GetTexture(pic->tex));
448 varray_color[0] = varray_color[4] = varray_color[ 8] = varray_color[12] = cr;
449 varray_color[1] = varray_color[5] = varray_color[ 9] = varray_color[13] = cg;
450 varray_color[2] = varray_color[6] = varray_color[10] = varray_color[14] = cb;
451 varray_color[3] = varray_color[7] = varray_color[11] = varray_color[15] = ca;
452 varray_texcoord[0] = 0;varray_texcoord[1] = 0;
453 varray_texcoord[2] = 1;varray_texcoord[3] = 0;
454 varray_texcoord[4] = 1;varray_texcoord[5] = 1;
455 varray_texcoord[6] = 0;varray_texcoord[7] = 1;
456 varray_vertex[ 0] = x ;varray_vertex[ 1] = y ;varray_vertex[ 2] = 0;
457 varray_vertex[ 4] = x+w;varray_vertex[ 5] = y ;varray_vertex[ 6] = 0;
458 varray_vertex[ 8] = x+w;varray_vertex[ 9] = y+h;varray_vertex[10] = 0;
459 varray_vertex[12] = x ;varray_vertex[13] = y+h;varray_vertex[14] = 0;
460 R_Mesh_Draw(4, 2, polygonelements);
462 case DRAWQUEUE_STRING:
463 str = (char *)(dq + 1);
464 if (strcmp("gfx/conchars", currentpic))
471 currentpic = "gfx/conchars";
472 qglBindTexture(GL_TEXTURE_2D, chartexnum);
476 at = varray_texcoord;
478 while ((num = *str++) && x < vid.conwidth)
482 s = (num & 15)*0.0625f + (0.5f / 256.0f);
483 t = (num >> 4)*0.0625f + (0.5f / 256.0f);
484 u = 0.0625f - (1.0f / 256.0f);
485 v = 0.0625f - (1.0f / 256.0f);
486 ac[0] = ac[4] = ac[ 8] = ac[12] = cr;
487 ac[1] = ac[5] = ac[ 9] = ac[13] = cg;
488 ac[2] = ac[6] = ac[10] = ac[14] = cb;
489 ac[3] = ac[7] = ac[11] = ac[15] = ca;
490 at[0] = s ;at[1] = t ;
491 at[2] = s+u;at[3] = t ;
492 at[4] = s+u;at[5] = t+v;
493 at[6] = s ;at[7] = t+v;
494 av[0] = x ;av[1] = y ;av[2] = 0;
495 av[4] = x+w;av[1] = y ;av[2] = 0;
496 av[8] = x+w;av[1] = y+h;av[2] = 0;
497 av[0] = x ;av[1] = y+h;av[2] = 0;
502 if (batchcount >= 128)
504 R_Mesh_Draw(batchcount * 4, batchcount * 2, polygonelements);
507 at = varray_texcoord;
513 R_Mesh_Draw(batchcount * 4, batchcount * 2, polygonelements);
516 mesh = (void *)(dq + 1);
517 m.tex[0] = R_GetTexture(mesh->texture);
518 R_Mesh_ResizeCheck(mesh->numvertices);
519 memcpy(varray_vertex, mesh->vertices, sizeof(float[4]) * mesh->numvertices);
520 memcpy(varray_texcoord, mesh->texcoords, sizeof(float[2]) * mesh->numvertices);
521 memcpy(varray_color, mesh->colors, sizeof(float[4]) * mesh->numvertices);
522 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->indices);
528 if (!v_hwgamma.integer)
530 t = v_contrast.value * (float) (1 << v_overbrightbits.integer);
533 m.blendfunc1 = GL_DST_COLOR;
534 m.blendfunc2 = GL_ONE;
535 m.depthdisable = true;
542 varray_color[0] = varray_color[4] = varray_color[ 8] = varray_color[12] = cr;
543 varray_color[1] = varray_color[5] = varray_color[ 9] = varray_color[13] = cr;
544 varray_color[2] = varray_color[6] = varray_color[10] = varray_color[14] = cr;
545 varray_texcoord[0] = 0;varray_texcoord[1] = 0;
546 varray_texcoord[2] = 0;varray_texcoord[3] = 0;
547 varray_texcoord[4] = 0;varray_texcoord[5] = 0;
548 varray_vertex[0] = -5000;varray_vertex[1] = -5000;varray_vertex[2] = 0;
549 varray_vertex[4] = 10000;varray_vertex[1] = -5000;varray_vertex[2] = 0;
550 varray_vertex[8] = -5000;varray_vertex[1] = 10000;varray_vertex[2] = 0;
551 R_Mesh_Draw(3, 1, polygonelements);
557 qglBlendFunc(GL_ZERO, GL_SRC_COLOR);
559 qglBegin(GL_TRIANGLES);
560 num = (int) (t * 255.0f);
561 qglColor4ub ((qbyte) num, (qbyte) num, (qbyte) num, 255);
562 qglVertex2f (-5000, -5000);
563 qglVertex2f (10000, -5000);
564 qglVertex2f (-5000, 10000);
568 if (v_brightness.value >= 0.01f)
570 qglBlendFunc (GL_ONE, GL_ONE);
572 num = (int) (v_brightness.value * 255.0f);
573 qglColor4ub ((qbyte) num, (qbyte) num, (qbyte) num, 255);
575 qglBegin (GL_TRIANGLES);
576 qglVertex2f (-5000, -5000);
577 qglVertex2f (10000, -5000);
578 qglVertex2f (-5000, 10000);
582 qglEnable(GL_TEXTURE_2D);
586 qglBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
588 qglEnable (GL_CULL_FACE);
590 qglEnable (GL_DEPTH_TEST);
592 qglDisable (GL_BLEND);
594 qglColor4ub (255, 255, 255, 255);
598 void R_DrawQueue(void)
600 int pos, num, chartexnum, overbright;
601 float x, y, w, h, s, t, u, v;
604 char *str, *currentpic;
605 int batch, batchcount, additive;
607 drawqueuemesh_t *mesh;
609 if (!r_render.integer)
612 qglMatrixMode(GL_PROJECTION);
614 qglOrtho(0, vid.conwidth, vid.conheight, 0, -99999, 99999);
616 qglMatrixMode(GL_MODELVIEW);
619 qglDisable(GL_DEPTH_TEST);
620 qglDisable(GL_CULL_FACE);
622 qglEnable(GL_TEXTURE_2D);
623 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
625 chartexnum = R_GetTexture(char_texture);
628 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
631 qglBindTexture(GL_TEXTURE_2D, 0);
633 qglColor4ub(0,0,0,0);
635 overbright = v_overbrightbits.integer;
638 for (pos = 0;pos < r_refdef.drawqueuesize;pos += ((drawqueue_t *)(r_refdef.drawqueue + pos))->size)
640 dq = (drawqueue_t *)(r_refdef.drawqueue + pos);
641 if (dq->flags & DRAWFLAG_ADDITIVE)
651 qglBlendFunc(GL_SRC_ALPHA, GL_ONE);
664 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
667 if (color != dq->color)
670 qglColor4ub((qbyte)(((color >> 24) & 0xFF) >> overbright), (qbyte)(((color >> 16) & 0xFF) >> overbright), (qbyte)(((color >> 8) & 0xFF) >> overbright), (qbyte)(color & 0xFF));
672 if (batch && batchcount > 128)
684 str = (char *)(dq + 1);
687 if (strcmp(str, currentpic))
695 pic = Draw_CachePic(str);
696 qglBindTexture(GL_TEXTURE_2D, R_GetTexture(pic->tex));
705 qglBegin(GL_TRIANGLES);
708 qglTexCoord2f (0, 0);qglVertex2f (x , y );
709 qglTexCoord2f (1, 0);qglVertex2f (x+w, y );
710 qglTexCoord2f (1, 1);qglVertex2f (x+w, y+h);
711 qglTexCoord2f (0, 0);qglVertex2f (x , y );
712 qglTexCoord2f (1, 1);qglVertex2f (x+w, y+h);
713 qglTexCoord2f (0, 1);qglVertex2f (x , y+h);
726 qglBindTexture(GL_TEXTURE_2D, 0);
731 qglBegin(GL_TRIANGLES);
734 qglTexCoord2f (0, 0);qglVertex2f (x , y );
735 qglTexCoord2f (1, 0);qglVertex2f (x+w, y );
736 qglTexCoord2f (1, 1);qglVertex2f (x+w, y+h);
737 qglTexCoord2f (0, 0);qglVertex2f (x , y );
738 qglTexCoord2f (1, 1);qglVertex2f (x+w, y+h);
739 qglTexCoord2f (0, 1);qglVertex2f (x , y+h);
743 case DRAWQUEUE_STRING:
744 str = (char *)(dq + 1);
745 if (strcmp("gfx/conchars", currentpic))
752 currentpic = "gfx/conchars";
753 qglBindTexture(GL_TEXTURE_2D, chartexnum);
758 qglBegin(GL_TRIANGLES);
761 while ((num = *str++) && x < vid.conwidth)
765 s = (num & 15)*0.0625f + (0.5f / 256.0f);
766 t = (num >> 4)*0.0625f + (0.5f / 256.0f);
767 u = 0.0625f - (1.0f / 256.0f);
768 v = 0.0625f - (1.0f / 256.0f);
769 qglTexCoord2f (s , t );qglVertex2f (x , y );
770 qglTexCoord2f (s+u, t );qglVertex2f (x+w, y );
771 qglTexCoord2f (s+u, t+v);qglVertex2f (x+w, y+h);
772 qglTexCoord2f (s , t );qglVertex2f (x , y );
773 qglTexCoord2f (s+u, t+v);qglVertex2f (x+w, y+h);
774 qglTexCoord2f (s , t+v);qglVertex2f (x , y+h);
787 mesh = (void *)(dq + 1);
788 qglBindTexture(GL_TEXTURE_2D, R_GetTexture(mesh->texture));
789 qglVertexPointer(3, GL_FLOAT, sizeof(float[4]), mesh->vertices);CHECKGLERROR
790 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), mesh->texcoords);CHECKGLERROR
791 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), mesh->colors);CHECKGLERROR
792 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
793 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
794 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
795 GL_DrawRangeElements(0, mesh->numvertices, mesh->numindices, mesh->indices);
796 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
797 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
798 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
800 // restore color, since it got trashed by using color array
801 qglColor4ub((qbyte)(((color >> 24) & 0xFF) >> overbright), (qbyte)(((color >> 16) & 0xFF) >> overbright), (qbyte)(((color >> 8) & 0xFF) >> overbright), (qbyte)(color & 0xFF));
811 if (!v_hwgamma.integer)
813 qglDisable(GL_TEXTURE_2D);
815 t = v_contrast.value * (float) (1 << v_overbrightbits.integer);
818 qglBlendFunc (GL_DST_COLOR, GL_ONE);
820 qglBegin (GL_TRIANGLES);
823 num = (int) ((t - 1.0f) * 255.0f);
826 qglColor4ub ((qbyte) num, (qbyte) num, (qbyte) num, 255);
827 qglVertex2f (-5000, -5000);
828 qglVertex2f (10000, -5000);
829 qglVertex2f (-5000, 10000);
837 qglBlendFunc(GL_ZERO, GL_SRC_COLOR);
839 qglBegin(GL_TRIANGLES);
840 num = (int) (t * 255.0f);
841 qglColor4ub ((qbyte) num, (qbyte) num, (qbyte) num, 255);
842 qglVertex2f (-5000, -5000);
843 qglVertex2f (10000, -5000);
844 qglVertex2f (-5000, 10000);
848 if (v_brightness.value >= 0.01f)
850 qglBlendFunc (GL_ONE, GL_ONE);
852 num = (int) (v_brightness.value * 255.0f);
853 qglColor4ub ((qbyte) num, (qbyte) num, (qbyte) num, 255);
855 qglBegin (GL_TRIANGLES);
856 qglVertex2f (-5000, -5000);
857 qglVertex2f (10000, -5000);
858 qglVertex2f (-5000, 10000);
862 qglEnable(GL_TEXTURE_2D);
866 qglBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
868 qglEnable (GL_CULL_FACE);
870 qglEnable (GL_DEPTH_TEST);
872 qglDisable (GL_BLEND);
874 qglColor4ub (255, 255, 255, 255);