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)
201 memset(data, 255, sizeof(data));
202 data[0] = data[1] = data[2] = data[12] = data[13] = data[14] = 0;
203 return R_LoadTexture2D(drawtexturepool, "ditherpattern", 2, 2, data, TEXTYPE_RGBA, TEXF_FORCENEAREST | TEXF_PRECACHE, NULL);
211 // FIXME: move this to client somehow
212 cachepic_t *Draw_CachePic (char *path)
218 crc = CRC_Block(path, strlen(path));
219 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
220 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
221 if (!strcmp (path, pic->name))
224 if (numcachepics == MAX_CACHED_PICS)
225 Sys_Error ("numcachepics == MAX_CACHED_PICS");
226 pic = cachepics + (numcachepics++);
227 strcpy (pic->name, path);
229 pic->chain = cachepichash[hashkey];
230 cachepichash[hashkey] = pic;
232 // load the pic from disk
233 pic->tex = loadtextureimage(drawtexturepool, path, 0, 0, false, TEXF_ALPHA | TEXF_PRECACHE);
234 if (pic->tex == NULL && !strncmp(path, "gfx/", 4))
236 // compatibility with older versions
237 pic->tex = loadtextureimage(drawtexturepool, path + 4, 0, 0, false, TEXF_ALPHA | TEXF_PRECACHE);
238 // failed to find gfx/whatever.tga or similar, try the wad
239 if (pic->tex == NULL && (p = W_GetLumpName (path + 4)))
241 if (!strcmp(path, "gfx/conchars"))
244 // conchars is a raw image and with the wrong transparent color
246 for (i = 0;i < 128 * 128;i++)
249 pic->tex = R_LoadTexture2D(drawtexturepool, path, 128, 128, pix, TEXTYPE_PALETTE, TEXF_ALPHA | TEXF_PRECACHE, palette_complete);
252 pic->tex = R_LoadTexture2D(drawtexturepool, path, p->width, p->height, p->data, TEXTYPE_PALETTE, TEXF_ALPHA | TEXF_PRECACHE, palette_complete);
255 if (pic->tex == NULL && !strcmp(path, "ui/mousepointer.tga"))
256 pic->tex = draw_generatemousepointer();
257 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair1.tga"))
258 pic->tex = draw_generatecrosshair(0);
259 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair2.tga"))
260 pic->tex = draw_generatecrosshair(1);
261 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair3.tga"))
262 pic->tex = draw_generatecrosshair(2);
263 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair4.tga"))
264 pic->tex = draw_generatecrosshair(3);
265 if (pic->tex == NULL && !strcmp(path, "gfx/crosshair5.tga"))
266 pic->tex = draw_generatecrosshair(4);
267 if (pic->tex == NULL && !strcmp(path, "gfx/colorcontrol/ditherpattern.tga"))
268 pic->tex = draw_generateditherpattern();
269 if (pic->tex == NULL)
271 Con_Printf ("Draw_CachePic: failed to load %s\n", path);
272 pic->tex = r_notexture;
275 pic->width = R_TextureWidth(pic->tex);
276 pic->height = R_TextureHeight(pic->tex);
280 cachepic_t *Draw_NewPic(char *picname, int width, int height, int alpha, qbyte *pixels)
285 crc = CRC_Block(picname, strlen(picname));
286 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
287 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
288 if (!strcmp (picname, pic->name))
293 if (pic->tex && pic->width == width && pic->height == height)
295 R_UpdateTexture(pic->tex, pixels);
303 if (numcachepics == MAX_CACHED_PICS)
304 Sys_Error ("numcachepics == MAX_CACHED_PICS");
305 pic = cachepics + (numcachepics++);
306 strcpy (pic->name, picname);
308 pic->chain = cachepichash[hashkey];
309 cachepichash[hashkey] = pic;
314 pic->height = height;
316 R_FreeTexture(pic->tex);
317 pic->tex = R_LoadTexture2D(drawtexturepool, picname, width, height, pixels, TEXTYPE_RGBA, alpha ? TEXF_ALPHA : 0, NULL);
321 void Draw_FreePic(char *picname)
326 // this doesn't really free the pic, but does free it's texture
327 crc = CRC_Block(picname, strlen(picname));
328 hashkey = ((crc >> 8) ^ crc) % CACHEPICHASHSIZE;
329 for (pic = cachepichash[hashkey];pic;pic = pic->chain)
331 if (!strcmp (picname, pic->name))
333 R_FreeTexture(pic->tex);
346 static void gl_draw_start(void)
348 drawtexturepool = R_AllocTexturePool();
351 memset(cachepichash, 0, sizeof(cachepichash));
353 char_texture = Draw_CachePic("gfx/conchars")->tex;
356 static void gl_draw_shutdown(void)
358 R_FreeTexturePool(&drawtexturepool);
361 memset(cachepichash, 0, sizeof(cachepichash));
364 static void gl_draw_newmap(void)
368 void GL_Draw_Init (void)
371 memset(cachepichash, 0, sizeof(cachepichash));
373 R_RegisterModule("GL_Draw", gl_draw_start, gl_draw_shutdown, gl_draw_newmap);
376 float blendvertex3f[9] = {-5000, -5000, 10, 10000, -5000, 10, -5000, 10000, 10};
378 int quadelements[768];
379 void R_DrawQueue(void)
381 int pos, num, chartexnum, overbright, texnum, additive, batch;
382 float x, y, w, h, s, t, u, v, *av, *at, c[4];
385 char *str, *currentpic;
388 drawqueuemesh_t *mesh;
391 if (!r_render.integer)
394 if (!quadelements[1])
396 // elements for rendering a series of quads as triangles
397 for (batch = 0, pos = 0, num = 0;batch < 128;batch++, num += 4)
399 quadelements[pos++] = num;
400 quadelements[pos++] = num + 1;
401 quadelements[pos++] = num + 2;
402 quadelements[pos++] = num;
403 quadelements[pos++] = num + 2;
404 quadelements[pos++] = num + 3;
407 GL_SetupView_ViewPort(vid.realx, vid.realy, vid.realwidth, vid.realheight);
408 GL_SetupView_Mode_Ortho(0, 0, vid.conwidth, vid.conheight, -10, 100);
409 qglDepthFunc(GL_LEQUAL);
411 R_Mesh_Matrix(&r_identitymatrix);
413 chartexnum = R_GetTexture(char_texture);
415 memset(&m, 0, sizeof(m));
417 R_Mesh_State_Texture(&m);
424 overbright = v_overbrightbits.integer;
427 for (pos = 0;pos < r_refdef.drawqueuesize;pos += ((drawqueue_t *)(r_refdef.drawqueue + pos))->size)
429 dq = (drawqueue_t *)(r_refdef.drawqueue + pos);
430 additive = (dq->flags & DRAWFLAG_ADDITIVE) != 0;
432 GL_BlendFunc(GL_SRC_ALPHA, additive ? GL_ONE : GL_ONE_MINUS_SRC_ALPHA);
436 c[0] = (float) ((color >> 24) & 0xFF) * (1.0f / 255.0f) * r_colorscale;
437 c[1] = (float) ((color >> 16) & 0xFF) * (1.0f / 255.0f) * r_colorscale;
438 c[2] = (float) ((color >> 8) & 0xFF) * (1.0f / 255.0f) * r_colorscale;
439 c[3] = (float) ( color & 0xFF) * (1.0f / 255.0f);
447 case DRAWQUEUE_STRING:
448 GL_Color(c[0], c[1], c[2], c[3]);
449 str = (char *)(dq + 1);
450 if (strcmp("gfx/conchars", currentpic))
452 currentpic = "gfx/conchars";
453 m.tex[0] = chartexnum;
456 GL_VertexPointer(varray_vertex3f);
457 m.pointer_texcoord[0] = varray_texcoord2f[0];
458 R_Mesh_State_Texture(&m);
459 at = varray_texcoord2f[0];
460 av = varray_vertex3f;
461 while ((num = *str++) && x < vid.conwidth)
465 s = (num & 15)*0.0625f + (0.5f / 256.0f);
466 t = (num >> 4)*0.0625f + (0.5f / 256.0f);
467 u = 0.0625f - (1.0f / 256.0f);
468 v = 0.0625f - (1.0f / 256.0f);
469 at[ 0] = s ;at[ 1] = t ;
470 at[ 2] = s+u;at[ 3] = t ;
471 at[ 4] = s+u;at[ 5] = t+v;
472 at[ 6] = s ;at[ 7] = t+v;
473 av[ 0] = x ;av[ 1] = y ;av[ 2] = 10;
474 av[ 3] = x+w;av[ 4] = y ;av[ 5] = 10;
475 av[ 6] = x+w;av[ 7] = y+h;av[ 8] = 10;
476 av[ 9] = x ;av[10] = y+h;av[11] = 10;
480 if (batchcount >= 128)
482 R_Mesh_Draw(batchcount * 4, batchcount * 2, quadelements);
484 at = varray_texcoord2f[0];
485 av = varray_vertex3f;
491 R_Mesh_Draw(batchcount * 4, batchcount * 2, quadelements);
494 mesh = (void *)(dq + 1);
495 GL_VertexPointer(mesh->vertex3f);
496 GL_ColorPointer(mesh->color4f);
497 m.tex[0] = R_GetTexture(mesh->texture);
498 m.pointer_texcoord[0] = mesh->texcoord2f;
499 R_Mesh_State_Texture(&m);
500 R_Mesh_Draw(mesh->numvertices, mesh->numtriangles, mesh->element3i);
506 if (!vid_usinghwgamma)
508 // all the blends ignore depth
509 memset(&m, 0, sizeof(m));
510 R_Mesh_State_Texture(&m);
513 if (v_color_enable.integer)
515 c[0] = v_color_white_r.value;
516 c[1] = v_color_white_g.value;
517 c[2] = v_color_white_b.value;
520 c[0] = c[1] = c[2] = v_contrast.value;
521 VectorScale(c, (float) (1 << v_overbrightbits.integer), c);
522 if (c[0] >= 1.01f || c[1] >= 1.01f || c[2] >= 1.01f)
524 GL_BlendFunc(GL_DST_COLOR, GL_ONE);
525 GL_VertexPointer(blendvertex3f);
526 while (c[0] >= 1.01f || c[1] >= 1.01f || c[2] >= 1.01f)
528 GL_Color(bound(0, c[0] - 1, 1), bound(0, c[1] - 1, 1), bound(0, c[2] - 1, 1), 1);
529 R_Mesh_Draw(3, 1, polygonelements);
530 VectorScale(c, 0.5, c);
533 if (v_color_enable.integer)
535 c[0] = v_color_black_r.value;
536 c[1] = v_color_black_g.value;
537 c[2] = v_color_black_b.value;
540 c[0] = c[1] = c[2] = v_brightness.value;
541 if (c[0] >= 0.01f || c[1] >= 0.01f || c[2] >= 0.01f)
543 GL_BlendFunc(GL_ONE, GL_ONE);
544 GL_VertexPointer(blendvertex3f);
545 GL_Color(c[0], c[1], c[2], 1);
546 R_Mesh_Draw(3, 1, polygonelements);