gl_rsurf.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // GL_RSURF.C: surface-related refresh code
  16. #include <assert.h>
  17. #include "gl_local.h"
  18. static vec3_t modelorg; // relative to viewpoint
  19. msurface_t *r_alpha_surfaces;
  20. #define DYNAMIC_LIGHT_WIDTH 128
  21. #define DYNAMIC_LIGHT_HEIGHT 128
  22. #define LIGHTMAP_BYTES 4
  23. #define BLOCK_WIDTH 128
  24. #define BLOCK_HEIGHT 128
  25. #define MAX_LIGHTMAPS 128
  26. int c_visible_lightmaps;
  27. int c_visible_textures;
  28. #define GL_LIGHTMAP_FORMAT GL_RGBA
  29. typedef struct
  30. {
  31. int internal_format;
  32. int current_lightmap_texture;
  33. msurface_t *lightmap_surfaces[MAX_LIGHTMAPS];
  34. int allocated[BLOCK_WIDTH];
  35. // the lightmap texture data needs to be kept in
  36. // main memory so texsubimage can update properly
  37. byte lightmap_buffer[4*BLOCK_WIDTH*BLOCK_HEIGHT];
  38. } gllightmapstate_t;
  39. static gllightmapstate_t gl_lms;
  40. static void LM_InitBlock( void );
  41. static void LM_UploadBlock( qboolean dynamic );
  42. static qboolean LM_AllocBlock (int w, int h, int *x, int *y);
  43. extern void R_SetCacheState( msurface_t *surf );
  44. extern void R_BuildLightMap (msurface_t *surf, byte *dest, int stride);
  45. /*
  46. =============================================================
  47. BRUSH MODELS
  48. =============================================================
  49. */
  50. /*
  51. ===============
  52. R_TextureAnimation
  53. Returns the proper texture for a given time and base texture
  54. ===============
  55. */
  56. image_t *R_TextureAnimation (mtexinfo_t *tex)
  57. {
  58. int c;
  59. if (!tex->next)
  60. return tex->image;
  61. c = currententity->frame % tex->numframes;
  62. while (c)
  63. {
  64. tex = tex->next;
  65. c--;
  66. }
  67. return tex->image;
  68. }
  69. #if 0
  70. /*
  71. =================
  72. WaterWarpPolyVerts
  73. Mangles the x and y coordinates in a copy of the poly
  74. so that any drawing routine can be water warped
  75. =================
  76. */
  77. glpoly_t *WaterWarpPolyVerts (glpoly_t *p)
  78. {
  79. int i;
  80. float *v, *nv;
  81. static byte buffer[1024];
  82. glpoly_t *out;
  83. out = (glpoly_t *)buffer;
  84. out->numverts = p->numverts;
  85. v = p->verts[0];
  86. nv = out->verts[0];
  87. for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE, nv+=VERTEXSIZE)
  88. {
  89. nv[0] = v[0] + 4*sin(v[1]*0.05+r_newrefdef.time)*sin(v[2]*0.05+r_newrefdef.time);
  90. nv[1] = v[1] + 4*sin(v[0]*0.05+r_newrefdef.time)*sin(v[2]*0.05+r_newrefdef.time);
  91. nv[2] = v[2];
  92. nv[3] = v[3];
  93. nv[4] = v[4];
  94. nv[5] = v[5];
  95. nv[6] = v[6];
  96. }
  97. return out;
  98. }
  99. /*
  100. ================
  101. DrawGLWaterPoly
  102. Warp the vertex coordinates
  103. ================
  104. */
  105. void DrawGLWaterPoly (glpoly_t *p)
  106. {
  107. int i;
  108. float *v;
  109. p = WaterWarpPolyVerts (p);
  110. qglBegin (GL_TRIANGLE_FAN);
  111. v = p->verts[0];
  112. for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
  113. {
  114. qglTexCoord2f (v[3], v[4]);
  115. qglVertex3fv (v);
  116. }
  117. qglEnd ();
  118. }
  119. void DrawGLWaterPolyLightmap (glpoly_t *p)
  120. {
  121. int i;
  122. float *v;
  123. p = WaterWarpPolyVerts (p);
  124. qglBegin (GL_TRIANGLE_FAN);
  125. v = p->verts[0];
  126. for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
  127. {
  128. qglTexCoord2f (v[5], v[6]);
  129. qglVertex3fv (v);
  130. }
  131. qglEnd ();
  132. }
  133. #endif
  134. /*
  135. ================
  136. DrawGLPoly
  137. ================
  138. */
  139. void DrawGLPoly (glpoly_t *p)
  140. {
  141. int i;
  142. float *v;
  143. qglBegin (GL_POLYGON);
  144. v = p->verts[0];
  145. for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
  146. {
  147. qglTexCoord2f (v[3], v[4]);
  148. qglVertex3fv (v);
  149. }
  150. qglEnd ();
  151. }
  152. //============
  153. //PGM
  154. /*
  155. ================
  156. DrawGLFlowingPoly -- version of DrawGLPoly that handles scrolling texture
  157. ================
  158. */
  159. void DrawGLFlowingPoly (msurface_t *fa)
  160. {
  161. int i;
  162. float *v;
  163. glpoly_t *p;
  164. float scroll;
  165. p = fa->polys;
  166. scroll = -64 * ( (r_newrefdef.time / 40.0) - (int)(r_newrefdef.time / 40.0) );
  167. if(scroll == 0.0)
  168. scroll = -64.0;
  169. qglBegin (GL_POLYGON);
  170. v = p->verts[0];
  171. for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
  172. {
  173. qglTexCoord2f ((v[3] + scroll), v[4]);
  174. qglVertex3fv (v);
  175. }
  176. qglEnd ();
  177. }
  178. //PGM
  179. //============
  180. /*
  181. ** R_DrawTriangleOutlines
  182. */
  183. void R_DrawTriangleOutlines (void)
  184. {
  185. int i, j;
  186. glpoly_t *p;
  187. if (!gl_showtris->value)
  188. return;
  189. qglDisable (GL_TEXTURE_2D);
  190. qglDisable (GL_DEPTH_TEST);
  191. qglColor4f (1,1,1,1);
  192. for (i=0 ; i<MAX_LIGHTMAPS ; i++)
  193. {
  194. msurface_t *surf;
  195. for ( surf = gl_lms.lightmap_surfaces[i]; surf != 0; surf = surf->lightmapchain )
  196. {
  197. p = surf->polys;
  198. for ( ; p ; p=p->chain)
  199. {
  200. for (j=2 ; j<p->numverts ; j++ )
  201. {
  202. qglBegin (GL_LINE_STRIP);
  203. qglVertex3fv (p->verts[0]);
  204. qglVertex3fv (p->verts[j-1]);
  205. qglVertex3fv (p->verts[j]);
  206. qglVertex3fv (p->verts[0]);
  207. qglEnd ();
  208. }
  209. }
  210. }
  211. }
  212. qglEnable (GL_DEPTH_TEST);
  213. qglEnable (GL_TEXTURE_2D);
  214. }
  215. /*
  216. ** DrawGLPolyChain
  217. */
  218. void DrawGLPolyChain( glpoly_t *p, float soffset, float toffset )
  219. {
  220. if ( soffset == 0 && toffset == 0 )
  221. {
  222. for ( ; p != 0; p = p->chain )
  223. {
  224. float *v;
  225. int j;
  226. qglBegin (GL_POLYGON);
  227. v = p->verts[0];
  228. for (j=0 ; j<p->numverts ; j++, v+= VERTEXSIZE)
  229. {
  230. qglTexCoord2f (v[5], v[6] );
  231. qglVertex3fv (v);
  232. }
  233. qglEnd ();
  234. }
  235. }
  236. else
  237. {
  238. for ( ; p != 0; p = p->chain )
  239. {
  240. float *v;
  241. int j;
  242. qglBegin (GL_POLYGON);
  243. v = p->verts[0];
  244. for (j=0 ; j<p->numverts ; j++, v+= VERTEXSIZE)
  245. {
  246. qglTexCoord2f (v[5] - soffset, v[6] - toffset );
  247. qglVertex3fv (v);
  248. }
  249. qglEnd ();
  250. }
  251. }
  252. }
  253. /*
  254. ** R_BlendLightMaps
  255. **
  256. ** This routine takes all the given light mapped surfaces in the world and
  257. ** blends them into the framebuffer.
  258. */
  259. void R_BlendLightmaps (void)
  260. {
  261. int i;
  262. msurface_t *surf, *newdrawsurf = 0;
  263. // don't bother if we're set to fullbright
  264. if (r_fullbright->value)
  265. return;
  266. if (!r_worldmodel->lightdata)
  267. return;
  268. // don't bother writing Z
  269. qglDepthMask( 0 );
  270. /*
  271. ** set the appropriate blending mode unless we're only looking at the
  272. ** lightmaps.
  273. */
  274. if (!gl_lightmap->value)
  275. {
  276. qglEnable (GL_BLEND);
  277. if ( gl_saturatelighting->value )
  278. {
  279. qglBlendFunc( GL_ONE, GL_ONE );
  280. }
  281. else
  282. {
  283. if ( gl_monolightmap->string[0] != '0' )
  284. {
  285. switch ( toupper( gl_monolightmap->string[0] ) )
  286. {
  287. case 'I':
  288. qglBlendFunc (GL_ZERO, GL_SRC_COLOR );
  289. break;
  290. case 'L':
  291. qglBlendFunc (GL_ZERO, GL_SRC_COLOR );
  292. break;
  293. case 'A':
  294. default:
  295. qglBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  296. break;
  297. }
  298. }
  299. else
  300. {
  301. qglBlendFunc (GL_ZERO, GL_SRC_COLOR );
  302. }
  303. }
  304. }
  305. if ( currentmodel == r_worldmodel )
  306. c_visible_lightmaps = 0;
  307. /*
  308. ** render static lightmaps first
  309. */
  310. for ( i = 1; i < MAX_LIGHTMAPS; i++ )
  311. {
  312. if ( gl_lms.lightmap_surfaces[i] )
  313. {
  314. if (currentmodel == r_worldmodel)
  315. c_visible_lightmaps++;
  316. GL_Bind( gl_state.lightmap_textures + i);
  317. for ( surf = gl_lms.lightmap_surfaces[i]; surf != 0; surf = surf->lightmapchain )
  318. {
  319. if ( surf->polys )
  320. DrawGLPolyChain( surf->polys, 0, 0 );
  321. }
  322. }
  323. }
  324. /*
  325. ** render dynamic lightmaps
  326. */
  327. if ( gl_dynamic->value )
  328. {
  329. LM_InitBlock();
  330. GL_Bind( gl_state.lightmap_textures+0 );
  331. if (currentmodel == r_worldmodel)
  332. c_visible_lightmaps++;
  333. newdrawsurf = gl_lms.lightmap_surfaces[0];
  334. for ( surf = gl_lms.lightmap_surfaces[0]; surf != 0; surf = surf->lightmapchain )
  335. {
  336. int smax, tmax;
  337. byte *base;
  338. smax = (surf->extents[0]>>4)+1;
  339. tmax = (surf->extents[1]>>4)+1;
  340. if ( LM_AllocBlock( smax, tmax, &surf->dlight_s, &surf->dlight_t ) )
  341. {
  342. base = gl_lms.lightmap_buffer;
  343. base += ( surf->dlight_t * BLOCK_WIDTH + surf->dlight_s ) * LIGHTMAP_BYTES;
  344. R_BuildLightMap (surf, base, BLOCK_WIDTH*LIGHTMAP_BYTES);
  345. }
  346. else
  347. {
  348. msurface_t *drawsurf;
  349. // upload what we have so far
  350. LM_UploadBlock( true );
  351. // draw all surfaces that use this lightmap
  352. for ( drawsurf = newdrawsurf; drawsurf != surf; drawsurf = drawsurf->lightmapchain )
  353. {
  354. if ( drawsurf->polys )
  355. DrawGLPolyChain( drawsurf->polys,
  356. ( drawsurf->light_s - drawsurf->dlight_s ) * ( 1.0 / 128.0 ),
  357. ( drawsurf->light_t - drawsurf->dlight_t ) * ( 1.0 / 128.0 ) );
  358. }
  359. newdrawsurf = drawsurf;
  360. // clear the block
  361. LM_InitBlock();
  362. // try uploading the block now
  363. if ( !LM_AllocBlock( smax, tmax, &surf->dlight_s, &surf->dlight_t ) )
  364. {
  365. ri.Sys_Error( ERR_FATAL, "Consecutive calls to LM_AllocBlock(%d,%d) failed (dynamic)\n", smax, tmax );
  366. }
  367. base = gl_lms.lightmap_buffer;
  368. base += ( surf->dlight_t * BLOCK_WIDTH + surf->dlight_s ) * LIGHTMAP_BYTES;
  369. R_BuildLightMap (surf, base, BLOCK_WIDTH*LIGHTMAP_BYTES);
  370. }
  371. }
  372. /*
  373. ** draw remainder of dynamic lightmaps that haven't been uploaded yet
  374. */
  375. if ( newdrawsurf )
  376. LM_UploadBlock( true );
  377. for ( surf = newdrawsurf; surf != 0; surf = surf->lightmapchain )
  378. {
  379. if ( surf->polys )
  380. DrawGLPolyChain( surf->polys, ( surf->light_s - surf->dlight_s ) * ( 1.0 / 128.0 ), ( surf->light_t - surf->dlight_t ) * ( 1.0 / 128.0 ) );
  381. }
  382. }
  383. /*
  384. ** restore state
  385. */
  386. qglDisable (GL_BLEND);
  387. qglBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  388. qglDepthMask( 1 );
  389. }
  390. /*
  391. ================
  392. R_RenderBrushPoly
  393. ================
  394. */
  395. void R_RenderBrushPoly (msurface_t *fa)
  396. {
  397. int maps;
  398. image_t *image;
  399. qboolean is_dynamic = false;
  400. c_brush_polys++;
  401. image = R_TextureAnimation (fa->texinfo);
  402. if (fa->flags & SURF_DRAWTURB)
  403. {
  404. GL_Bind( image->texnum );
  405. // warp texture, no lightmaps
  406. GL_TexEnv( GL_MODULATE );
  407. qglColor4f( gl_state.inverse_intensity,
  408. gl_state.inverse_intensity,
  409. gl_state.inverse_intensity,
  410. 1.0F );
  411. EmitWaterPolys (fa);
  412. GL_TexEnv( GL_REPLACE );
  413. return;
  414. }
  415. else
  416. {
  417. GL_Bind( image->texnum );
  418. GL_TexEnv( GL_REPLACE );
  419. }
  420. //======
  421. //PGM
  422. if(fa->texinfo->flags & SURF_FLOWING)
  423. DrawGLFlowingPoly (fa);
  424. else
  425. DrawGLPoly (fa->polys);
  426. //PGM
  427. //======
  428. /*
  429. ** check for lightmap modification
  430. */
  431. for ( maps = 0; maps < MAXLIGHTMAPS && fa->styles[maps] != 255; maps++ )
  432. {
  433. if ( r_newrefdef.lightstyles[fa->styles[maps]].white != fa->cached_light[maps] )
  434. goto dynamic;
  435. }
  436. // dynamic this frame or dynamic previously
  437. if ( ( fa->dlightframe == r_framecount ) )
  438. {
  439. dynamic:
  440. if ( gl_dynamic->value )
  441. {
  442. if (!( fa->texinfo->flags & (SURF_SKY|SURF_TRANS33|SURF_TRANS66|SURF_WARP ) ) )
  443. {
  444. is_dynamic = true;
  445. }
  446. }
  447. }
  448. if ( is_dynamic )
  449. {
  450. if ( ( fa->styles[maps] >= 32 || fa->styles[maps] == 0 ) && ( fa->dlightframe != r_framecount ) )
  451. {
  452. unsigned temp[34*34];
  453. int smax, tmax;
  454. smax = (fa->extents[0]>>4)+1;
  455. tmax = (fa->extents[1]>>4)+1;
  456. R_BuildLightMap( fa, (void *)temp, smax*4 );
  457. R_SetCacheState( fa );
  458. GL_Bind( gl_state.lightmap_textures + fa->lightmaptexturenum );
  459. qglTexSubImage2D( GL_TEXTURE_2D, 0,
  460. fa->light_s, fa->light_t,
  461. smax, tmax,
  462. GL_LIGHTMAP_FORMAT,
  463. GL_UNSIGNED_BYTE, temp );
  464. fa->lightmapchain = gl_lms.lightmap_surfaces[fa->lightmaptexturenum];
  465. gl_lms.lightmap_surfaces[fa->lightmaptexturenum] = fa;
  466. }
  467. else
  468. {
  469. fa->lightmapchain = gl_lms.lightmap_surfaces[0];
  470. gl_lms.lightmap_surfaces[0] = fa;
  471. }
  472. }
  473. else
  474. {
  475. fa->lightmapchain = gl_lms.lightmap_surfaces[fa->lightmaptexturenum];
  476. gl_lms.lightmap_surfaces[fa->lightmaptexturenum] = fa;
  477. }
  478. }
  479. /*
  480. ================
  481. R_DrawAlphaSurfaces
  482. Draw water surfaces and windows.
  483. The BSP tree is waled front to back, so unwinding the chain
  484. of alpha_surfaces will draw back to front, giving proper ordering.
  485. ================
  486. */
  487. void R_DrawAlphaSurfaces (void)
  488. {
  489. msurface_t *s;
  490. float intens;
  491. //
  492. // go back to the world matrix
  493. //
  494. qglLoadMatrixf (r_world_matrix);
  495. qglEnable (GL_BLEND);
  496. GL_TexEnv( GL_MODULATE );
  497. // the textures are prescaled up for a better lighting range,
  498. // so scale it back down
  499. intens = gl_state.inverse_intensity;
  500. for (s=r_alpha_surfaces ; s ; s=s->texturechain)
  501. {
  502. GL_Bind(s->texinfo->image->texnum);
  503. c_brush_polys++;
  504. if (s->texinfo->flags & SURF_TRANS33)
  505. qglColor4f (intens,intens,intens,0.33);
  506. else if (s->texinfo->flags & SURF_TRANS66)
  507. qglColor4f (intens,intens,intens,0.66);
  508. else
  509. qglColor4f (intens,intens,intens,1);
  510. if (s->flags & SURF_DRAWTURB)
  511. EmitWaterPolys (s);
  512. else
  513. DrawGLPoly (s->polys);
  514. }
  515. GL_TexEnv( GL_REPLACE );
  516. qglColor4f (1,1,1,1);
  517. qglDisable (GL_BLEND);
  518. r_alpha_surfaces = NULL;
  519. }
  520. /*
  521. ================
  522. DrawTextureChains
  523. ================
  524. */
  525. void DrawTextureChains (void)
  526. {
  527. int i;
  528. msurface_t *s;
  529. image_t *image;
  530. c_visible_textures = 0;
  531. // GL_TexEnv( GL_REPLACE );
  532. if ( !qglSelectTextureSGIS )
  533. {
  534. for ( i = 0, image=gltextures ; i<numgltextures ; i++,image++)
  535. {
  536. if (!image->registration_sequence)
  537. continue;
  538. s = image->texturechain;
  539. if (!s)
  540. continue;
  541. c_visible_textures++;
  542. for ( ; s ; s=s->texturechain)
  543. R_RenderBrushPoly (s);
  544. image->texturechain = NULL;
  545. }
  546. }
  547. else
  548. {
  549. for ( i = 0, image=gltextures ; i<numgltextures ; i++,image++)
  550. {
  551. if (!image->registration_sequence)
  552. continue;
  553. if (!image->texturechain)
  554. continue;
  555. c_visible_textures++;
  556. for ( s = image->texturechain; s ; s=s->texturechain)
  557. {
  558. if ( !( s->flags & SURF_DRAWTURB ) )
  559. R_RenderBrushPoly (s);
  560. }
  561. }
  562. GL_EnableMultitexture( false );
  563. for ( i = 0, image=gltextures ; i<numgltextures ; i++,image++)
  564. {
  565. if (!image->registration_sequence)
  566. continue;
  567. s = image->texturechain;
  568. if (!s)
  569. continue;
  570. for ( ; s ; s=s->texturechain)
  571. {
  572. if ( s->flags & SURF_DRAWTURB )
  573. R_RenderBrushPoly (s);
  574. }
  575. image->texturechain = NULL;
  576. }
  577. // GL_EnableMultitexture( true );
  578. }
  579. GL_TexEnv( GL_REPLACE );
  580. }
  581. static void GL_RenderLightmappedPoly( msurface_t *surf )
  582. {
  583. int i, nv = surf->polys->numverts;
  584. int map;
  585. float *v;
  586. image_t *image = R_TextureAnimation( surf->texinfo );
  587. qboolean is_dynamic = false;
  588. unsigned lmtex = surf->lightmaptexturenum;
  589. glpoly_t *p;
  590. for ( map = 0; map < MAXLIGHTMAPS && surf->styles[map] != 255; map++ )
  591. {
  592. if ( r_newrefdef.lightstyles[surf->styles[map]].white != surf->cached_light[map] )
  593. goto dynamic;
  594. }
  595. // dynamic this frame or dynamic previously
  596. if ( ( surf->dlightframe == r_framecount ) )
  597. {
  598. dynamic:
  599. if ( gl_dynamic->value )
  600. {
  601. if ( !(surf->texinfo->flags & (SURF_SKY|SURF_TRANS33|SURF_TRANS66|SURF_WARP ) ) )
  602. {
  603. is_dynamic = true;
  604. }
  605. }
  606. }
  607. if ( is_dynamic )
  608. {
  609. unsigned temp[128*128];
  610. int smax, tmax;
  611. if ( ( surf->styles[map] >= 32 || surf->styles[map] == 0 ) && ( surf->dlightframe != r_framecount ) )
  612. {
  613. smax = (surf->extents[0]>>4)+1;
  614. tmax = (surf->extents[1]>>4)+1;
  615. R_BuildLightMap( surf, (void *)temp, smax*4 );
  616. R_SetCacheState( surf );
  617. GL_MBind( GL_TEXTURE1_SGIS, gl_state.lightmap_textures + surf->lightmaptexturenum );
  618. lmtex = surf->lightmaptexturenum;
  619. qglTexSubImage2D( GL_TEXTURE_2D, 0,
  620. surf->light_s, surf->light_t,
  621. smax, tmax,
  622. GL_LIGHTMAP_FORMAT,
  623. GL_UNSIGNED_BYTE, temp );
  624. }
  625. else
  626. {
  627. smax = (surf->extents[0]>>4)+1;
  628. tmax = (surf->extents[1]>>4)+1;
  629. R_BuildLightMap( surf, (void *)temp, smax*4 );
  630. GL_MBind( GL_TEXTURE1_SGIS, gl_state.lightmap_textures + 0 );
  631. lmtex = 0;
  632. qglTexSubImage2D( GL_TEXTURE_2D, 0,
  633. surf->light_s, surf->light_t,
  634. smax, tmax,
  635. GL_LIGHTMAP_FORMAT,
  636. GL_UNSIGNED_BYTE, temp );
  637. }
  638. c_brush_polys++;
  639. GL_MBind( GL_TEXTURE0_SGIS, image->texnum );
  640. GL_MBind( GL_TEXTURE1_SGIS, gl_state.lightmap_textures + lmtex );
  641. //==========
  642. //PGM
  643. if (surf->texinfo->flags & SURF_FLOWING)
  644. {
  645. float scroll;
  646. scroll = -64 * ( (r_newrefdef.time / 40.0) - (int)(r_newrefdef.time / 40.0) );
  647. if(scroll == 0.0)
  648. scroll = -64.0;
  649. for ( p = surf->polys; p; p = p->chain )
  650. {
  651. v = p->verts[0];
  652. qglBegin (GL_POLYGON);
  653. for (i=0 ; i< nv; i++, v+= VERTEXSIZE)
  654. {
  655. qglMTexCoord2fSGIS( GL_TEXTURE0_SGIS, (v[3]+scroll), v[4]);
  656. qglMTexCoord2fSGIS( GL_TEXTURE1_SGIS, v[5], v[6]);
  657. qglVertex3fv (v);
  658. }
  659. qglEnd ();
  660. }
  661. }
  662. else
  663. {
  664. for ( p = surf->polys; p; p = p->chain )
  665. {
  666. v = p->verts[0];
  667. qglBegin (GL_POLYGON);
  668. for (i=0 ; i< nv; i++, v+= VERTEXSIZE)
  669. {
  670. qglMTexCoord2fSGIS( GL_TEXTURE0_SGIS, v[3], v[4]);
  671. qglMTexCoord2fSGIS( GL_TEXTURE1_SGIS, v[5], v[6]);
  672. qglVertex3fv (v);
  673. }
  674. qglEnd ();
  675. }
  676. }
  677. //PGM
  678. //==========
  679. }
  680. else
  681. {
  682. c_brush_polys++;
  683. GL_MBind( GL_TEXTURE0_SGIS, image->texnum );
  684. GL_MBind( GL_TEXTURE1_SGIS, gl_state.lightmap_textures + lmtex );
  685. //==========
  686. //PGM
  687. if (surf->texinfo->flags & SURF_FLOWING)
  688. {
  689. float scroll;
  690. scroll = -64 * ( (r_newrefdef.time / 40.0) - (int)(r_newrefdef.time / 40.0) );
  691. if(scroll == 0.0)
  692. scroll = -64.0;
  693. for ( p = surf->polys; p; p = p->chain )
  694. {
  695. v = p->verts[0];
  696. qglBegin (GL_POLYGON);
  697. for (i=0 ; i< nv; i++, v+= VERTEXSIZE)
  698. {
  699. qglMTexCoord2fSGIS( GL_TEXTURE0_SGIS, (v[3]+scroll), v[4]);
  700. qglMTexCoord2fSGIS( GL_TEXTURE1_SGIS, v[5], v[6]);
  701. qglVertex3fv (v);
  702. }
  703. qglEnd ();
  704. }
  705. }
  706. else
  707. {
  708. //PGM
  709. //==========
  710. for ( p = surf->polys; p; p = p->chain )
  711. {
  712. v = p->verts[0];
  713. qglBegin (GL_POLYGON);
  714. for (i=0 ; i< nv; i++, v+= VERTEXSIZE)
  715. {
  716. qglMTexCoord2fSGIS( GL_TEXTURE0_SGIS, v[3], v[4]);
  717. qglMTexCoord2fSGIS( GL_TEXTURE1_SGIS, v[5], v[6]);
  718. qglVertex3fv (v);
  719. }
  720. qglEnd ();
  721. }
  722. //==========
  723. //PGM
  724. }
  725. //PGM
  726. //==========
  727. }
  728. }
  729. /*
  730. =================
  731. R_DrawInlineBModel
  732. =================
  733. */
  734. void R_DrawInlineBModel (void)
  735. {
  736. int i, k;
  737. cplane_t *pplane;
  738. float dot;
  739. msurface_t *psurf;
  740. dlight_t *lt;
  741. // calculate dynamic lighting for bmodel
  742. if ( !gl_flashblend->value )
  743. {
  744. lt = r_newrefdef.dlights;
  745. for (k=0 ; k<r_newrefdef.num_dlights ; k++, lt++)
  746. {
  747. R_MarkLights (lt, 1<<k, currentmodel->nodes + currentmodel->firstnode);
  748. }
  749. }
  750. psurf = &currentmodel->surfaces[currentmodel->firstmodelsurface];
  751. if ( currententity->flags & RF_TRANSLUCENT )
  752. {
  753. qglEnable (GL_BLEND);
  754. qglColor4f (1,1,1,0.25);
  755. GL_TexEnv( GL_MODULATE );
  756. }
  757. //
  758. // draw texture
  759. //
  760. for (i=0 ; i<currentmodel->nummodelsurfaces ; i++, psurf++)
  761. {
  762. // find which side of the node we are on
  763. pplane = psurf->plane;
  764. dot = DotProduct (modelorg, pplane->normal) - pplane->dist;
  765. // draw the polygon
  766. if (((psurf->flags & SURF_PLANEBACK) && (dot < -BACKFACE_EPSILON)) ||
  767. (!(psurf->flags & SURF_PLANEBACK) && (dot > BACKFACE_EPSILON)))
  768. {
  769. if (psurf->texinfo->flags & (SURF_TRANS33|SURF_TRANS66) )
  770. { // add to the translucent chain
  771. psurf->texturechain = r_alpha_surfaces;
  772. r_alpha_surfaces = psurf;
  773. }
  774. else if ( qglMTexCoord2fSGIS && !( psurf->flags & SURF_DRAWTURB ) )
  775. {
  776. GL_RenderLightmappedPoly( psurf );
  777. }
  778. else
  779. {
  780. GL_EnableMultitexture( false );
  781. R_RenderBrushPoly( psurf );
  782. GL_EnableMultitexture( true );
  783. }
  784. }
  785. }
  786. if ( !(currententity->flags & RF_TRANSLUCENT) )
  787. {
  788. if ( !qglMTexCoord2fSGIS )
  789. R_BlendLightmaps ();
  790. }
  791. else
  792. {
  793. qglDisable (GL_BLEND);
  794. qglColor4f (1,1,1,1);
  795. GL_TexEnv( GL_REPLACE );
  796. }
  797. }
  798. /*
  799. =================
  800. R_DrawBrushModel
  801. =================
  802. */
  803. void R_DrawBrushModel (entity_t *e)
  804. {
  805. vec3_t mins, maxs;
  806. int i;
  807. qboolean rotated;
  808. if (currentmodel->nummodelsurfaces == 0)
  809. return;
  810. currententity = e;
  811. gl_state.currenttextures[0] = gl_state.currenttextures[1] = -1;
  812. if (e->angles[0] || e->angles[1] || e->angles[2])
  813. {
  814. rotated = true;
  815. for (i=0 ; i<3 ; i++)
  816. {
  817. mins[i] = e->origin[i] - currentmodel->radius;
  818. maxs[i] = e->origin[i] + currentmodel->radius;
  819. }
  820. }
  821. else
  822. {
  823. rotated = false;
  824. VectorAdd (e->origin, currentmodel->mins, mins);
  825. VectorAdd (e->origin, currentmodel->maxs, maxs);
  826. }
  827. if (R_CullBox (mins, maxs))
  828. return;
  829. qglColor3f (1,1,1);
  830. memset (gl_lms.lightmap_surfaces, 0, sizeof(gl_lms.lightmap_surfaces));
  831. VectorSubtract (r_newrefdef.vieworg, e->origin, modelorg);
  832. if (rotated)
  833. {
  834. vec3_t temp;
  835. vec3_t forward, right, up;
  836. VectorCopy (modelorg, temp);
  837. AngleVectors (e->angles, forward, right, up);
  838. modelorg[0] = DotProduct (temp, forward);
  839. modelorg[1] = -DotProduct (temp, right);
  840. modelorg[2] = DotProduct (temp, up);
  841. }
  842. qglPushMatrix ();
  843. e->angles[0] = -e->angles[0]; // stupid quake bug
  844. e->angles[2] = -e->angles[2]; // stupid quake bug
  845. R_RotateForEntity (e);
  846. e->angles[0] = -e->angles[0]; // stupid quake bug
  847. e->angles[2] = -e->angles[2]; // stupid quake bug
  848. GL_EnableMultitexture( true );
  849. GL_SelectTexture( GL_TEXTURE0_SGIS );
  850. GL_TexEnv( GL_REPLACE );
  851. GL_SelectTexture( GL_TEXTURE1_SGIS );
  852. GL_TexEnv( GL_MODULATE );
  853. R_DrawInlineBModel ();
  854. GL_EnableMultitexture( false );
  855. qglPopMatrix ();
  856. }
  857. /*
  858. =============================================================
  859. WORLD MODEL
  860. =============================================================
  861. */
  862. /*
  863. ================
  864. R_RecursiveWorldNode
  865. ================
  866. */
  867. void R_RecursiveWorldNode (mnode_t *node)
  868. {
  869. int c, side, sidebit;
  870. cplane_t *plane;
  871. msurface_t *surf, **mark;
  872. mleaf_t *pleaf;
  873. float dot;
  874. image_t *image;
  875. if (node->contents == CONTENTS_SOLID)
  876. return; // solid
  877. if (node->visframe != r_visframecount)
  878. return;
  879. if (R_CullBox (node->minmaxs, node->minmaxs+3))
  880. return;
  881. // if a leaf node, draw stuff
  882. if (node->contents != -1)
  883. {
  884. pleaf = (mleaf_t *)node;
  885. // check for door connected areas
  886. if (r_newrefdef.areabits)
  887. {
  888. if (! (r_newrefdef.areabits[pleaf->area>>3] & (1<<(pleaf->area&7)) ) )
  889. return; // not visible
  890. }
  891. mark = pleaf->firstmarksurface;
  892. c = pleaf->nummarksurfaces;
  893. if (c)
  894. {
  895. do
  896. {
  897. (*mark)->visframe = r_framecount;
  898. mark++;
  899. } while (--c);
  900. }
  901. return;
  902. }
  903. // node is just a decision point, so go down the apropriate sides
  904. // find which side of the node we are on
  905. plane = node->plane;
  906. switch (plane->type)
  907. {
  908. case PLANE_X:
  909. dot = modelorg[0] - plane->dist;
  910. break;
  911. case PLANE_Y:
  912. dot = modelorg[1] - plane->dist;
  913. break;
  914. case PLANE_Z:
  915. dot = modelorg[2] - plane->dist;
  916. break;
  917. default:
  918. dot = DotProduct (modelorg, plane->normal) - plane->dist;
  919. break;
  920. }
  921. if (dot >= 0)
  922. {
  923. side = 0;
  924. sidebit = 0;
  925. }
  926. else
  927. {
  928. side = 1;
  929. sidebit = SURF_PLANEBACK;
  930. }
  931. // recurse down the children, front side first
  932. R_RecursiveWorldNode (node->children[side]);
  933. // draw stuff
  934. for ( c = node->numsurfaces, surf = r_worldmodel->surfaces + node->firstsurface; c ; c--, surf++)
  935. {
  936. if (surf->visframe != r_framecount)
  937. continue;
  938. if ( (surf->flags & SURF_PLANEBACK) != sidebit )
  939. continue; // wrong side
  940. if (surf->texinfo->flags & SURF_SKY)
  941. { // just adds to visible sky bounds
  942. R_AddSkySurface (surf);
  943. }
  944. else if (surf->texinfo->flags & (SURF_TRANS33|SURF_TRANS66))
  945. { // add to the translucent chain
  946. surf->texturechain = r_alpha_surfaces;
  947. r_alpha_surfaces = surf;
  948. }
  949. else
  950. {
  951. if ( qglMTexCoord2fSGIS && !( surf->flags & SURF_DRAWTURB ) )
  952. {
  953. GL_RenderLightmappedPoly( surf );
  954. }
  955. else
  956. {
  957. // the polygon is visible, so add it to the texture
  958. // sorted chain
  959. // FIXME: this is a hack for animation
  960. image = R_TextureAnimation (surf->texinfo);
  961. surf->texturechain = image->texturechain;
  962. image->texturechain = surf;
  963. }
  964. }
  965. }
  966. // recurse down the back side
  967. R_RecursiveWorldNode (node->children[!side]);
  968. /*
  969. for ( ; c ; c--, surf++)
  970. {
  971. if (surf->visframe != r_framecount)
  972. continue;
  973. if ( (surf->flags & SURF_PLANEBACK) != sidebit )
  974. continue; // wrong side
  975. if (surf->texinfo->flags & SURF_SKY)
  976. { // just adds to visible sky bounds
  977. R_AddSkySurface (surf);
  978. }
  979. else if (surf->texinfo->flags & (SURF_TRANS33|SURF_TRANS66))
  980. { // add to the translucent chain
  981. // surf->texturechain = alpha_surfaces;
  982. // alpha_surfaces = surf;
  983. }
  984. else
  985. {
  986. if ( qglMTexCoord2fSGIS && !( surf->flags & SURF_DRAWTURB ) )
  987. {
  988. GL_RenderLightmappedPoly( surf );
  989. }
  990. else
  991. {
  992. // the polygon is visible, so add it to the texture
  993. // sorted chain
  994. // FIXME: this is a hack for animation
  995. image = R_TextureAnimation (surf->texinfo);
  996. surf->texturechain = image->texturechain;
  997. image->texturechain = surf;
  998. }
  999. }
  1000. }
  1001. */
  1002. }
  1003. /*
  1004. =============
  1005. R_DrawWorld
  1006. =============
  1007. */
  1008. void R_DrawWorld (void)
  1009. {
  1010. entity_t ent;
  1011. if (!r_drawworld->value)
  1012. return;
  1013. if ( r_newrefdef.rdflags & RDF_NOWORLDMODEL )
  1014. return;
  1015. currentmodel = r_worldmodel;
  1016. VectorCopy (r_newrefdef.vieworg, modelorg);
  1017. // auto cycle the world frame for texture animation
  1018. memset (&ent, 0, sizeof(ent));
  1019. ent.frame = (int)(r_newrefdef.time*2);
  1020. currententity = &ent;
  1021. gl_state.currenttextures[0] = gl_state.currenttextures[1] = -1;
  1022. qglColor3f (1,1,1);
  1023. memset (gl_lms.lightmap_surfaces, 0, sizeof(gl_lms.lightmap_surfaces));
  1024. R_ClearSkyBox ();
  1025. if ( qglMTexCoord2fSGIS )
  1026. {
  1027. GL_EnableMultitexture( true );
  1028. GL_SelectTexture( GL_TEXTURE0_SGIS );
  1029. GL_TexEnv( GL_REPLACE );
  1030. GL_SelectTexture( GL_TEXTURE1_SGIS );
  1031. if ( gl_lightmap->value )
  1032. GL_TexEnv( GL_REPLACE );
  1033. else
  1034. GL_TexEnv( GL_MODULATE );
  1035. R_RecursiveWorldNode (r_worldmodel->nodes);
  1036. GL_EnableMultitexture( false );
  1037. }
  1038. else
  1039. {
  1040. R_RecursiveWorldNode (r_worldmodel->nodes);
  1041. }
  1042. /*
  1043. ** theoretically nothing should happen in the next two functions
  1044. ** if multitexture is enabled
  1045. */
  1046. DrawTextureChains ();
  1047. R_BlendLightmaps ();
  1048. R_DrawSkyBox ();
  1049. R_DrawTriangleOutlines ();
  1050. }
  1051. /*
  1052. ===============
  1053. R_MarkLeaves
  1054. Mark the leaves and nodes that are in the PVS for the current
  1055. cluster
  1056. ===============
  1057. */
  1058. void R_MarkLeaves (void)
  1059. {
  1060. byte *vis;
  1061. byte fatvis[MAX_MAP_LEAFS/8];
  1062. mnode_t *node;
  1063. int i, c;
  1064. mleaf_t *leaf;
  1065. int cluster;
  1066. if (r_oldviewcluster == r_viewcluster && r_oldviewcluster2 == r_viewcluster2 && !r_novis->value && r_viewcluster != -1)
  1067. return;
  1068. // development aid to let you run around and see exactly where
  1069. // the pvs ends
  1070. if (gl_lockpvs->value)
  1071. return;
  1072. r_visframecount++;
  1073. r_oldviewcluster = r_viewcluster;
  1074. r_oldviewcluster2 = r_viewcluster2;
  1075. if (r_novis->value || r_viewcluster == -1 || !r_worldmodel->vis)
  1076. {
  1077. // mark everything
  1078. for (i=0 ; i<r_worldmodel->numleafs ; i++)
  1079. r_worldmodel->leafs[i].visframe = r_visframecount;
  1080. for (i=0 ; i<r_worldmodel->numnodes ; i++)
  1081. r_worldmodel->nodes[i].visframe = r_visframecount;
  1082. return;
  1083. }
  1084. vis = Mod_ClusterPVS (r_viewcluster, r_worldmodel);
  1085. // may have to combine two clusters because of solid water boundaries
  1086. if (r_viewcluster2 != r_viewcluster)
  1087. {
  1088. memcpy (fatvis, vis, (r_worldmodel->numleafs+7)/8);
  1089. vis = Mod_ClusterPVS (r_viewcluster2, r_worldmodel);
  1090. c = (r_worldmodel->numleafs+31)/32;
  1091. for (i=0 ; i<c ; i++)
  1092. ((int *)fatvis)[i] |= ((int *)vis)[i];
  1093. vis = fatvis;
  1094. }
  1095. for (i=0,leaf=r_worldmodel->leafs ; i<r_worldmodel->numleafs ; i++, leaf++)
  1096. {
  1097. cluster = leaf->cluster;
  1098. if (cluster == -1)
  1099. continue;
  1100. if (vis[cluster>>3] & (1<<(cluster&7)))
  1101. {
  1102. node = (mnode_t *)leaf;
  1103. do
  1104. {
  1105. if (node->visframe == r_visframecount)
  1106. break;
  1107. node->visframe = r_visframecount;
  1108. node = node->parent;
  1109. } while (node);
  1110. }
  1111. }
  1112. #if 0
  1113. for (i=0 ; i<r_worldmodel->vis->numclusters ; i++)
  1114. {
  1115. if (vis[i>>3] & (1<<(i&7)))
  1116. {
  1117. node = (mnode_t *)&r_worldmodel->leafs[i]; // FIXME: cluster
  1118. do
  1119. {
  1120. if (node->visframe == r_visframecount)
  1121. break;
  1122. node->visframe = r_visframecount;
  1123. node = node->parent;
  1124. } while (node);
  1125. }
  1126. }
  1127. #endif
  1128. }
  1129. /*
  1130. =============================================================================
  1131. LIGHTMAP ALLOCATION
  1132. =============================================================================
  1133. */
  1134. static void LM_InitBlock( void )
  1135. {
  1136. memset( gl_lms.allocated, 0, sizeof( gl_lms.allocated ) );
  1137. }
  1138. static void LM_UploadBlock( qboolean dynamic )
  1139. {
  1140. int texture;
  1141. int height = 0;
  1142. if ( dynamic )
  1143. {
  1144. texture = 0;
  1145. }
  1146. else
  1147. {
  1148. texture = gl_lms.current_lightmap_texture;
  1149. }
  1150. GL_Bind( gl_state.lightmap_textures + texture );
  1151. qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1152. qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1153. if ( dynamic )
  1154. {
  1155. int i;
  1156. for ( i = 0; i < BLOCK_WIDTH; i++ )
  1157. {
  1158. if ( gl_lms.allocated[i] > height )
  1159. height = gl_lms.allocated[i];
  1160. }
  1161. qglTexSubImage2D( GL_TEXTURE_2D,
  1162. 0,
  1163. 0, 0,
  1164. BLOCK_WIDTH, height,
  1165. GL_LIGHTMAP_FORMAT,
  1166. GL_UNSIGNED_BYTE,
  1167. gl_lms.lightmap_buffer );
  1168. }
  1169. else
  1170. {
  1171. qglTexImage2D( GL_TEXTURE_2D,
  1172. 0,
  1173. gl_lms.internal_format,
  1174. BLOCK_WIDTH, BLOCK_HEIGHT,
  1175. 0,
  1176. GL_LIGHTMAP_FORMAT,
  1177. GL_UNSIGNED_BYTE,
  1178. gl_lms.lightmap_buffer );
  1179. if ( ++gl_lms.current_lightmap_texture == MAX_LIGHTMAPS )
  1180. ri.Sys_Error( ERR_DROP, "LM_UploadBlock() - MAX_LIGHTMAPS exceeded\n" );
  1181. }
  1182. }
  1183. // returns a texture number and the position inside it
  1184. static qboolean LM_AllocBlock (int w, int h, int *x, int *y)
  1185. {
  1186. int i, j;
  1187. int best, best2;
  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 (gl_lms.allocated[i+j] >= best)
  1195. break;
  1196. if (gl_lms.allocated[i+j] > best2)
  1197. best2 = gl_lms.allocated[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. return false;
  1207. for (i=0 ; i<w ; i++)
  1208. gl_lms.allocated[*x + i] = best + h;
  1209. return true;
  1210. }
  1211. /*
  1212. ================
  1213. GL_BuildPolygonFromSurface
  1214. ================
  1215. */
  1216. void GL_BuildPolygonFromSurface(msurface_t *fa)
  1217. {
  1218. int i, lindex, lnumverts;
  1219. medge_t *pedges, *r_pedge;
  1220. int vertpage;
  1221. float *vec;
  1222. float s, t;
  1223. glpoly_t *poly;
  1224. vec3_t total;
  1225. // reconstruct the polygon
  1226. pedges = currentmodel->edges;
  1227. lnumverts = fa->numedges;
  1228. vertpage = 0;
  1229. VectorClear (total);
  1230. //
  1231. // draw texture
  1232. //
  1233. poly = Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float));
  1234. poly->next = fa->polys;
  1235. poly->flags = fa->flags;
  1236. fa->polys = poly;
  1237. poly->numverts = lnumverts;
  1238. for (i=0 ; i<lnumverts ; i++)
  1239. {
  1240. lindex = currentmodel->surfedges[fa->firstedge + i];
  1241. if (lindex > 0)
  1242. {
  1243. r_pedge = &pedges[lindex];
  1244. vec = currentmodel->vertexes[r_pedge->v[0]].position;
  1245. }
  1246. else
  1247. {
  1248. r_pedge = &pedges[-lindex];
  1249. vec = currentmodel->vertexes[r_pedge->v[1]].position;
  1250. }
  1251. s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
  1252. s /= fa->texinfo->image->width;
  1253. t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
  1254. t /= fa->texinfo->image->height;
  1255. VectorAdd (total, vec, total);
  1256. VectorCopy (vec, poly->verts[i]);
  1257. poly->verts[i][3] = s;
  1258. poly->verts[i][4] = t;
  1259. //
  1260. // lightmap texture coordinates
  1261. //
  1262. s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
  1263. s -= fa->texturemins[0];
  1264. s += fa->light_s*16;
  1265. s += 8;
  1266. s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
  1267. t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
  1268. t -= fa->texturemins[1];
  1269. t += fa->light_t*16;
  1270. t += 8;
  1271. t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
  1272. poly->verts[i][5] = s;
  1273. poly->verts[i][6] = t;
  1274. }
  1275. poly->numverts = lnumverts;
  1276. }
  1277. /*
  1278. ========================
  1279. GL_CreateSurfaceLightmap
  1280. ========================
  1281. */
  1282. void GL_CreateSurfaceLightmap (msurface_t *surf)
  1283. {
  1284. int smax, tmax;
  1285. byte *base;
  1286. if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
  1287. return;
  1288. smax = (surf->extents[0]>>4)+1;
  1289. tmax = (surf->extents[1]>>4)+1;
  1290. if ( !LM_AllocBlock( smax, tmax, &surf->light_s, &surf->light_t ) )
  1291. {
  1292. LM_UploadBlock( false );
  1293. LM_InitBlock();
  1294. if ( !LM_AllocBlock( smax, tmax, &surf->light_s, &surf->light_t ) )
  1295. {
  1296. ri.Sys_Error( ERR_FATAL, "Consecutive calls to LM_AllocBlock(%d,%d) failed\n", smax, tmax );
  1297. }
  1298. }
  1299. surf->lightmaptexturenum = gl_lms.current_lightmap_texture;
  1300. base = gl_lms.lightmap_buffer;
  1301. base += (surf->light_t * BLOCK_WIDTH + surf->light_s) * LIGHTMAP_BYTES;
  1302. R_SetCacheState( surf );
  1303. R_BuildLightMap (surf, base, BLOCK_WIDTH*LIGHTMAP_BYTES);
  1304. }
  1305. /*
  1306. ==================
  1307. GL_BeginBuildingLightmaps
  1308. ==================
  1309. */
  1310. void GL_BeginBuildingLightmaps (model_t *m)
  1311. {
  1312. static lightstyle_t lightstyles[MAX_LIGHTSTYLES];
  1313. int i;
  1314. unsigned dummy[128*128];
  1315. memset( gl_lms.allocated, 0, sizeof(gl_lms.allocated) );
  1316. r_framecount = 1; // no dlightcache
  1317. GL_EnableMultitexture( true );
  1318. GL_SelectTexture( GL_TEXTURE1_SGIS );
  1319. /*
  1320. ** setup the base lightstyles so the lightmaps won't have to be regenerated
  1321. ** the first time they're seen
  1322. */
  1323. for (i=0 ; i<MAX_LIGHTSTYLES ; i++)
  1324. {
  1325. lightstyles[i].rgb[0] = 1;
  1326. lightstyles[i].rgb[1] = 1;
  1327. lightstyles[i].rgb[2] = 1;
  1328. lightstyles[i].white = 3;
  1329. }
  1330. r_newrefdef.lightstyles = lightstyles;
  1331. if (!gl_state.lightmap_textures)
  1332. {
  1333. gl_state.lightmap_textures = TEXNUM_LIGHTMAPS;
  1334. // gl_state.lightmap_textures = gl_state.texture_extension_number;
  1335. // gl_state.texture_extension_number = gl_state.lightmap_textures + MAX_LIGHTMAPS;
  1336. }
  1337. gl_lms.current_lightmap_texture = 1;
  1338. /*
  1339. ** if mono lightmaps are enabled and we want to use alpha
  1340. ** blending (a,1-a) then we're likely running on a 3DLabs
  1341. ** Permedia2. In a perfect world we'd use a GL_ALPHA lightmap
  1342. ** in order to conserve space and maximize bandwidth, however
  1343. ** this isn't a perfect world.
  1344. **
  1345. ** So we have to use alpha lightmaps, but stored in GL_RGBA format,
  1346. ** which means we only get 1/16th the color resolution we should when
  1347. ** using alpha lightmaps. If we find another board that supports
  1348. ** only alpha lightmaps but that can at least support the GL_ALPHA
  1349. ** format then we should change this code to use real alpha maps.
  1350. */
  1351. if ( toupper( gl_monolightmap->string[0] ) == 'A' )
  1352. {
  1353. gl_lms.internal_format = gl_tex_alpha_format;
  1354. }
  1355. /*
  1356. ** try to do hacked colored lighting with a blended texture
  1357. */
  1358. else if ( toupper( gl_monolightmap->string[0] ) == 'C' )
  1359. {
  1360. gl_lms.internal_format = gl_tex_alpha_format;
  1361. }
  1362. else if ( toupper( gl_monolightmap->string[0] ) == 'I' )
  1363. {
  1364. gl_lms.internal_format = GL_INTENSITY8;
  1365. }
  1366. else if ( toupper( gl_monolightmap->string[0] ) == 'L' )
  1367. {
  1368. gl_lms.internal_format = GL_LUMINANCE8;
  1369. }
  1370. else
  1371. {
  1372. gl_lms.internal_format = gl_tex_solid_format;
  1373. }
  1374. /*
  1375. ** initialize the dynamic lightmap texture
  1376. */
  1377. GL_Bind( gl_state.lightmap_textures + 0 );
  1378. qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  1379. qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  1380. qglTexImage2D( GL_TEXTURE_2D,
  1381. 0,
  1382. gl_lms.internal_format,
  1383. BLOCK_WIDTH, BLOCK_HEIGHT,
  1384. 0,
  1385. GL_LIGHTMAP_FORMAT,
  1386. GL_UNSIGNED_BYTE,
  1387. dummy );
  1388. }
  1389. /*
  1390. =======================
  1391. GL_EndBuildingLightmaps
  1392. =======================
  1393. */
  1394. void GL_EndBuildingLightmaps (void)
  1395. {
  1396. LM_UploadBlock( false );
  1397. GL_EnableMultitexture( false );
  1398. }