gl_rmain.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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_main.c
  16. #include "quakedef.h"
  17. entity_t r_worldentity;
  18. qboolean r_cache_thrash; // compatability
  19. vec3_t modelorg, r_entorigin;
  20. entity_t *currententity;
  21. int r_visframecount; // bumped when going to a new PVS
  22. int r_framecount; // used for dlight push checking
  23. mplane_t frustum[4];
  24. int c_brush_polys, c_alias_polys;
  25. qboolean envmap; // true during envmap command capture
  26. int currenttexture = -1; // to avoid unnecessary texture sets
  27. int cnttextures[2] = {-1, -1}; // cached
  28. int particletexture; // little dot for particles
  29. int playertextures; // up to 16 color translated skins
  30. int mirrortexturenum; // quake texturenum, not gltexturenum
  31. qboolean mirror;
  32. mplane_t *mirror_plane;
  33. //
  34. // view origin
  35. //
  36. vec3_t vup;
  37. vec3_t vpn;
  38. vec3_t vright;
  39. vec3_t r_origin;
  40. float r_world_matrix[16];
  41. float r_base_world_matrix[16];
  42. //
  43. // screen size info
  44. //
  45. refdef_t r_refdef;
  46. mleaf_t *r_viewleaf, *r_oldviewleaf;
  47. texture_t *r_notexture_mip;
  48. int d_lightstylevalue[256]; // 8.8 fraction of base light value
  49. void R_MarkLeaves (void);
  50. cvar_t r_norefresh = {"r_norefresh","0"};
  51. cvar_t r_drawentities = {"r_drawentities","1"};
  52. cvar_t r_drawviewmodel = {"r_drawviewmodel","1"};
  53. cvar_t r_speeds = {"r_speeds","0"};
  54. cvar_t r_fullbright = {"r_fullbright","0"};
  55. cvar_t r_lightmap = {"r_lightmap","0"};
  56. cvar_t r_shadows = {"r_shadows","0"};
  57. cvar_t r_mirroralpha = {"r_mirroralpha","1"};
  58. cvar_t r_wateralpha = {"r_wateralpha","1"};
  59. cvar_t r_dynamic = {"r_dynamic","1"};
  60. cvar_t r_novis = {"r_novis","0"};
  61. cvar_t gl_finish = {"gl_finish","0"};
  62. cvar_t gl_clear = {"gl_clear","0"};
  63. cvar_t gl_cull = {"gl_cull","1"};
  64. cvar_t gl_texsort = {"gl_texsort","1"};
  65. cvar_t gl_smoothmodels = {"gl_smoothmodels","1"};
  66. cvar_t gl_affinemodels = {"gl_affinemodels","0"};
  67. cvar_t gl_polyblend = {"gl_polyblend","1"};
  68. cvar_t gl_flashblend = {"gl_flashblend","1"};
  69. cvar_t gl_playermip = {"gl_playermip","0"};
  70. cvar_t gl_nocolors = {"gl_nocolors","0"};
  71. cvar_t gl_keeptjunctions = {"gl_keeptjunctions","0"};
  72. cvar_t gl_reporttjunctions = {"gl_reporttjunctions","0"};
  73. cvar_t gl_doubleeyes = {"gl_doubleeys", "1"};
  74. extern cvar_t gl_ztrick;
  75. /*
  76. =================
  77. R_CullBox
  78. Returns true if the box is completely outside the frustom
  79. =================
  80. */
  81. qboolean R_CullBox (vec3_t mins, vec3_t maxs)
  82. {
  83. int i;
  84. for (i=0 ; i<4 ; i++)
  85. if (BoxOnPlaneSide (mins, maxs, &frustum[i]) == 2)
  86. return true;
  87. return false;
  88. }
  89. void R_RotateForEntity (entity_t *e)
  90. {
  91. glTranslatef (e->origin[0], e->origin[1], e->origin[2]);
  92. glRotatef (e->angles[1], 0, 0, 1);
  93. glRotatef (-e->angles[0], 0, 1, 0);
  94. glRotatef (e->angles[2], 1, 0, 0);
  95. }
  96. /*
  97. =============================================================
  98. SPRITE MODELS
  99. =============================================================
  100. */
  101. /*
  102. ================
  103. R_GetSpriteFrame
  104. ================
  105. */
  106. mspriteframe_t *R_GetSpriteFrame (entity_t *currententity)
  107. {
  108. msprite_t *psprite;
  109. mspritegroup_t *pspritegroup;
  110. mspriteframe_t *pspriteframe;
  111. int i, numframes, frame;
  112. float *pintervals, fullinterval, targettime, time;
  113. psprite = currententity->model->cache.data;
  114. frame = currententity->frame;
  115. if ((frame >= psprite->numframes) || (frame < 0))
  116. {
  117. Con_Printf ("R_DrawSprite: no such frame %d\n", frame);
  118. frame = 0;
  119. }
  120. if (psprite->frames[frame].type == SPR_SINGLE)
  121. {
  122. pspriteframe = psprite->frames[frame].frameptr;
  123. }
  124. else
  125. {
  126. pspritegroup = (mspritegroup_t *)psprite->frames[frame].frameptr;
  127. pintervals = pspritegroup->intervals;
  128. numframes = pspritegroup->numframes;
  129. fullinterval = pintervals[numframes-1];
  130. time = cl.time + currententity->syncbase;
  131. // when loading in Mod_LoadSpriteGroup, we guaranteed all interval values
  132. // are positive, so we don't have to worry about division by 0
  133. targettime = time - ((int)(time / fullinterval)) * fullinterval;
  134. for (i=0 ; i<(numframes-1) ; i++)
  135. {
  136. if (pintervals[i] > targettime)
  137. break;
  138. }
  139. pspriteframe = pspritegroup->frames[i];
  140. }
  141. return pspriteframe;
  142. }
  143. /*
  144. =================
  145. R_DrawSpriteModel
  146. =================
  147. */
  148. void R_DrawSpriteModel (entity_t *e)
  149. {
  150. vec3_t point;
  151. mspriteframe_t *frame;
  152. float *up, *right;
  153. vec3_t v_forward, v_right, v_up;
  154. msprite_t *psprite;
  155. // don't even bother culling, because it's just a single
  156. // polygon without a surface cache
  157. frame = R_GetSpriteFrame (e);
  158. psprite = currententity->model->cache.data;
  159. if (psprite->type == SPR_ORIENTED)
  160. { // bullet marks on walls
  161. AngleVectors (currententity->angles, v_forward, v_right, v_up);
  162. up = v_up;
  163. right = v_right;
  164. }
  165. else
  166. { // normal sprite
  167. up = vup;
  168. right = vright;
  169. }
  170. glColor3f (1,1,1);
  171. GL_DisableMultitexture();
  172. GL_Bind(frame->gl_texturenum);
  173. glEnable (GL_ALPHA_TEST);
  174. glBegin (GL_QUADS);
  175. glTexCoord2f (0, 1);
  176. VectorMA (e->origin, frame->down, up, point);
  177. VectorMA (point, frame->left, right, point);
  178. glVertex3fv (point);
  179. glTexCoord2f (0, 0);
  180. VectorMA (e->origin, frame->up, up, point);
  181. VectorMA (point, frame->left, right, point);
  182. glVertex3fv (point);
  183. glTexCoord2f (1, 0);
  184. VectorMA (e->origin, frame->up, up, point);
  185. VectorMA (point, frame->right, right, point);
  186. glVertex3fv (point);
  187. glTexCoord2f (1, 1);
  188. VectorMA (e->origin, frame->down, up, point);
  189. VectorMA (point, frame->right, right, point);
  190. glVertex3fv (point);
  191. glEnd ();
  192. glDisable (GL_ALPHA_TEST);
  193. }
  194. /*
  195. =============================================================
  196. ALIAS MODELS
  197. =============================================================
  198. */
  199. #define NUMVERTEXNORMALS 162
  200. float r_avertexnormals[NUMVERTEXNORMALS][3] = {
  201. #include "anorms.h"
  202. };
  203. vec3_t shadevector;
  204. float shadelight, ambientlight;
  205. // precalculated dot products for quantized angles
  206. #define SHADEDOT_QUANT 16
  207. float r_avertexnormal_dots[SHADEDOT_QUANT][256] =
  208. #include "anorm_dots.h"
  209. ;
  210. float *shadedots = r_avertexnormal_dots[0];
  211. int lastposenum;
  212. /*
  213. =============
  214. GL_DrawAliasFrame
  215. =============
  216. */
  217. void GL_DrawAliasFrame (aliashdr_t *paliashdr, int posenum)
  218. {
  219. float s, t;
  220. float l;
  221. int i, j;
  222. int index;
  223. trivertx_t *v, *verts;
  224. int list;
  225. int *order;
  226. vec3_t point;
  227. float *normal;
  228. int count;
  229. lastposenum = posenum;
  230. verts = (trivertx_t *)((byte *)paliashdr + paliashdr->posedata);
  231. verts += posenum * paliashdr->poseverts;
  232. order = (int *)((byte *)paliashdr + paliashdr->commands);
  233. while (1)
  234. {
  235. // get the vertex count and primitive type
  236. count = *order++;
  237. if (!count)
  238. break; // done
  239. if (count < 0)
  240. {
  241. count = -count;
  242. glBegin (GL_TRIANGLE_FAN);
  243. }
  244. else
  245. glBegin (GL_TRIANGLE_STRIP);
  246. do
  247. {
  248. // texture coordinates come from the draw list
  249. glTexCoord2f (((float *)order)[0], ((float *)order)[1]);
  250. order += 2;
  251. // normals and vertexes come from the frame list
  252. l = shadedots[verts->lightnormalindex] * shadelight;
  253. glColor3f (l, l, l);
  254. glVertex3f (verts->v[0], verts->v[1], verts->v[2]);
  255. verts++;
  256. } while (--count);
  257. glEnd ();
  258. }
  259. }
  260. /*
  261. =============
  262. GL_DrawAliasShadow
  263. =============
  264. */
  265. extern vec3_t lightspot;
  266. void GL_DrawAliasShadow (aliashdr_t *paliashdr, int posenum)
  267. {
  268. float s, t, l;
  269. int i, j;
  270. int index;
  271. trivertx_t *v, *verts;
  272. int list;
  273. int *order;
  274. vec3_t point;
  275. float *normal;
  276. float height, lheight;
  277. int count;
  278. lheight = currententity->origin[2] - lightspot[2];
  279. height = 0;
  280. verts = (trivertx_t *)((byte *)paliashdr + paliashdr->posedata);
  281. verts += posenum * paliashdr->poseverts;
  282. order = (int *)((byte *)paliashdr + paliashdr->commands);
  283. height = -lheight + 1.0;
  284. while (1)
  285. {
  286. // get the vertex count and primitive type
  287. count = *order++;
  288. if (!count)
  289. break; // done
  290. if (count < 0)
  291. {
  292. count = -count;
  293. glBegin (GL_TRIANGLE_FAN);
  294. }
  295. else
  296. glBegin (GL_TRIANGLE_STRIP);
  297. do
  298. {
  299. // texture coordinates come from the draw list
  300. // (skipped for shadows) glTexCoord2fv ((float *)order);
  301. order += 2;
  302. // normals and vertexes come from the frame list
  303. point[0] = verts->v[0] * paliashdr->scale[0] + paliashdr->scale_origin[0];
  304. point[1] = verts->v[1] * paliashdr->scale[1] + paliashdr->scale_origin[1];
  305. point[2] = verts->v[2] * paliashdr->scale[2] + paliashdr->scale_origin[2];
  306. point[0] -= shadevector[0]*(point[2]+lheight);
  307. point[1] -= shadevector[1]*(point[2]+lheight);
  308. point[2] = height;
  309. // height -= 0.001;
  310. glVertex3fv (point);
  311. verts++;
  312. } while (--count);
  313. glEnd ();
  314. }
  315. }
  316. /*
  317. =================
  318. R_SetupAliasFrame
  319. =================
  320. */
  321. void R_SetupAliasFrame (int frame, aliashdr_t *paliashdr)
  322. {
  323. int pose, numposes;
  324. float interval;
  325. if ((frame >= paliashdr->numframes) || (frame < 0))
  326. {
  327. Con_DPrintf ("R_AliasSetupFrame: no such frame %d\n", frame);
  328. frame = 0;
  329. }
  330. pose = paliashdr->frames[frame].firstpose;
  331. numposes = paliashdr->frames[frame].numposes;
  332. if (numposes > 1)
  333. {
  334. interval = paliashdr->frames[frame].interval;
  335. pose += (int)(cl.time / interval) % numposes;
  336. }
  337. GL_DrawAliasFrame (paliashdr, pose);
  338. }
  339. /*
  340. =================
  341. R_DrawAliasModel
  342. =================
  343. */
  344. void R_DrawAliasModel (entity_t *e)
  345. {
  346. int i, j;
  347. int lnum;
  348. vec3_t dist;
  349. float add;
  350. model_t *clmodel;
  351. vec3_t mins, maxs;
  352. aliashdr_t *paliashdr;
  353. trivertx_t *verts, *v;
  354. int index;
  355. float s, t, an;
  356. int anim;
  357. clmodel = currententity->model;
  358. VectorAdd (currententity->origin, clmodel->mins, mins);
  359. VectorAdd (currententity->origin, clmodel->maxs, maxs);
  360. if (R_CullBox (mins, maxs))
  361. return;
  362. VectorCopy (currententity->origin, r_entorigin);
  363. VectorSubtract (r_origin, r_entorigin, modelorg);
  364. //
  365. // get lighting information
  366. //
  367. ambientlight = shadelight = R_LightPoint (currententity->origin);
  368. // allways give the gun some light
  369. if (e == &cl.viewent && ambientlight < 24)
  370. ambientlight = shadelight = 24;
  371. for (lnum=0 ; lnum<MAX_DLIGHTS ; lnum++)
  372. {
  373. if (cl_dlights[lnum].die >= cl.time)
  374. {
  375. VectorSubtract (currententity->origin,
  376. cl_dlights[lnum].origin,
  377. dist);
  378. add = cl_dlights[lnum].radius - Length(dist);
  379. if (add > 0) {
  380. ambientlight += add;
  381. //ZOID models should be affected by dlights as well
  382. shadelight += add;
  383. }
  384. }
  385. }
  386. // clamp lighting so it doesn't overbright as much
  387. if (ambientlight > 128)
  388. ambientlight = 128;
  389. if (ambientlight + shadelight > 192)
  390. shadelight = 192 - ambientlight;
  391. // ZOID: never allow players to go totally black
  392. i = currententity - cl_entities;
  393. if (i >= 1 && i<=cl.maxclients /* && !strcmp (currententity->model->name, "progs/player.mdl") */)
  394. if (ambientlight < 8)
  395. ambientlight = shadelight = 8;
  396. // HACK HACK HACK -- no fullbright colors, so make torches full light
  397. if (!strcmp (clmodel->name, "progs/flame2.mdl")
  398. || !strcmp (clmodel->name, "progs/flame.mdl") )
  399. ambientlight = shadelight = 256;
  400. shadedots = r_avertexnormal_dots[((int)(e->angles[1] * (SHADEDOT_QUANT / 360.0))) & (SHADEDOT_QUANT - 1)];
  401. shadelight = shadelight / 200.0;
  402. an = e->angles[1]/180*M_PI;
  403. shadevector[0] = cos(-an);
  404. shadevector[1] = sin(-an);
  405. shadevector[2] = 1;
  406. VectorNormalize (shadevector);
  407. //
  408. // locate the proper data
  409. //
  410. paliashdr = (aliashdr_t *)Mod_Extradata (currententity->model);
  411. c_alias_polys += paliashdr->numtris;
  412. //
  413. // draw all the triangles
  414. //
  415. GL_DisableMultitexture();
  416. glPushMatrix ();
  417. R_RotateForEntity (e);
  418. if (!strcmp (clmodel->name, "progs/eyes.mdl") && gl_doubleeyes.value) {
  419. glTranslatef (paliashdr->scale_origin[0], paliashdr->scale_origin[1], paliashdr->scale_origin[2] - (22 + 8));
  420. // double size of eyes, since they are really hard to see in gl
  421. glScalef (paliashdr->scale[0]*2, paliashdr->scale[1]*2, paliashdr->scale[2]*2);
  422. } else {
  423. glTranslatef (paliashdr->scale_origin[0], paliashdr->scale_origin[1], paliashdr->scale_origin[2]);
  424. glScalef (paliashdr->scale[0], paliashdr->scale[1], paliashdr->scale[2]);
  425. }
  426. anim = (int)(cl.time*10) & 3;
  427. GL_Bind(paliashdr->gl_texturenum[currententity->skinnum][anim]);
  428. // we can't dynamically colormap textures, so they are cached
  429. // seperately for the players. Heads are just uncolored.
  430. if (currententity->colormap != vid.colormap && !gl_nocolors.value)
  431. {
  432. i = currententity - cl_entities;
  433. if (i >= 1 && i<=cl.maxclients /* && !strcmp (currententity->model->name, "progs/player.mdl") */)
  434. GL_Bind(playertextures - 1 + i);
  435. }
  436. if (gl_smoothmodels.value)
  437. glShadeModel (GL_SMOOTH);
  438. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  439. if (gl_affinemodels.value)
  440. glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  441. R_SetupAliasFrame (currententity->frame, paliashdr);
  442. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  443. glShadeModel (GL_FLAT);
  444. if (gl_affinemodels.value)
  445. glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  446. glPopMatrix ();
  447. if (r_shadows.value)
  448. {
  449. glPushMatrix ();
  450. R_RotateForEntity (e);
  451. glDisable (GL_TEXTURE_2D);
  452. glEnable (GL_BLEND);
  453. glColor4f (0,0,0,0.5);
  454. GL_DrawAliasShadow (paliashdr, lastposenum);
  455. glEnable (GL_TEXTURE_2D);
  456. glDisable (GL_BLEND);
  457. glColor4f (1,1,1,1);
  458. glPopMatrix ();
  459. }
  460. }
  461. //==================================================================================
  462. /*
  463. =============
  464. R_DrawEntitiesOnList
  465. =============
  466. */
  467. void R_DrawEntitiesOnList (void)
  468. {
  469. int i;
  470. if (!r_drawentities.value)
  471. return;
  472. // draw sprites seperately, because of alpha blending
  473. for (i=0 ; i<cl_numvisedicts ; i++)
  474. {
  475. currententity = cl_visedicts[i];
  476. switch (currententity->model->type)
  477. {
  478. case mod_alias:
  479. R_DrawAliasModel (currententity);
  480. break;
  481. case mod_brush:
  482. R_DrawBrushModel (currententity);
  483. break;
  484. default:
  485. break;
  486. }
  487. }
  488. for (i=0 ; i<cl_numvisedicts ; i++)
  489. {
  490. currententity = cl_visedicts[i];
  491. switch (currententity->model->type)
  492. {
  493. case mod_sprite:
  494. R_DrawSpriteModel (currententity);
  495. break;
  496. }
  497. }
  498. }
  499. /*
  500. =============
  501. R_DrawViewModel
  502. =============
  503. */
  504. void R_DrawViewModel (void)
  505. {
  506. float ambient[4], diffuse[4];
  507. int j;
  508. int lnum;
  509. vec3_t dist;
  510. float add;
  511. dlight_t *dl;
  512. int ambientlight, shadelight;
  513. if (!r_drawviewmodel.value)
  514. return;
  515. if (chase_active.value)
  516. return;
  517. if (envmap)
  518. return;
  519. if (!r_drawentities.value)
  520. return;
  521. if (cl.items & IT_INVISIBILITY)
  522. return;
  523. if (cl.stats[STAT_HEALTH] <= 0)
  524. return;
  525. currententity = &cl.viewent;
  526. if (!currententity->model)
  527. return;
  528. j = R_LightPoint (currententity->origin);
  529. if (j < 24)
  530. j = 24; // allways give some light on gun
  531. ambientlight = j;
  532. shadelight = j;
  533. // add dynamic lights
  534. for (lnum=0 ; lnum<MAX_DLIGHTS ; lnum++)
  535. {
  536. dl = &cl_dlights[lnum];
  537. if (!dl->radius)
  538. continue;
  539. if (!dl->radius)
  540. continue;
  541. if (dl->die < cl.time)
  542. continue;
  543. VectorSubtract (currententity->origin, dl->origin, dist);
  544. add = dl->radius - Length(dist);
  545. if (add > 0)
  546. ambientlight += add;
  547. }
  548. ambient[0] = ambient[1] = ambient[2] = ambient[3] = (float)ambientlight / 128;
  549. diffuse[0] = diffuse[1] = diffuse[2] = diffuse[3] = (float)shadelight / 128;
  550. // hack the depth range to prevent view model from poking into walls
  551. glDepthRange (gldepthmin, gldepthmin + 0.3*(gldepthmax-gldepthmin));
  552. R_DrawAliasModel (currententity);
  553. glDepthRange (gldepthmin, gldepthmax);
  554. }
  555. /*
  556. ============
  557. R_PolyBlend
  558. ============
  559. */
  560. void R_PolyBlend (void)
  561. {
  562. if (!gl_polyblend.value)
  563. return;
  564. if (!v_blend[3])
  565. return;
  566. GL_DisableMultitexture();
  567. glDisable (GL_ALPHA_TEST);
  568. glEnable (GL_BLEND);
  569. glDisable (GL_DEPTH_TEST);
  570. glDisable (GL_TEXTURE_2D);
  571. glLoadIdentity ();
  572. glRotatef (-90, 1, 0, 0); // put Z going up
  573. glRotatef (90, 0, 0, 1); // put Z going up
  574. glColor4fv (v_blend);
  575. glBegin (GL_QUADS);
  576. glVertex3f (10, 100, 100);
  577. glVertex3f (10, -100, 100);
  578. glVertex3f (10, -100, -100);
  579. glVertex3f (10, 100, -100);
  580. glEnd ();
  581. glDisable (GL_BLEND);
  582. glEnable (GL_TEXTURE_2D);
  583. glEnable (GL_ALPHA_TEST);
  584. }
  585. int SignbitsForPlane (mplane_t *out)
  586. {
  587. int bits, j;
  588. // for fast box on planeside test
  589. bits = 0;
  590. for (j=0 ; j<3 ; j++)
  591. {
  592. if (out->normal[j] < 0)
  593. bits |= 1<<j;
  594. }
  595. return bits;
  596. }
  597. void R_SetFrustum (void)
  598. {
  599. int i;
  600. if (r_refdef.fov_x == 90)
  601. {
  602. // front side is visible
  603. VectorAdd (vpn, vright, frustum[0].normal);
  604. VectorSubtract (vpn, vright, frustum[1].normal);
  605. VectorAdd (vpn, vup, frustum[2].normal);
  606. VectorSubtract (vpn, vup, frustum[3].normal);
  607. }
  608. else
  609. {
  610. // rotate VPN right by FOV_X/2 degrees
  611. RotatePointAroundVector( frustum[0].normal, vup, vpn, -(90-r_refdef.fov_x / 2 ) );
  612. // rotate VPN left by FOV_X/2 degrees
  613. RotatePointAroundVector( frustum[1].normal, vup, vpn, 90-r_refdef.fov_x / 2 );
  614. // rotate VPN up by FOV_X/2 degrees
  615. RotatePointAroundVector( frustum[2].normal, vright, vpn, 90-r_refdef.fov_y / 2 );
  616. // rotate VPN down by FOV_X/2 degrees
  617. RotatePointAroundVector( frustum[3].normal, vright, vpn, -( 90 - r_refdef.fov_y / 2 ) );
  618. }
  619. for (i=0 ; i<4 ; i++)
  620. {
  621. frustum[i].type = PLANE_ANYZ;
  622. frustum[i].dist = DotProduct (r_origin, frustum[i].normal);
  623. frustum[i].signbits = SignbitsForPlane (&frustum[i]);
  624. }
  625. }
  626. /*
  627. ===============
  628. R_SetupFrame
  629. ===============
  630. */
  631. void R_SetupFrame (void)
  632. {
  633. int edgecount;
  634. vrect_t vrect;
  635. float w, h;
  636. // don't allow cheats in multiplayer
  637. if (cl.maxclients > 1)
  638. Cvar_Set ("r_fullbright", "0");
  639. R_AnimateLight ();
  640. r_framecount++;
  641. // build the transformation matrix for the given view angles
  642. VectorCopy (r_refdef.vieworg, r_origin);
  643. AngleVectors (r_refdef.viewangles, vpn, vright, vup);
  644. // current viewleaf
  645. r_oldviewleaf = r_viewleaf;
  646. r_viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
  647. V_SetContentsColor (r_viewleaf->contents);
  648. V_CalcBlend ();
  649. r_cache_thrash = false;
  650. c_brush_polys = 0;
  651. c_alias_polys = 0;
  652. }
  653. void MYgluPerspective( GLdouble fovy, GLdouble aspect,
  654. GLdouble zNear, GLdouble zFar )
  655. {
  656. GLdouble xmin, xmax, ymin, ymax;
  657. ymax = zNear * tan( fovy * M_PI / 360.0 );
  658. ymin = -ymax;
  659. xmin = ymin * aspect;
  660. xmax = ymax * aspect;
  661. glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
  662. }
  663. /*
  664. =============
  665. R_SetupGL
  666. =============
  667. */
  668. void R_SetupGL (void)
  669. {
  670. float screenaspect;
  671. float yfov;
  672. int i;
  673. extern int glwidth, glheight;
  674. int x, x2, y2, y, w, h;
  675. //
  676. // set up viewpoint
  677. //
  678. glMatrixMode(GL_PROJECTION);
  679. glLoadIdentity ();
  680. x = r_refdef.vrect.x * glwidth/vid.width;
  681. x2 = (r_refdef.vrect.x + r_refdef.vrect.width) * glwidth/vid.width;
  682. y = (vid.height-r_refdef.vrect.y) * glheight/vid.height;
  683. y2 = (vid.height - (r_refdef.vrect.y + r_refdef.vrect.height)) * glheight/vid.height;
  684. // fudge around because of frac screen scale
  685. if (x > 0)
  686. x--;
  687. if (x2 < glwidth)
  688. x2++;
  689. if (y2 < 0)
  690. y2--;
  691. if (y < glheight)
  692. y++;
  693. w = x2 - x;
  694. h = y - y2;
  695. if (envmap)
  696. {
  697. x = y2 = 0;
  698. w = h = 256;
  699. }
  700. glViewport (glx + x, gly + y2, w, h);
  701. screenaspect = (float)r_refdef.vrect.width/r_refdef.vrect.height;
  702. // yfov = 2*atan((float)r_refdef.vrect.height/r_refdef.vrect.width)*180/M_PI;
  703. MYgluPerspective (r_refdef.fov_y, screenaspect, 4, 4096);
  704. if (mirror)
  705. {
  706. if (mirror_plane->normal[2])
  707. glScalef (1, -1, 1);
  708. else
  709. glScalef (-1, 1, 1);
  710. glCullFace(GL_BACK);
  711. }
  712. else
  713. glCullFace(GL_FRONT);
  714. glMatrixMode(GL_MODELVIEW);
  715. glLoadIdentity ();
  716. glRotatef (-90, 1, 0, 0); // put Z going up
  717. glRotatef (90, 0, 0, 1); // put Z going up
  718. glRotatef (-r_refdef.viewangles[2], 1, 0, 0);
  719. glRotatef (-r_refdef.viewangles[0], 0, 1, 0);
  720. glRotatef (-r_refdef.viewangles[1], 0, 0, 1);
  721. glTranslatef (-r_refdef.vieworg[0], -r_refdef.vieworg[1], -r_refdef.vieworg[2]);
  722. glGetFloatv (GL_MODELVIEW_MATRIX, r_world_matrix);
  723. //
  724. // set drawing parms
  725. //
  726. if (gl_cull.value)
  727. glEnable(GL_CULL_FACE);
  728. else
  729. glDisable(GL_CULL_FACE);
  730. glDisable(GL_BLEND);
  731. glDisable(GL_ALPHA_TEST);
  732. glEnable(GL_DEPTH_TEST);
  733. }
  734. /*
  735. ================
  736. R_RenderScene
  737. r_refdef must be set before the first call
  738. ================
  739. */
  740. void R_RenderScene (void)
  741. {
  742. R_SetupFrame ();
  743. R_SetFrustum ();
  744. R_SetupGL ();
  745. R_MarkLeaves (); // done here so we know if we're in water
  746. R_DrawWorld (); // adds static entities to the list
  747. S_ExtraUpdate (); // don't let sound get messed up if going slow
  748. R_DrawEntitiesOnList ();
  749. GL_DisableMultitexture();
  750. R_RenderDlights ();
  751. R_DrawParticles ();
  752. #ifdef GLTEST
  753. Test_Draw ();
  754. #endif
  755. }
  756. /*
  757. =============
  758. R_Clear
  759. =============
  760. */
  761. void R_Clear (void)
  762. {
  763. if (r_mirroralpha.value != 1.0)
  764. {
  765. if (gl_clear.value)
  766. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  767. else
  768. glClear (GL_DEPTH_BUFFER_BIT);
  769. gldepthmin = 0;
  770. gldepthmax = 0.5;
  771. glDepthFunc (GL_LEQUAL);
  772. }
  773. else if (gl_ztrick.value)
  774. {
  775. static int trickframe;
  776. if (gl_clear.value)
  777. glClear (GL_COLOR_BUFFER_BIT);
  778. trickframe++;
  779. if (trickframe & 1)
  780. {
  781. gldepthmin = 0;
  782. gldepthmax = 0.49999;
  783. glDepthFunc (GL_LEQUAL);
  784. }
  785. else
  786. {
  787. gldepthmin = 1;
  788. gldepthmax = 0.5;
  789. glDepthFunc (GL_GEQUAL);
  790. }
  791. }
  792. else
  793. {
  794. if (gl_clear.value)
  795. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  796. else
  797. glClear (GL_DEPTH_BUFFER_BIT);
  798. gldepthmin = 0;
  799. gldepthmax = 1;
  800. glDepthFunc (GL_LEQUAL);
  801. }
  802. glDepthRange (gldepthmin, gldepthmax);
  803. }
  804. /*
  805. =============
  806. R_Mirror
  807. =============
  808. */
  809. void R_Mirror (void)
  810. {
  811. float d;
  812. msurface_t *s;
  813. entity_t *ent;
  814. if (!mirror)
  815. return;
  816. memcpy (r_base_world_matrix, r_world_matrix, sizeof(r_base_world_matrix));
  817. d = DotProduct (r_refdef.vieworg, mirror_plane->normal) - mirror_plane->dist;
  818. VectorMA (r_refdef.vieworg, -2*d, mirror_plane->normal, r_refdef.vieworg);
  819. d = DotProduct (vpn, mirror_plane->normal);
  820. VectorMA (vpn, -2*d, mirror_plane->normal, vpn);
  821. r_refdef.viewangles[0] = -asin (vpn[2])/M_PI*180;
  822. r_refdef.viewangles[1] = atan2 (vpn[1], vpn[0])/M_PI*180;
  823. r_refdef.viewangles[2] = -r_refdef.viewangles[2];
  824. ent = &cl_entities[cl.viewentity];
  825. if (cl_numvisedicts < MAX_VISEDICTS)
  826. {
  827. cl_visedicts[cl_numvisedicts] = ent;
  828. cl_numvisedicts++;
  829. }
  830. gldepthmin = 0.5;
  831. gldepthmax = 1;
  832. glDepthRange (gldepthmin, gldepthmax);
  833. glDepthFunc (GL_LEQUAL);
  834. R_RenderScene ();
  835. R_DrawWaterSurfaces ();
  836. gldepthmin = 0;
  837. gldepthmax = 0.5;
  838. glDepthRange (gldepthmin, gldepthmax);
  839. glDepthFunc (GL_LEQUAL);
  840. // blend on top
  841. glEnable (GL_BLEND);
  842. glMatrixMode(GL_PROJECTION);
  843. if (mirror_plane->normal[2])
  844. glScalef (1,-1,1);
  845. else
  846. glScalef (-1,1,1);
  847. glCullFace(GL_FRONT);
  848. glMatrixMode(GL_MODELVIEW);
  849. glLoadMatrixf (r_base_world_matrix);
  850. glColor4f (1,1,1,r_mirroralpha.value);
  851. s = cl.worldmodel->textures[mirrortexturenum]->texturechain;
  852. for ( ; s ; s=s->texturechain)
  853. R_RenderBrushPoly (s);
  854. cl.worldmodel->textures[mirrortexturenum]->texturechain = NULL;
  855. glDisable (GL_BLEND);
  856. glColor4f (1,1,1,1);
  857. }
  858. /*
  859. ================
  860. R_RenderView
  861. r_refdef must be set before the first call
  862. ================
  863. */
  864. void R_RenderView (void)
  865. {
  866. double time1, time2;
  867. GLfloat colors[4] = {(GLfloat) 0.0, (GLfloat) 0.0, (GLfloat) 1, (GLfloat) 0.20};
  868. if (r_norefresh.value)
  869. return;
  870. if (!r_worldentity.model || !cl.worldmodel)
  871. Sys_Error ("R_RenderView: NULL worldmodel");
  872. if (r_speeds.value)
  873. {
  874. glFinish ();
  875. time1 = Sys_FloatTime ();
  876. c_brush_polys = 0;
  877. c_alias_polys = 0;
  878. }
  879. mirror = false;
  880. if (gl_finish.value)
  881. glFinish ();
  882. R_Clear ();
  883. // render normal view
  884. /***** Experimental silly looking fog ******
  885. ****** Use r_fullbright if you enable ******
  886. glFogi(GL_FOG_MODE, GL_LINEAR);
  887. glFogfv(GL_FOG_COLOR, colors);
  888. glFogf(GL_FOG_END, 512.0);
  889. glEnable(GL_FOG);
  890. ********************************************/
  891. R_RenderScene ();
  892. R_DrawViewModel ();
  893. R_DrawWaterSurfaces ();
  894. // More fog right here :)
  895. // glDisable(GL_FOG);
  896. // End of all fog code...
  897. // render mirror view
  898. R_Mirror ();
  899. R_PolyBlend ();
  900. if (r_speeds.value)
  901. {
  902. // glFinish ();
  903. time2 = Sys_FloatTime ();
  904. Con_Printf ("%3i ms %4i wpoly %4i epoly\n", (int)((time2-time1)*1000), c_brush_polys, c_alias_polys);
  905. }
  906. }