gl_rlight.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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_light.c
  16. #include "quakedef.h"
  17. int r_dlightframecount;
  18. /*
  19. ==================
  20. R_AnimateLight
  21. ==================
  22. */
  23. void R_AnimateLight (void)
  24. {
  25. int i,j,k;
  26. //
  27. // light animations
  28. // 'm' is normal light, 'a' is no light, 'z' is double bright
  29. i = (int)(cl.time*10);
  30. for (j=0 ; j<MAX_LIGHTSTYLES ; j++)
  31. {
  32. if (!cl_lightstyle[j].length)
  33. {
  34. d_lightstylevalue[j] = 256;
  35. continue;
  36. }
  37. k = i % cl_lightstyle[j].length;
  38. k = cl_lightstyle[j].map[k] - 'a';
  39. k = k*22;
  40. d_lightstylevalue[j] = k;
  41. }
  42. }
  43. /*
  44. =============================================================================
  45. DYNAMIC LIGHTS BLEND RENDERING
  46. =============================================================================
  47. */
  48. void AddLightBlend (float r, float g, float b, float a2)
  49. {
  50. float a;
  51. v_blend[3] = a = v_blend[3] + a2*(1-v_blend[3]);
  52. a2 = a2/a;
  53. v_blend[0] = v_blend[1]*(1-a2) + r*a2;
  54. v_blend[1] = v_blend[1]*(1-a2) + g*a2;
  55. v_blend[2] = v_blend[2]*(1-a2) + b*a2;
  56. }
  57. void R_RenderDlight (dlight_t *light)
  58. {
  59. int i, j;
  60. float a;
  61. vec3_t v;
  62. float rad;
  63. rad = light->radius * 0.35;
  64. VectorSubtract (light->origin, r_origin, v);
  65. if (Length (v) < rad)
  66. { // view is inside the dlight
  67. AddLightBlend (1, 0.5, 0, light->radius * 0.0003);
  68. return;
  69. }
  70. glBegin (GL_TRIANGLE_FAN);
  71. glColor3f (0.2,0.1,0.0);
  72. for (i=0 ; i<3 ; i++)
  73. v[i] = light->origin[i] - vpn[i]*rad;
  74. glVertex3fv (v);
  75. glColor3f (0,0,0);
  76. for (i=16 ; i>=0 ; i--)
  77. {
  78. a = i/16.0 * M_PI*2;
  79. for (j=0 ; j<3 ; j++)
  80. v[j] = light->origin[j] + vright[j]*cos(a)*rad
  81. + vup[j]*sin(a)*rad;
  82. glVertex3fv (v);
  83. }
  84. glEnd ();
  85. }
  86. /*
  87. =============
  88. R_RenderDlights
  89. =============
  90. */
  91. void R_RenderDlights (void)
  92. {
  93. int i;
  94. dlight_t *l;
  95. if (!gl_flashblend.value)
  96. return;
  97. r_dlightframecount = r_framecount + 1; // because the count hasn't
  98. // advanced yet for this frame
  99. glDepthMask (0);
  100. glDisable (GL_TEXTURE_2D);
  101. glShadeModel (GL_SMOOTH);
  102. glEnable (GL_BLEND);
  103. glBlendFunc (GL_ONE, GL_ONE);
  104. l = cl_dlights;
  105. for (i=0 ; i<MAX_DLIGHTS ; i++, l++)
  106. {
  107. if (l->die < cl.time || !l->radius)
  108. continue;
  109. R_RenderDlight (l);
  110. }
  111. glColor3f (1,1,1);
  112. glDisable (GL_BLEND);
  113. glEnable (GL_TEXTURE_2D);
  114. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  115. glDepthMask (1);
  116. }
  117. /*
  118. =============================================================================
  119. DYNAMIC LIGHTS
  120. =============================================================================
  121. */
  122. /*
  123. =============
  124. R_MarkLights
  125. =============
  126. */
  127. void R_MarkLights (dlight_t *light, int bit, mnode_t *node)
  128. {
  129. mplane_t *splitplane;
  130. float dist;
  131. msurface_t *surf;
  132. int i;
  133. if (node->contents < 0)
  134. return;
  135. splitplane = node->plane;
  136. dist = DotProduct (light->origin, splitplane->normal) - splitplane->dist;
  137. if (dist > light->radius)
  138. {
  139. R_MarkLights (light, bit, node->children[0]);
  140. return;
  141. }
  142. if (dist < -light->radius)
  143. {
  144. R_MarkLights (light, bit, node->children[1]);
  145. return;
  146. }
  147. // mark the polygons
  148. surf = cl.worldmodel->surfaces + node->firstsurface;
  149. for (i=0 ; i<node->numsurfaces ; i++, surf++)
  150. {
  151. if (surf->dlightframe != r_dlightframecount)
  152. {
  153. surf->dlightbits = 0;
  154. surf->dlightframe = r_dlightframecount;
  155. }
  156. surf->dlightbits |= bit;
  157. }
  158. R_MarkLights (light, bit, node->children[0]);
  159. R_MarkLights (light, bit, node->children[1]);
  160. }
  161. /*
  162. =============
  163. R_PushDlights
  164. =============
  165. */
  166. void R_PushDlights (void)
  167. {
  168. int i;
  169. dlight_t *l;
  170. if (gl_flashblend.value)
  171. return;
  172. r_dlightframecount = r_framecount + 1; // because the count hasn't
  173. // advanced yet for this frame
  174. l = cl_dlights;
  175. for (i=0 ; i<MAX_DLIGHTS ; i++, l++)
  176. {
  177. if (l->die < cl.time || !l->radius)
  178. continue;
  179. R_MarkLights ( l, 1<<i, cl.worldmodel->nodes );
  180. }
  181. }
  182. /*
  183. =============================================================================
  184. LIGHT SAMPLING
  185. =============================================================================
  186. */
  187. mplane_t *lightplane;
  188. vec3_t lightspot;
  189. int RecursiveLightPoint (mnode_t *node, vec3_t start, vec3_t end)
  190. {
  191. int r;
  192. float front, back, frac;
  193. int side;
  194. mplane_t *plane;
  195. vec3_t mid;
  196. msurface_t *surf;
  197. int s, t, ds, dt;
  198. int i;
  199. mtexinfo_t *tex;
  200. byte *lightmap;
  201. unsigned scale;
  202. int maps;
  203. if (node->contents < 0)
  204. return -1; // didn't hit anything
  205. // calculate mid point
  206. // FIXME: optimize for axial
  207. plane = node->plane;
  208. front = DotProduct (start, plane->normal) - plane->dist;
  209. back = DotProduct (end, plane->normal) - plane->dist;
  210. side = front < 0;
  211. if ( (back < 0) == side)
  212. return RecursiveLightPoint (node->children[side], start, end);
  213. frac = front / (front-back);
  214. mid[0] = start[0] + (end[0] - start[0])*frac;
  215. mid[1] = start[1] + (end[1] - start[1])*frac;
  216. mid[2] = start[2] + (end[2] - start[2])*frac;
  217. // go down front side
  218. r = RecursiveLightPoint (node->children[side], start, mid);
  219. if (r >= 0)
  220. return r; // hit something
  221. if ( (back < 0) == side )
  222. return -1; // didn't hit anuthing
  223. // check for impact on this node
  224. VectorCopy (mid, lightspot);
  225. lightplane = plane;
  226. surf = cl.worldmodel->surfaces + node->firstsurface;
  227. for (i=0 ; i<node->numsurfaces ; i++, surf++)
  228. {
  229. if (surf->flags & SURF_DRAWTILED)
  230. continue; // no lightmaps
  231. tex = surf->texinfo;
  232. s = DotProduct (mid, tex->vecs[0]) + tex->vecs[0][3];
  233. t = DotProduct (mid, tex->vecs[1]) + tex->vecs[1][3];;
  234. if (s < surf->texturemins[0] ||
  235. t < surf->texturemins[1])
  236. continue;
  237. ds = s - surf->texturemins[0];
  238. dt = t - surf->texturemins[1];
  239. if ( ds > surf->extents[0] || dt > surf->extents[1] )
  240. continue;
  241. if (!surf->samples)
  242. return 0;
  243. ds >>= 4;
  244. dt >>= 4;
  245. lightmap = surf->samples;
  246. r = 0;
  247. if (lightmap)
  248. {
  249. lightmap += dt * ((surf->extents[0]>>4)+1) + ds;
  250. for (maps = 0 ; maps < MAXLIGHTMAPS && surf->styles[maps] != 255 ;
  251. maps++)
  252. {
  253. scale = d_lightstylevalue[surf->styles[maps]];
  254. r += *lightmap * scale;
  255. lightmap += ((surf->extents[0]>>4)+1) *
  256. ((surf->extents[1]>>4)+1);
  257. }
  258. r >>= 8;
  259. }
  260. return r;
  261. }
  262. // go down back side
  263. return RecursiveLightPoint (node->children[!side], mid, end);
  264. }
  265. int R_LightPoint (vec3_t p)
  266. {
  267. vec3_t end;
  268. int r;
  269. if (!cl.worldmodel->lightdata)
  270. return 255;
  271. end[0] = p[0];
  272. end[1] = p[1];
  273. end[2] = p[2] - 2048;
  274. r = RecursiveLightPoint (cl.worldmodel->nodes, p, end);
  275. if (r == -1)
  276. r = 0;
  277. return r;
  278. }