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.
24 static rtexture_t *char_texture;
26 //=============================================================================
27 /* Support Routines */
29 #define MAX_CACHED_PICS 256
30 #define CACHEPICHASHSIZE 256
31 static cachepic_t *cachepichash[CACHEPICHASHSIZE];
32 static cachepic_t cachepics[MAX_CACHED_PICS];
33 static int numcachepics;
35 static rtexturepool_t *drawtexturepool;
37 static qbyte concharimage[11356] =
42 extern qbyte *LoadTGA (qbyte *f, int matchwidth, int matchheight);
44 static rtexture_t *draw_generateconchars(void)
47 qbyte buffer[65536][4], *data = NULL;
50 data = LoadTGA (concharimage, 256, 256);
53 for (i = 0;i < 32768;i++)
58 buffer[i][3] = data[i*4+0];
61 for (i = 32768;i < 65536;i++)
66 buffer[i][3] = data[i*4+0];
69 return R_LoadTexture2D(drawtexturepool, "conchars", 256, 256, &buffer[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
72 static qbyte pointerimage[256] =
92 static rtexture_t *draw_generatemousepointer(void)
96 for (i = 0;i < 256;i++)
98 if (pointerimage[i] == '.')
107 buffer[i][0] = (pointerimage[i] - '0') * 16;
108 buffer[i][1] = (pointerimage[i] - '0') * 16;
109 buffer[i][2] = (pointerimage[i] - '0') * 16;
113 return R_LoadTexture2D(drawtexturepool, "mousepointer", 16, 16, &buffer[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
116 // must match NUMCROSSHAIRS in r_crosshairs.c
117 #define NUMCROSSHAIRS 5
119 static qbyte *crosshairtexdata[NUMCROSSHAIRS] =
207 static rtexture_t *draw_generatecrosshair(int num)
211 qbyte data[16*16][4];
212 in = crosshairtexdata[num];
213 for (i = 0;i < 16*16;i++)
227 data[i][3] = (qbyte) ((int) (in[i] - '0') * 255 / 7);
230 return R_LoadTexture2D(drawtexturepool, va("crosshair%i", num), 16, 16, &data[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
233 static rtexture_t *draw_generateditherpattern(void)
238 for (y = 0;y < 8;y++)
240 for (x = 0;x < 8;x++)
242 data[(y*8+x)*4+0] = data[(y*8+x)*4+1] = data[(y*8+x)*4+2] = ((x^y) & 4) ? 255 : 0;
243 data[(y*8+x)*4+3] = 255;
246 return R_LoadTexture2D(drawtexturepool, "ditherpattern", 8, 8, data, TEXTYPE_RGBA, TEXF_FORCENEAREST | TEXF_PRECACHE, NULL);
249 memset(data, 255, sizeof(data));
250 data[0] = data[1] = data[2] = data[12] = data[13] = data[14] = 0;
251 return R_LoadTexture2D(drawtexturepool, "ditherpattern", 2, 2, data, TEXTYPE_RGBA, TEXF_FORCENEAREST | TEXF_PRECACHE, NULL);
260 // FIXME: move this to client somehow
261 cachepic_t *Draw_CachePic (char *path)
267 crc = CRC_Block(path, strlen(path));
268 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
269 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
270 if (!strcmp (path, pic->name))
273 if (numcachepics == MAX_CACHED_PICS)
274 Sys_Error ("numcachepics == MAX_CACHED_PICS");
275 pic = cachepics + (numcachepics++);
276 strcpy (pic->name, path);
278 pic->chain = cachepichash[hashkey];
279 cachepichash[hashkey] = pic;
281 // load the pic from disk
282 pic->tex = loadtextureimage(drawtexturepool, path, 0, 0, false, TEXF_ALPHA | TEXF_PRECACHE);
283 if (pic->tex == NULL && !strncmp(path, "gfx/", 4))
285 // compatibility with older versions
286 pic->tex = loadtextureimage(drawtexturepool, path + 4, 0, 0, false, TEXF_ALPHA | TEXF_PRECACHE);
287 // failed to find gfx/whatever.tga or similar, try the wad
288 if (pic->tex == NULL && (p = W_GetLumpName (path + 4)))
290 if (!strcmp(path, "gfx/conchars"))
293 // conchars is a raw image and with the wrong transparent color
295 for (i = 0;i < 128 * 128;i++)
298 pic->tex = R_LoadTexture2D(drawtexturepool, path, 128, 128, pix, TEXTYPE_PALETTE, TEXF_ALPHA | TEXF_PRECACHE, palette_complete);
301 pic->tex = R_LoadTexture2D(drawtexturepool, path, p->width, p->height, p->data, TEXTYPE_PALETTE, TEXF_ALPHA | TEXF_PRECACHE, palette_complete);
305 if (pic->tex == NULL && !strcmp(path, "gfx/conchars"))
306 pic->tex = draw_generateconchars();
307 if (pic->tex == NULL && !strcmp(path, "ui/mousepointer.tga"))
308 pic->tex = draw_generatemousepointer();
309 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair1.tga"))
310 pic->tex = draw_generatecrosshair(0);
311 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair2.tga"))
312 pic->tex = draw_generatecrosshair(1);
313 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair3.tga"))
314 pic->tex = draw_generatecrosshair(2);
315 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair4.tga"))
316 pic->tex = draw_generatecrosshair(3);
317 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair5.tga"))
318 pic->tex = draw_generatecrosshair(4);
319 if (pic->tex == NULL && !strcmp(path, "gfx/colorcontrol/ditherpattern.tga"))
320 pic->tex = draw_generateditherpattern();
321 if (pic->tex == NULL)
323 Con_Printf("Draw_CachePic: failed to load %s\n", path);
324 pic->tex = r_notexture;
327 pic->width = R_TextureWidth(pic->tex);
328 pic->height = R_TextureHeight(pic->tex);
332 cachepic_t *Draw_NewPic(char *picname, int width, int height, int alpha, qbyte *pixels)
337 crc = CRC_Block(picname, strlen(picname));
338 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
339 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
340 if (!strcmp (picname, pic->name))
345 if (pic->tex && pic->width == width && pic->height == height)
347 R_UpdateTexture(pic->tex, pixels);
355 if (numcachepics == MAX_CACHED_PICS)
356 Sys_Error ("numcachepics == MAX_CACHED_PICS");
357 pic = cachepics + (numcachepics++);
358 strcpy (pic->name, picname);
360 pic->chain = cachepichash[hashkey];
361 cachepichash[hashkey] = pic;
366 pic->height = height;
368 R_FreeTexture(pic->tex);
369 pic->tex = R_LoadTexture2D(drawtexturepool, picname, width, height, pixels, TEXTYPE_RGBA, alpha ? TEXF_ALPHA : 0, NULL);
373 void Draw_FreePic(char *picname)
378 // this doesn't really free the pic, but does free it's texture
379 crc = CRC_Block(picname, strlen(picname));
380 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
381 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
383 if (!strcmp (picname, pic->name) && pic->tex)
385 R_FreeTexture(pic->tex);
398 static void gl_draw_start(void)
400 drawtexturepool = R_AllocTexturePool();
403 memset(cachepichash, 0, sizeof(cachepichash));
405 char_texture = Draw_CachePic("gfx/conchars")->tex;
408 static void gl_draw_shutdown(void)
410 R_FreeTexturePool(&drawtexturepool);
413 memset(cachepichash, 0, sizeof(cachepichash));
416 static void gl_draw_newmap(void)
420 void GL_Draw_Init (void)
423 memset(cachepichash, 0, sizeof(cachepichash));
425 R_RegisterModule("GL_Draw", gl_draw_start, gl_draw_shutdown, gl_draw_newmap);
428 float blendvertex3f[9] = {-5000, -5000, 10, 10000, -5000, 10, -5000, 10000, 10};
430 int quadelements[768];
431 void R_DrawQueue(void)
433 int pos, num, chartexnum, texnum, batch;
434 float x, y, w, h, s, t, u, v, *av, *at, c[4];
440 drawqueuemesh_t *mesh;
443 if (!r_render.integer)
446 if (!quadelements[1])
448 // elements for rendering a series of quads as triangles
449 for (batch = 0, pos = 0, num = 0;batch < 128;batch++, num += 4)
451 quadelements[pos++] = num;
452 quadelements[pos++] = num + 1;
453 quadelements[pos++] = num + 2;
454 quadelements[pos++] = num;
455 quadelements[pos++] = num + 2;
456 quadelements[pos++] = num + 3;
460 r_view_width = bound(0, r_refdef.width, vid.realwidth);
461 r_view_height = bound(0, r_refdef.height, vid.realheight);
463 r_view_x = bound(0, r_refdef.x, vid.realwidth - r_refdef.width);
464 r_view_y = bound(0, r_refdef.y, vid.realheight - r_refdef.height);
466 r_view_fov_x = bound(1, r_refdef.fov_x, 170);
467 r_view_fov_y = bound(1, r_refdef.fov_y, 170);
468 r_view_matrix = r_refdef.viewentitymatrix;
469 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
471 qglViewport(r_view_x, vid.realheight - (r_view_y + r_view_height), r_view_width, r_view_height);
472 GL_SetupView_Mode_Ortho(0, 0, vid.conwidth, vid.conheight, -10, 100);
473 qglDepthFunc(GL_LEQUAL);
474 R_Mesh_Matrix(&r_identitymatrix);
476 chartexnum = R_GetTexture(char_texture);
478 memset(&m, 0, sizeof(m));
487 for (pos = 0;pos < r_refdef.drawqueuesize;pos += ((drawqueue_t *)(r_refdef.drawqueue + pos))->size)
489 dq = (drawqueue_t *)(r_refdef.drawqueue + pos);
492 if(dq->flags == DRAWFLAG_ADDITIVE)
493 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
494 else if(dq->flags == DRAWFLAG_MODULATE)
495 GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
496 else if(dq->flags == DRAWFLAG_2XMODULATE)
497 GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
499 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
504 c[0] = (float) ((color >> 24) & 0xFF) * (1.0f / 255.0f);
505 c[1] = (float) ((color >> 16) & 0xFF) * (1.0f / 255.0f);
506 c[2] = (float) ((color >> 8) & 0xFF) * (1.0f / 255.0f);
507 c[3] = (float) ( color & 0xFF) * (1.0f / 255.0f);
515 case DRAWQUEUE_STRING:
516 GL_Color(c[0], c[1], c[2], c[3]);
517 str = (char *)(dq + 1);
519 m.pointer_vertex = varray_vertex3f;
520 m.pointer_color = NULL;
521 m.pointer_texcoord[0] = varray_texcoord2f[0];
522 m.tex[0] = chartexnum;
524 at = varray_texcoord2f[0];
525 av = varray_vertex3f;
526 while ((num = *str++) && x < vid.conwidth)
530 s = (num & 15)*0.0625f + (0.5f / 256.0f);
531 t = (num >> 4)*0.0625f + (0.5f / 256.0f);
532 u = 0.0625f - (1.0f / 256.0f);
533 v = 0.0625f - (1.0f / 256.0f);
534 at[ 0] = s ;at[ 1] = t ;
535 at[ 2] = s+u;at[ 3] = t ;
536 at[ 4] = s+u;at[ 5] = t+v;
537 at[ 6] = s ;at[ 7] = t+v;
538 av[ 0] = x ;av[ 1] = y ;av[ 2] = 10;
539 av[ 3] = x+w;av[ 4] = y ;av[ 5] = 10;
540 av[ 6] = x+w;av[ 7] = y+h;av[ 8] = 10;
541 av[ 9] = x ;av[10] = y+h;av[11] = 10;
545 if (batchcount >= 128)
547 GL_LockArrays(0, batchcount * 4);
548 R_Mesh_Draw(batchcount * 4, batchcount * 2, quadelements);
551 at = varray_texcoord2f[0];
552 av = varray_vertex3f;
559 GL_LockArrays(0, batchcount * 4);
560 R_Mesh_Draw(batchcount * 4, batchcount * 2, quadelements);
565 mesh = (void *)(dq + 1);
566 m.pointer_vertex = mesh->data_vertex3f;
567 m.pointer_color = mesh->data_color4f;
568 m.pointer_texcoord[0] = mesh->data_texcoord2f;
569 m.tex[0] = R_GetTexture(mesh->texture);
571 m.pointer_texcoord[0] = NULL;
573 GL_LockArrays(0, mesh->num_vertices);
574 R_Mesh_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i);
577 case DRAWQUEUE_SETCLIP:
579 // We have to convert the con coords into real coords
580 int x , y, width, height;
581 x = dq->x * ((float)vid.realwidth / vid.conwidth);
582 // OGL uses top to bottom
583 y = dq->y * ((float) vid.realheight / vid.conheight);
584 width = dq->scalex * ((float)vid.realwidth / vid.conwidth);
585 height = dq->scaley * ((float)vid.realheight / vid.conheight);
587 GL_Scissor(x, y, width, height);
589 GL_ScissorTest(true);
592 case DRAWQUEUE_RESETCLIP:
593 GL_ScissorTest(false);
598 if (!vid_usinghwgamma)
600 // all the blends ignore depth
601 memset(&m, 0, sizeof(m));
602 m.pointer_vertex = blendvertex3f;
606 if (v_color_enable.integer)
608 c[0] = v_color_white_r.value;
609 c[1] = v_color_white_g.value;
610 c[2] = v_color_white_b.value;
613 c[0] = c[1] = c[2] = v_contrast.value;
614 if (c[0] >= 1.01f || c[1] >= 1.01f || c[2] >= 1.01f)
616 GL_BlendFunc(GL_DST_COLOR, GL_ONE);
617 while (c[0] >= 1.01f || c[1] >= 1.01f || c[2] >= 1.01f)
619 GL_Color(bound(0, c[0] - 1, 1), bound(0, c[1] - 1, 1), bound(0, c[2] - 1, 1), 1);
620 R_Mesh_Draw(3, 1, polygonelements);
621 VectorScale(c, 0.5, c);
624 if (v_color_enable.integer)
626 c[0] = v_color_black_r.value;
627 c[1] = v_color_black_g.value;
628 c[2] = v_color_black_b.value;
631 c[0] = c[1] = c[2] = v_brightness.value;
632 if (c[0] >= 0.01f || c[1] >= 0.01f || c[2] >= 0.01f)
634 GL_BlendFunc(GL_ONE, GL_ONE);
635 GL_Color(c[0], c[1], c[2], 1);
636 R_Mesh_Draw(3, 1, polygonelements);