]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - sv_light.c
added VectorMultiply (not used by anything, but for completeness...)
[xonotic/darkplaces.git] / sv_light.c
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
21 #include "quakedef.h"
22
23 int SV_RecursiveLightPoint (vec3_t color, mnode_t *node, float x, float y, float startz, float endz)
24 {
25         int             side, distz = endz - startz;
26         float   front, back;
27         float   mid;
28
29 loc0:
30         if (node->contents < 0)
31                 return false;           // didn't hit anything
32
33         switch (node->plane->type)
34         {
35         case PLANE_X:
36                 node = node->children[x < node->plane->dist];
37                 goto loc0;
38         case PLANE_Y:
39                 node = node->children[y < node->plane->dist];
40                 goto loc0;
41         case PLANE_Z:
42                 side = startz < node->plane->dist;
43                 if ((endz < node->plane->dist) == side)
44                 {
45                         node = node->children[side];
46                         goto loc0;
47                 }
48                 // found an intersection
49                 mid = node->plane->dist;
50                 break;
51         default:
52                 back = front = x * node->plane->normal[0] + y * node->plane->normal[1];
53                 front += startz * node->plane->normal[2];
54                 back += endz * node->plane->normal[2];
55                 side = front < node->plane->dist;
56                 if ((back < node->plane->dist) == side)
57                 {
58                         node = node->children[side];
59                         goto loc0;
60                 }
61                 // found an intersection
62                 mid = startz + distz * (front - node->plane->dist) / (front - back);
63                 break;
64         }
65         
66         // go down front side
67         if (node->children[side]->contents >= 0 && SV_RecursiveLightPoint (color, node->children[side], x, y, startz, mid))
68                 return true;    // hit something
69         else
70         {
71                 // check for impact on this node
72                 if (node->numsurfaces)
73                 {
74                         int i, ds, dt;
75                         msurface_t *surf;
76
77                         surf = sv.worldmodel->surfaces + node->firstsurface;
78                         for (i = 0;i < node->numsurfaces;i++, surf++)
79                         {
80                                 if (!(surf->flags & SURF_LIGHTMAP))
81                                         continue;
82
83                                 ds = (int) (x * surf->texinfo->vecs[0][0] + y * surf->texinfo->vecs[0][1] + mid * surf->texinfo->vecs[0][2] + surf->texinfo->vecs[0][3]);
84                                 dt = (int) (x * surf->texinfo->vecs[1][0] + y * surf->texinfo->vecs[1][1] + mid * surf->texinfo->vecs[1][2] + surf->texinfo->vecs[1][3]);
85
86                                 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
87                                         continue;
88                                 
89                                 ds -= surf->texturemins[0];
90                                 dt -= surf->texturemins[1];
91                                 
92                                 if (ds > surf->extents[0] || dt > surf->extents[1])
93                                         continue;
94
95                                 if (surf->samples)
96                                 {
97                                         qbyte *lightmap;
98                                         int maps, line3, size3, dsfrac = ds & 15, dtfrac = dt & 15, scale = 0, r00 = 0, g00 = 0, b00 = 0, r01 = 0, g01 = 0, b01 = 0, r10 = 0, g10 = 0, b10 = 0, r11 = 0, g11 = 0, b11 = 0;
99                                         line3 = ((surf->extents[0]>>4)+1)*3;
100                                         size3 = ((surf->extents[0]>>4)+1) * ((surf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
101
102                                         lightmap = surf->samples + ((dt>>4) * ((surf->extents[0]>>4)+1) + (ds>>4))*3; // LordHavoc: *3 for color
103
104                                         for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
105                                         {
106                                                 scale = 256; // FIXME: server doesn't know what light styles are doing
107                                                 r00 += lightmap[      0] * scale;g00 += lightmap[      1] * scale;b00 += lightmap[      2] * scale;
108                                                 r01 += lightmap[      3] * scale;g01 += lightmap[      4] * scale;b01 += lightmap[      5] * scale;
109                                                 r10 += lightmap[line3+0] * scale;g10 += lightmap[line3+1] * scale;b10 += lightmap[line3+2] * scale;
110                                                 r11 += lightmap[line3+3] * scale;g11 += lightmap[line3+4] * scale;b11 += lightmap[line3+5] * scale;
111                                                 lightmap += size3;
112                                         }
113
114                                         color[0] += (float) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)) * (1.0f / 256.0f);
115                                         color[1] += (float) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)) * (1.0f / 256.0f);
116                                         color[2] += (float) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)) * (1.0f / 256.0f);
117                                 }
118                                 return true; // success
119                         }
120                 }
121
122                 // go down back side
123                 node = node->children[side ^ 1];
124                 startz = mid;
125                 distz = endz - startz;
126                 goto loc0;
127         }
128 }
129
130 // LordHavoc: added light checking to the server
131 void SV_LightPoint (vec3_t color, vec3_t p)
132 {
133         Mod_CheckLoaded(sv.worldmodel);
134         if (!sv.worldmodel->lightdata)
135         {
136                 color[0] = color[1] = color[2] = 255;
137                 return;
138         }
139
140         color[0] = color[1] = color[2] = 0;
141         SV_RecursiveLightPoint (color, sv.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
142 }
143