File: tr_util.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 (459 lines) | stat: -rw-r--r-- 10,267 bytes parent folder | download | duplicates (2)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
===========================================================================
Copyright (C) 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_util.c -- renderer utility

#include "tr_local.h"
#include "../qcommon/str.h"

static byte  cntColor[4];
static float cntSt[2];

static int Numbers[12][8] = {
    {1, 3,  4,  5,  6, 7, 0, 0},
    {4, 5,  0,  0,  0, 0, 0, 0},
    {1, 4,  2,  7,  3, 0, 0, 0},
    {1, 4,  2,  5,  3, 0, 0, 0},
    {6, 4,  2,  5,  0, 0, 0, 0},
    {1, 6,  2,  5,  3, 0, 0, 0},
    {1, 6,  2,  5,  7, 3, 0, 0},
    {1, 8,  0,  0,  0, 0, 0, 0},
    {1, 2,  3,  4,  5, 6, 7, 0},
    {1, 6,  4,  2,  5, 3, 0, 0},
    {9, 10, 11, 12, 0, 0, 0, 0},
    {2, 0,  0,  0,  0, 0, 0, 0}
};

static float Lines[13][4] = {
    {0.0,  0.0, 0.0,  0.0},
    {-4.0, 8.0, 4.0,  8.0},
    {-4.0, 4.0, 4.0,  4.0},
    {-4.0, 0.0, 4.0,  0.0},
    {4.0,  8.0, 4.0,  4.0},
    {4.0,  4.0, 4.0,  0.0},
    {-4.0, 8.0, -4.0, 4.0},
    {-4.0, 4.0, -4.0, 0.0},
    {4.0,  8.0, -4.0, 0.0},
    {-1.0, 2.0, 1.0,  2.0},
    {1.0,  2.0, 1.0,  0.0},
    {-1.0, 0.0, 1.0,  0.0},
    {-1.0, 0.0, -1.0, 2.0}
};

/*
===============
RB_StreamBegin
===============
*/
void RB_StreamBegin(shader_t *shader)
{
    RB_BeginSurface(shader);
}

/*
===============
RB_StreamEnd
===============
*/
void RB_StreamEnd(void)
{
    int i;

    if (tess.numVertexes <= 2) {
        RB_EndSurface();
        return;
    }

    tess.indexes[0] = 0;
    tess.indexes[1] = 1;
    tess.indexes[2] = 2;

    for (i = 0; i < tess.numVertexes - 2; i++) {
        tess.indexes[i * 3 + 0] = (i & 1) + i;
        tess.indexes[i * 3 + 1] = i - ((i & 1) - 1);
        tess.indexes[i * 3 + 2] = i + 2;
        tess.numIndexes += 3;
    }

    RB_EndSurface();
}

/*
===============
RB_StreamBeginDrawSurf
===============
*/
void RB_StreamBeginDrawSurf(void)
{
    backEnd.dsStreamVert = tess.numVertexes;
}

/*
===============
RB_StreamEndDrawSurf
===============
*/
void RB_StreamEndDrawSurf(void)
{
    int numverts;
    int i;

    if (tess.numVertexes - backEnd.dsStreamVert <= 2) {
        tess.numVertexes = backEnd.dsStreamVert;
        return;
    }

    numverts = tess.numVertexes - backEnd.dsStreamVert - 2;
    for (i = 0; i < numverts; i++) {
        tess.indexes[i + tess.numIndexes]     = (i & 1) + i + backEnd.dsStreamVert;
        tess.indexes[i + tess.numIndexes + 1] = i + backEnd.dsStreamVert - ((i & 1) - 1);
        tess.indexes[i + tess.numIndexes + 2] = i + backEnd.dsStreamVert + 2;
        tess.numIndexes += 3;
    }
}

/*
===============
addTriangle
===============
*/
static void addTriangle(void)
{
    tess.texCoords[tess.numVertexes][0][0] = cntSt[0];
    tess.texCoords[tess.numVertexes][0][1] = cntSt[1];
    tess.vertexColors[tess.numVertexes][0] = cntColor[0];
    tess.vertexColors[tess.numVertexes][1] = cntColor[1];
    tess.vertexColors[tess.numVertexes][2] = cntColor[2];
    tess.vertexColors[tess.numVertexes][3] = cntColor[3];
    tess.vertexColorValid                  = qtrue;
    tess.numVertexes++;
}

/*
===============
RB_Vertex3fv
===============
*/
void RB_Vertex3fv(vec3_t v)
{
    VectorCopy(v, tess.xyz[tess.numVertexes]);
    addTriangle();
}

/*
===============
RB_Vertex3f
===============
*/
void RB_Vertex3f(vec_t x, vec_t y, vec_t z)
{
    tess.xyz[tess.numVertexes][0] = x;
    tess.xyz[tess.numVertexes][1] = y;
    tess.xyz[tess.numVertexes][2] = z;
    addTriangle();
}

/*
===============
RB_Vertex2f
===============
*/
void RB_Vertex2f(vec_t x, vec_t y)
{
    RB_Vertex3f(x, y, 0);
}

/*
===============
RB_Color4f
===============
*/
void RB_Color4f(vec_t r, vec_t g, vec_t b, vec_t a)
{
    cntColor[0] = r * tr.identityLightByte;
    cntColor[1] = g * tr.identityLightByte;
    cntColor[2] = b * tr.identityLightByte;
    cntColor[3] = a * 255.0;
}

/*
===============
RB_Color3f
===============
*/
void RB_Color3f(vec_t r, vec_t g, vec_t b)
{
    RB_Color4f(r, g, b, 1.0);
}

/*
===============
RB_Color3fv
===============
*/
void RB_Color3fv(vec3_t col)
{
    RB_Color3f(col[0], col[1], col[2]);
}

/*
===============
RB_Color4bv
===============
*/
void RB_Color4bv(unsigned char *colors)
{
    cntColor[0] = colors[0];
    cntColor[1] = colors[1];
    cntColor[2] = colors[2];
    cntColor[3] = colors[3];
}

/*
===============
RB_Texcoord2f
===============
*/
void RB_Texcoord2f(float s, float t)
{
    cntSt[0] = s;
    cntSt[1] = t;
}

/*
===============
RB_Texcoord2fv
===============
*/
void RB_Texcoord2fv(vec2_t st)
{
    cntSt[0] = st[0];
    cntSt[1] = st[1];
}

/*
===============
R_DrawDebugNumber
===============
*/
void R_DrawDebugNumber(const vec3_t org, float number, float scale, float r, float g, float b, int precision)
{
    vec3_t up, right;
    vec3_t pos1, pos2;
    vec3_t start;
    str    text;
    char   format[20];
    int    i;
    int    j;
    int    l;
    int    num;

    VectorCopy(tr.viewParms.ori.axis[2], up);
    VectorCopy(tr.viewParms.ori.axis[1], right);
    VectorNegate(right, right);

    VectorNormalize(up);
    VectorNormalize(right);

    VectorScale(up, scale, up);
    VectorScale(right, scale, right);

    if (precision > 0) {
        Com_sprintf(format, sizeof(format), "%%.%df", precision);
        text = va(format, number);
    } else {
        text = va("%d", (int)number);
    }

    // NOTE: this cast here is needed!
    // Otherwise the compiler will do very nasty implicit casting of negative
    // numbers to unsigned numbers, resulting in incorrect values.
    // -------------------v
    VectorMA(org, 5 - 5 * (int)text.length(), right, start);

    // Draw each character/digit of the text
    for (i = 0; i < text.length(); i++) {
        if (text[i] == '.') {
            num = 10;
        } else if (text[i] == '-') {
            num = 11;
        } else {
            num = text[i] - '0';
        }

        // Get the first line index for the number to be drawn
        l = Numbers[num][0];

        // Draw each line of the character/digit
        for (j = 0; j < 8 && l; ++j, l = Numbers[num][j]) {
            pos1[0] = start[0] + Lines[l][0] * right[0] + Lines[l][1] * up[0];
            pos1[1] = start[1] + Lines[l][0] * right[1] + Lines[l][1] * up[1];
            pos1[2] = start[2] + Lines[l][0] * right[2] + Lines[l][1] * up[2];

            pos2[0] = start[0] + Lines[l][2] * right[0] + Lines[l][3] * up[0];
            pos2[1] = start[1] + Lines[l][2] * right[1] + Lines[l][3] * up[1];
            pos2[2] = start[2] + Lines[l][2] * right[2] + Lines[l][3] * up[2];

            R_DebugLine(pos1, pos2, r, g, b, 1.0);
        }

        VectorMA(start, 10.0, right, start);
    }
}

/*
===============
R_DebugRotatedBBox
===============
*/
void R_DebugRotatedBBox(
    const vec3_t org, const vec3_t ang, const vec3_t mins, const vec3_t maxs, float r, float g, float b, float alpha
)
{
    int    i;
    vec3_t tmp;
    vec3_t points[8];
    vec3_t axis[3];

    AnglesToAxis(ang, axis);

    for (i = 0; i < 8; i++) {
        if (i & 1) {
            tmp[0] = mins[0];
        } else {
            tmp[0] = maxs[0];
        }

        if (i & 2) {
            tmp[1] = mins[1];
        } else {
            tmp[1] = maxs[1];
        }

        if (i & 4) {
            tmp[2] = mins[2];
        } else {
            tmp[2] = maxs[2];
        }

        points[i][0] = tmp[0] * axis[0][0];
        points[i][1] = tmp[0] * axis[0][1];
        points[i][2] = tmp[0] * axis[0][2];

        points[i][0] += tmp[1] * axis[1][0];
        points[i][1] += tmp[1] * axis[1][1];
        points[i][2] += tmp[1] * axis[1][2];

        points[i][0] += tmp[2] * axis[2][0];
        points[i][1] += tmp[2] * axis[2][1];
        points[i][2] += tmp[2] * axis[2][2];

        points[i][0] += org[0];
        points[i][1] += org[1];
        points[i][2] += org[2];
    }

    R_DebugLine(points[0], points[1], r, g, b, alpha);
    R_DebugLine(points[1], points[3], r, g, b, alpha);
    R_DebugLine(points[3], points[2], r, g, b, alpha);
    R_DebugLine(points[2], points[0], r, g, b, alpha);
    R_DebugLine(points[4], points[5], r, g, b, alpha);
    R_DebugLine(points[5], points[7], r, g, b, alpha);
    R_DebugLine(points[7], points[6], r, g, b, alpha);
    R_DebugLine(points[6], points[4], r, g, b, alpha);

    for (i = 0; i < 4; i++) {
        R_DebugLine(points[i], points[i + 4], r, g, b, alpha);
    }
}

/*
===============
RE_GetShaderWidth
===============
*/
int RE_GetShaderWidth(qhandle_t hShader)
{
    shader_t *shader;

    if (hShader) {
        shader = R_GetShaderByHandle(hShader);
    } else {
        shader = tr.defaultShader;
    }

    return shader->unfoggedStages[0]->bundle[0].image[0]->uploadWidth;
}

/*
===============
RE_GetShaderHeight
===============
*/
int RE_GetShaderHeight(qhandle_t hShader)
{
    shader_t *shader;

    if (hShader) {
        shader = R_GetShaderByHandle(hShader);
    } else {
        shader = tr.defaultShader;
    }

    return shader->unfoggedStages[0]->bundle[0].image[0]->uploadHeight;
}

/*
===============
RE_GetShaderName
===============
*/
const char *RE_GetShaderName(qhandle_t hShader)
{
    shader_t *shader;

    if (hShader) {
        shader = R_GetShaderByHandle(hShader);
    } else {
        shader = tr.defaultShader;
    }

    return shader->name;
}

/*
===============
RE_GetModelName
===============
*/
const char *RE_GetModelName(qhandle_t hModel)
{
    model_t *model;

    if (!hModel) {
        return "";
    }

    model = R_GetModelByHandle(hModel);

    return model->name;
}