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[11356] =
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 < 32768;i++)
62 buffer[i][3] = data[i*4+0];
65 for (i = 32768;i < 65536;i++)
70 buffer[i][3] = data[i*4+0];
73 return R_LoadTexture2D(drawtexturepool, "conchars", 256, 256, &buffer[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
76 static qbyte pointerimage[256] =
96 static rtexture_t *draw_generatemousepointer(void)
100 for (i = 0;i < 256;i++)
102 if (pointerimage[i] == '.')
111 buffer[i][0] = (pointerimage[i] - '0') * 16;
112 buffer[i][1] = (pointerimage[i] - '0') * 16;
113 buffer[i][2] = (pointerimage[i] - '0') * 16;
117 return R_LoadTexture2D(drawtexturepool, "mousepointer", 16, 16, &buffer[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
120 // must match NUMCROSSHAIRS in r_crosshairs.c
121 #define NUMCROSSHAIRS 5
123 static qbyte *crosshairtexdata[NUMCROSSHAIRS] =
211 static rtexture_t *draw_generatecrosshair(int num)
215 qbyte data[16*16][4];
216 in = crosshairtexdata[num];
217 for (i = 0;i < 16*16;i++)
231 data[i][3] = (qbyte) ((int) (in[i] - '0') * 255 / 7);
234 return R_LoadTexture2D(drawtexturepool, va("crosshair%i", num), 16, 16, &data[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
237 static rtexture_t *draw_generateditherpattern(void)
242 for (y = 0;y < 8;y++)
244 for (x = 0;x < 8;x++)
246 data[(y*8+x)*4+0] = data[(y*8+x)*4+1] = data[(y*8+x)*4+2] = ((x^y) & 4) ? 255 : 0;
247 data[(y*8+x)*4+3] = 255;
250 return R_LoadTexture2D(drawtexturepool, "ditherpattern", 8, 8, data, TEXTYPE_RGBA, TEXF_FORCENEAREST | TEXF_PRECACHE, NULL);
253 memset(data, 255, sizeof(data));
254 data[0] = data[1] = data[2] = data[12] = data[13] = data[14] = 0;
255 return R_LoadTexture2D(drawtexturepool, "ditherpattern", 2, 2, data, TEXTYPE_RGBA, TEXF_FORCENEAREST | TEXF_PRECACHE, NULL);
264 // FIXME: move this to client somehow
265 cachepic_t *Draw_CachePic (char *path, qboolean persistent)
272 if (!strncmp(CLVIDEOPREFIX, path, sizeof(CLVIDEOPREFIX) - 1)) {
275 video = CL_GetVideo(path);
281 persistentflag = TEXF_PRECACHE;
286 crc = CRC_Block(path, strlen(path));
287 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
288 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
289 if (!strcmp (path, pic->name))
292 if (numcachepics == MAX_CACHED_PICS)
293 Sys_Error ("numcachepics == MAX_CACHED_PICS");
294 pic = cachepics + (numcachepics++);
295 strcpy (pic->name, path);
297 pic->chain = cachepichash[hashkey];
298 cachepichash[hashkey] = pic;
300 // load the pic from disk
301 pic->tex = loadtextureimage(drawtexturepool, path, 0, 0, false, TEXF_ALPHA | persistentflag);
302 if (pic->tex == NULL && !strncmp(path, "gfx/", 4))
304 // compatibility with older versions
305 pic->tex = loadtextureimage(drawtexturepool, path + 4, 0, 0, false, TEXF_ALPHA | persistentflag);
306 // failed to find gfx/whatever.tga or similar, try the wad
307 if (pic->tex == NULL && (p = W_GetLumpName (path + 4)))
309 if (!strcmp(path, "gfx/conchars"))
312 // conchars is a raw image and with the wrong transparent color
314 for (i = 0;i < 128 * 128;i++)
317 pic->tex = R_LoadTexture2D(drawtexturepool, path, 128, 128, pix, TEXTYPE_PALETTE, TEXF_ALPHA | persistentflag, palette_complete);
320 pic->tex = R_LoadTexture2D(drawtexturepool, path, p->width, p->height, p->data, TEXTYPE_PALETTE, TEXF_ALPHA | persistentflag, palette_complete);
324 if (pic->tex == NULL && !strcmp(path, "gfx/conchars"))
325 pic->tex = draw_generateconchars();
326 if (pic->tex == NULL && !strcmp(path, "ui/mousepointer.tga"))
327 pic->tex = draw_generatemousepointer();
328 if (pic->tex == NULL && !strcmp(path, "gfx/prydoncursor001.tga"))
329 pic->tex = draw_generatemousepointer();
330 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair1.tga"))
331 pic->tex = draw_generatecrosshair(0);
332 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair2.tga"))
333 pic->tex = draw_generatecrosshair(1);
334 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair3.tga"))
335 pic->tex = draw_generatecrosshair(2);
336 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair4.tga"))
337 pic->tex = draw_generatecrosshair(3);
338 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair5.tga"))
339 pic->tex = draw_generatecrosshair(4);
340 if (pic->tex == NULL && !strcmp(path, "gfx/colorcontrol/ditherpattern.tga"))
341 pic->tex = draw_generateditherpattern();
342 if (pic->tex == NULL)
344 Con_Printf("Draw_CachePic: failed to load %s\n", path);
345 pic->tex = r_texture_notexture;
348 pic->width = R_TextureWidth(pic->tex);
349 pic->height = R_TextureHeight(pic->tex);
353 cachepic_t *Draw_NewPic(char *picname, int width, int height, int alpha, qbyte *pixels)
358 crc = CRC_Block(picname, strlen(picname));
359 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
360 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
361 if (!strcmp (picname, pic->name))
366 if (pic->tex && pic->width == width && pic->height == height)
368 R_UpdateTexture(pic->tex, pixels);
376 if (numcachepics == MAX_CACHED_PICS)
377 Sys_Error ("numcachepics == MAX_CACHED_PICS");
378 pic = cachepics + (numcachepics++);
379 strcpy (pic->name, picname);
381 pic->chain = cachepichash[hashkey];
382 cachepichash[hashkey] = pic;
387 pic->height = height;
389 R_FreeTexture(pic->tex);
390 pic->tex = R_LoadTexture2D(drawtexturepool, picname, width, height, pixels, TEXTYPE_RGBA, alpha ? TEXF_ALPHA : 0, NULL);
394 void Draw_FreePic(char *picname)
399 // this doesn't really free the pic, but does free it's texture
400 crc = CRC_Block(picname, strlen(picname));
401 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
402 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
404 if (!strcmp (picname, pic->name) && pic->tex)
406 R_FreeTexture(pic->tex);
419 static void gl_draw_start(void)
421 drawtexturepool = R_AllocTexturePool();
424 memset(cachepichash, 0, sizeof(cachepichash));
426 char_texture = Draw_CachePic("gfx/conchars", true)->tex;
429 static void gl_draw_shutdown(void)
431 R_FreeTexturePool(&drawtexturepool);
434 memset(cachepichash, 0, sizeof(cachepichash));
437 static void gl_draw_newmap(void)
441 void GL_Draw_Init (void)
444 memset(cachepichash, 0, sizeof(cachepichash));
446 R_RegisterModule("GL_Draw", gl_draw_start, gl_draw_shutdown, gl_draw_newmap);
449 float blendvertex3f[9] = {-5000, -5000, 10, 10000, -5000, 10, -5000, 10000, 10};
451 int quadelements[768];
452 void R_DrawQueue(void)
454 int pos, num, chartexnum, texnum, batch;
455 float x, y, w, h, s, t, u, v, *av, *at, c[4];
461 drawqueuemesh_t *mesh;
464 if (!r_render.integer)
467 if (!quadelements[1])
469 // elements for rendering a series of quads as triangles
470 for (batch = 0, pos = 0, num = 0;batch < 128;batch++, num += 4)
472 quadelements[pos++] = num;
473 quadelements[pos++] = num + 1;
474 quadelements[pos++] = num + 2;
475 quadelements[pos++] = num;
476 quadelements[pos++] = num + 2;
477 quadelements[pos++] = num + 3;
481 r_view_width = bound(0, r_refdef.width, vid.realwidth);
482 r_view_height = bound(0, r_refdef.height, vid.realheight);
484 r_view_x = bound(0, r_refdef.x, vid.realwidth - r_refdef.width);
485 r_view_y = bound(0, r_refdef.y, vid.realheight - r_refdef.height);
487 r_view_fov_x = bound(0.1, r_refdef.fov_x, 170);
488 r_view_fov_y = bound(0.1, r_refdef.fov_y, 170);
489 r_view_matrix = r_refdef.viewentitymatrix;
490 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
492 qglViewport(r_view_x, vid.realheight - (r_view_y + r_view_height), r_view_width, r_view_height);
493 GL_SetupView_Mode_Ortho(0, 0, vid.conwidth, vid.conheight, -10, 100);
494 qglDepthFunc(GL_LEQUAL);
495 R_Mesh_Matrix(&r_identitymatrix);
497 chartexnum = R_GetTexture(char_texture);
499 memset(&m, 0, sizeof(m));
508 for (pos = 0;pos < r_refdef.drawqueuesize;pos += ((drawqueue_t *)(r_refdef.drawqueue + pos))->size)
510 dq = (drawqueue_t *)(r_refdef.drawqueue + pos);
513 if(dq->flags == DRAWFLAG_ADDITIVE)
514 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
515 else if(dq->flags == DRAWFLAG_MODULATE)
516 GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
517 else if(dq->flags == DRAWFLAG_2XMODULATE)
518 GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
520 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
525 c[0] = (float) ((color >> 24) & 0xFF) * (1.0f / 255.0f);
526 c[1] = (float) ((color >> 16) & 0xFF) * (1.0f / 255.0f);
527 c[2] = (float) ((color >> 8) & 0xFF) * (1.0f / 255.0f);
528 c[3] = (float) ( color & 0xFF) * (1.0f / 255.0f);
536 case DRAWQUEUE_STRING:
537 GL_Color(c[0], c[1], c[2], c[3]);
538 str = (char *)(dq + 1);
540 m.pointer_vertex = varray_vertex3f;
541 m.pointer_color = NULL;
542 m.pointer_texcoord[0] = varray_texcoord2f[0];
543 m.tex[0] = chartexnum;
545 at = varray_texcoord2f[0];
546 av = varray_vertex3f;
547 while ((num = *str++) && x < vid.conwidth)
551 s = (num & 15)*0.0625f + (0.5f / 256.0f);
552 t = (num >> 4)*0.0625f + (0.5f / 256.0f);
553 u = 0.0625f - (1.0f / 256.0f);
554 v = 0.0625f - (1.0f / 256.0f);
555 at[ 0] = s ;at[ 1] = t ;
556 at[ 2] = s+u;at[ 3] = t ;
557 at[ 4] = s+u;at[ 5] = t+v;
558 at[ 6] = s ;at[ 7] = t+v;
559 av[ 0] = x ;av[ 1] = y ;av[ 2] = 10;
560 av[ 3] = x+w;av[ 4] = y ;av[ 5] = 10;
561 av[ 6] = x+w;av[ 7] = y+h;av[ 8] = 10;
562 av[ 9] = x ;av[10] = y+h;av[11] = 10;
566 if (batchcount >= 128)
568 GL_LockArrays(0, batchcount * 4);
569 R_Mesh_Draw(0, batchcount * 4, batchcount * 2, quadelements);
572 at = varray_texcoord2f[0];
573 av = varray_vertex3f;
580 GL_LockArrays(0, batchcount * 4);
581 R_Mesh_Draw(0, batchcount * 4, batchcount * 2, quadelements);
586 mesh = (void *)(dq + 1);
587 m.pointer_vertex = mesh->data_vertex3f;
588 m.pointer_color = mesh->data_color4f;
589 m.pointer_texcoord[0] = mesh->data_texcoord2f;
590 m.tex[0] = R_GetTexture(mesh->texture);
592 m.pointer_texcoord[0] = NULL;
594 GL_LockArrays(0, mesh->num_vertices);
595 R_Mesh_Draw(0, mesh->num_vertices, mesh->num_triangles, mesh->data_element3i);
598 case DRAWQUEUE_SETCLIP:
600 // We have to convert the con coords into real coords
601 int x , y, width, height;
602 x = dq->x * ((float)vid.realwidth / vid.conwidth);
603 // OGL uses top to bottom
604 y = dq->y * ((float) vid.realheight / vid.conheight);
605 width = dq->scalex * ((float)vid.realwidth / vid.conwidth);
606 height = dq->scaley * ((float)vid.realheight / vid.conheight);
608 GL_Scissor(x, y, width, height);
610 GL_ScissorTest(true);
613 case DRAWQUEUE_RESETCLIP:
614 GL_ScissorTest(false);
619 if (!vid_usinghwgamma)
621 // all the blends ignore depth
622 memset(&m, 0, sizeof(m));
623 m.pointer_vertex = blendvertex3f;
627 if (v_color_enable.integer)
629 c[0] = v_color_white_r.value;
630 c[1] = v_color_white_g.value;
631 c[2] = v_color_white_b.value;
634 c[0] = c[1] = c[2] = v_contrast.value;
635 if (c[0] >= 1.01f || c[1] >= 1.01f || c[2] >= 1.01f)
637 GL_BlendFunc(GL_DST_COLOR, GL_ONE);
638 while (c[0] >= 1.01f || c[1] >= 1.01f || c[2] >= 1.01f)
640 GL_Color(bound(0, c[0] - 1, 1), bound(0, c[1] - 1, 1), bound(0, c[2] - 1, 1), 1);
641 R_Mesh_Draw(0, 3, 1, polygonelements);
642 VectorScale(c, 0.5, c);
645 if (v_color_enable.integer)
647 c[0] = v_color_black_r.value;
648 c[1] = v_color_black_g.value;
649 c[2] = v_color_black_b.value;
652 c[0] = c[1] = c[2] = v_brightness.value;
653 if (c[0] >= 0.01f || c[1] >= 0.01f || c[2] >= 0.01f)
655 GL_BlendFunc(GL_ONE, GL_ONE);
656 GL_Color(c[0], c[1], c[2], 1);
657 R_Mesh_Draw(0, 3, 1, polygonelements);