2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
23 #define MAX_DECALS 2048
25 typedef struct decal_s
33 byte *lightmapaddress;
39 int currentdecal; // wraps around in decal array, replacing old ones when a new one is needed
41 cvar_t r_drawdecals = {"r_drawdecals", "1"};
42 cvar_t r_decals_lighting = {"r_decals_lighting", "1"};
46 decals = (decal_t *) qmalloc(MAX_DECALS * sizeof(decal_t));
47 memset(decals, 0, MAX_DECALS * sizeof(decal_t));
51 void r_decals_shutdown()
56 void r_decals_newmap()
58 memset(decals, 0, MAX_DECALS * sizeof(decal_t));
64 Cvar_RegisterVariable (&r_drawdecals);
65 Cvar_RegisterVariable (&r_decals_lighting);
67 R_RegisterModule("R_Decals", r_decals_start, r_decals_shutdown, r_decals_newmap);
70 void R_Decal(vec3_t org, rtexture_t *tex, float scale, int cred, int cgreen, int cblue, int alpha)
72 int i, ds, dt, bestlightmapofs;
74 vec3_t impact, right, up;
77 msurface_t *surf/*, **mark, **endmark*/, *bestsurf;
82 // leaf = Mod_PointInLeaf(org, cl.worldmodel);
83 // if (!leaf->nummarksurfaces)
86 // mark = leaf->firstmarksurface;
87 // endmark = mark + leaf->nummarksurfaces;
89 // find the best surface to place the decal on
93 // while(mark < endmark)
96 surf = &cl.worldmodel->surfaces[cl.worldmodel->firstmodelsurface];
97 for (i = 0;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
99 if (surf->flags & SURF_DRAWTILED)
100 continue; // no lightmaps
102 dist = PlaneDiff(org, surf->plane);
103 if (surf->flags & SURF_PLANEBACK)
107 if (dist >= bestdist)
110 impact[0] = org[0] - surf->plane->normal[0] * dist;
111 impact[1] = org[1] - surf->plane->normal[1] * dist;
112 impact[2] = org[2] - surf->plane->normal[2] * dist;
114 ds = (int) (DotProduct(impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3]);
115 dt = (int) (DotProduct(impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3]);
117 if (ds < surf->texturemins[0] || dt < surf->texturemins[1])
120 ds -= surf->texturemins[0];
121 dt -= surf->texturemins[1];
123 if (ds > surf->extents[0] || dt > surf->extents[1])
128 bestlightmapofs = (dt >> 4) * ((surf->extents[0] >> 4) + 1) + (ds >> 4);
130 // abort if no suitable surface was found
131 if (bestsurf == NULL)
134 // grab a decal from the array and advance to the next decal to replace, wrapping to replace an old decal if necessary
135 decal = decals + currentdecal;
137 if (currentdecal >= MAX_DECALS)
141 if (bestsurf->flags & SURF_PLANEBACK)
143 VectorCopy(bestsurf->plane->normal, decal->direction);
147 VectorNegate(bestsurf->plane->normal, decal->direction);
149 // - 0.25 to push it off the surface a bit
150 decal->org[0] = impact[0] = org[0] + decal->direction[0] * (bestdist - 0.25f);
151 decal->org[1] = impact[1] = org[1] + decal->direction[1] * (bestdist - 0.25f);
152 decal->org[2] = impact[2] = org[2] + decal->direction[2] * (bestdist - 0.25f);
153 // set up the 4 corners
155 VectorVectors(decal->direction, right, up);
156 decal->vert[0][0] = impact[0] - up[0] * scale - right[0] * scale;
157 decal->vert[0][1] = impact[1] - up[1] * scale - right[1] * scale;
158 decal->vert[0][2] = impact[2] - up[2] * scale - right[2] * scale;
159 decal->vert[1][0] = impact[0] + up[0] * scale - right[0] * scale;
160 decal->vert[1][1] = impact[1] + up[1] * scale - right[1] * scale;
161 decal->vert[1][2] = impact[2] + up[2] * scale - right[2] * scale;
162 decal->vert[2][0] = impact[0] + up[0] * scale + right[0] * scale;
163 decal->vert[2][1] = impact[1] + up[1] * scale + right[1] * scale;
164 decal->vert[2][2] = impact[2] + up[2] * scale + right[2] * scale;
165 decal->vert[3][0] = impact[0] - up[0] * scale + right[0] * scale;
166 decal->vert[3][1] = impact[1] - up[1] * scale + right[1] * scale;
167 decal->vert[3][2] = impact[2] - up[2] * scale + right[2] * scale;
169 decal->color[0] = (byte) bound(0, cred, 255);
170 decal->color[1] = (byte) bound(0, cgreen, 255);
171 decal->color[2] = (byte) bound(0, cblue, 255);
172 decal->color[3] = (byte) bound(0, alpha, 255);
173 // store the surface information for lighting
174 decal->surface = bestsurf;
175 decal->lightmapstep = ((bestsurf->extents[0]>>4)+1) * ((bestsurf->extents[1]>>4)+1)*3; // LordHavoc: *3 for colored lighting
176 if (bestsurf->samples)
177 decal->lightmapaddress = bestsurf->samples + bestlightmapofs * 3; // LordHavoc: *3 for colored lighitng
179 decal->lightmapaddress = NULL;
182 void R_DrawDecals (void)
185 int i, j, k, dynamiclight, ir, ig, ib, maps, bits;
187 float scale, fr, fg, fb, dist, rad, mindist;
193 if (!r_drawdecals.value)
196 dynamiclight = (int) r_dynamic.value != 0 && (int) r_decals_lighting.value != 0;
198 mindist = DotProduct(r_refdef.vieworg, vpn) + 4.0f;
200 for (i = 0, p = decals;i < MAX_DECALS;i++, p++)
205 // do not render if the decal is behind the view
206 if (DotProduct(p->org, vpn) < mindist)
209 // do not render if the view origin is behind the decal
210 VectorSubtract(p->org, r_refdef.vieworg, v);
211 if (DotProduct(p->direction, v) < 0)
214 // get the surface lighting
216 lightmap = p->lightmapaddress;
220 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
222 scale = d_lightstylevalue[surf->styles[maps]];
223 fr += lightmap[0] * scale;
224 fg += lightmap[1] * scale;
225 fb += lightmap[2] * scale;
226 lightmap += p->lightmapstep;
229 fr *= (1.0f / 256.0f);
230 fg *= (1.0f / 256.0f);
231 fb *= (1.0f / 256.0f);
235 if (surf->dlightframe == r_dlightframecount)
237 for (j = 0;j < 8;j++)
239 bits = surf->dlightbits[j];
242 for (k = 0, dl = cl_dlights + j * 32;bits;k++, dl++)
247 VectorSubtract(p->org, dl->origin, v);
248 dist = DotProduct(v, v) + LIGHTOFFSET;
249 rad = dl->radius * dl->radius;
252 rad *= 128.0f / dist;
253 fr += rad * dl->color[0];
254 fg += rad * dl->color[1];
255 fb += rad * dl->color[2];
263 // apply color to lighting
264 ir = (int) (fr * p->color[0] * (1.0f / 128.0f));
265 ig = (int) (fg * p->color[1] * (1.0f / 128.0f));
266 ib = (int) (fb * p->color[2] * (1.0f / 128.0f));
267 // compute byte color
268 br = (byte) min(ir, 255);
269 bg = (byte) min(ig, 255);
270 bb = (byte) min(ib, 255);
272 // put into transpoly system for sorted drawing later
273 transpolybegin(R_GetTexture(p->tex), 0, R_GetTexture(p->tex), TPOLYTYPE_ALPHA);
274 transpolyvertub(p->vert[0][0], p->vert[0][1], p->vert[0][2], 0,1,br,bg,bb,ba);
275 transpolyvertub(p->vert[1][0], p->vert[1][1], p->vert[1][2], 0,0,br,bg,bb,ba);
276 transpolyvertub(p->vert[2][0], p->vert[2][1], p->vert[2][2], 1,0,br,bg,bb,ba);
277 transpolyvertub(p->vert[3][0], p->vert[3][1], p->vert[3][2], 1,1,br,bg,bb,ba);