gl_rsurf.c 34 KB

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