gl_rmisc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. Copyright (C) 1996-1997 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. // r_misc.c
  16. #include "quakedef.h"
  17. extern void R_InitBubble();
  18. /*
  19. ==================
  20. R_InitTextures
  21. ==================
  22. */
  23. void R_InitTextures (void)
  24. {
  25. int x,y, m;
  26. byte *dest;
  27. // create a simple checkerboard texture for the default
  28. r_notexture_mip = Hunk_AllocName (sizeof(texture_t) + 16*16+8*8+4*4+2*2, "notexture");
  29. r_notexture_mip->width = r_notexture_mip->height = 16;
  30. r_notexture_mip->offsets[0] = sizeof(texture_t);
  31. r_notexture_mip->offsets[1] = r_notexture_mip->offsets[0] + 16*16;
  32. r_notexture_mip->offsets[2] = r_notexture_mip->offsets[1] + 8*8;
  33. r_notexture_mip->offsets[3] = r_notexture_mip->offsets[2] + 4*4;
  34. for (m=0 ; m<4 ; m++)
  35. {
  36. dest = (byte *)r_notexture_mip + r_notexture_mip->offsets[m];
  37. for (y=0 ; y< (16>>m) ; y++)
  38. for (x=0 ; x< (16>>m) ; x++)
  39. {
  40. if ( (y< (8>>m) ) ^ (x< (8>>m) ) )
  41. *dest++ = 0;
  42. else
  43. *dest++ = 0xff;
  44. }
  45. }
  46. }
  47. byte dottexture[8][8] =
  48. {
  49. {0,1,1,0,0,0,0,0},
  50. {1,1,1,1,0,0,0,0},
  51. {1,1,1,1,0,0,0,0},
  52. {0,1,1,0,0,0,0,0},
  53. {0,0,0,0,0,0,0,0},
  54. {0,0,0,0,0,0,0,0},
  55. {0,0,0,0,0,0,0,0},
  56. {0,0,0,0,0,0,0,0},
  57. };
  58. void R_InitParticleTexture (void)
  59. {
  60. int x,y;
  61. byte data[8][8][4];
  62. //
  63. // particle texture
  64. //
  65. particletexture = texture_extension_number++;
  66. GL_Bind(particletexture);
  67. for (x=0 ; x<8 ; x++)
  68. {
  69. for (y=0 ; y<8 ; y++)
  70. {
  71. data[y][x][0] = 255;
  72. data[y][x][1] = 255;
  73. data[y][x][2] = 255;
  74. data[y][x][3] = dottexture[x][y]*255;
  75. }
  76. }
  77. glTexImage2D (GL_TEXTURE_2D, 0, gl_alpha_format, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  78. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  79. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  80. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  81. }
  82. /*
  83. ===============
  84. R_Envmap_f
  85. Grab six views for environment mapping tests
  86. ===============
  87. */
  88. void R_Envmap_f (void)
  89. {
  90. byte buffer[256*256*4];
  91. glDrawBuffer (GL_FRONT);
  92. glReadBuffer (GL_FRONT);
  93. envmap = true;
  94. r_refdef.vrect.x = 0;
  95. r_refdef.vrect.y = 0;
  96. r_refdef.vrect.width = 256;
  97. r_refdef.vrect.height = 256;
  98. r_refdef.viewangles[0] = 0;
  99. r_refdef.viewangles[1] = 0;
  100. r_refdef.viewangles[2] = 0;
  101. GL_BeginRendering (&glx, &gly, &glwidth, &glheight);
  102. R_RenderView ();
  103. glReadPixels (0, 0, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  104. COM_WriteFile ("env0.rgb", buffer, sizeof(buffer));
  105. r_refdef.viewangles[1] = 90;
  106. GL_BeginRendering (&glx, &gly, &glwidth, &glheight);
  107. R_RenderView ();
  108. glReadPixels (0, 0, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  109. COM_WriteFile ("env1.rgb", buffer, sizeof(buffer));
  110. r_refdef.viewangles[1] = 180;
  111. GL_BeginRendering (&glx, &gly, &glwidth, &glheight);
  112. R_RenderView ();
  113. glReadPixels (0, 0, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  114. COM_WriteFile ("env2.rgb", buffer, sizeof(buffer));
  115. r_refdef.viewangles[1] = 270;
  116. GL_BeginRendering (&glx, &gly, &glwidth, &glheight);
  117. R_RenderView ();
  118. glReadPixels (0, 0, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  119. COM_WriteFile ("env3.rgb", buffer, sizeof(buffer));
  120. r_refdef.viewangles[0] = -90;
  121. r_refdef.viewangles[1] = 0;
  122. GL_BeginRendering (&glx, &gly, &glwidth, &glheight);
  123. R_RenderView ();
  124. glReadPixels (0, 0, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  125. COM_WriteFile ("env4.rgb", buffer, sizeof(buffer));
  126. r_refdef.viewangles[0] = 90;
  127. r_refdef.viewangles[1] = 0;
  128. GL_BeginRendering (&glx, &gly, &glwidth, &glheight);
  129. R_RenderView ();
  130. glReadPixels (0, 0, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  131. COM_WriteFile ("env5.rgb", buffer, sizeof(buffer));
  132. envmap = false;
  133. glDrawBuffer (GL_BACK);
  134. glReadBuffer (GL_BACK);
  135. GL_EndRendering ();
  136. }
  137. /*
  138. ===============
  139. R_Init
  140. ===============
  141. */
  142. void R_Init (void)
  143. {
  144. Cmd_AddCommand ("timerefresh", R_TimeRefresh_f);
  145. Cmd_AddCommand ("envmap", R_Envmap_f);
  146. Cmd_AddCommand ("pointfile", R_ReadPointFile_f);
  147. Cvar_RegisterVariable (&r_norefresh);
  148. Cvar_RegisterVariable (&r_lightmap);
  149. Cvar_RegisterVariable (&r_fullbright);
  150. Cvar_RegisterVariable (&r_drawentities);
  151. Cvar_RegisterVariable (&r_drawviewmodel);
  152. Cvar_RegisterVariable (&r_shadows);
  153. Cvar_RegisterVariable (&r_mirroralpha);
  154. Cvar_RegisterVariable (&r_wateralpha);
  155. Cvar_RegisterVariable (&r_dynamic);
  156. Cvar_RegisterVariable (&r_novis);
  157. Cvar_RegisterVariable (&r_speeds);
  158. Cvar_RegisterVariable (&r_netgraph);
  159. Cvar_RegisterVariable (&gl_clear);
  160. Cvar_RegisterVariable (&gl_texsort);
  161. if (gl_mtexable)
  162. Cvar_SetValue ("gl_texsort", 0.0);
  163. Cvar_RegisterVariable (&gl_cull);
  164. Cvar_RegisterVariable (&gl_smoothmodels);
  165. Cvar_RegisterVariable (&gl_affinemodels);
  166. Cvar_RegisterVariable (&gl_polyblend);
  167. Cvar_RegisterVariable (&gl_flashblend);
  168. Cvar_RegisterVariable (&gl_playermip);
  169. Cvar_RegisterVariable (&gl_nocolors);
  170. Cvar_RegisterVariable (&gl_finish);
  171. Cvar_RegisterVariable (&gl_keeptjunctions);
  172. Cvar_RegisterVariable (&gl_reporttjunctions);
  173. R_InitBubble();
  174. R_InitParticles ();
  175. R_InitParticleTexture ();
  176. #ifdef GLTEST
  177. Test_Init ();
  178. #endif
  179. netgraphtexture = texture_extension_number;
  180. texture_extension_number++;
  181. playertextures = texture_extension_number;
  182. texture_extension_number += MAX_CLIENTS;
  183. }
  184. /*
  185. ===============
  186. R_TranslatePlayerSkin
  187. Translates a skin texture by the per-player color lookup
  188. ===============
  189. */
  190. void R_TranslatePlayerSkin (int playernum)
  191. {
  192. int top, bottom;
  193. byte translate[256];
  194. unsigned translate32[256];
  195. int i, j;
  196. byte *original;
  197. unsigned pixels[512*256], *out;
  198. unsigned scaled_width, scaled_height;
  199. int inwidth, inheight;
  200. int tinwidth, tinheight;
  201. byte *inrow;
  202. unsigned frac, fracstep;
  203. player_info_t *player;
  204. extern byte player_8bit_texels[320*200];
  205. char s[512];
  206. GL_DisableMultitexture();
  207. player = &cl.players[playernum];
  208. if (!player->name[0])
  209. return;
  210. strcpy(s, Info_ValueForKey(player->userinfo, "skin"));
  211. COM_StripExtension(s, s);
  212. if (player->skin && !stricmp(s, player->skin->name))
  213. player->skin = NULL;
  214. if (player->_topcolor != player->topcolor ||
  215. player->_bottomcolor != player->bottomcolor || !player->skin) {
  216. player->_topcolor = player->topcolor;
  217. player->_bottomcolor = player->bottomcolor;
  218. top = player->topcolor;
  219. bottom = player->bottomcolor;
  220. top = (top < 0) ? 0 : ((top > 13) ? 13 : top);
  221. bottom = (bottom < 0) ? 0 : ((bottom > 13) ? 13 : bottom);
  222. top *= 16;
  223. bottom *= 16;
  224. for (i=0 ; i<256 ; i++)
  225. translate[i] = i;
  226. for (i=0 ; i<16 ; i++)
  227. {
  228. if (top < 128) // the artists made some backwards ranges. sigh.
  229. translate[TOP_RANGE+i] = top+i;
  230. else
  231. translate[TOP_RANGE+i] = top+15-i;
  232. if (bottom < 128)
  233. translate[BOTTOM_RANGE+i] = bottom+i;
  234. else
  235. translate[BOTTOM_RANGE+i] = bottom+15-i;
  236. }
  237. //
  238. // locate the original skin pixels
  239. //
  240. // real model width
  241. tinwidth = 296;
  242. tinheight = 194;
  243. if (!player->skin)
  244. Skin_Find(player);
  245. if ((original = Skin_Cache(player->skin)) != NULL) {
  246. //skin data width
  247. inwidth = 320;
  248. inheight = 200;
  249. } else {
  250. original = player_8bit_texels;
  251. inwidth = 296;
  252. inheight = 194;
  253. }
  254. // because this happens during gameplay, do it fast
  255. // instead of sending it through gl_upload 8
  256. GL_Bind(playertextures + playernum);
  257. #if 0
  258. s = 320*200;
  259. byte translated[320*200];
  260. for (i=0 ; i<s ; i+=4)
  261. {
  262. translated[i] = translate[original[i]];
  263. translated[i+1] = translate[original[i+1]];
  264. translated[i+2] = translate[original[i+2]];
  265. translated[i+3] = translate[original[i+3]];
  266. }
  267. // don't mipmap these, because it takes too long
  268. GL_Upload8 (translated, paliashdr->skinwidth, paliashdr->skinheight,
  269. false, false, true);
  270. #endif
  271. scaled_width = gl_max_size.value < 512 ? gl_max_size.value : 512;
  272. scaled_height = gl_max_size.value < 256 ? gl_max_size.value : 256;
  273. // allow users to crunch sizes down even more if they want
  274. scaled_width >>= (int)gl_playermip.value;
  275. scaled_height >>= (int)gl_playermip.value;
  276. if (VID_Is8bit()) { // 8bit texture upload
  277. byte *out2;
  278. out2 = (byte *)pixels;
  279. memset(pixels, 0, sizeof(pixels));
  280. fracstep = tinwidth*0x10000/scaled_width;
  281. for (i=0 ; i<scaled_height ; i++, out2 += scaled_width)
  282. {
  283. inrow = original + inwidth*(i*tinheight/scaled_height);
  284. frac = fracstep >> 1;
  285. for (j=0 ; j<scaled_width ; j+=4)
  286. {
  287. out2[j] = translate[inrow[frac>>16]];
  288. frac += fracstep;
  289. out2[j+1] = translate[inrow[frac>>16]];
  290. frac += fracstep;
  291. out2[j+2] = translate[inrow[frac>>16]];
  292. frac += fracstep;
  293. out2[j+3] = translate[inrow[frac>>16]];
  294. frac += fracstep;
  295. }
  296. }
  297. GL_Upload8_EXT ((byte *)pixels, scaled_width, scaled_height, false, false);
  298. return;
  299. }
  300. for (i=0 ; i<256 ; i++)
  301. translate32[i] = d_8to24table[translate[i]];
  302. out = pixels;
  303. memset(pixels, 0, sizeof(pixels));
  304. fracstep = tinwidth*0x10000/scaled_width;
  305. for (i=0 ; i<scaled_height ; i++, out += scaled_width)
  306. {
  307. inrow = original + inwidth*(i*tinheight/scaled_height);
  308. frac = fracstep >> 1;
  309. for (j=0 ; j<scaled_width ; j+=4)
  310. {
  311. out[j] = translate32[inrow[frac>>16]];
  312. frac += fracstep;
  313. out[j+1] = translate32[inrow[frac>>16]];
  314. frac += fracstep;
  315. out[j+2] = translate32[inrow[frac>>16]];
  316. frac += fracstep;
  317. out[j+3] = translate32[inrow[frac>>16]];
  318. frac += fracstep;
  319. }
  320. }
  321. glTexImage2D (GL_TEXTURE_2D, 0, gl_solid_format,
  322. scaled_width, scaled_height, 0, GL_RGBA,
  323. GL_UNSIGNED_BYTE, pixels);
  324. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  325. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  326. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  327. }
  328. }
  329. /*
  330. ===============
  331. R_NewMap
  332. ===============
  333. */
  334. void R_NewMap (void)
  335. {
  336. int i;
  337. for (i=0 ; i<256 ; i++)
  338. d_lightstylevalue[i] = 264; // normal light value
  339. memset (&r_worldentity, 0, sizeof(r_worldentity));
  340. r_worldentity.model = cl.worldmodel;
  341. // clear out efrags in case the level hasn't been reloaded
  342. // FIXME: is this one short?
  343. for (i=0 ; i<cl.worldmodel->numleafs ; i++)
  344. cl.worldmodel->leafs[i].efrags = NULL;
  345. r_viewleaf = NULL;
  346. R_ClearParticles ();
  347. GL_BuildLightmaps ();
  348. // identify sky texture
  349. skytexturenum = -1;
  350. mirrortexturenum = -1;
  351. for (i=0 ; i<cl.worldmodel->numtextures ; i++)
  352. {
  353. if (!cl.worldmodel->textures[i])
  354. continue;
  355. if (!Q_strncmp(cl.worldmodel->textures[i]->name,"sky",3) )
  356. skytexturenum = i;
  357. if (!Q_strncmp(cl.worldmodel->textures[i]->name,"window02_1",10) )
  358. mirrortexturenum = i;
  359. cl.worldmodel->textures[i]->texturechain = NULL;
  360. }
  361. #ifdef QUAKE2
  362. R_LoadSkys ();
  363. #endif
  364. }
  365. /*
  366. ====================
  367. R_TimeRefresh_f
  368. For program optimization
  369. ====================
  370. */
  371. void R_TimeRefresh_f (void)
  372. {
  373. int i;
  374. float start, stop, time;
  375. glDrawBuffer (GL_FRONT);
  376. glFinish ();
  377. start = Sys_DoubleTime ();
  378. for (i=0 ; i<128 ; i++)
  379. {
  380. r_refdef.viewangles[1] = i/128.0*360.0;
  381. R_RenderView ();
  382. }
  383. glFinish ();
  384. stop = Sys_DoubleTime ();
  385. time = stop-start;
  386. Con_Printf ("%f seconds (%f fps)\n", time, 128/time);
  387. glDrawBuffer (GL_BACK);
  388. GL_EndRendering ();
  389. }
  390. void D_FlushCaches (void)
  391. {
  392. }