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.
28 static rtexture_t *char_texture;
30 //=============================================================================
31 /* Support Routines */
33 #define MAX_CACHED_PICS 256
34 #define CACHEPICHASHSIZE 256
35 static cachepic_t *cachepichash[CACHEPICHASHSIZE];
36 static cachepic_t cachepics[MAX_CACHED_PICS];
37 static int numcachepics;
39 static rtexturepool_t *drawtexturepool;
41 static qbyte concharimage[13396] =
46 extern qbyte *LoadTGA (qbyte *f, int matchwidth, int matchheight);
48 static rtexture_t *draw_generateconchars(void)
51 qbyte buffer[65536][4], *data = NULL;
54 data = LoadTGA (concharimage, 256, 256);
57 for (i = 0;i < 8192;i++)
62 buffer[i][3] = data[i*4+0];
65 for (i = 8192;i < 32768;i++)
70 buffer[i][3] = data[i*4+0];
73 for (i = 32768;i < 40960;i++)
78 buffer[i][3] = data[i*4+0];
81 for (i = 40960;i < 65536;i++)
86 buffer[i][3] = data[i*4+0];
90 return R_LoadTexture2D(drawtexturepool, "conchars", 256, 256, &buffer[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
93 static qbyte pointerimage[256] =
113 static rtexture_t *draw_generatemousepointer(void)
116 qbyte buffer[256][4];
117 for (i = 0;i < 256;i++)
119 if (pointerimage[i] == '.')
128 buffer[i][0] = (pointerimage[i] - '0') * 16;
129 buffer[i][1] = (pointerimage[i] - '0') * 16;
130 buffer[i][2] = (pointerimage[i] - '0') * 16;
134 return R_LoadTexture2D(drawtexturepool, "mousepointer", 16, 16, &buffer[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
137 // must match NUMCROSSHAIRS in r_crosshairs.c
138 #define NUMCROSSHAIRS 5
140 static qbyte *crosshairtexdata[NUMCROSSHAIRS] =
228 static rtexture_t *draw_generatecrosshair(int num)
232 qbyte data[16*16][4];
233 in = crosshairtexdata[num];
234 for (i = 0;i < 16*16;i++)
248 data[i][3] = (qbyte) ((int) (in[i] - '0') * 255 / 7);
251 return R_LoadTexture2D(drawtexturepool, va("crosshair%i", num), 16, 16, &data[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
254 static rtexture_t *draw_generateditherpattern(void)
259 for (y = 0;y < 8;y++)
261 for (x = 0;x < 8;x++)
263 data[(y*8+x)*4+0] = data[(y*8+x)*4+1] = data[(y*8+x)*4+2] = ((x^y) & 4) ? 255 : 0;
264 data[(y*8+x)*4+3] = 255;
267 return R_LoadTexture2D(drawtexturepool, "ditherpattern", 8, 8, data, TEXTYPE_RGBA, TEXF_FORCENEAREST | TEXF_PRECACHE, NULL);
270 memset(data, 255, sizeof(data));
271 data[0] = data[1] = data[2] = data[12] = data[13] = data[14] = 0;
272 return R_LoadTexture2D(drawtexturepool, "ditherpattern", 2, 2, data, TEXTYPE_RGBA, TEXF_FORCENEAREST | TEXF_PRECACHE, NULL);
281 // FIXME: move this to client somehow
282 cachepic_t *Draw_CachePic (const char *path, qboolean persistent)
289 if (!strncmp(CLVIDEOPREFIX, path, sizeof(CLVIDEOPREFIX) - 1)) {
292 video = CL_GetVideo(path);
298 persistentflag = TEXF_PRECACHE;
303 crc = CRC_Block(path, strlen(path));
304 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
305 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
306 if (!strcmp (path, pic->name))
309 if (numcachepics == MAX_CACHED_PICS)
310 Sys_Error ("numcachepics == MAX_CACHED_PICS");
311 pic = cachepics + (numcachepics++);
312 strlcpy (pic->name, path, sizeof(pic->name));
314 pic->chain = cachepichash[hashkey];
315 cachepichash[hashkey] = pic;
317 // load the pic from disk
318 pic->tex = loadtextureimage(drawtexturepool, path, 0, 0, false, TEXF_ALPHA | persistentflag);
319 if (pic->tex == NULL && !strncmp(path, "gfx/", 4))
321 // compatibility with older versions
322 pic->tex = loadtextureimage(drawtexturepool, path + 4, 0, 0, false, TEXF_ALPHA | persistentflag);
323 // failed to find gfx/whatever.tga or similar, try the wad
324 if (pic->tex == NULL && (p = W_GetLumpName (path + 4)))
326 if (!strcmp(path, "gfx/conchars"))
329 // conchars is a raw image and with the wrong transparent color
331 for (i = 0;i < 128 * 128;i++)
334 pic->tex = R_LoadTexture2D(drawtexturepool, path, 128, 128, pix, TEXTYPE_PALETTE, TEXF_ALPHA | persistentflag, palette_complete);
337 pic->tex = R_LoadTexture2D(drawtexturepool, path, p->width, p->height, p->data, TEXTYPE_PALETTE, TEXF_ALPHA | persistentflag, palette_complete);
341 if (pic->tex == NULL && !strcmp(path, "gfx/conchars"))
342 pic->tex = draw_generateconchars();
343 if (pic->tex == NULL && !strcmp(path, "ui/mousepointer.tga"))
344 pic->tex = draw_generatemousepointer();
345 if (pic->tex == NULL && !strcmp(path, "gfx/prydoncursor001.tga"))
346 pic->tex = draw_generatemousepointer();
347 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair1.tga"))
348 pic->tex = draw_generatecrosshair(0);
349 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair2.tga"))
350 pic->tex = draw_generatecrosshair(1);
351 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair3.tga"))
352 pic->tex = draw_generatecrosshair(2);
353 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair4.tga"))
354 pic->tex = draw_generatecrosshair(3);
355 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair5.tga"))
356 pic->tex = draw_generatecrosshair(4);
357 if (pic->tex == NULL && !strcmp(path, "gfx/colorcontrol/ditherpattern.tga"))
358 pic->tex = draw_generateditherpattern();
359 if (pic->tex == NULL)
361 Con_Printf("Draw_CachePic: failed to load %s\n", path);
362 pic->tex = r_texture_notexture;
365 pic->width = R_TextureWidth(pic->tex);
366 pic->height = R_TextureHeight(pic->tex);
370 cachepic_t *Draw_NewPic(const char *picname, int width, int height, int alpha, qbyte *pixels)
375 crc = CRC_Block(picname, strlen(picname));
376 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
377 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
378 if (!strcmp (picname, pic->name))
383 if (pic->tex && pic->width == width && pic->height == height)
385 R_UpdateTexture(pic->tex, pixels);
393 if (numcachepics == MAX_CACHED_PICS)
394 Sys_Error ("numcachepics == MAX_CACHED_PICS");
395 pic = cachepics + (numcachepics++);
396 strcpy (pic->name, picname);
398 pic->chain = cachepichash[hashkey];
399 cachepichash[hashkey] = pic;
404 pic->height = height;
406 R_FreeTexture(pic->tex);
407 pic->tex = R_LoadTexture2D(drawtexturepool, picname, width, height, pixels, TEXTYPE_RGBA, alpha ? TEXF_ALPHA : 0, NULL);
411 void Draw_FreePic(const char *picname)
416 // this doesn't really free the pic, but does free it's texture
417 crc = CRC_Block(picname, strlen(picname));
418 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
419 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
421 if (!strcmp (picname, pic->name) && pic->tex)
423 R_FreeTexture(pic->tex);
436 static void gl_draw_start(void)
438 drawtexturepool = R_AllocTexturePool();
441 memset(cachepichash, 0, sizeof(cachepichash));
443 char_texture = Draw_CachePic("gfx/conchars", true)->tex;
446 static void gl_draw_shutdown(void)
448 R_FreeTexturePool(&drawtexturepool);
451 memset(cachepichash, 0, sizeof(cachepichash));
454 static void gl_draw_newmap(void)
458 void GL_Draw_Init (void)
461 memset(cachepichash, 0, sizeof(cachepichash));
463 R_RegisterModule("GL_Draw", gl_draw_start, gl_draw_shutdown, gl_draw_newmap);
466 float blendvertex3f[9] = {-5000, -5000, 10, 10000, -5000, 10, -5000, 10000, 10};
468 int quadelements[768];
469 void R_DrawQueue(void)
471 int pos, num, chartexnum, texnum, batch;
472 float x, y, w, h, s, t, u, v, *av, *at, c[4];
478 drawqueuemesh_t *mesh;
481 if (!r_render.integer)
484 if (!quadelements[1])
486 // elements for rendering a series of quads as triangles
487 for (batch = 0, pos = 0, num = 0;batch < 128;batch++, num += 4)
489 quadelements[pos++] = num;
490 quadelements[pos++] = num + 1;
491 quadelements[pos++] = num + 2;
492 quadelements[pos++] = num;
493 quadelements[pos++] = num + 2;
494 quadelements[pos++] = num + 3;
498 r_view_width = bound(0, r_refdef.width, vid.width);
499 r_view_height = bound(0, r_refdef.height, vid.height);
501 r_view_x = bound(0, r_refdef.x, vid.width - r_refdef.width);
502 r_view_y = bound(0, r_refdef.y, vid.height - r_refdef.height);
504 r_view_fov_x = bound(0.1, r_refdef.fov_x, 170);
505 r_view_fov_y = bound(0.1, r_refdef.fov_y, 170);
506 r_view_matrix = r_refdef.viewentitymatrix;
507 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
509 qglViewport(r_view_x, vid.height - (r_view_y + r_view_height), r_view_width, r_view_height);
510 GL_SetupView_Mode_Ortho(0, 0, vid_conwidth.integer, vid_conheight.integer, -10, 100);
511 qglDepthFunc(GL_LEQUAL);
512 R_Mesh_Matrix(&r_identitymatrix);
514 chartexnum = R_GetTexture(char_texture);
516 memset(&m, 0, sizeof(m));
525 for (pos = 0;pos < r_refdef.drawqueuesize;pos += ((drawqueue_t *)(r_refdef.drawqueue + pos))->size)
527 dq = (drawqueue_t *)(r_refdef.drawqueue + pos);
530 if(dq->flags == DRAWFLAG_ADDITIVE)
531 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
532 else if(dq->flags == DRAWFLAG_MODULATE)
533 GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
534 else if(dq->flags == DRAWFLAG_2XMODULATE)
535 GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
537 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
542 c[0] = (float) ((color >> 24) & 0xFF) * (1.0f / 255.0f);
543 c[1] = (float) ((color >> 16) & 0xFF) * (1.0f / 255.0f);
544 c[2] = (float) ((color >> 8) & 0xFF) * (1.0f / 255.0f);
545 c[3] = (float) ( color & 0xFF) * (1.0f / 255.0f);
553 case DRAWQUEUE_STRING:
554 GL_Color(c[0], c[1], c[2], c[3]);
555 str = (char *)(dq + 1);
557 m.pointer_vertex = varray_vertex3f;
558 m.pointer_color = NULL;
559 m.pointer_texcoord[0] = varray_texcoord2f[0];
560 m.tex[0] = chartexnum;
562 at = varray_texcoord2f[0];
563 av = varray_vertex3f;
564 while ((num = *str++) && x < vid_conwidth.integer)
568 s = (num & 15)*0.0625f + (0.5f / 256.0f);
569 t = (num >> 4)*0.0625f + (0.5f / 256.0f);
570 u = 0.0625f - (1.0f / 256.0f);
571 v = 0.0625f - (1.0f / 256.0f);
572 at[ 0] = s ;at[ 1] = t ;
573 at[ 2] = s+u;at[ 3] = t ;
574 at[ 4] = s+u;at[ 5] = t+v;
575 at[ 6] = s ;at[ 7] = t+v;
576 av[ 0] = x ;av[ 1] = y ;av[ 2] = 10;
577 av[ 3] = x+w;av[ 4] = y ;av[ 5] = 10;
578 av[ 6] = x+w;av[ 7] = y+h;av[ 8] = 10;
579 av[ 9] = x ;av[10] = y+h;av[11] = 10;
583 if (batchcount >= 128)
585 GL_LockArrays(0, batchcount * 4);
586 R_Mesh_Draw(0, batchcount * 4, batchcount * 2, quadelements);
589 at = varray_texcoord2f[0];
590 av = varray_vertex3f;
597 GL_LockArrays(0, batchcount * 4);
598 R_Mesh_Draw(0, batchcount * 4, batchcount * 2, quadelements);
603 mesh = (void *)(dq + 1);
604 m.pointer_vertex = mesh->data_vertex3f;
605 m.pointer_color = mesh->data_color4f;
606 m.pointer_texcoord[0] = mesh->data_texcoord2f;
607 m.tex[0] = R_GetTexture(mesh->texture);
609 m.pointer_texcoord[0] = NULL;
611 GL_LockArrays(0, mesh->num_vertices);
612 R_Mesh_Draw(0, mesh->num_vertices, mesh->num_triangles, mesh->data_element3i);
615 case DRAWQUEUE_SETCLIP:
617 // We have to convert the con coords into real coords
618 int x , y, width, height;
619 x = dq->x * ((float)vid.width / vid_conwidth.integer);
620 // OGL uses top to bottom
621 y = dq->y * ((float) vid.height / vid_conheight.integer);
622 width = dq->scalex * ((float)vid.width / vid_conwidth.integer);
623 height = dq->scaley * ((float)vid.height / vid_conheight.integer);
625 GL_Scissor(x, y, width, height);
627 GL_ScissorTest(true);
630 case DRAWQUEUE_RESETCLIP:
631 GL_ScissorTest(false);
636 if (!vid_usinghwgamma)
638 // all the blends ignore depth
639 memset(&m, 0, sizeof(m));
640 m.pointer_vertex = blendvertex3f;
644 if (v_color_enable.integer)
646 c[0] = v_color_white_r.value;
647 c[1] = v_color_white_g.value;
648 c[2] = v_color_white_b.value;
651 c[0] = c[1] = c[2] = v_contrast.value;
652 if (c[0] >= 1.01f || c[1] >= 1.01f || c[2] >= 1.01f)
654 GL_BlendFunc(GL_DST_COLOR, GL_ONE);
655 while (c[0] >= 1.01f || c[1] >= 1.01f || c[2] >= 1.01f)
657 GL_Color(bound(0, c[0] - 1, 1), bound(0, c[1] - 1, 1), bound(0, c[2] - 1, 1), 1);
658 R_Mesh_Draw(0, 3, 1, polygonelements);
659 VectorScale(c, 0.5, c);
662 if (v_color_enable.integer)
664 c[0] = v_color_black_r.value;
665 c[1] = v_color_black_g.value;
666 c[2] = v_color_black_b.value;
669 c[0] = c[1] = c[2] = v_brightness.value;
670 if (c[0] >= 0.01f || c[1] >= 0.01f || c[2] >= 0.01f)
672 GL_BlendFunc(GL_ONE, GL_ONE);
673 GL_Color(c[0], c[1], c[2], 1);
674 R_Mesh_Draw(0, 3, 1, polygonelements);