1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20 // r_light.c
21
22 #include "quakedef.h"
23
24 int r_dlightframecount;
25
26
27 /*
28 ==================
29 R_AnimateLight
30 ==================
31 */
R_AnimateLight(void)32 void R_AnimateLight (void)
33 {
34 int i,j,k;
35
36 //
37 // light animations
38 // 'm' is normal light, 'a' is no light, 'z' is double bright
39 i = (int)(cl.time*10);
40 for (j=0 ; j<MAX_LIGHTSTYLES ; j++)
41 {
42 if (!cl_lightstyle[j].length)
43 {
44 d_lightstylevalue[j] = 256;
45 continue;
46 }
47 k = i % cl_lightstyle[j].length;
48 k = cl_lightstyle[j].map[k] - 'a';
49 k = k*22;
50 d_lightstylevalue[j] = k;
51 }
52 }
53
54 /*
55 =============================================================================
56
57 DYNAMIC LIGHTS BLEND RENDERING
58
59 =============================================================================
60 */
61
AddLightBlend(float r,float g,float b,float a2)62 void AddLightBlend (float r, float g, float b, float a2)
63 {
64 float a;
65
66 v_blend[3] = a = v_blend[3] + a2*(1-v_blend[3]);
67
68 a2 = a2/a;
69
70 v_blend[0] = v_blend[1]*(1-a2) + r*a2;
71 v_blend[1] = v_blend[1]*(1-a2) + g*a2;
72 v_blend[2] = v_blend[2]*(1-a2) + b*a2;
73 //Con_Printf("AddLightBlend(): %4.2f %4.2f %4.2f %4.6f\n", v_blend[0], v_blend[1], v_blend[2], v_blend[3]);
74 }
75
76 float bubble_sintable[17], bubble_costable[17];
77
R_InitBubble()78 void R_InitBubble() {
79 float a;
80 int i;
81 float *bub_sin, *bub_cos;
82
83 bub_sin = bubble_sintable;
84 bub_cos = bubble_costable;
85
86 for (i=16 ; i>=0 ; i--)
87 {
88 a = i/16.0 * M_PI*2;
89 *bub_sin++ = sin(a);
90 *bub_cos++ = cos(a);
91 }
92 }
93
R_RenderDlight(dlight_t * light)94 void R_RenderDlight (dlight_t *light)
95 {
96 int i, j;
97 // float a;
98 vec3_t v;
99 float rad;
100 float *bub_sin, *bub_cos;
101
102 bub_sin = bubble_sintable;
103 bub_cos = bubble_costable;
104 rad = light->radius * 0.35;
105
106 VectorSubtract (light->origin, r_origin, v);
107 if (Length (v) < rad)
108 { // view is inside the dlight
109 AddLightBlend (1, 0.5, 0, light->radius * 0.0003);
110 return;
111 }
112
113 #ifdef USE_OPENGLES
114 // !!! Implement this.
115 #else
116 glBegin (GL_TRIANGLE_FAN);
117 // glColor3f (0.2,0.1,0.0);
118 // glColor3f (0.2,0.1,0.05); // changed dimlight effect
119 glColor4f (light->color[0], light->color[1], light->color[2],
120 light->color[3]);
121 for (i=0 ; i<3 ; i++)
122 v[i] = light->origin[i] - vpn[i]*rad;
123 glVertex3fv (v);
124 glColor3f (0,0,0);
125 for (i=16 ; i>=0 ; i--)
126 {
127 // a = i/16.0 * M_PI*2;
128 for (j=0 ; j<3 ; j++)
129 v[j] = light->origin[j] + (vright[j]*(*bub_cos) +
130 + vup[j]*(*bub_sin)) * rad;
131 bub_sin++;
132 bub_cos++;
133 glVertex3fv (v);
134 }
135 glEnd ();
136 #endif
137 }
138
139 /*
140 =============
141 R_RenderDlights
142 =============
143 */
R_RenderDlights(void)144 void R_RenderDlights (void)
145 {
146 int i;
147 dlight_t *l;
148
149 if (!gl_flashblend.value)
150 return;
151
152 r_dlightframecount = r_framecount + 1; // because the count hasn't
153 // advanced yet for this frame
154 glDepthMask (0);
155 glDisable (GL_TEXTURE_2D);
156 glShadeModel (GL_SMOOTH);
157 glEnable (GL_BLEND);
158 glBlendFunc (GL_ONE, GL_ONE);
159
160 l = cl_dlights;
161 for (i=0 ; i<MAX_DLIGHTS ; i++, l++)
162 {
163 if (l->die < cl.time || !l->radius)
164 continue;
165 R_RenderDlight (l);
166 }
167
168 glColor3f (1,1,1);
169 glDisable (GL_BLEND);
170 glEnable (GL_TEXTURE_2D);
171 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
172 glDepthMask (1);
173 }
174
175
176 /*
177 =============================================================================
178
179 DYNAMIC LIGHTS
180
181 =============================================================================
182 */
183
184 /*
185 =============
186 R_MarkLights
187 =============
188 */
R_MarkLights(dlight_t * light,int bit,mnode_t * node)189 void R_MarkLights (dlight_t *light, int bit, mnode_t *node)
190 {
191 mplane_t *splitplane;
192 float dist;
193 msurface_t *surf;
194 int i;
195
196 if (node->contents < 0)
197 return;
198
199 splitplane = node->plane;
200 dist = DotProduct (light->origin, splitplane->normal) - splitplane->dist;
201
202 if (dist > light->radius)
203 {
204 R_MarkLights (light, bit, node->children[0]);
205 return;
206 }
207 if (dist < -light->radius)
208 {
209 R_MarkLights (light, bit, node->children[1]);
210 return;
211 }
212
213 // mark the polygons
214 surf = cl.worldmodel->surfaces + node->firstsurface;
215 for (i=0 ; i<node->numsurfaces ; i++, surf++)
216 {
217 if (surf->dlightframe != r_dlightframecount)
218 {
219 surf->dlightbits = 0;
220 surf->dlightframe = r_dlightframecount;
221 }
222 surf->dlightbits |= bit;
223 }
224
225 R_MarkLights (light, bit, node->children[0]);
226 R_MarkLights (light, bit, node->children[1]);
227 }
228
229
230 /*
231 =============
232 R_PushDlights
233 =============
234 */
R_PushDlights(void)235 void R_PushDlights (void)
236 {
237 int i;
238 dlight_t *l;
239
240 if (gl_flashblend.value)
241 return;
242
243 r_dlightframecount = r_framecount + 1; // because the count hasn't
244 // advanced yet for this frame
245 l = cl_dlights;
246
247 for (i=0 ; i<MAX_DLIGHTS ; i++, l++)
248 {
249 if (l->die < cl.time || !l->radius)
250 continue;
251 R_MarkLights ( l, 1<<i, cl.worldmodel->nodes );
252 }
253 }
254
255
256 /*
257 =============================================================================
258
259 LIGHT SAMPLING
260
261 =============================================================================
262 */
263
264 mplane_t *lightplane;
265 vec3_t lightspot;
266
RecursiveLightPoint(mnode_t * node,vec3_t start,vec3_t end)267 int RecursiveLightPoint (mnode_t *node, vec3_t start, vec3_t end)
268 {
269 int r;
270 float front, back, frac;
271 int side;
272 mplane_t *plane;
273 vec3_t mid;
274 msurface_t *surf;
275 int s, t, ds, dt;
276 int i;
277 mtexinfo_t *tex;
278 byte *lightmap;
279 unsigned scale;
280 int maps;
281
282 if (node->contents < 0)
283 return -1; // didn't hit anything
284
285 // calculate mid point
286
287 // FIXME: optimize for axial
288 plane = node->plane;
289 front = DotProduct (start, plane->normal) - plane->dist;
290 back = DotProduct (end, plane->normal) - plane->dist;
291 side = front < 0;
292
293 if ( (back < 0) == side)
294 return RecursiveLightPoint (node->children[side], start, end);
295
296 frac = front / (front-back);
297 mid[0] = start[0] + (end[0] - start[0])*frac;
298 mid[1] = start[1] + (end[1] - start[1])*frac;
299 mid[2] = start[2] + (end[2] - start[2])*frac;
300
301 // go down front side
302 r = RecursiveLightPoint (node->children[side], start, mid);
303 if (r >= 0)
304 return r; // hit something
305
306 if ( (back < 0) == side )
307 return -1; // didn't hit anuthing
308
309 // check for impact on this node
310 VectorCopy (mid, lightspot);
311 lightplane = plane;
312
313 surf = cl.worldmodel->surfaces + node->firstsurface;
314 for (i=0 ; i<node->numsurfaces ; i++, surf++)
315 {
316 if (surf->flags & SURF_DRAWTILED)
317 continue; // no lightmaps
318
319 tex = surf->texinfo;
320
321 s = DotProduct (mid, tex->vecs[0]) + tex->vecs[0][3];
322 t = DotProduct (mid, tex->vecs[1]) + tex->vecs[1][3];;
323
324 if (s < surf->texturemins[0] ||
325 t < surf->texturemins[1])
326 continue;
327
328 ds = s - surf->texturemins[0];
329 dt = t - surf->texturemins[1];
330
331 if ( ds > surf->extents[0] || dt > surf->extents[1] )
332 continue;
333
334 if (!surf->samples)
335 return 0;
336
337 ds >>= 4;
338 dt >>= 4;
339
340 lightmap = surf->samples;
341 r = 0;
342 if (lightmap)
343 {
344
345 lightmap += dt * ((surf->extents[0]>>4)+1) + ds;
346
347 for (maps = 0 ; maps < MAXLIGHTMAPS && surf->styles[maps] != 255 ;
348 maps++)
349 {
350 scale = d_lightstylevalue[surf->styles[maps]];
351 r += *lightmap * scale;
352 lightmap += ((surf->extents[0]>>4)+1) *
353 ((surf->extents[1]>>4)+1);
354 }
355
356 r >>= 8;
357 }
358
359 return r;
360 }
361
362 // go down back side
363 return RecursiveLightPoint (node->children[!side], mid, end);
364 }
365
R_LightPoint(vec3_t p)366 int R_LightPoint (vec3_t p)
367 {
368 vec3_t end;
369 int r;
370
371 if (!cl.worldmodel->lightdata)
372 return 255;
373
374 end[0] = p[0];
375 end[1] = p[1];
376 end[2] = p[2] - 2048;
377
378 r = RecursiveLightPoint (cl.worldmodel->nodes, p, end);
379
380 if (r == -1)
381 r = 0;
382
383 return r;
384 }
385
386