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 pointerimage[256] =
57 static rtexture_t *draw_generatemousepointer(void)
61 for (i = 0;i < 256;i++)
63 if (pointerimage[i] == '.')
72 buffer[i][0] = (pointerimage[i] - '0') * 16;
73 buffer[i][1] = (pointerimage[i] - '0') * 16;
74 buffer[i][2] = (pointerimage[i] - '0') * 16;
78 return R_LoadTexture2D(drawtexturepool, "mousepointer", 16, 16, &buffer[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
81 // must match NUMCROSSHAIRS in r_crosshairs.c
82 #define NUMCROSSHAIRS 5
84 static qbyte *crosshairtexdata[NUMCROSSHAIRS] =
172 static rtexture_t *draw_generatecrosshair(int num)
176 qbyte data[16*16][4];
177 in = crosshairtexdata[num];
178 for (i = 0;i < 16*16;i++)
192 data[i][3] = (qbyte) ((int) (in[i] - '0') * 255 / 7);
195 return R_LoadTexture2D(drawtexturepool, va("crosshair%i", num), 16, 16, &data[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
198 static rtexture_t *draw_generateditherpattern(void)
203 for (y = 0;y < 8;y++)
205 for (x = 0;x < 8;x++)
207 data[(y*8+x)*4+0] = data[(y*8+x)*4+1] = data[(y*8+x)*4+2] = ((x^y) & 4) ? 255 : 0;
208 data[(y*8+x)*4+3] = 255;
211 return R_LoadTexture2D(drawtexturepool, "ditherpattern", 8, 8, data, TEXTYPE_RGBA, TEXF_FORCENEAREST | TEXF_PRECACHE, NULL);
214 memset(data, 255, sizeof(data));
215 data[0] = data[1] = data[2] = data[12] = data[13] = data[14] = 0;
216 return R_LoadTexture2D(drawtexturepool, "ditherpattern", 2, 2, data, TEXTYPE_RGBA, TEXF_FORCENEAREST | TEXF_PRECACHE, NULL);
225 // FIXME: move this to client somehow
226 cachepic_t *Draw_CachePic (char *path)
232 crc = CRC_Block(path, strlen(path));
233 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
234 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
235 if (!strcmp (path, pic->name))
238 if (numcachepics == MAX_CACHED_PICS)
239 Sys_Error ("numcachepics == MAX_CACHED_PICS");
240 pic = cachepics + (numcachepics++);
241 strcpy (pic->name, path);
243 pic->chain = cachepichash[hashkey];
244 cachepichash[hashkey] = pic;
246 // load the pic from disk
247 pic->tex = loadtextureimage(drawtexturepool, path, 0, 0, false, TEXF_ALPHA | TEXF_PRECACHE);
248 if (pic->tex == NULL && !strncmp(path, "gfx/", 4))
250 // compatibility with older versions
251 pic->tex = loadtextureimage(drawtexturepool, path + 4, 0, 0, false, TEXF_ALPHA | TEXF_PRECACHE);
252 // failed to find gfx/whatever.tga or similar, try the wad
253 if (pic->tex == NULL && (p = W_GetLumpName (path + 4)))
255 if (!strcmp(path, "gfx/conchars"))
258 // conchars is a raw image and with the wrong transparent color
260 for (i = 0;i < 128 * 128;i++)
263 pic->tex = R_LoadTexture2D(drawtexturepool, path, 128, 128, pix, TEXTYPE_PALETTE, TEXF_ALPHA | TEXF_PRECACHE, palette_complete);
266 pic->tex = R_LoadTexture2D(drawtexturepool, path, p->width, p->height, p->data, TEXTYPE_PALETTE, TEXF_ALPHA | TEXF_PRECACHE, palette_complete);
269 if (pic->tex == NULL && !strcmp(path, "ui/mousepointer.tga"))
270 pic->tex = draw_generatemousepointer();
271 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair1.tga"))
272 pic->tex = draw_generatecrosshair(0);
273 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair2.tga"))
274 pic->tex = draw_generatecrosshair(1);
275 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair3.tga"))
276 pic->tex = draw_generatecrosshair(2);
277 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair4.tga"))
278 pic->tex = draw_generatecrosshair(3);
279 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair5.tga"))
280 pic->tex = draw_generatecrosshair(4);
281 if (pic->tex == NULL && !strcmp(path, "gfx/colorcontrol/ditherpattern.tga"))
282 pic->tex = draw_generateditherpattern();
283 if (pic->tex == NULL)
285 Con_Printf("Draw_CachePic: failed to load %s\n", path);
286 pic->tex = r_notexture;
289 pic->width = R_TextureWidth(pic->tex);
290 pic->height = R_TextureHeight(pic->tex);
294 cachepic_t *Draw_NewPic(char *picname, int width, int height, int alpha, qbyte *pixels)
299 crc = CRC_Block(picname, strlen(picname));
300 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
301 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
302 if (!strcmp (picname, pic->name))
307 if (pic->tex && pic->width == width && pic->height == height)
309 R_UpdateTexture(pic->tex, pixels);
317 if (numcachepics == MAX_CACHED_PICS)
318 Sys_Error ("numcachepics == MAX_CACHED_PICS");
319 pic = cachepics + (numcachepics++);
320 strcpy (pic->name, picname);
322 pic->chain = cachepichash[hashkey];
323 cachepichash[hashkey] = pic;
328 pic->height = height;
330 R_FreeTexture(pic->tex);
331 pic->tex = R_LoadTexture2D(drawtexturepool, picname, width, height, pixels, TEXTYPE_RGBA, alpha ? TEXF_ALPHA : 0, NULL);
335 void Draw_FreePic(char *picname)
340 // this doesn't really free the pic, but does free it's texture
341 crc = CRC_Block(picname, strlen(picname));
342 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
343 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
345 if (!strcmp (picname, pic->name) && pic->tex)
347 R_FreeTexture(pic->tex);
360 static void gl_draw_start(void)
362 drawtexturepool = R_AllocTexturePool();
365 memset(cachepichash, 0, sizeof(cachepichash));
367 char_texture = Draw_CachePic("gfx/conchars")->tex;
370 static void gl_draw_shutdown(void)
372 R_FreeTexturePool(&drawtexturepool);
375 memset(cachepichash, 0, sizeof(cachepichash));
378 static void gl_draw_newmap(void)
382 void GL_Draw_Init (void)
385 memset(cachepichash, 0, sizeof(cachepichash));
387 R_RegisterModule("GL_Draw", gl_draw_start, gl_draw_shutdown, gl_draw_newmap);
390 float blendvertex3f[9] = {-5000, -5000, 10, 10000, -5000, 10, -5000, 10000, 10};
392 int quadelements[768];
393 void R_DrawQueue(void)
395 int pos, num, chartexnum, texnum, batch;
396 float x, y, w, h, s, t, u, v, *av, *at, c[4];
399 char *str, *currentpic;
402 drawqueuemesh_t *mesh;
405 if (!r_render.integer)
408 if (!quadelements[1])
410 // elements for rendering a series of quads as triangles
411 for (batch = 0, pos = 0, num = 0;batch < 128;batch++, num += 4)
413 quadelements[pos++] = num;
414 quadelements[pos++] = num + 1;
415 quadelements[pos++] = num + 2;
416 quadelements[pos++] = num;
417 quadelements[pos++] = num + 2;
418 quadelements[pos++] = num + 3;
421 qglViewport(0, 0, vid.realwidth, vid.realheight);
422 GL_SetupView_Mode_Ortho(0, 0, vid.conwidth, vid.conheight, -10, 100);
423 qglDepthFunc(GL_LEQUAL);
424 R_Mesh_Matrix(&r_identitymatrix);
426 chartexnum = R_GetTexture(char_texture);
428 memset(&m, 0, sizeof(m));
438 for (pos = 0;pos < r_refdef.drawqueuesize;pos += ((drawqueue_t *)(r_refdef.drawqueue + pos))->size)
440 dq = (drawqueue_t *)(r_refdef.drawqueue + pos);
443 if(dq->flags == DRAWFLAG_ADDITIVE)
444 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
445 else if(dq->flags == DRAWFLAG_MODULATE)
446 GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
447 else if(dq->flags == DRAWFLAG_2XMODULATE)
448 GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
450 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
455 c[0] = (float) ((color >> 24) & 0xFF) * (1.0f / 255.0f);
456 c[1] = (float) ((color >> 16) & 0xFF) * (1.0f / 255.0f);
457 c[2] = (float) ((color >> 8) & 0xFF) * (1.0f / 255.0f);
458 c[3] = (float) ( color & 0xFF) * (1.0f / 255.0f);
466 case DRAWQUEUE_STRING:
467 GL_Color(c[0], c[1], c[2], c[3]);
468 str = (char *)(dq + 1);
469 if (strcmp("gfx/conchars", currentpic))
471 currentpic = "gfx/conchars";
472 m.tex[0] = chartexnum;
475 m.pointer_vertex = varray_vertex3f;
476 m.pointer_texcoord[0] = varray_texcoord2f[0];
478 at = varray_texcoord2f[0];
479 av = varray_vertex3f;
480 while ((num = *str++) && x < vid.conwidth)
484 s = (num & 15)*0.0625f + (0.5f / 256.0f);
485 t = (num >> 4)*0.0625f + (0.5f / 256.0f);
486 u = 0.0625f - (1.0f / 256.0f);
487 v = 0.0625f - (1.0f / 256.0f);
488 at[ 0] = s ;at[ 1] = t ;
489 at[ 2] = s+u;at[ 3] = t ;
490 at[ 4] = s+u;at[ 5] = t+v;
491 at[ 6] = s ;at[ 7] = t+v;
492 av[ 0] = x ;av[ 1] = y ;av[ 2] = 10;
493 av[ 3] = x+w;av[ 4] = y ;av[ 5] = 10;
494 av[ 6] = x+w;av[ 7] = y+h;av[ 8] = 10;
495 av[ 9] = x ;av[10] = y+h;av[11] = 10;
499 if (batchcount >= 128)
501 GL_LockArrays(0, batchcount * 4);
502 R_Mesh_Draw(batchcount * 4, batchcount * 2, quadelements);
505 at = varray_texcoord2f[0];
506 av = varray_vertex3f;
513 GL_LockArrays(0, batchcount * 4);
514 R_Mesh_Draw(batchcount * 4, batchcount * 2, quadelements);
519 mesh = (void *)(dq + 1);
520 m.pointer_vertex = mesh->data_vertex3f;
521 m.pointer_color = mesh->data_color4f;
522 m.tex[0] = R_GetTexture(mesh->texture);
523 m.pointer_texcoord[0] = mesh->data_texcoord2f;
525 GL_LockArrays(0, mesh->num_vertices);
526 R_Mesh_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i);
528 m.pointer_color = NULL;
531 case DRAWQUEUE_SETCLIP:
533 // We have to convert the con coords into real coords
534 int x , y, width, height;
535 x = dq->x * ((float)vid.realwidth / vid.conwidth);
536 // OGL uses top to bottom
537 y = dq->y * ((float) vid.realheight / vid.conheight);
538 width = dq->scalex * ((float)vid.realwidth / vid.conwidth);
539 height = dq->scaley * ((float)vid.realheight / vid.conheight);
541 GL_Scissor(x, y, width, height);
543 GL_ScissorTest(true);
546 case DRAWQUEUE_RESETCLIP:
547 GL_ScissorTest(false);
552 if (!vid_usinghwgamma)
554 // all the blends ignore depth
555 memset(&m, 0, sizeof(m));
556 m.pointer_vertex = blendvertex3f;
560 if (v_color_enable.integer)
562 c[0] = v_color_white_r.value;
563 c[1] = v_color_white_g.value;
564 c[2] = v_color_white_b.value;
567 c[0] = c[1] = c[2] = v_contrast.value;
568 if (c[0] >= 1.01f || c[1] >= 1.01f || c[2] >= 1.01f)
570 GL_BlendFunc(GL_DST_COLOR, GL_ONE);
571 while (c[0] >= 1.01f || c[1] >= 1.01f || c[2] >= 1.01f)
573 GL_Color(bound(0, c[0] - 1, 1), bound(0, c[1] - 1, 1), bound(0, c[2] - 1, 1), 1);
574 R_Mesh_Draw(3, 1, polygonelements);
575 VectorScale(c, 0.5, c);
578 if (v_color_enable.integer)
580 c[0] = v_color_black_r.value;
581 c[1] = v_color_black_g.value;
582 c[2] = v_color_black_b.value;
585 c[0] = c[1] = c[2] = v_brightness.value;
586 if (c[0] >= 0.01f || c[1] >= 0.01f || c[2] >= 0.01f)
588 GL_BlendFunc(GL_ONE, GL_ONE);
589 GL_Color(c[0], c[1], c[2], 1);
590 R_Mesh_Draw(3, 1, polygonelements);