gl_rsurf.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  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_surf.c: surface-related refresh code
  16. #include "quakedef.h"
  17. int skytexturenum;
  18. #ifndef GL_RGBA4
  19. #define GL_RGBA4 0
  20. #endif
  21. int lightmap_bytes; // 1, 2, or 4
  22. int lightmap_textures;
  23. unsigned blocklights[18*18];
  24. #define BLOCK_WIDTH 128
  25. #define BLOCK_HEIGHT 128
  26. #define MAX_LIGHTMAPS 64
  27. int active_lightmaps;
  28. typedef struct glRect_s {
  29. unsigned char l,t,w,h;
  30. } glRect_t;
  31. glpoly_t *lightmap_polys[MAX_LIGHTMAPS];
  32. qboolean lightmap_modified[MAX_LIGHTMAPS];
  33. glRect_t lightmap_rectchange[MAX_LIGHTMAPS];
  34. int allocated[MAX_LIGHTMAPS][BLOCK_WIDTH];
  35. // the lightmap texture data needs to be kept in
  36. // main memory so texsubimage can update properly
  37. byte lightmaps[4*MAX_LIGHTMAPS*BLOCK_WIDTH*BLOCK_HEIGHT];
  38. // For gl_texsort 0
  39. msurface_t *skychain = NULL;
  40. msurface_t *waterchain = NULL;
  41. void R_RenderDynamicLightmaps (msurface_t *fa);
  42. /*
  43. ===============
  44. R_AddDynamicLights
  45. ===============
  46. */
  47. void R_AddDynamicLights (msurface_t *surf)
  48. {
  49. int lnum;
  50. int sd, td;
  51. float dist, rad, minlight;
  52. vec3_t impact, local;
  53. int s, t;
  54. int i;
  55. int smax, tmax;
  56. mtexinfo_t *tex;
  57. smax = (surf->extents[0]>>4)+1;
  58. tmax = (surf->extents[1]>>4)+1;
  59. tex = surf->texinfo;
  60. for (lnum=0 ; lnum<MAX_DLIGHTS ; lnum++)
  61. {
  62. if ( !(surf->dlightbits & (1<<lnum) ) )
  63. continue; // not lit by this light
  64. rad = cl_dlights[lnum].radius;
  65. dist = DotProduct (cl_dlights[lnum].origin, surf->plane->normal) -
  66. surf->plane->dist;
  67. rad -= fabs(dist);
  68. minlight = cl_dlights[lnum].minlight;
  69. if (rad < minlight)
  70. continue;
  71. minlight = rad - minlight;
  72. for (i=0 ; i<3 ; i++)
  73. {
  74. impact[i] = cl_dlights[lnum].origin[i] -
  75. surf->plane->normal[i]*dist;
  76. }
  77. local[0] = DotProduct (impact, tex->vecs[0]) + tex->vecs[0][3];
  78. local[1] = DotProduct (impact, tex->vecs[1]) + tex->vecs[1][3];
  79. local[0] -= surf->texturemins[0];
  80. local[1] -= surf->texturemins[1];
  81. for (t = 0 ; t<tmax ; t++)
  82. {
  83. td = local[1] - t*16;
  84. if (td < 0)
  85. td = -td;
  86. for (s=0 ; s<smax ; s++)
  87. {
  88. sd = local[0] - s*16;
  89. if (sd < 0)
  90. sd = -sd;
  91. if (sd > td)
  92. dist = sd + (td>>1);
  93. else
  94. dist = td + (sd>>1);
  95. if (dist < minlight)
  96. blocklights[t*smax + s] += (rad - dist)*256;
  97. }
  98. }
  99. }
  100. }
  101. /*
  102. ===============
  103. R_BuildLightMap
  104. Combine and scale multiple lightmaps into the 8.8 format in blocklights
  105. ===============
  106. */
  107. void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
  108. {
  109. int smax, tmax;
  110. int t;
  111. int i, j, size;
  112. byte *lightmap;
  113. unsigned scale;
  114. int maps;
  115. unsigned *bl;
  116. surf->cached_dlight = (surf->dlightframe == r_framecount);
  117. smax = (surf->extents[0]>>4)+1;
  118. tmax = (surf->extents[1]>>4)+1;
  119. size = smax*tmax;
  120. lightmap = surf->samples;
  121. // set to full bright if no light data
  122. if (/* r_fullbright.value || */ !cl.worldmodel->lightdata)
  123. {
  124. for (i=0 ; i<size ; i++)
  125. blocklights[i] = 255*256;
  126. goto store;
  127. }
  128. // clear to no light
  129. for (i=0 ; i<size ; i++)
  130. blocklights[i] = 0;
  131. // add all the lightmaps
  132. if (lightmap)
  133. for (maps = 0 ; maps < MAXLIGHTMAPS && surf->styles[maps] != 255 ;
  134. maps++)
  135. {
  136. scale = d_lightstylevalue[surf->styles[maps]];
  137. surf->cached_light[maps] = scale; // 8.8 fraction
  138. for (i=0 ; i<size ; i++)
  139. blocklights[i] += lightmap[i] * scale;
  140. lightmap += size; // skip to next lightmap
  141. }
  142. // add all the dynamic lights
  143. if (surf->dlightframe == r_framecount)
  144. R_AddDynamicLights (surf);
  145. // bound, invert, and shift
  146. store:
  147. switch (gl_lightmap_format)
  148. {
  149. case GL_RGBA:
  150. stride -= (smax<<2);
  151. bl = blocklights;
  152. for (i=0 ; i<tmax ; i++, dest += stride)
  153. {
  154. for (j=0 ; j<smax ; j++)
  155. {
  156. t = *bl++;
  157. t >>= 7;
  158. if (t > 255)
  159. t = 255;
  160. dest[3] = 255-t;
  161. dest += 4;
  162. }
  163. }
  164. break;
  165. case GL_ALPHA:
  166. case GL_LUMINANCE:
  167. case GL_INTENSITY:
  168. bl = blocklights;
  169. for (i=0 ; i<tmax ; i++, dest += stride)
  170. {
  171. for (j=0 ; j<smax ; j++)
  172. {
  173. t = *bl++;
  174. t >>= 7;
  175. if (t > 255)
  176. t = 255;
  177. dest[j] = 255-t;
  178. }
  179. }
  180. break;
  181. default:
  182. Sys_Error ("Bad lightmap format");
  183. }
  184. }
  185. /*
  186. ===============
  187. R_TextureAnimation
  188. Returns the proper texture for a given time and base texture
  189. ===============
  190. */
  191. texture_t *R_TextureAnimation (texture_t *base)
  192. {
  193. int reletive;
  194. int count;
  195. if (currententity->frame)
  196. {
  197. if (base->alternate_anims)
  198. base = base->alternate_anims;
  199. }
  200. if (!base->anim_total)
  201. return base;
  202. reletive = (int)(cl.time*10) % base->anim_total;
  203. count = 0;
  204. while (base->anim_min > reletive || base->anim_max <= reletive)
  205. {
  206. base = base->anim_next;
  207. if (!base)
  208. Sys_Error ("R_TextureAnimation: broken cycle");
  209. if (++count > 100)
  210. Sys_Error ("R_TextureAnimation: infinite cycle");
  211. }
  212. return base;
  213. }
  214. /*
  215. =============================================================
  216. BRUSH MODELS
  217. =============================================================
  218. */
  219. extern int solidskytexture;
  220. extern int alphaskytexture;
  221. extern float speedscale; // for top sky and bottom sky
  222. void DrawGLWaterPoly (glpoly_t *p);
  223. void DrawGLWaterPolyLightmap (glpoly_t *p);
  224. #ifdef _WIN32
  225. lpMTexFUNC qglMTexCoord2fSGIS = NULL;
  226. lpSelTexFUNC qglSelectTextureSGIS = NULL;
  227. #endif
  228. qboolean mtexenabled = false;
  229. void GL_SelectTexture (GLenum target);
  230. void GL_DisableMultitexture(void)
  231. {
  232. if (mtexenabled) {
  233. glDisable(GL_TEXTURE_2D);
  234. GL_SelectTexture(TEXTURE0_SGIS);
  235. mtexenabled = false;
  236. }
  237. }
  238. void GL_EnableMultitexture(void)
  239. {
  240. if (gl_mtexable) {
  241. GL_SelectTexture(TEXTURE1_SGIS);
  242. glEnable(GL_TEXTURE_2D);
  243. mtexenabled = true;
  244. }
  245. }
  246. #ifndef _WIN32
  247. /*
  248. ================
  249. R_DrawSequentialPoly
  250. Systems that have fast state and texture changes can
  251. just do everything as it passes with no need to sort
  252. ================
  253. */
  254. void R_DrawSequentialPoly (msurface_t *s)
  255. {
  256. glpoly_t *p;
  257. float *v;
  258. int i;
  259. texture_t *t;
  260. //
  261. // normal lightmaped poly
  262. //
  263. // if ((!(s->flags & (SURF_DRAWSKY|SURF_DRAWTURB)))
  264. // && ((r_viewleaf->contents!=CONTENTS_EMPTY && (s->flags & SURF_UNDERWATER)) ||
  265. // (r_viewleaf->contents==CONTENTS_EMPTY && !(s->flags & SURF_UNDERWATER))))
  266. if (0)
  267. {
  268. p = s->polys;
  269. t = R_TextureAnimation (s->texinfo->texture);
  270. GL_Bind (t->gl_texturenum);
  271. glBegin (GL_POLYGON);
  272. v = p->verts[0];
  273. for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
  274. {
  275. glTexCoord2f (v[3], v[4]);
  276. glVertex3fv (v);
  277. }
  278. glEnd ();
  279. GL_Bind (lightmap_textures + s->lightmaptexturenum);
  280. glEnable (GL_BLEND);
  281. glBegin (GL_POLYGON);
  282. v = p->verts[0];
  283. for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
  284. {
  285. glTexCoord2f (v[5], v[6]);
  286. glVertex3fv (v);
  287. }
  288. glEnd ();
  289. glDisable (GL_BLEND);
  290. return;
  291. }
  292. //
  293. // subdivided water surface warp
  294. //
  295. if (s->flags & SURF_DRAWTURB)
  296. {
  297. GL_Bind (s->texinfo->texture->gl_texturenum);
  298. EmitWaterPolys (s);
  299. return;
  300. }
  301. //
  302. // subdivided sky warp
  303. //
  304. if (s->flags & SURF_DRAWSKY)
  305. {
  306. GL_Bind (solidskytexture);
  307. speedscale = realtime*8;
  308. speedscale -= (int)speedscale;
  309. EmitSkyPolys (s);
  310. glEnable (GL_BLEND);
  311. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  312. GL_Bind (alphaskytexture);
  313. speedscale = realtime*16;
  314. speedscale -= (int)speedscale;
  315. EmitSkyPolys (s);
  316. if (gl_lightmap_format == GL_LUMINANCE)
  317. glBlendFunc (GL_ZERO, GL_ONE_MINUS_SRC_COLOR);
  318. glDisable (GL_BLEND);
  319. }
  320. //
  321. // underwater warped with lightmap
  322. //
  323. p = s->polys;
  324. t = R_TextureAnimation (s->texinfo->texture);
  325. GL_Bind (t->gl_texturenum);
  326. DrawGLWaterPoly (p);
  327. GL_Bind (lightmap_textures + s->lightmaptexturenum);
  328. glEnable (GL_BLEND);
  329. DrawGLWaterPolyLightmap (p);
  330. glDisable (GL_BLEND);
  331. }
  332. #else
  333. /*
  334. ================
  335. R_DrawSequentialPoly
  336. Systems that have fast state and texture changes can
  337. just do everything as it passes with no need to sort
  338. ================
  339. */
  340. void R_DrawSequentialPoly (msurface_t *s)
  341. {
  342. glpoly_t *p;
  343. float *v;
  344. int i;
  345. texture_t *t;
  346. vec3_t nv, dir;
  347. float ss, ss2, length;
  348. float s1, t1;
  349. glRect_t *theRect;
  350. //
  351. // normal lightmaped poly
  352. //
  353. if (! (s->flags & (SURF_DRAWSKY|SURF_DRAWTURB|SURF_UNDERWATER) ) )
  354. {
  355. R_RenderDynamicLightmaps (s);
  356. if (gl_mtexable) {
  357. p = s->polys;
  358. t = R_TextureAnimation (s->texinfo->texture);
  359. // Binds world to texture env 0
  360. GL_SelectTexture(TEXTURE0_SGIS);
  361. GL_Bind (t->gl_texturenum);
  362. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  363. // Binds lightmap to texenv 1
  364. GL_EnableMultitexture(); // Same as SelectTexture (TEXTURE1)
  365. GL_Bind (lightmap_textures + s->lightmaptexturenum);
  366. i = s->lightmaptexturenum;
  367. if (lightmap_modified[i])
  368. {
  369. lightmap_modified[i] = false;
  370. theRect = &lightmap_rectchange[i];
  371. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, theRect->t,
  372. BLOCK_WIDTH, theRect->h, gl_lightmap_format, GL_UNSIGNED_BYTE,
  373. lightmaps+(i* BLOCK_HEIGHT + theRect->t) *BLOCK_WIDTH*lightmap_bytes);
  374. theRect->l = BLOCK_WIDTH;
  375. theRect->t = BLOCK_HEIGHT;
  376. theRect->h = 0;
  377. theRect->w = 0;
  378. }
  379. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
  380. glBegin(GL_POLYGON);
  381. v = p->verts[0];
  382. for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
  383. {
  384. qglMTexCoord2fSGIS (TEXTURE0_SGIS, v[3], v[4]);
  385. qglMTexCoord2fSGIS (TEXTURE1_SGIS, v[5], v[6]);
  386. glVertex3fv (v);
  387. }
  388. glEnd ();
  389. return;
  390. } else {
  391. p = s->polys;
  392. t = R_TextureAnimation (s->texinfo->texture);
  393. GL_Bind (t->gl_texturenum);
  394. glBegin (GL_POLYGON);
  395. v = p->verts[0];
  396. for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
  397. {
  398. glTexCoord2f (v[3], v[4]);
  399. glVertex3fv (v);
  400. }
  401. glEnd ();
  402. GL_Bind (lightmap_textures + s->lightmaptexturenum);
  403. glEnable (GL_BLEND);
  404. glBegin (GL_POLYGON);
  405. v = p->verts[0];
  406. for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
  407. {
  408. glTexCoord2f (v[5], v[6]);
  409. glVertex3fv (v);
  410. }
  411. glEnd ();
  412. glDisable (GL_BLEND);
  413. }
  414. return;
  415. }
  416. //
  417. // subdivided water surface warp
  418. //
  419. if (s->flags & SURF_DRAWTURB)
  420. {
  421. GL_DisableMultitexture();
  422. GL_Bind (s->texinfo->texture->gl_texturenum);
  423. EmitWaterPolys (s);
  424. return;
  425. }
  426. //
  427. // subdivided sky warp
  428. //
  429. if (s->flags & SURF_DRAWSKY)
  430. {
  431. GL_DisableMultitexture();
  432. GL_Bind (solidskytexture);
  433. speedscale = realtime*8;
  434. speedscale -= (int)speedscale & ~127;
  435. EmitSkyPolys (s);
  436. glEnable (GL_BLEND);
  437. GL_Bind (alphaskytexture);
  438. speedscale = realtime*16;
  439. speedscale -= (int)speedscale & ~127;
  440. EmitSkyPolys (s);
  441. glDisable (GL_BLEND);
  442. return;
  443. }
  444. //
  445. // underwater warped with lightmap
  446. //
  447. R_RenderDynamicLightmaps (s);
  448. if (gl_mtexable) {
  449. p = s->polys;
  450. t = R_TextureAnimation (s->texinfo->texture);
  451. GL_SelectTexture(TEXTURE0_SGIS);
  452. GL_Bind (t->gl_texturenum);
  453. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  454. GL_EnableMultitexture();
  455. GL_Bind (lightmap_textures + s->lightmaptexturenum);
  456. i = s->lightmaptexturenum;
  457. if (lightmap_modified[i])
  458. {
  459. lightmap_modified[i] = false;
  460. theRect = &lightmap_rectchange[i];
  461. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, theRect->t,
  462. BLOCK_WIDTH, theRect->h, gl_lightmap_format, GL_UNSIGNED_BYTE,
  463. lightmaps+(i* BLOCK_HEIGHT + theRect->t) *BLOCK_WIDTH*lightmap_bytes);
  464. theRect->l = BLOCK_WIDTH;
  465. theRect->t = BLOCK_HEIGHT;
  466. theRect->h = 0;
  467. theRect->w = 0;
  468. }
  469. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
  470. glBegin (GL_TRIANGLE_FAN);
  471. v = p->verts[0];
  472. for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
  473. {
  474. qglMTexCoord2fSGIS (TEXTURE0_SGIS, v[3], v[4]);
  475. qglMTexCoord2fSGIS (TEXTURE1_SGIS, v[5], v[6]);
  476. nv[0] = v[0] + 8*sin(v[1]*0.05+realtime)*sin(v[2]*0.05+realtime);
  477. nv[1] = v[1] + 8*sin(v[0]*0.05+realtime)*sin(v[2]*0.05+realtime);
  478. nv[2] = v[2];
  479. glVertex3fv (nv);
  480. }
  481. glEnd ();
  482. } else {
  483. p = s->polys;
  484. t = R_TextureAnimation (s->texinfo->texture);
  485. GL_Bind (t->gl_texturenum);
  486. DrawGLWaterPoly (p);
  487. GL_Bind (lightmap_textures + s->lightmaptexturenum);
  488. glEnable (GL_BLEND);
  489. DrawGLWaterPolyLightmap (p);
  490. glDisable (GL_BLEND);
  491. }
  492. }
  493. #endif
  494. /*
  495. ================
  496. DrawGLWaterPoly
  497. Warp the vertex coordinates
  498. ================
  499. */
  500. void DrawGLWaterPoly (glpoly_t *p)
  501. {
  502. int i;
  503. float *v;
  504. vec3_t nv;
  505. GL_DisableMultitexture();
  506. glBegin (GL_TRIANGLE_FAN);
  507. v = p->verts[0];
  508. for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
  509. {
  510. glTexCoord2f (v[3], v[4]);
  511. nv[0] = v[0] + 8*sin(v[1]*0.05+realtime)*sin(v[2]*0.05+realtime);
  512. nv[1] = v[1] + 8*sin(v[0]*0.05+realtime)*sin(v[2]*0.05+realtime);
  513. nv[2] = v[2];
  514. glVertex3fv (nv);
  515. }
  516. glEnd ();
  517. }
  518. void DrawGLWaterPolyLightmap (glpoly_t *p)
  519. {
  520. int i;
  521. float *v;
  522. vec3_t nv;
  523. GL_DisableMultitexture();
  524. glBegin (GL_TRIANGLE_FAN);
  525. v = p->verts[0];
  526. for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
  527. {
  528. glTexCoord2f (v[5], v[6]);
  529. nv[0] = v[0] + 8*sin(v[1]*0.05+realtime)*sin(v[2]*0.05+realtime);
  530. nv[1] = v[1] + 8*sin(v[0]*0.05+realtime)*sin(v[2]*0.05+realtime);
  531. nv[2] = v[2];
  532. glVertex3fv (nv);
  533. }
  534. glEnd ();
  535. }
  536. /*
  537. ================
  538. DrawGLPoly
  539. ================
  540. */
  541. void DrawGLPoly (glpoly_t *p)
  542. {
  543. int i;
  544. float *v;
  545. glBegin (GL_POLYGON);
  546. v = p->verts[0];
  547. for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
  548. {
  549. glTexCoord2f (v[3], v[4]);
  550. glVertex3fv (v);
  551. }
  552. glEnd ();
  553. }
  554. /*
  555. ================
  556. R_BlendLightmaps
  557. ================
  558. */
  559. void R_BlendLightmaps (void)
  560. {
  561. int i, j;
  562. glpoly_t *p;
  563. float *v;
  564. glRect_t *theRect;
  565. #if 0
  566. if (r_fullbright.value)
  567. return;
  568. #endif
  569. if (!gl_texsort.value)
  570. return;
  571. glDepthMask (0); // don't bother writing Z
  572. if (gl_lightmap_format == GL_LUMINANCE)
  573. glBlendFunc (GL_ZERO, GL_ONE_MINUS_SRC_COLOR);
  574. else if (gl_lightmap_format == GL_INTENSITY)
  575. {
  576. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  577. glColor4f (0,0,0,1);
  578. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  579. }
  580. if (!r_lightmap.value)
  581. {
  582. glEnable (GL_BLEND);
  583. }
  584. for (i=0 ; i<MAX_LIGHTMAPS ; i++)
  585. {
  586. p = lightmap_polys[i];
  587. if (!p)
  588. continue;
  589. GL_Bind(lightmap_textures+i);
  590. if (lightmap_modified[i])
  591. {
  592. lightmap_modified[i] = false;
  593. theRect = &lightmap_rectchange[i];
  594. // theRect->l = 0;
  595. // theRect->t = 0;
  596. // theRect->w = BLOCK_WIDTH;
  597. // theRect->h = BLOCK_HEIGHT;
  598. // glTexImage2D (GL_TEXTURE_2D, 0, lightmap_bytes
  599. // , BLOCK_WIDTH, BLOCK_HEIGHT, 0,
  600. // gl_lightmap_format, GL_UNSIGNED_BYTE, lightmaps+i*BLOCK_WIDTH*BLOCK_HEIGHT*lightmap_bytes);
  601. // glTexImage2D (GL_TEXTURE_2D, 0, lightmap_bytes
  602. // , BLOCK_WIDTH, theRect->h, 0,
  603. // gl_lightmap_format, GL_UNSIGNED_BYTE, lightmaps+(i*BLOCK_HEIGHT+theRect->t)*BLOCK_WIDTH*lightmap_bytes);
  604. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, theRect->t,
  605. BLOCK_WIDTH, theRect->h, gl_lightmap_format, GL_UNSIGNED_BYTE,
  606. lightmaps+(i* BLOCK_HEIGHT + theRect->t) *BLOCK_WIDTH*lightmap_bytes);
  607. theRect->l = BLOCK_WIDTH;
  608. theRect->t = BLOCK_HEIGHT;
  609. theRect->h = 0;
  610. theRect->w = 0;
  611. }
  612. for ( ; p ; p=p->chain)
  613. {
  614. // if (p->flags & SURF_UNDERWATER)
  615. // DrawGLWaterPolyLightmap (p);
  616. if (((r_viewleaf->contents==CONTENTS_EMPTY && (p->flags & SURF_UNDERWATER)) ||
  617. (r_viewleaf->contents!=CONTENTS_EMPTY && !(p->flags & SURF_UNDERWATER)))
  618. && !(p->flags & SURF_DONTWARP))
  619. DrawGLWaterPolyLightmap (p);
  620. else
  621. {
  622. glBegin (GL_POLYGON);
  623. v = p->verts[0];
  624. for (j=0 ; j<p->numverts ; j++, v+= VERTEXSIZE)
  625. {
  626. glTexCoord2f (v[5], v[6]);
  627. glVertex3fv (v);
  628. }
  629. glEnd ();
  630. }
  631. }
  632. }
  633. glDisable (GL_BLEND);
  634. if (gl_lightmap_format == GL_LUMINANCE)
  635. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  636. else if (gl_lightmap_format == GL_INTENSITY)
  637. {
  638. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  639. glColor4f (1,1,1,1);
  640. }
  641. glDepthMask (1); // back to normal Z buffering
  642. }
  643. /*
  644. ================
  645. R_RenderBrushPoly
  646. ================
  647. */
  648. void R_RenderBrushPoly (msurface_t *fa)
  649. {
  650. texture_t *t;
  651. byte *base;
  652. int maps;
  653. glRect_t *theRect;
  654. int smax, tmax;
  655. c_brush_polys++;
  656. if (fa->flags & SURF_DRAWSKY)
  657. { // warp texture, no lightmaps
  658. EmitBothSkyLayers (fa);
  659. return;
  660. }
  661. t = R_TextureAnimation (fa->texinfo->texture);
  662. GL_Bind (t->gl_texturenum);
  663. if (fa->flags & SURF_DRAWTURB)
  664. { // warp texture, no lightmaps
  665. EmitWaterPolys (fa);
  666. return;
  667. }
  668. if (((r_viewleaf->contents==CONTENTS_EMPTY && (fa->flags & SURF_UNDERWATER)) ||
  669. (r_viewleaf->contents!=CONTENTS_EMPTY && !(fa->flags & SURF_UNDERWATER)))
  670. && !(fa->flags & SURF_DONTWARP))
  671. DrawGLWaterPoly (fa->polys);
  672. else
  673. DrawGLPoly (fa->polys);
  674. // add the poly to the proper lightmap chain
  675. fa->polys->chain = lightmap_polys[fa->lightmaptexturenum];
  676. lightmap_polys[fa->lightmaptexturenum] = fa->polys;
  677. // check for lightmap modification
  678. for (maps = 0 ; maps < MAXLIGHTMAPS && fa->styles[maps] != 255 ;
  679. maps++)
  680. if (d_lightstylevalue[fa->styles[maps]] != fa->cached_light[maps])
  681. goto dynamic;
  682. if (fa->dlightframe == r_framecount // dynamic this frame
  683. || fa->cached_dlight) // dynamic previously
  684. {
  685. dynamic:
  686. if (r_dynamic.value)
  687. {
  688. lightmap_modified[fa->lightmaptexturenum] = true;
  689. theRect = &lightmap_rectchange[fa->lightmaptexturenum];
  690. if (fa->light_t < theRect->t) {
  691. if (theRect->h)
  692. theRect->h += theRect->t - fa->light_t;
  693. theRect->t = fa->light_t;
  694. }
  695. if (fa->light_s < theRect->l) {
  696. if (theRect->w)
  697. theRect->w += theRect->l - fa->light_s;
  698. theRect->l = fa->light_s;
  699. }
  700. smax = (fa->extents[0]>>4)+1;
  701. tmax = (fa->extents[1]>>4)+1;
  702. if ((theRect->w + theRect->l) < (fa->light_s + smax))
  703. theRect->w = (fa->light_s-theRect->l)+smax;
  704. if ((theRect->h + theRect->t) < (fa->light_t + tmax))
  705. theRect->h = (fa->light_t-theRect->t)+tmax;
  706. base = lightmaps + fa->lightmaptexturenum*lightmap_bytes*BLOCK_WIDTH*BLOCK_HEIGHT;
  707. base += fa->light_t * BLOCK_WIDTH * lightmap_bytes + fa->light_s * lightmap_bytes;
  708. R_BuildLightMap (fa, base, BLOCK_WIDTH*lightmap_bytes);
  709. }
  710. }
  711. }
  712. /*
  713. ================
  714. R_RenderDynamicLightmaps
  715. Multitexture
  716. ================
  717. */
  718. void R_RenderDynamicLightmaps (msurface_t *fa)
  719. {
  720. byte *base;
  721. int maps;
  722. glRect_t *theRect;
  723. int smax, tmax;
  724. c_brush_polys++;
  725. if (fa->flags & ( SURF_DRAWSKY | SURF_DRAWTURB) )
  726. return;
  727. fa->polys->chain = lightmap_polys[fa->lightmaptexturenum];
  728. lightmap_polys[fa->lightmaptexturenum] = fa->polys;
  729. // check for lightmap modification
  730. for (maps = 0 ; maps < MAXLIGHTMAPS && fa->styles[maps] != 255 ;
  731. maps++)
  732. if (d_lightstylevalue[fa->styles[maps]] != fa->cached_light[maps])
  733. goto dynamic;
  734. if (fa->dlightframe == r_framecount // dynamic this frame
  735. || fa->cached_dlight) // dynamic previously
  736. {
  737. dynamic:
  738. if (r_dynamic.value)
  739. {
  740. lightmap_modified[fa->lightmaptexturenum] = true;
  741. theRect = &lightmap_rectchange[fa->lightmaptexturenum];
  742. if (fa->light_t < theRect->t) {
  743. if (theRect->h)
  744. theRect->h += theRect->t - fa->light_t;
  745. theRect->t = fa->light_t;
  746. }
  747. if (fa->light_s < theRect->l) {
  748. if (theRect->w)
  749. theRect->w += theRect->l - fa->light_s;
  750. theRect->l = fa->light_s;
  751. }
  752. smax = (fa->extents[0]>>4)+1;
  753. tmax = (fa->extents[1]>>4)+1;
  754. if ((theRect->w + theRect->l) < (fa->light_s + smax))
  755. theRect->w = (fa->light_s-theRect->l)+smax;
  756. if ((theRect->h + theRect->t) < (fa->light_t + tmax))
  757. theRect->h = (fa->light_t-theRect->t)+tmax;
  758. base = lightmaps + fa->lightmaptexturenum*lightmap_bytes*BLOCK_WIDTH*BLOCK_HEIGHT;
  759. base += fa->light_t * BLOCK_WIDTH * lightmap_bytes + fa->light_s * lightmap_bytes;
  760. R_BuildLightMap (fa, base, BLOCK_WIDTH*lightmap_bytes);
  761. }
  762. }
  763. }
  764. /*
  765. ================
  766. R_MirrorChain
  767. ================
  768. */
  769. void R_MirrorChain (msurface_t *s)
  770. {
  771. if (mirror)
  772. return;
  773. mirror = true;
  774. mirror_plane = s->plane;
  775. }
  776. #if 0
  777. /*
  778. ================
  779. R_DrawWaterSurfaces
  780. ================
  781. */
  782. void R_DrawWaterSurfaces (void)
  783. {
  784. int i;
  785. msurface_t *s;
  786. texture_t *t;
  787. if (r_wateralpha.value == 1.0)
  788. return;
  789. //
  790. // go back to the world matrix
  791. //
  792. glLoadMatrixf (r_world_matrix);
  793. glEnable (GL_BLEND);
  794. glColor4f (1,1,1,r_wateralpha.value);
  795. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  796. for (i=0 ; i<cl.worldmodel->numtextures ; i++)
  797. {
  798. t = cl.worldmodel->textures[i];
  799. if (!t)
  800. continue;
  801. s = t->texturechain;
  802. if (!s)
  803. continue;
  804. if ( !(s->flags & SURF_DRAWTURB) )
  805. continue;
  806. // set modulate mode explicitly
  807. GL_Bind (t->gl_texturenum);
  808. for ( ; s ; s=s->texturechain)
  809. R_RenderBrushPoly (s);
  810. t->texturechain = NULL;
  811. }
  812. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  813. glColor4f (1,1,1,1);
  814. glDisable (GL_BLEND);
  815. }
  816. #else
  817. /*
  818. ================
  819. R_DrawWaterSurfaces
  820. ================
  821. */
  822. void R_DrawWaterSurfaces (void)
  823. {
  824. int i;
  825. msurface_t *s;
  826. texture_t *t;
  827. if (r_wateralpha.value == 1.0 && gl_texsort.value)
  828. return;
  829. //
  830. // go back to the world matrix
  831. //
  832. glLoadMatrixf (r_world_matrix);
  833. if (r_wateralpha.value < 1.0) {
  834. glEnable (GL_BLEND);
  835. glColor4f (1,1,1,r_wateralpha.value);
  836. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  837. }
  838. if (!gl_texsort.value) {
  839. if (!waterchain)
  840. return;
  841. for ( s = waterchain ; s ; s=s->texturechain) {
  842. GL_Bind (s->texinfo->texture->gl_texturenum);
  843. EmitWaterPolys (s);
  844. }
  845. waterchain = NULL;
  846. } else {
  847. for (i=0 ; i<cl.worldmodel->numtextures ; i++)
  848. {
  849. t = cl.worldmodel->textures[i];
  850. if (!t)
  851. continue;
  852. s = t->texturechain;
  853. if (!s)
  854. continue;
  855. if ( !(s->flags & SURF_DRAWTURB ) )
  856. continue;
  857. // set modulate mode explicitly
  858. GL_Bind (t->gl_texturenum);
  859. for ( ; s ; s=s->texturechain)
  860. EmitWaterPolys (s);
  861. t->texturechain = NULL;
  862. }
  863. }
  864. if (r_wateralpha.value < 1.0) {
  865. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  866. glColor4f (1,1,1,1);
  867. glDisable (GL_BLEND);
  868. }
  869. }
  870. #endif
  871. /*
  872. ================
  873. DrawTextureChains
  874. ================
  875. */
  876. void DrawTextureChains (void)
  877. {
  878. int i;
  879. msurface_t *s;
  880. texture_t *t;
  881. if (!gl_texsort.value) {
  882. GL_DisableMultitexture();
  883. if (skychain) {
  884. R_DrawSkyChain(skychain);
  885. skychain = NULL;
  886. }
  887. return;
  888. }
  889. for (i=0 ; i<cl.worldmodel->numtextures ; i++)
  890. {
  891. t = cl.worldmodel->textures[i];
  892. if (!t)
  893. continue;
  894. s = t->texturechain;
  895. if (!s)
  896. continue;
  897. if (i == skytexturenum)
  898. R_DrawSkyChain (s);
  899. else if (i == mirrortexturenum && r_mirroralpha.value != 1.0)
  900. {
  901. R_MirrorChain (s);
  902. continue;
  903. }
  904. else
  905. {
  906. if ((s->flags & SURF_DRAWTURB) && r_wateralpha.value != 1.0)
  907. continue; // draw translucent water later
  908. for ( ; s ; s=s->texturechain)
  909. R_RenderBrushPoly (s);
  910. }
  911. t->texturechain = NULL;
  912. }
  913. }
  914. /*
  915. =================
  916. R_DrawBrushModel
  917. =================
  918. */
  919. void R_DrawBrushModel (entity_t *e)
  920. {
  921. int i;
  922. int k;
  923. vec3_t mins, maxs;
  924. msurface_t *psurf;
  925. float dot;
  926. mplane_t *pplane;
  927. model_t *clmodel;
  928. qboolean rotated;
  929. currententity = e;
  930. currenttexture = -1;
  931. clmodel = e->model;
  932. if (e->angles[0] || e->angles[1] || e->angles[2])
  933. {
  934. rotated = true;
  935. for (i=0 ; i<3 ; i++)
  936. {
  937. mins[i] = e->origin[i] - clmodel->radius;
  938. maxs[i] = e->origin[i] + clmodel->radius;
  939. }
  940. }
  941. else
  942. {
  943. rotated = false;
  944. VectorAdd (e->origin, clmodel->mins, mins);
  945. VectorAdd (e->origin, clmodel->maxs, maxs);
  946. }
  947. if (R_CullBox (mins, maxs))
  948. return;
  949. glColor3f (1,1,1);
  950. memset (lightmap_polys, 0, sizeof(lightmap_polys));
  951. VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
  952. if (rotated)
  953. {
  954. vec3_t temp;
  955. vec3_t forward, right, up;
  956. VectorCopy (modelorg, temp);
  957. AngleVectors (e->angles, forward, right, up);
  958. modelorg[0] = DotProduct (temp, forward);
  959. modelorg[1] = -DotProduct (temp, right);
  960. modelorg[2] = DotProduct (temp, up);
  961. }
  962. psurf = &clmodel->surfaces[clmodel->firstmodelsurface];
  963. // calculate dynamic lighting for bmodel if it's not an
  964. // instanced model
  965. if (clmodel->firstmodelsurface != 0 && !gl_flashblend.value)
  966. {
  967. for (k=0 ; k<MAX_DLIGHTS ; k++)
  968. {
  969. if ((cl_dlights[k].die < cl.time) ||
  970. (!cl_dlights[k].radius))
  971. continue;
  972. R_MarkLights (&cl_dlights[k], 1<<k,
  973. clmodel->nodes + clmodel->hulls[0].firstclipnode);
  974. }
  975. }
  976. glPushMatrix ();
  977. e->angles[0] = -e->angles[0]; // stupid quake bug
  978. R_RotateForEntity (e);
  979. e->angles[0] = -e->angles[0]; // stupid quake bug
  980. //
  981. // draw texture
  982. //
  983. for (i=0 ; i<clmodel->nummodelsurfaces ; i++, psurf++)
  984. {
  985. // find which side of the node we are on
  986. pplane = psurf->plane;
  987. dot = DotProduct (modelorg, pplane->normal) - pplane->dist;
  988. // draw the polygon
  989. if (((psurf->flags & SURF_PLANEBACK) && (dot < -BACKFACE_EPSILON)) ||
  990. (!(psurf->flags & SURF_PLANEBACK) && (dot > BACKFACE_EPSILON)))
  991. {
  992. if (gl_texsort.value)
  993. R_RenderBrushPoly (psurf);
  994. else
  995. R_DrawSequentialPoly (psurf);
  996. }
  997. }
  998. R_BlendLightmaps ();
  999. glPopMatrix ();
  1000. }
  1001. /*
  1002. =============================================================
  1003. WORLD MODEL
  1004. =============================================================
  1005. */
  1006. /*
  1007. ================
  1008. R_RecursiveWorldNode
  1009. ================
  1010. */
  1011. void R_RecursiveWorldNode (mnode_t *node)
  1012. {
  1013. int c, side;
  1014. mplane_t *plane;
  1015. msurface_t *surf, **mark;
  1016. mleaf_t *pleaf;
  1017. double dot;
  1018. if (node->contents == CONTENTS_SOLID)
  1019. return; // solid
  1020. if (node->visframe != r_visframecount)
  1021. return;
  1022. if (R_CullBox (node->minmaxs, node->minmaxs+3))
  1023. return;
  1024. // if a leaf node, draw stuff
  1025. if (node->contents < 0)
  1026. {
  1027. pleaf = (mleaf_t *)node;
  1028. mark = pleaf->firstmarksurface;
  1029. c = pleaf->nummarksurfaces;
  1030. if (c)
  1031. {
  1032. do
  1033. {
  1034. (*mark)->visframe = r_framecount;
  1035. mark++;
  1036. } while (--c);
  1037. }
  1038. // deal with model fragments in this leaf
  1039. if (pleaf->efrags)
  1040. R_StoreEfrags (&pleaf->efrags);
  1041. return;
  1042. }
  1043. // node is just a decision point, so go down the apropriate sides
  1044. // find which side of the node we are on
  1045. plane = node->plane;
  1046. switch (plane->type)
  1047. {
  1048. case PLANE_X:
  1049. dot = modelorg[0] - plane->dist;
  1050. break;
  1051. case PLANE_Y:
  1052. dot = modelorg[1] - plane->dist;
  1053. break;
  1054. case PLANE_Z:
  1055. dot = modelorg[2] - plane->dist;
  1056. break;
  1057. default:
  1058. dot = DotProduct (modelorg, plane->normal) - plane->dist;
  1059. break;
  1060. }
  1061. if (dot >= 0)
  1062. side = 0;
  1063. else
  1064. side = 1;
  1065. // recurse down the children, front side first
  1066. R_RecursiveWorldNode (node->children[side]);
  1067. // draw stuff
  1068. c = node->numsurfaces;
  1069. if (c)
  1070. {
  1071. surf = cl.worldmodel->surfaces + node->firstsurface;
  1072. if (dot < 0 -BACKFACE_EPSILON)
  1073. side = SURF_PLANEBACK;
  1074. else if (dot > BACKFACE_EPSILON)
  1075. side = 0;
  1076. {
  1077. for ( ; c ; c--, surf++)
  1078. {
  1079. if (surf->visframe != r_framecount)
  1080. continue;
  1081. // don't backface underwater surfaces, because they warp
  1082. // if ( !(surf->flags & SURF_UNDERWATER) && ( (dot < 0) ^ !!(surf->flags & SURF_PLANEBACK)) )
  1083. // continue; // wrong side
  1084. if ( !(((r_viewleaf->contents==CONTENTS_EMPTY && (surf->flags & SURF_UNDERWATER)) ||
  1085. (r_viewleaf->contents!=CONTENTS_EMPTY && !(surf->flags & SURF_UNDERWATER)))
  1086. && !(surf->flags & SURF_DONTWARP)) && ( (dot < 0) ^ !!(surf->flags & SURF_PLANEBACK)) )
  1087. continue; // wrong side
  1088. // if sorting by texture, just store it out
  1089. if (gl_texsort.value)
  1090. {
  1091. if (!mirror
  1092. || surf->texinfo->texture != cl.worldmodel->textures[mirrortexturenum])
  1093. {
  1094. surf->texturechain = surf->texinfo->texture->texturechain;
  1095. surf->texinfo->texture->texturechain = surf;
  1096. }
  1097. } else if (surf->flags & SURF_DRAWSKY) {
  1098. surf->texturechain = skychain;
  1099. skychain = surf;
  1100. } else if (surf->flags & SURF_DRAWTURB) {
  1101. surf->texturechain = waterchain;
  1102. waterchain = surf;
  1103. } else
  1104. R_DrawSequentialPoly (surf);
  1105. }
  1106. }
  1107. }
  1108. // recurse down the back side
  1109. R_RecursiveWorldNode (node->children[!side]);
  1110. }
  1111. /*
  1112. =============
  1113. R_DrawWorld
  1114. =============
  1115. */
  1116. void R_DrawWorld (void)
  1117. {
  1118. entity_t ent;
  1119. memset (&ent, 0, sizeof(ent));
  1120. ent.model = cl.worldmodel;
  1121. VectorCopy (r_refdef.vieworg, modelorg);
  1122. currententity = &ent;
  1123. currenttexture = -1;
  1124. glColor3f (1,1,1);
  1125. memset (lightmap_polys, 0, sizeof(lightmap_polys));
  1126. #ifdef QUAKE2
  1127. R_ClearSkyBox ();
  1128. #endif
  1129. R_RecursiveWorldNode (cl.worldmodel->nodes);
  1130. DrawTextureChains ();
  1131. R_BlendLightmaps ();
  1132. #ifdef QUAKE2
  1133. R_DrawSkyBox ();
  1134. #endif
  1135. }
  1136. /*
  1137. ===============
  1138. R_MarkLeaves
  1139. ===============
  1140. */
  1141. void R_MarkLeaves (void)
  1142. {
  1143. byte *vis;
  1144. mnode_t *node;
  1145. int i;
  1146. byte solid[4096];
  1147. if (r_oldviewleaf == r_viewleaf && !r_novis.value)
  1148. return;
  1149. if (mirror)
  1150. return;
  1151. r_visframecount++;
  1152. r_oldviewleaf = r_viewleaf;
  1153. if (r_novis.value)
  1154. {
  1155. vis = solid;
  1156. memset (solid, 0xff, (cl.worldmodel->numleafs+7)>>3);
  1157. }
  1158. else
  1159. vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
  1160. for (i=0 ; i<cl.worldmodel->numleafs ; i++)
  1161. {
  1162. if (vis[i>>3] & (1<<(i&7)))
  1163. {
  1164. node = (mnode_t *)&cl.worldmodel->leafs[i+1];
  1165. do
  1166. {
  1167. if (node->visframe == r_visframecount)
  1168. break;
  1169. node->visframe = r_visframecount;
  1170. node = node->parent;
  1171. } while (node);
  1172. }
  1173. }
  1174. }
  1175. /*
  1176. =============================================================================
  1177. LIGHTMAP ALLOCATION
  1178. =============================================================================
  1179. */
  1180. // returns a texture number and the position inside it
  1181. int AllocBlock (int w, int h, int *x, int *y)
  1182. {
  1183. int i, j;
  1184. int best, best2;
  1185. int texnum;
  1186. for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
  1187. {
  1188. best = BLOCK_HEIGHT;
  1189. for (i=0 ; i<BLOCK_WIDTH-w ; i++)
  1190. {
  1191. best2 = 0;
  1192. for (j=0 ; j<w ; j++)
  1193. {
  1194. if (allocated[texnum][i+j] >= best)
  1195. break;
  1196. if (allocated[texnum][i+j] > best2)
  1197. best2 = allocated[texnum][i+j];
  1198. }
  1199. if (j == w)
  1200. { // this is a valid spot
  1201. *x = i;
  1202. *y = best = best2;
  1203. }
  1204. }
  1205. if (best + h > BLOCK_HEIGHT)
  1206. continue;
  1207. for (i=0 ; i<w ; i++)
  1208. allocated[texnum][*x + i] = best + h;
  1209. return texnum;
  1210. }
  1211. Sys_Error ("AllocBlock: full");
  1212. return 0;
  1213. }
  1214. mvertex_t *r_pcurrentvertbase;
  1215. model_t *currentmodel;
  1216. int nColinElim;
  1217. /*
  1218. ================
  1219. BuildSurfaceDisplayList
  1220. ================
  1221. */
  1222. void BuildSurfaceDisplayList (msurface_t *fa)
  1223. {
  1224. int i, lindex, lnumverts;
  1225. medge_t *pedges, *r_pedge;
  1226. int vertpage;
  1227. float *vec;
  1228. float s, t;
  1229. glpoly_t *poly;
  1230. // reconstruct the polygon
  1231. pedges = currentmodel->edges;
  1232. lnumverts = fa->numedges;
  1233. vertpage = 0;
  1234. //
  1235. // draw texture
  1236. //
  1237. poly = Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float));
  1238. poly->next = fa->polys;
  1239. poly->flags = fa->flags;
  1240. fa->polys = poly;
  1241. poly->numverts = lnumverts;
  1242. for (i=0 ; i<lnumverts ; i++)
  1243. {
  1244. lindex = currentmodel->surfedges[fa->firstedge + i];
  1245. if (lindex > 0)
  1246. {
  1247. r_pedge = &pedges[lindex];
  1248. vec = r_pcurrentvertbase[r_pedge->v[0]].position;
  1249. }
  1250. else
  1251. {
  1252. r_pedge = &pedges[-lindex];
  1253. vec = r_pcurrentvertbase[r_pedge->v[1]].position;
  1254. }
  1255. s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
  1256. s /= fa->texinfo->texture->width;
  1257. t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
  1258. t /= fa->texinfo->texture->height;
  1259. VectorCopy (vec, poly->verts[i]);
  1260. poly->verts[i][3] = s;
  1261. poly->verts[i][4] = t;
  1262. //
  1263. // lightmap texture coordinates
  1264. //
  1265. s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
  1266. s -= fa->texturemins[0];
  1267. s += fa->light_s*16;
  1268. s += 8;
  1269. s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
  1270. t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
  1271. t -= fa->texturemins[1];
  1272. t += fa->light_t*16;
  1273. t += 8;
  1274. t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
  1275. poly->verts[i][5] = s;
  1276. poly->verts[i][6] = t;
  1277. }
  1278. //
  1279. // remove co-linear points - Ed
  1280. //
  1281. if (!gl_keeptjunctions.value && !(fa->flags & SURF_UNDERWATER) )
  1282. {
  1283. for (i = 0 ; i < lnumverts ; ++i)
  1284. {
  1285. vec3_t v1, v2;
  1286. float *prev, *this, *next;
  1287. prev = poly->verts[(i + lnumverts - 1) % lnumverts];
  1288. this = poly->verts[i];
  1289. next = poly->verts[(i + 1) % lnumverts];
  1290. VectorSubtract( this, prev, v1 );
  1291. VectorNormalize( v1 );
  1292. VectorSubtract( next, prev, v2 );
  1293. VectorNormalize( v2 );
  1294. // skip co-linear points
  1295. #define COLINEAR_EPSILON 0.001
  1296. if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
  1297. (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
  1298. (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
  1299. {
  1300. int j;
  1301. for (j = i + 1; j < lnumverts; ++j)
  1302. {
  1303. int k;
  1304. for (k = 0; k < VERTEXSIZE; ++k)
  1305. poly->verts[j - 1][k] = poly->verts[j][k];
  1306. }
  1307. --lnumverts;
  1308. ++nColinElim;
  1309. // retry next vertex next time, which is now current vertex
  1310. --i;
  1311. }
  1312. }
  1313. }
  1314. poly->numverts = lnumverts;
  1315. }
  1316. /*
  1317. ========================
  1318. GL_CreateSurfaceLightmap
  1319. ========================
  1320. */
  1321. void GL_CreateSurfaceLightmap (msurface_t *surf)
  1322. {
  1323. int smax, tmax;
  1324. byte *base;
  1325. if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
  1326. return;
  1327. smax = (surf->extents[0]>>4)+1;
  1328. tmax = (surf->extents[1]>>4)+1;
  1329. surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
  1330. base = lightmaps + surf->lightmaptexturenum*lightmap_bytes*BLOCK_WIDTH*BLOCK_HEIGHT;
  1331. base += (surf->light_t * BLOCK_WIDTH + surf->light_s) * lightmap_bytes;
  1332. R_BuildLightMap (surf, base, BLOCK_WIDTH*lightmap_bytes);
  1333. }
  1334. /*
  1335. ==================
  1336. GL_BuildLightmaps
  1337. Builds the lightmap texture
  1338. with all the surfaces from all brush models
  1339. ==================
  1340. */
  1341. void GL_BuildLightmaps (void)
  1342. {
  1343. int i, j;
  1344. model_t *m;
  1345. memset (allocated, 0, sizeof(allocated));
  1346. r_framecount = 1; // no dlightcache
  1347. if (!lightmap_textures)
  1348. {
  1349. lightmap_textures = texture_extension_number;
  1350. texture_extension_number += MAX_LIGHTMAPS;
  1351. }
  1352. gl_lightmap_format = GL_LUMINANCE;
  1353. if (COM_CheckParm ("-lm_1"))
  1354. gl_lightmap_format = GL_LUMINANCE;
  1355. if (COM_CheckParm ("-lm_a"))
  1356. gl_lightmap_format = GL_ALPHA;
  1357. if (COM_CheckParm ("-lm_i"))
  1358. gl_lightmap_format = GL_INTENSITY;
  1359. if (COM_CheckParm ("-lm_2"))
  1360. gl_lightmap_format = GL_RGBA4;
  1361. if (COM_CheckParm ("-lm_4"))
  1362. gl_lightmap_format = GL_RGBA;
  1363. switch (gl_lightmap_format)
  1364. {
  1365. case GL_RGBA:
  1366. lightmap_bytes = 4;
  1367. break;
  1368. case GL_RGBA4:
  1369. lightmap_bytes = 2;
  1370. break;
  1371. case GL_LUMINANCE:
  1372. case GL_INTENSITY:
  1373. case GL_ALPHA:
  1374. lightmap_bytes = 1;
  1375. break;
  1376. }
  1377. for (j=1 ; j<MAX_MODELS ; j++)
  1378. {
  1379. m = cl.model_precache[j];
  1380. if (!m)
  1381. break;
  1382. if (m->name[0] == '*')
  1383. continue;
  1384. r_pcurrentvertbase = m->vertexes;
  1385. currentmodel = m;
  1386. for (i=0 ; i<m->numsurfaces ; i++)
  1387. {
  1388. GL_CreateSurfaceLightmap (m->surfaces + i);
  1389. if ( m->surfaces[i].flags & SURF_DRAWTURB )
  1390. continue;
  1391. #ifndef QUAKE2
  1392. if ( m->surfaces[i].flags & SURF_DRAWSKY )
  1393. continue;
  1394. #endif
  1395. BuildSurfaceDisplayList (m->surfaces + i);
  1396. }
  1397. }
  1398. if (!gl_texsort.value)
  1399. GL_SelectTexture(TEXTURE1_SGIS);
  1400. //
  1401. // upload all lightmaps that were filled
  1402. //
  1403. for (i=0 ; i<MAX_LIGHTMAPS ; i++)
  1404. {
  1405. if (!allocated[i][0])
  1406. break; // no more used
  1407. lightmap_modified[i] = false;
  1408. lightmap_rectchange[i].l = BLOCK_WIDTH;
  1409. lightmap_rectchange[i].t = BLOCK_HEIGHT;
  1410. lightmap_rectchange[i].w = 0;
  1411. lightmap_rectchange[i].h = 0;
  1412. GL_Bind(lightmap_textures + i);
  1413. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1414. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1415. glTexImage2D (GL_TEXTURE_2D, 0, lightmap_bytes
  1416. , BLOCK_WIDTH, BLOCK_HEIGHT, 0,
  1417. gl_lightmap_format, GL_UNSIGNED_BYTE, lightmaps+i*BLOCK_WIDTH*BLOCK_HEIGHT*lightmap_bytes);
  1418. }
  1419. if (!gl_texsort.value)
  1420. GL_SelectTexture(TEXTURE0_SGIS);
  1421. }