File: tr_vis.cpp

package info (click to toggle)
openmohaa 0.81.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 29,124 kB
  • sloc: ansic: 270,865; cpp: 250,173; sh: 234; asm: 141; xml: 64; makefile: 7
file content (361 lines) | stat: -rw-r--r-- 9,321 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
===========================================================================
Copyright (C) 2023-2024 the OpenMoHAA team

This file is part of OpenMoHAA source code.

OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

// tr_vis.cpp: visibility culling

#include "tr_local.h"
#include "tr_vis.h"

#define MAX_POINTS_ON_WINDING 128
#define MAX_MAP_VISIBILITY    0x200000
#define MAX_VPORTALS          1024

typedef struct {
    int    numpoints;
    vec3_t p[24];
} viswinding_t;

typedef struct {
    int    numpoints;
    vec3_t p[4];
} winding_t;

typedef struct {
    vec3_t normal;
    float  dist;
} plane_t;

typedef struct {
    int           num;
    qboolean      hint;
    plane_t       plane;
    int           leaf;
    vec3_t        origin;
    float         radius;
    viswinding_t *winding;
} vportal_t;

typedef struct leaf_s {
    int        numportals;
    vportal_t *portals[MAX_VPORTALS];
} leaf_t;

int             portalclusters;
int             numportals;
int             numfaces;
int             leafbytes;
int             leaflongs;
int             portalbytes;
int             portallongs;
vportal_t      *portals;
leaf_t         *leafs;
int             numVisBytes;
static char     mapname[256];
static qboolean prev_showportal = qfalse;

winding_t *AllocWinding(int points)
{
    winding_t *w;
    int        s;

    if (points > MAX_POINTS_ON_WINDING) {
        ri.Error(ERR_DROP, "Exceeded MAX_POINTS_ON_WINDING");
        return NULL;
    }
    s = sizeof(vec_t) * 4 * points + sizeof(int) * 4;

    w = (winding_t *)malloc(s);
    if (!w) {
        ri.Error(ERR_DROP, "Couldn't allocate winding");
        return NULL;
    }

    if (s < 0) {
        s = sizeof(vec_t) * 4 * points + sizeof(int) * 4 + 3;
    }

    memset(w, 0, s);

    return w;
}

viswinding_t *AllocVisWinding(int points)
{
    return (viswinding_t *)AllocWinding(points);
}

void PlaneFromWinding(viswinding_t *w, plane_t *plane)
{
    vec3_t v1, v2;

    VectorSubtract(w->p[2], w->p[1], v1);
    VectorSubtract(w->p[2], w->p[1], v1);
    CrossProduct(v2, v1, plane->normal);
    VectorNormalize(plane->normal);

    plane->dist = DotProduct(w->p[0], plane->normal);
}

void SetPortalSphere(vportal_t *p)
{
    int           i;
    vec3_t        total;
    vec3_t        dist;
    viswinding_t *w;
    float         r;
    float         bestr;

    w = p->winding;
    VectorCopy(vec3_origin, total);

    for (i = 0; i < w->numpoints; i++) {
        VectorAdd(total, w->p[i], total);
    }

    for (i = 0; i < 3; i++) {
        total[i] /= w->numpoints;
    }

    bestr = 0;

    for (i = 0; i < w->numpoints; i++) {
        VectorSubtract(w->p[i], total, dist);
        r = VectorLength(dist);
        if (bestr < r) {
            bestr = r;
        }
    }

    VectorCopy(total, p->origin);
    p->radius = bestr;
}

void LoadPortals(const char *name)
{
    int         i, j;
    vportal_t  *p;
    leaf_t     *l;
    const char *magic;
    int         leafnums[2];
    plane_t     plane;
    char       *data;
    char       *f;
    int         numpoints;
    qboolean    bHint;

    if (ri.FS_ReadFile(name, (void **)&f) == -1) {
        ri.Error(ERR_DROP, "LoadPortals: couldn't read %s\n", name);
        return;
    }

    data  = f;
    magic = COM_Parse(&data);

    if (strcmp(magic, "PRT1")) {
        ri.Error(1, "LoadPortals: not a portal file");
    }

    portalclusters = atoi(COM_Parse(&data));
    numportals     = atoi(COM_Parse(&data));
    numfaces       = atoi(COM_Parse(&data));

    ri.Printf(PRINT_DEVELOPER, "%6i portalclusters\n", portalclusters);
    ri.Printf(PRINT_DEVELOPER, "%6i numportals\n", numportals);
    ri.Printf(PRINT_DEVELOPER, "%6i numfaces\n", numfaces);

    leafbytes   = ((portalclusters + 63) & 0xC0) >> 3;
    leaflongs   = leafbytes >> 2;
    portalbytes = ((numportals * 2 + 63) & 0xC0) >> 3;
    portallongs = portalbytes >> 2;

    //
    // Initialize portals
    //
    portals = (vportal_t *)malloc(numportals * sizeof(vportal_t));
    if (!portals) {
        ri.Error(ERR_DROP, "LoadPortals: portals allocation failed\n");
        return;
    }
    memset(portals, 0, numportals * sizeof(vportal_t));

    //
    // Initialize leafs
    //
    leafs = (leaf_t *)malloc(portalclusters * sizeof(leaf_t));
    if (!leafs) {
        ri.Error(ERR_DROP, "LoadPortals: leafs allocation failed\n");
        return;
    }
    memset(leafs, 0, portalclusters * sizeof(leaf_t));

    assert(leafbytes * portalclusters + sizeof(leafnums) <= MAX_MAP_VISIBILITY);
    if (numVisBytes > MAX_MAP_VISIBILITY) {
        ri.Error(ERR_DROP, "LoadPortals: NumVisBytes %d exceeds %d\n", numVisBytes, 0x200000);
    }

    for (i = 0; i < numportals; i++) {
        viswinding_t *w;

        p = &portals[i];

        numpoints   = atoi(COM_Parse(&data));
        leafnums[0] = atoi(COM_Parse(&data));
        leafnums[1] = atoi(COM_Parse(&data));

        if (numpoints > MAX_POINTS_ON_WINDING) {
            ri.Error(ERR_DROP, "LoadPortals: portal %i has too many points", i);
            return;
        }

        if (leafnums[0] > portalclusters || leafnums[1] > portalclusters) {
            ri.Error(ERR_DROP, "LoadPortals: reading portal %i", i);
            return;
        }

        bHint        = atoi(COM_Parse(&data));
        w            = AllocVisWinding(numpoints);
        w->numpoints = numpoints;
        p->winding   = w;

        for (j = 0; j < numpoints; j++) {
            magic = COM_Parse(&data);
            if (magic[0] != '(') {
                ri.Error(ERR_DROP, "LoadPortals: expecting '(' not '%s'", magic);
                return;
            }

            if (magic[1]) {
                w->p[j][0] = atof(magic + 1);
            } else {
                w->p[j][0] = atof(COM_Parse(&data));
            }

            w->p[j][1] = atof(COM_Parse(&data));
            w->p[j][2] = atof(COM_Parse(&data));

            magic = COM_Parse(&data);
            if (strcmp(")", magic)) {
                ri.Error(ERR_DROP, "LoadPortals: expecting ')' not '%s'", magic);
            }
        }

        PlaneFromWinding(w, &plane);

        //
        // Leafs
        //

        // First leaf

        l = &leafs[leafnums[0]];

        if (l->numportals == ARRAY_LEN(l->portals)) {
            ri.Error(ERR_DROP, "Leaf with too many portals");
            return;
        }

        l->portals[l->numportals++] = p;
        p->num                      = i + 1;
        p->hint                     = bHint;
        p->winding                  = w;
        VectorSubtract(vec3_origin, plane.normal, p->plane.normal);
        p->plane.dist = -plane.dist;
        p->leaf = leafnums[1];
        SetPortalSphere(p);

        // Second leaf

        p++;

        l = &leafs[leafnums[1]];
        if (l->numportals == ARRAY_LEN(l->portals)) {
            ri.Error(ERR_DROP, "Leaf with too many portals");
            return;
        }

        l->portals[l->numportals++] = p;
        p->num                      = i + 1;
        p->hint                     = bHint;
        p->winding                  = (viswinding_t *)AllocVisWinding(w->numpoints);
        p->winding->numpoints       = w->numpoints;

        for (j = 0; j < w->numpoints; j++) {
            VectorCopy(w->p[numpoints - (j + 1)], p->winding->p[j]);
        }

        p->plane = plane;
        p->leaf  = leafnums[0];
        SetPortalSphere(p);
    }

    ri.FS_FreeFile(f);
}

void R_VisDebug()
{
    int           leafnum;
    int           cluster;
    int           numportals;
    leaf_t       *leaf;
    vportal_t    *p;
    int           pnum;
    viswinding_t *w;
    int           numpoints;
    int           i;

    if (!r_showportal->integer) {
        return;
    }

    if (!prev_showportal) {
        char buffer[256];

        prev_showportal = qtrue;
        COM_StripExtension(mapname, buffer, sizeof(buffer));
        Q_strcat(buffer, sizeof(buffer), ".prt");
        LoadPortals(buffer);
    }

    leafnum = ri.CM_PointLeafnum(tr.refdef.vieworg);
    cluster = ri.CM_LeafCluster(leafnum);
    if (cluster < 0) {
        return;
    }

    leaf       = &leafs[cluster];
    numportals = leaf->numportals;
    for (pnum = 0; pnum < numportals; pnum++) {
        p = leaf->portals[pnum];
        w = p->winding;

        numpoints = w->numpoints;
        for (i = 0; i < numpoints; i++) {
            R_DebugLine(w->p[i], w->p[(i + 1) % numpoints], 0.0, 1.0, 0.0, 1.0);
        }
    }
}

void R_VisDebugLoad(const char *szBSPName)
{
    prev_showportal = qfalse;
    return Q_strncpyz(mapname, szBSPName, sizeof(mapname));
}