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, texnum, 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);
426 for (pos = 0;pos < r_refdef.drawqueuesize;pos += ((drawqueue_t *)(r_refdef.drawqueue + pos))->size)
428 dq = (drawqueue_t *)(r_refdef.drawqueue + pos);
431 if(dq->flags == DRAWFLAG_ADDITIVE)
432 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
433 else if(dq->flags == DRAWFLAG_MODULATE)
434 GL_BlendFunc(GL_DST_COLOR, GL_ZERO);
435 else if(dq->flags == DRAWFLAG_2XMODULATE)
436 GL_BlendFunc(GL_DST_COLOR,GL_SRC_COLOR);
438 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
443 c[0] = (float) ((color >> 24) & 0xFF) * (1.0f / 255.0f);
444 c[1] = (float) ((color >> 16) & 0xFF) * (1.0f / 255.0f);
445 c[2] = (float) ((color >> 8) & 0xFF) * (1.0f / 255.0f);
446 c[3] = (float) ( color & 0xFF) * (1.0f / 255.0f);
454 case DRAWQUEUE_STRING:
455 GL_Color(c[0], c[1], c[2], c[3]);
456 str = (char *)(dq + 1);
457 if (strcmp("gfx/conchars", currentpic))
459 currentpic = "gfx/conchars";
460 m.tex[0] = chartexnum;
463 GL_VertexPointer(varray_vertex3f);
464 m.pointer_texcoord[0] = varray_texcoord2f[0];
465 R_Mesh_State_Texture(&m);
466 at = varray_texcoord2f[0];
467 av = varray_vertex3f;
468 while ((num = *str++) && x < vid.conwidth)
472 s = (num & 15)*0.0625f + (0.5f / 256.0f);
473 t = (num >> 4)*0.0625f + (0.5f / 256.0f);
474 u = 0.0625f - (1.0f / 256.0f);
475 v = 0.0625f - (1.0f / 256.0f);
476 at[ 0] = s ;at[ 1] = t ;
477 at[ 2] = s+u;at[ 3] = t ;
478 at[ 4] = s+u;at[ 5] = t+v;
479 at[ 6] = s ;at[ 7] = t+v;
480 av[ 0] = x ;av[ 1] = y ;av[ 2] = 10;
481 av[ 3] = x+w;av[ 4] = y ;av[ 5] = 10;
482 av[ 6] = x+w;av[ 7] = y+h;av[ 8] = 10;
483 av[ 9] = x ;av[10] = y+h;av[11] = 10;
487 if (batchcount >= 128)
489 R_Mesh_Draw(batchcount * 4, batchcount * 2, quadelements);
491 at = varray_texcoord2f[0];
492 av = varray_vertex3f;
498 R_Mesh_Draw(batchcount * 4, batchcount * 2, quadelements);
501 mesh = (void *)(dq + 1);
502 GL_VertexPointer(mesh->data_vertex3f);
503 GL_ColorPointer(mesh->data_color4f);
504 m.tex[0] = R_GetTexture(mesh->texture);
505 m.pointer_texcoord[0] = mesh->data_texcoord2f;
506 R_Mesh_State_Texture(&m);
507 R_Mesh_Draw(mesh->num_vertices, mesh->num_triangles, mesh->data_element3i);
513 if (!vid_usinghwgamma)
515 // all the blends ignore depth
516 memset(&m, 0, sizeof(m));
517 R_Mesh_State_Texture(&m);
520 if (v_color_enable.integer)
522 c[0] = v_color_white_r.value;
523 c[1] = v_color_white_g.value;
524 c[2] = v_color_white_b.value;
527 c[0] = c[1] = c[2] = v_contrast.value;
528 if (c[0] >= 1.01f || c[1] >= 1.01f || c[2] >= 1.01f)
530 GL_BlendFunc(GL_DST_COLOR, GL_ONE);
531 GL_VertexPointer(blendvertex3f);
532 while (c[0] >= 1.01f || c[1] >= 1.01f || c[2] >= 1.01f)
534 GL_Color(bound(0, c[0] - 1, 1), bound(0, c[1] - 1, 1), bound(0, c[2] - 1, 1), 1);
535 R_Mesh_Draw(3, 1, polygonelements);
536 VectorScale(c, 0.5, c);
539 if (v_color_enable.integer)
541 c[0] = v_color_black_r.value;
542 c[1] = v_color_black_g.value;
543 c[2] = v_color_black_b.value;
546 c[0] = c[1] = c[2] = v_brightness.value;
547 if (c[0] >= 0.01f || c[1] >= 0.01f || c[2] >= 0.01f)
549 GL_BlendFunc(GL_ONE, GL_ONE);
550 GL_VertexPointer(blendvertex3f);
551 GL_Color(c[0], c[1], c[2], 1);
552 R_Mesh_Draw(3, 1, polygonelements);