gl_rlight.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. //Con_Printf("AddLightBlend(): %4.2f %4.2f %4.2f %4.6f\n", v_blend[0], v_blend[1], v_blend[2], v_blend[3]);
  57. }
  58. float bubble_sintable[17], bubble_costable[17];
  59. void R_InitBubble() {
  60. float a;
  61. int i;
  62. float *bub_sin, *bub_cos;
  63. bub_sin = bubble_sintable;
  64. bub_cos = bubble_costable;
  65. for (i=16 ; i>=0 ; i--)
  66. {
  67. a = i/16.0 * M_PI*2;
  68. *bub_sin++ = sin(a);
  69. *bub_cos++ = cos(a);
  70. }
  71. }
  72. void R_RenderDlight (dlight_t *light)
  73. {
  74. int i, j;
  75. // float a;
  76. vec3_t v;
  77. float rad;
  78. float *bub_sin, *bub_cos;
  79. bub_sin = bubble_sintable;
  80. bub_cos = bubble_costable;
  81. rad = light->radius * 0.35;
  82. VectorSubtract (light->origin, r_origin, v);
  83. if (Length (v) < rad)
  84. { // view is inside the dlight
  85. AddLightBlend (1, 0.5, 0, light->radius * 0.0003);
  86. return;
  87. }
  88. glBegin (GL_TRIANGLE_FAN);
  89. // glColor3f (0.2,0.1,0.0);
  90. // glColor3f (0.2,0.1,0.05); // changed dimlight effect
  91. glColor4f (light->color[0], light->color[1], light->color[2],
  92. light->color[3]);
  93. for (i=0 ; i<3 ; i++)
  94. v[i] = light->origin[i] - vpn[i]*rad;
  95. glVertex3fv (v);
  96. glColor3f (0,0,0);
  97. for (i=16 ; i>=0 ; i--)
  98. {
  99. // a = i/16.0 * M_PI*2;
  100. for (j=0 ; j<3 ; j++)
  101. v[j] = light->origin[j] + (vright[j]*(*bub_cos) +
  102. + vup[j]*(*bub_sin)) * rad;
  103. bub_sin++;
  104. bub_cos++;
  105. glVertex3fv (v);
  106. }
  107. glEnd ();
  108. }
  109. /*
  110. =============
  111. R_RenderDlights
  112. =============
  113. */
  114. void R_RenderDlights (void)
  115. {
  116. int i;
  117. dlight_t *l;
  118. if (!gl_flashblend.value)
  119. return;
  120. r_dlightframecount = r_framecount + 1; // because the count hasn't
  121. // advanced yet for this frame
  122. glDepthMask (0);
  123. glDisable (GL_TEXTURE_2D);
  124. glShadeModel (GL_SMOOTH);
  125. glEnable (GL_BLEND);
  126. glBlendFunc (GL_ONE, GL_ONE);
  127. l = cl_dlights;
  128. for (i=0 ; i<MAX_DLIGHTS ; i++, l++)
  129. {
  130. if (l->die < cl.time || !l->radius)
  131. continue;
  132. R_RenderDlight (l);
  133. }
  134. glColor3f (1,1,1);
  135. glDisable (GL_BLEND);
  136. glEnable (GL_TEXTURE_2D);
  137. glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  138. glDepthMask (1);
  139. }
  140. /*
  141. =============================================================================
  142. DYNAMIC LIGHTS
  143. =============================================================================
  144. */
  145. /*
  146. =============
  147. R_MarkLights
  148. =============
  149. */
  150. void R_MarkLights (dlight_t *light, int bit, mnode_t *node)
  151. {
  152. mplane_t *splitplane;
  153. float dist;
  154. msurface_t *surf;
  155. int i;
  156. if (node->contents < 0)
  157. return;
  158. splitplane = node->plane;
  159. dist = DotProduct (light->origin, splitplane->normal) - splitplane->dist;
  160. if (dist > light->radius)
  161. {
  162. R_MarkLights (light, bit, node->children[0]);
  163. return;
  164. }
  165. if (dist < -light->radius)
  166. {
  167. R_MarkLights (light, bit, node->children[1]);
  168. return;
  169. }
  170. // mark the polygons
  171. surf = cl.worldmodel->surfaces + node->firstsurface;
  172. for (i=0 ; i<node->numsurfaces ; i++, surf++)
  173. {
  174. if (surf->dlightframe != r_dlightframecount)
  175. {
  176. surf->dlightbits = 0;
  177. surf->dlightframe = r_dlightframecount;
  178. }
  179. surf->dlightbits |= bit;
  180. }
  181. R_MarkLights (light, bit, node->children[0]);
  182. R_MarkLights (light, bit, node->children[1]);
  183. }
  184. /*
  185. =============
  186. R_PushDlights
  187. =============
  188. */
  189. void R_PushDlights (void)
  190. {
  191. int i;
  192. dlight_t *l;
  193. if (gl_flashblend.value)
  194. return;
  195. r_dlightframecount = r_framecount + 1; // because the count hasn't
  196. // advanced yet for this frame
  197. l = cl_dlights;
  198. for (i=0 ; i<MAX_DLIGHTS ; i++, l++)
  199. {
  200. if (l->die < cl.time || !l->radius)
  201. continue;
  202. R_MarkLights ( l, 1<<i, cl.worldmodel->nodes );
  203. }
  204. }
  205. /*
  206. =============================================================================
  207. LIGHT SAMPLING
  208. =============================================================================
  209. */
  210. mplane_t *lightplane;
  211. vec3_t lightspot;
  212. int RecursiveLightPoint (mnode_t *node, vec3_t start, vec3_t end)
  213. {
  214. int r;
  215. float front, back, frac;
  216. int side;
  217. mplane_t *plane;
  218. vec3_t mid;
  219. msurface_t *surf;
  220. int s, t, ds, dt;
  221. int i;
  222. mtexinfo_t *tex;
  223. byte *lightmap;
  224. unsigned scale;
  225. int maps;
  226. if (node->contents < 0)
  227. return -1; // didn't hit anything
  228. // calculate mid point
  229. // FIXME: optimize for axial
  230. plane = node->plane;
  231. front = DotProduct (start, plane->normal) - plane->dist;
  232. back = DotProduct (end, plane->normal) - plane->dist;
  233. side = front < 0;
  234. if ( (back < 0) == side)
  235. return RecursiveLightPoint (node->children[side], start, end);
  236. frac = front / (front-back);
  237. mid[0] = start[0] + (end[0] - start[0])*frac;
  238. mid[1] = start[1] + (end[1] - start[1])*frac;
  239. mid[2] = start[2] + (end[2] - start[2])*frac;
  240. // go down front side
  241. r = RecursiveLightPoint (node->children[side], start, mid);
  242. if (r >= 0)
  243. return r; // hit something
  244. if ( (back < 0) == side )
  245. return -1; // didn't hit anuthing
  246. // check for impact on this node
  247. VectorCopy (mid, lightspot);
  248. lightplane = plane;
  249. surf = cl.worldmodel->surfaces + node->firstsurface;
  250. for (i=0 ; i<node->numsurfaces ; i++, surf++)
  251. {
  252. if (surf->flags & SURF_DRAWTILED)
  253. continue; // no lightmaps
  254. tex = surf->texinfo;
  255. s = DotProduct (mid, tex->vecs[0]) + tex->vecs[0][3];
  256. t = DotProduct (mid, tex->vecs[1]) + tex->vecs[1][3];;
  257. if (s < surf->texturemins[0] ||
  258. t < surf->texturemins[1])
  259. continue;
  260. ds = s - surf->texturemins[0];
  261. dt = t - surf->texturemins[1];
  262. if ( ds > surf->extents[0] || dt > surf->extents[1] )
  263. continue;
  264. if (!surf->samples)
  265. return 0;
  266. ds >>= 4;
  267. dt >>= 4;
  268. lightmap = surf->samples;
  269. r = 0;
  270. if (lightmap)
  271. {
  272. lightmap += dt * ((surf->extents[0]>>4)+1) + ds;
  273. for (maps = 0 ; maps < MAXLIGHTMAPS && surf->styles[maps] != 255 ;
  274. maps++)
  275. {
  276. scale = d_lightstylevalue[surf->styles[maps]];
  277. r += *lightmap * scale;
  278. lightmap += ((surf->extents[0]>>4)+1) *
  279. ((surf->extents[1]>>4)+1);
  280. }
  281. r >>= 8;
  282. }
  283. return r;
  284. }
  285. // go down back side
  286. return RecursiveLightPoint (node->children[!side], mid, end);
  287. }
  288. int R_LightPoint (vec3_t p)
  289. {
  290. vec3_t end;
  291. int r;
  292. if (!cl.worldmodel->lightdata)
  293. return 255;
  294. end[0] = p[0];
  295. end[1] = p[1];
  296. end[2] = p[2] - 2048;
  297. r = RecursiveLightPoint (cl.worldmodel->nodes, p, end);
  298. if (r == -1)
  299. r = 0;
  300. return r;
  301. }