2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 vec3_t texture_reflectivity[MAX_MAP_TEXINFO];
27 ===================================================================
31 ===================================================================
35 ======================
36 CalcTextureReflectivity_Quake2
37 ======================
39 void CalcTextureReflectivity_Quake2 (void)
50 sprintf (path, "%spics/colormap.pcx", gamedir);
52 // get the game palette
53 Load256Image (path, NULL, &palette, NULL, NULL);
55 // allways set index 0 even if no textures
56 texture_reflectivity[0][0] = 0.5;
57 texture_reflectivity[0][1] = 0.5;
58 texture_reflectivity[0][2] = 0.5;
60 for (i=0 ; i<numtexinfo ; i++)
62 // see if an earlier texinfo allready got the value
65 if (!strcmp (texinfo[i].texture, texinfo[j].texture))
67 VectorCopy (texture_reflectivity[j], texture_reflectivity[i]);
75 sprintf (path, "%stextures/%s.wal", gamedir, texinfo[i].texture);
76 if (TryLoadFile (path, (void **)&mt) == -1)
78 Sys_Printf ("Couldn't load %s\n", path);
79 texture_reflectivity[i][0] = 0.5;
80 texture_reflectivity[i][1] = 0.5;
81 texture_reflectivity[i][2] = 0.5;
84 texels = LittleLong(mt->width)*LittleLong(mt->height);
85 color[0] = color[1] = color[2] = 0;
87 for (j=0 ; j<texels ; j++)
89 texel = ((byte *)mt)[LittleLong(mt->offsets[0]) + j];
91 color[k] += palette[texel*3+k];
96 r = color[j]/texels/255.0;
97 texture_reflectivity[i][j] = r;
99 // scale the reflectivity up, because the textures are
101 scale = ColorNormalize (texture_reflectivity[i],
102 texture_reflectivity[i]);
106 VectorScale (texture_reflectivity[i], scale, texture_reflectivity[i]);
109 texture_reflectivity[i][0] = 0.5;
110 texture_reflectivity[i][1] = 0.5;
111 texture_reflectivity[i][2] = 0.5;
117 ======================
118 CalcTextureReflectivity_Heretic2
119 ======================
121 void CalcTextureReflectivity_Heretic2 (void)
134 // allways set index 0 even if no textures
135 texture_reflectivity[0][0] = 0.5;
136 texture_reflectivity[0][1] = 0.5;
137 texture_reflectivity[0][2] = 0.5;
139 for (i=0 ; i<numtexinfo ; i++)
141 // see if an earlier texinfo allready got the value
142 for (j=0 ; j<i ; j++)
144 if (!strcmp (texinfo[i].texture, texinfo[j].texture))
146 VectorCopy (texture_reflectivity[j], texture_reflectivity[i]);
155 sprintf (path, "%stextures/%s.m32", gamedir, texinfo[i].texture);
156 if (TryLoadFile (path, (void **)&mt32) == -1)
158 sprintf (path, "%stextures/%s.m8", gamedir, texinfo[i].texture);
159 if (TryLoadFile (path, (void **)&mt) == -1)
161 Sys_Printf ("Couldn't load %s\n", path);
162 texture_reflectivity[i][0] = 0.5;
163 texture_reflectivity[i][1] = 0.5;
164 texture_reflectivity[i][2] = 0.5;
167 texels = LittleLong(mt->width[0])*LittleLong(mt->height[0]);
168 color[0] = color[1] = color[2] = 0;
170 for (j=0 ; j<texels ; j++)
172 texel = ((byte *)mt)[LittleLong(mt->offsets[0]) + j];
173 color[0] += mt->palette[texel].r;
174 color[1] += mt->palette[texel].g;
175 color[2] += mt->palette[texel].b;
182 texels = LittleLong(mt32->width[0])*LittleLong(mt32->height[0]);
183 color[0] = color[1] = color[2] = 0;
185 for (j=0 ; j<texels ; j++)
187 pos = (byte *)mt32 + mt32->offsets[0] + (j<<2);
188 color[0] += *pos++; // r
189 color[1] += *pos++; // g
190 color[2] += *pos++; // b
197 for (j=0 ; j<3 ; j++)
199 r = color[j]/((float) texels*255.0);
200 texture_reflectivity[i][j] = r;
206 =======================================================================
210 =======================================================================
218 winding_t *WindingFromFace (dface_t *f)
226 w = AllocWinding (f->numedges);
227 w->numpoints = f->numedges;
229 for (i=0 ; i<f->numedges ; i++)
231 se = dsurfedges[f->firstedge + i];
233 v = dedges[-se].v[1];
238 VectorCopy (dv->point, w->p[i]);
241 RemoveColinearPoints (w);
251 void BaseLightForFace (dface_t *f, vec3_t color)
256 // check for light emited by texture
258 tx = &texinfo[f->texinfo];
259 if (!(tx->flags & SURF_LIGHT) || tx->value == 0)
265 VectorScale (texture_reflectivity[f->texinfo], tx->value, color);
268 qboolean IsSky (dface_t *f)
272 tx = &texinfo[f->texinfo];
273 if (tx->flags & SURF_SKY)
284 void MakePatchForFace (int fn, winding_t *w)
296 area = WindingArea (w);
299 patch = &patches[num_patches];
300 if (num_patches == MAX_PATCHES)
301 Error ("num_patches == MAX_PATCHES");
302 patch->next = face_patches[fn];
303 face_patches[fn] = patch;
308 patch->plane = &backplanes[f->planenum];
310 patch->plane = &dplanes[f->planenum];
311 if (face_offset[fn][0] || face_offset[fn][1] || face_offset[fn][2] )
312 { // origin offset faces must create new planes
313 if (numplanes + fakeplanes >= MAX_MAP_PLANES)
314 Error ("numplanes + fakeplanes >= MAX_MAP_PLANES");
315 pl = &dplanes[numplanes + fakeplanes];
318 *pl = *(patch->plane);
319 pl->dist += DotProduct (face_offset[fn], pl->normal);
323 WindingCenter (w, patch->origin);
324 VectorAdd (patch->origin, patch->plane->normal, patch->origin);
325 leaf = Rad_PointInLeaf(patch->origin);
326 patch->cluster = leaf->cluster;
327 if (patch->cluster == -1)
328 Sys_FPrintf( SYS_VRB, "patch->cluster == -1\n");
331 if (patch->area <= 1)
333 patch->sky = IsSky (f);
335 VectorCopy (texture_reflectivity[f->texinfo], patch->reflectivity);
337 // non-bmodel patches can emit light
338 if (fn < dmodels[0].numfaces)
340 BaseLightForFace (f, patch->baselight);
342 ColorNormalize (patch->reflectivity, color);
344 for (i=0 ; i<3 ; i++)
345 patch->baselight[i] *= color[i];
347 VectorCopy (patch->baselight, patch->totallight);
353 entity_t *EntityForModel (int modnum)
359 sprintf (name, "*%i", modnum);
360 // search the entities for one using modnum
361 for (i=0 ; i<num_entities ; i++)
363 s = ValueForKey (&entities[i], "model");
364 if (!strcmp (s, name))
376 void MakePatches (void)
386 Sys_FPrintf( SYS_VRB, "%i faces\n", numfaces);
388 for (i=0 ; i<nummodels ; i++)
391 ent = EntityForModel (i);
392 // bmodels with origin brushes need to be offset into their
394 GetVectorForKey (ent, "origin", origin);
395 //VectorCopy (vec3_origin, origin);
397 for (j=0 ; j<mod->numfaces ; j++)
399 fn = mod->firstface + j;
400 face_entity[fn] = ent;
401 VectorCopy (origin, face_offset[fn]);
403 w = WindingFromFace (f);
404 for (k=0 ; k<w->numpoints ; k++)
406 VectorAdd (w->p[k], origin, w->p[k]);
408 MakePatchForFace (fn, w);
412 Sys_FPrintf( SYS_VRB, "%i sqaure feet\n", (int)(totalarea/64));
416 =======================================================================
420 =======================================================================
423 void FinishSplit (patch_t *patch, patch_t *newp)
427 VectorCopy (patch->baselight, newp->baselight);
428 VectorCopy (patch->totallight, newp->totallight);
429 VectorCopy (patch->reflectivity, newp->reflectivity);
430 newp->plane = patch->plane;
431 newp->sky = patch->sky;
433 patch->area = WindingArea (patch->winding);
434 newp->area = WindingArea (newp->winding);
436 if (patch->area <= 1)
441 WindingCenter (patch->winding, patch->origin);
442 VectorAdd (patch->origin, patch->plane->normal, patch->origin);
443 leaf = Rad_PointInLeaf(patch->origin);
444 patch->cluster = leaf->cluster;
445 if (patch->cluster == -1)
446 Sys_FPrintf( SYS_VRB, "patch->cluster == -1\n");
448 WindingCenter (newp->winding, newp->origin);
449 VectorAdd (newp->origin, newp->plane->normal, newp->origin);
450 leaf = Rad_PointInLeaf(newp->origin);
451 newp->cluster = leaf->cluster;
452 if (newp->cluster == -1)
453 Sys_FPrintf( SYS_VRB, "patch->cluster == -1\n");
460 Chops the patch only if its local bounds exceed the max size
463 void SubdividePatch (patch_t *patch)
465 winding_t *w, *o1, *o2;
466 vec3_t mins, maxs, total;
474 mins[0] = mins[1] = mins[2] = 99999;
475 maxs[0] = maxs[1] = maxs[2] = -99999;
476 for (i=0 ; i<w->numpoints ; i++)
478 for (j=0 ; j<3 ; j++)
487 VectorSubtract (maxs, mins, total);
488 for (i=0 ; i<3 ; i++)
489 if (total[i] > (subdiv+1) )
493 // no splitting needed
500 VectorCopy (vec3_origin, split);
502 dist = (mins[i] + maxs[i])*0.5;
503 ClipWindingEpsilon (w, split, dist, ON_EPSILON, &o1, &o2);
506 // create a new patch
508 if (num_patches == MAX_PATCHES)
509 Error ("MAX_PATCHES");
510 newp = &patches[num_patches];
513 newp->next = patch->next;
519 FinishSplit (patch, newp);
521 SubdividePatch (patch);
522 SubdividePatch (newp);
530 Chops the patch by a global grid
533 void DicePatch (patch_t *patch)
535 winding_t *w, *o1, *o2;
543 WindingBounds (w, mins, maxs);
544 for (i=0 ; i<3 ; i++)
545 if (floor((mins[i]+1)/subdiv) < floor((maxs[i]-1)/subdiv))
549 // no splitting needed
556 VectorCopy (vec3_origin, split);
558 dist = subdiv*(1+floor((mins[i]+1)/subdiv));
559 ClipWindingEpsilon (w, split, dist, ON_EPSILON, &o1, &o2);
562 // create a new patch
564 if (num_patches == MAX_PATCHES)
565 Error ("MAX_PATCHES");
566 newp = &patches[num_patches];
569 newp->next = patch->next;
575 FinishSplit (patch, newp);
587 void SubdividePatches (void)
594 num = num_patches; // because the list will grow
595 for (i=0 ; i<num ; i++)
597 // SubdividePatch (&patches[i]);
598 DicePatch (&patches[i]);
600 Sys_FPrintf( SYS_VRB, "%i patches after subdivision\n", num_patches);
603 //=====================================================================