gl_draw.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // draw.c
  16. #include "gl_local.h"
  17. image_t *draw_chars;
  18. extern qboolean scrap_dirty;
  19. void Scrap_Upload (void);
  20. /*
  21. ===============
  22. Draw_InitLocal
  23. ===============
  24. */
  25. void Draw_InitLocal (void)
  26. {
  27. // load console characters (don't bilerp characters)
  28. draw_chars = GL_FindImage ("pics/conchars.pcx", it_pic);
  29. GL_Bind( draw_chars->texnum );
  30. qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  31. qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  32. }
  33. /*
  34. ================
  35. Draw_Char
  36. Draws one 8*8 graphics character with 0 being transparent.
  37. It can be clipped to the top of the screen to allow the console to be
  38. smoothly scrolled off.
  39. ================
  40. */
  41. void Draw_Char (int x, int y, int num)
  42. {
  43. int row, col;
  44. float frow, fcol, size;
  45. num &= 255;
  46. if ( (num&127) == 32 )
  47. return; // space
  48. if (y <= -8)
  49. return; // totally off screen
  50. row = num>>4;
  51. col = num&15;
  52. frow = row*0.0625;
  53. fcol = col*0.0625;
  54. size = 0.0625;
  55. GL_Bind (draw_chars->texnum);
  56. qglBegin (GL_QUADS);
  57. qglTexCoord2f (fcol, frow);
  58. qglVertex2f (x, y);
  59. qglTexCoord2f (fcol + size, frow);
  60. qglVertex2f (x+8, y);
  61. qglTexCoord2f (fcol + size, frow + size);
  62. qglVertex2f (x+8, y+8);
  63. qglTexCoord2f (fcol, frow + size);
  64. qglVertex2f (x, y+8);
  65. qglEnd ();
  66. }
  67. /*
  68. =============
  69. Draw_FindPic
  70. =============
  71. */
  72. image_t *Draw_FindPic (char *name)
  73. {
  74. image_t *gl;
  75. char fullname[MAX_QPATH];
  76. if (name[0] != '/' && name[0] != '\\')
  77. {
  78. Com_sprintf (fullname, sizeof(fullname), "pics/%s.pcx", name);
  79. gl = GL_FindImage (fullname, it_pic);
  80. }
  81. else
  82. gl = GL_FindImage (name+1, it_pic);
  83. return gl;
  84. }
  85. /*
  86. =============
  87. Draw_GetPicSize
  88. =============
  89. */
  90. void Draw_GetPicSize (int *w, int *h, char *pic)
  91. {
  92. image_t *gl;
  93. gl = Draw_FindPic (pic);
  94. if (!gl)
  95. {
  96. *w = *h = -1;
  97. return;
  98. }
  99. *w = gl->width;
  100. *h = gl->height;
  101. }
  102. /*
  103. =============
  104. Draw_StretchPic
  105. =============
  106. */
  107. void Draw_StretchPic (int x, int y, int w, int h, char *pic)
  108. {
  109. image_t *gl;
  110. gl = Draw_FindPic (pic);
  111. if (!gl)
  112. {
  113. ri.Con_Printf (PRINT_ALL, "Can't find pic: %s\n", pic);
  114. return;
  115. }
  116. if (scrap_dirty)
  117. Scrap_Upload ();
  118. if ( ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( gl_config.renderer & GL_RENDERER_RENDITION ) ) && !gl->has_alpha)
  119. qglDisable (GL_ALPHA_TEST);
  120. GL_Bind (gl->texnum);
  121. qglBegin (GL_QUADS);
  122. qglTexCoord2f (gl->sl, gl->tl);
  123. qglVertex2f (x, y);
  124. qglTexCoord2f (gl->sh, gl->tl);
  125. qglVertex2f (x+w, y);
  126. qglTexCoord2f (gl->sh, gl->th);
  127. qglVertex2f (x+w, y+h);
  128. qglTexCoord2f (gl->sl, gl->th);
  129. qglVertex2f (x, y+h);
  130. qglEnd ();
  131. if ( ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( gl_config.renderer & GL_RENDERER_RENDITION ) ) && !gl->has_alpha)
  132. qglEnable (GL_ALPHA_TEST);
  133. }
  134. /*
  135. =============
  136. Draw_Pic
  137. =============
  138. */
  139. void Draw_Pic (int x, int y, char *pic)
  140. {
  141. image_t *gl;
  142. gl = Draw_FindPic (pic);
  143. if (!gl)
  144. {
  145. ri.Con_Printf (PRINT_ALL, "Can't find pic: %s\n", pic);
  146. return;
  147. }
  148. if (scrap_dirty)
  149. Scrap_Upload ();
  150. if ( ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( gl_config.renderer & GL_RENDERER_RENDITION ) ) && !gl->has_alpha)
  151. qglDisable (GL_ALPHA_TEST);
  152. GL_Bind (gl->texnum);
  153. qglBegin (GL_QUADS);
  154. qglTexCoord2f (gl->sl, gl->tl);
  155. qglVertex2f (x, y);
  156. qglTexCoord2f (gl->sh, gl->tl);
  157. qglVertex2f (x+gl->width, y);
  158. qglTexCoord2f (gl->sh, gl->th);
  159. qglVertex2f (x+gl->width, y+gl->height);
  160. qglTexCoord2f (gl->sl, gl->th);
  161. qglVertex2f (x, y+gl->height);
  162. qglEnd ();
  163. if ( ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( gl_config.renderer & GL_RENDERER_RENDITION ) ) && !gl->has_alpha)
  164. qglEnable (GL_ALPHA_TEST);
  165. }
  166. /*
  167. =============
  168. Draw_TileClear
  169. This repeats a 64*64 tile graphic to fill the screen around a sized down
  170. refresh window.
  171. =============
  172. */
  173. void Draw_TileClear (int x, int y, int w, int h, char *pic)
  174. {
  175. image_t *image;
  176. image = Draw_FindPic (pic);
  177. if (!image)
  178. {
  179. ri.Con_Printf (PRINT_ALL, "Can't find pic: %s\n", pic);
  180. return;
  181. }
  182. if ( ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( gl_config.renderer & GL_RENDERER_RENDITION ) ) && !image->has_alpha)
  183. qglDisable (GL_ALPHA_TEST);
  184. GL_Bind (image->texnum);
  185. qglBegin (GL_QUADS);
  186. qglTexCoord2f (x/64.0, y/64.0);
  187. qglVertex2f (x, y);
  188. qglTexCoord2f ( (x+w)/64.0, y/64.0);
  189. qglVertex2f (x+w, y);
  190. qglTexCoord2f ( (x+w)/64.0, (y+h)/64.0);
  191. qglVertex2f (x+w, y+h);
  192. qglTexCoord2f ( x/64.0, (y+h)/64.0 );
  193. qglVertex2f (x, y+h);
  194. qglEnd ();
  195. if ( ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( gl_config.renderer & GL_RENDERER_RENDITION ) ) && !image->has_alpha)
  196. qglEnable (GL_ALPHA_TEST);
  197. }
  198. /*
  199. =============
  200. Draw_Fill
  201. Fills a box of pixels with a single color
  202. =============
  203. */
  204. void Draw_Fill (int x, int y, int w, int h, int c)
  205. {
  206. union
  207. {
  208. unsigned c;
  209. byte v[4];
  210. } color;
  211. if ( (unsigned)c > 255)
  212. ri.Sys_Error (ERR_FATAL, "Draw_Fill: bad color");
  213. qglDisable (GL_TEXTURE_2D);
  214. color.c = d_8to24table[c];
  215. qglColor3f (color.v[0]/255.0,
  216. color.v[1]/255.0,
  217. color.v[2]/255.0);
  218. qglBegin (GL_QUADS);
  219. qglVertex2f (x,y);
  220. qglVertex2f (x+w, y);
  221. qglVertex2f (x+w, y+h);
  222. qglVertex2f (x, y+h);
  223. qglEnd ();
  224. qglColor3f (1,1,1);
  225. qglEnable (GL_TEXTURE_2D);
  226. }
  227. //=============================================================================
  228. /*
  229. ================
  230. Draw_FadeScreen
  231. ================
  232. */
  233. void Draw_FadeScreen (void)
  234. {
  235. qglEnable (GL_BLEND);
  236. qglDisable (GL_TEXTURE_2D);
  237. qglColor4f (0, 0, 0, 0.8);
  238. qglBegin (GL_QUADS);
  239. qglVertex2f (0,0);
  240. qglVertex2f (vid.width, 0);
  241. qglVertex2f (vid.width, vid.height);
  242. qglVertex2f (0, vid.height);
  243. qglEnd ();
  244. qglColor4f (1,1,1,1);
  245. qglEnable (GL_TEXTURE_2D);
  246. qglDisable (GL_BLEND);
  247. }
  248. //====================================================================
  249. /*
  250. =============
  251. Draw_StretchRaw
  252. =============
  253. */
  254. extern unsigned r_rawpalette[256];
  255. void Draw_StretchRaw (int x, int y, int w, int h, int cols, int rows, byte *data)
  256. {
  257. unsigned image32[256*256];
  258. unsigned char image8[256*256];
  259. int i, j, trows;
  260. byte *source;
  261. int frac, fracstep;
  262. float hscale;
  263. int row;
  264. float t;
  265. GL_Bind (0);
  266. if (rows<=256)
  267. {
  268. hscale = 1;
  269. trows = rows;
  270. }
  271. else
  272. {
  273. hscale = rows/256.0;
  274. trows = 256;
  275. }
  276. t = rows*hscale / 256;
  277. if ( !qglColorTableEXT )
  278. {
  279. unsigned *dest;
  280. for (i=0 ; i<trows ; i++)
  281. {
  282. row = (int)(i*hscale);
  283. if (row > rows)
  284. break;
  285. source = data + cols*row;
  286. dest = &image32[i*256];
  287. fracstep = cols*0x10000/256;
  288. frac = fracstep >> 1;
  289. for (j=0 ; j<256 ; j++)
  290. {
  291. dest[j] = r_rawpalette[source[frac>>16]];
  292. frac += fracstep;
  293. }
  294. }
  295. qglTexImage2D (GL_TEXTURE_2D, 0, gl_tex_solid_format, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, image32);
  296. }
  297. else
  298. {
  299. unsigned char *dest;
  300. for (i=0 ; i<trows ; i++)
  301. {
  302. row = (int)(i*hscale);
  303. if (row > rows)
  304. break;
  305. source = data + cols*row;
  306. dest = &image8[i*256];
  307. fracstep = cols*0x10000/256;
  308. frac = fracstep >> 1;
  309. for (j=0 ; j<256 ; j++)
  310. {
  311. dest[j] = source[frac>>16];
  312. frac += fracstep;
  313. }
  314. }
  315. qglTexImage2D( GL_TEXTURE_2D,
  316. 0,
  317. GL_COLOR_INDEX8_EXT,
  318. 256, 256,
  319. 0,
  320. GL_COLOR_INDEX,
  321. GL_UNSIGNED_BYTE,
  322. image8 );
  323. }
  324. qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  325. qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  326. if ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( gl_config.renderer & GL_RENDERER_RENDITION ) )
  327. qglDisable (GL_ALPHA_TEST);
  328. qglBegin (GL_QUADS);
  329. qglTexCoord2f (0, 0);
  330. qglVertex2f (x, y);
  331. qglTexCoord2f (1, 0);
  332. qglVertex2f (x+w, y);
  333. qglTexCoord2f (1, t);
  334. qglVertex2f (x+w, y+h);
  335. qglTexCoord2f (0, t);
  336. qglVertex2f (x, y+h);
  337. qglEnd ();
  338. if ( ( gl_config.renderer == GL_RENDERER_MCD ) || ( gl_config.renderer & GL_RENDERER_RENDITION ) )
  339. qglEnable (GL_ALPHA_TEST);
  340. }