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 - PENTA: almost obsolete now
21 
22 #include "quakedef.h"
23 
24 int	r_dlightframecount;
25 
26 //PENTA: Math utitity's
ProjectVector(const vec3_t b,const vec3_t a,vec3_t c)27 void ProjectVector(const vec3_t b,const vec3_t a,vec3_t c) {
28 
29    float dpa,dpab;
30 
31    dpa = DotProduct(a,a);
32    dpab = DotProduct(a,b)/dpa;
33    c[0] = a[0] * dpab;
34    c[1] = a[1] * dpab;
35    c[2] = a[2] * dpab;
36 }
37 
38 
ProjectPlane(const vec3_t src,const vec3_t v1,const vec3_t v2,vec3_t dst)39 void ProjectPlane(const vec3_t src,const vec3_t v1,const vec3_t v2,vec3_t dst) {
40 
41 	vec3_t t1,t2;
42 
43 	ProjectVector(src, v1, t1);
44 	ProjectVector(src, v2, t2);
45 
46 	VectorAdd(t1,t2,dst);
47 }
48 
49 /*
50 ==================
51 R_AnimateLight
52 ==================
53 */
R_AnimateLight(void)54 void R_AnimateLight (void)
55 {
56 	int			i,j,k;
57 
58 //
59 // light animations
60 // 'm' is normal light, 'a' is no light, 'z' is double bright
61 	i = (int)(cl.time*10);
62 	for (j=0 ; j<MAX_LIGHTSTYLES ; j++)
63 	{
64 		if (!cl_lightstyle[j].length)
65 		{
66 			d_lightstylevalue[j] = 256;
67 			continue;
68 		}
69 		k = i % cl_lightstyle[j].length;
70 		k = cl_lightstyle[j].map[k] - 'a';
71 		k = k*22;
72 		d_lightstylevalue[j] = k;
73 	}
74 }
75 
76 /*
77 =============================================================================
78 
79 LIGHT SAMPLING
80 
81 =============================================================================
82 */
83 
84 mplane_t		*lightplane;
85 vec3_t			lightspot;
86 
RecursiveLightPoint(mnode_t * node,vec3_t start,vec3_t end)87 int RecursiveLightPoint (mnode_t *node, vec3_t start, vec3_t end)
88 {
89 	int			r;
90 	float		front, back, frac;
91 	int			side;
92 	mplane_t	*plane;
93 	vec3_t		mid;
94 	msurface_t	*surf;
95 	int			s, t, ds, dt;
96 	int			i;
97 	mtexinfo_t	*tex;
98 	byte		*lightmap;
99 	unsigned	scale;
100 	int			maps;
101 
102 	if (node->contents < 0)
103 		return -1;		// didn't hit anything
104 
105 // calculate mid point
106 
107 // FIXME: optimize for axial
108 	plane = node->plane;
109 	front = DotProduct (start, plane->normal) - plane->dist;
110 	back = DotProduct (end, plane->normal) - plane->dist;
111 	side = front < 0;
112 
113 	if ( (back < 0) == side)
114 		return RecursiveLightPoint (node->children[side], start, end);
115 
116 	frac = front / (front-back);
117 	mid[0] = start[0] + (end[0] - start[0])*frac;
118 	mid[1] = start[1] + (end[1] - start[1])*frac;
119 	mid[2] = start[2] + (end[2] - start[2])*frac;
120 
121 // go down front side
122 	r = RecursiveLightPoint (node->children[side], start, mid);
123 	if (r >= 0)
124 		return r;		// hit something
125 
126 	if ( (back < 0) == side )
127 		return -1;		// didn't hit anuthing
128 
129 // check for impact on this node
130 	VectorCopy (mid, lightspot);
131 	lightplane = plane;
132 
133 	surf = cl.worldmodel->surfaces + node->firstsurface;
134 	for (i=0 ; i<node->numsurfaces ; i++, surf++)
135 	{
136 		if (surf->flags & SURF_DRAWTILED)
137 			continue;	// no lightmaps
138 
139 		tex = surf->texinfo;
140 
141 		s = DotProduct (mid, tex->vecs[0]) + tex->vecs[0][3];
142 		t = DotProduct (mid, tex->vecs[1]) + tex->vecs[1][3];;
143 
144 		if (s < surf->texturemins[0] ||
145 		t < surf->texturemins[1])
146 			continue;
147 
148 		ds = s - surf->texturemins[0];
149 		dt = t - surf->texturemins[1];
150 
151 		if ( ds > surf->extents[0] || dt > surf->extents[1] )
152 			continue;
153 
154 		if (!surf->samples)
155 			return 0;
156 
157 		ds >>= 4;
158 		dt >>= 4;
159 
160 		lightmap = surf->samples;
161 		r = 0;
162 		if (lightmap)
163 		{
164 
165 			lightmap += dt * ((surf->extents[0]>>4)+1) + ds;
166 
167 			for (maps = 0 ; maps < MAXLIGHTMAPS && surf->styles[maps] != 255 ;
168 					maps++)
169 			{
170 				scale = d_lightstylevalue[surf->styles[maps]];
171 				r += *lightmap * scale;
172 				lightmap += ((surf->extents[0]>>4)+1) *
173 						((surf->extents[1]>>4)+1);
174 			}
175 
176 			r >>= 8;
177 		}
178 
179 		return r;
180 	}
181 
182 // go down back side
183 	return RecursiveLightPoint (node->children[!side], mid, end);
184 }
185 
R_LightPoint(vec3_t p)186 int R_LightPoint (vec3_t p)
187 {
188 	vec3_t		end;
189 	int			r;
190 
191 	if (!cl.worldmodel->lightdata)
192 		return 255;
193 
194 	end[0] = p[0];
195 	end[1] = p[1];
196 	end[2] = p[2] - 2048;
197 
198 	r = RecursiveLightPoint (cl.worldmodel->nodes, p, end);
199 
200 	if (r == -1)
201 		r = 0;
202 
203 	return r;
204 }
205 
206