forked from NVIDIA/Q2RTX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.c
560 lines (457 loc) · 13 KB
/
view.c
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
Copyright (C) 1997-2001 Id Software, Inc.
Copyright (C) 2019, NVIDIA CORPORATION. All rights reserved.
This program 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.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// cl_view.c -- player rendering positioning
#include "client.h"
//=============
//
// development tools for weapons
//
int gun_frame;
qhandle_t gun_model;
//=============
static cvar_t *cl_add_particles;
#if USE_DLIGHTS
static cvar_t *cl_add_lights;
static cvar_t *cl_show_lights;
#endif
static cvar_t *cl_add_entities;
static cvar_t *cl_add_blend;
#ifdef _DEBUG
static cvar_t *cl_testparticles;
static cvar_t *cl_testentities;
#if USE_DLIGHTS
static cvar_t *cl_testlights;
#endif
static cvar_t *cl_testblend;
static cvar_t *cl_stats;
#endif
static cvar_t *cl_adjustfov;
#if USE_DLIGHTS
int r_numdlights;
dlight_t r_dlights[MAX_DLIGHTS];
#endif
int r_numentities;
entity_t r_entities[MAX_ENTITIES];
int r_numparticles;
particle_t r_particles[MAX_PARTICLES];
lightstyle_t r_lightstyles[MAX_LIGHTSTYLES];
/*
====================
V_ClearScene
Specifies the model that will be used as the world
====================
*/
static void V_ClearScene(void)
{
#if USE_DLIGHTS
r_numdlights = 0;
#endif
r_numentities = 0;
r_numparticles = 0;
}
/*
=====================
V_AddEntity
=====================
*/
void V_AddEntity(entity_t *ent)
{
if (r_numentities >= MAX_ENTITIES)
return;
r_entities[r_numentities++] = *ent;
}
/*
=====================
V_AddParticle
=====================
*/
void V_AddParticle(particle_t *p)
{
if (r_numparticles >= MAX_PARTICLES)
return;
r_particles[r_numparticles++] = *p;
}
#if USE_DLIGHTS
/*
=====================
V_AddLight
=====================
*/
void V_AddLightEx(vec3_t org, float intensity, float r, float g, float b, float radius)
{
dlight_t *dl;
if (r_numdlights >= MAX_DLIGHTS)
return;
dl = &r_dlights[r_numdlights++];
VectorCopy(org, dl->origin);
dl->intensity = intensity;
dl->color[0] = r;
dl->color[1] = g;
dl->color[2] = b;
dl->radius = radius;
if (cl_show_lights->integer && r_numparticles < MAX_PARTICLES)
{
particle_t* part = &r_particles[r_numparticles++];
VectorCopy(dl->origin, part->origin);
part->radius = radius;
part->brightness = max(r, max(g, b));
part->color = -1;
part->rgba.u8[0] = (uint8_t)max(0.f, min(255.f, r / part->brightness * 255.f));
part->rgba.u8[1] = (uint8_t)max(0.f, min(255.f, g / part->brightness * 255.f));
part->rgba.u8[2] = (uint8_t)max(0.f, min(255.f, b / part->brightness * 255.f));
part->rgba.u8[3] = 255;
part->alpha = 1.f;
}
}
void V_AddLight(vec3_t org, float intensity, float r, float g, float b)
{
V_AddLightEx(org, intensity, r, g, b, 10.f);
}
#endif
/*
=====================
V_AddLightStyle
=====================
*/
void V_AddLightStyle(int style, vec4_t value)
{
lightstyle_t *ls;
if (style < 0 || style >= MAX_LIGHTSTYLES)
Com_Error(ERR_DROP, "Bad light style %i", style);
ls = &r_lightstyles[style];
//ls->white = r+g+b;
ls->rgb[0] = value[0];
ls->rgb[1] = value[1];
ls->rgb[2] = value[2];
ls->white = value[3];
}
#ifdef _DEBUG
/*
================
V_TestParticles
If cl_testparticles is set, create 4096 particles in the view
================
*/
static void V_TestParticles(void)
{
particle_t *p;
int i, j;
float d, r, u;
r_numparticles = MAX_PARTICLES;
for (i = 0; i < r_numparticles; i++) {
d = i * 0.25f;
r = 4 * ((i & 7) - 3.5f);
u = 4 * (((i >> 3) & 7) - 3.5f);
p = &r_particles[i];
for (j = 0; j < 3; j++)
p->origin[j] = cl.refdef.vieworg[j] + cl.v_forward[j] * d +
cl.v_right[j] * r + cl.v_up[j] * u;
p->color = 8;
p->alpha = 1;
}
}
/*
================
V_TestEntities
If cl_testentities is set, create 32 player models
================
*/
static void V_TestEntities(void)
{
int i, j;
float f, r;
entity_t *ent;
r_numentities = 32;
memset(r_entities, 0, sizeof(r_entities));
for (i = 0; i < r_numentities; i++) {
ent = &r_entities[i];
r = 64 * ((i % 4) - 1.5f);
f = 64 * (i / 4) + 128;
for (j = 0; j < 3; j++)
ent->origin[j] = cl.refdef.vieworg[j] + cl.v_forward[j] * f +
cl.v_right[j] * r;
ent->model = cl.baseclientinfo.model;
ent->skin = cl.baseclientinfo.skin;
}
}
#if USE_DLIGHTS
/*
================
V_TestLights
If cl_testlights is set, create 32 lights models
================
*/
static void V_TestLights(void)
{
int i, j;
float f, r;
dlight_t *dl;
if (cl_testlights->integer != 1) {
dl = &r_dlights[0];
r_numdlights = 1;
VectorMA(cl.refdef.vieworg, 256, cl.v_forward, dl->origin);
if (cl_testlights->integer == -1)
VectorSet(dl->color, -1, -1, -1);
else
VectorSet(dl->color, 1, 1, 1);
dl->intensity = 256;
return;
}
r_numdlights = 32;
memset(r_dlights, 0, sizeof(r_dlights));
for (i = 0; i < r_numdlights; i++) {
dl = &r_dlights[i];
r = 64 * ((i % 4) - 1.5f);
f = 64 * (i / 4) + 128;
for (j = 0; j < 3; j++)
dl->origin[j] = cl.refdef.vieworg[j] + cl.v_forward[j] * f +
cl.v_right[j] * r;
dl->color[0] = ((i % 6) + 1) & 1;
dl->color[1] = (((i % 6) + 1) & 2) >> 1;
dl->color[2] = (((i % 6) + 1) & 4) >> 2;
dl->intensity = 200;
}
}
#endif
#endif
//===================================================================
void CL_UpdateBlendSetting(void)
{
if (cls.state < ca_connected) {
return;
}
if (cls.serverProtocol < PROTOCOL_VERSION_R1Q2) {
return;
}
MSG_WriteByte(clc_setting);
MSG_WriteShort(CLS_NOBLEND);
MSG_WriteShort(!cl_add_blend->integer);
MSG_FlushTo(&cls.netchan->message);
}
//============================================================================
// gun frame debugging functions
static void V_Gun_Next_f(void)
{
gun_frame++;
Com_Printf("frame %i\n", gun_frame);
}
static void V_Gun_Prev_f(void)
{
gun_frame--;
if (gun_frame < 0)
gun_frame = 0;
Com_Printf("frame %i\n", gun_frame);
}
static void V_Gun_Model_f(void)
{
char name[MAX_QPATH];
if (Cmd_Argc() != 2) {
gun_model = 0;
return;
}
Q_concat(name, sizeof(name), "models/", Cmd_Argv(1), "/tris.md2");
gun_model = R_RegisterModel(name);
}
//============================================================================
static int entitycmpfnc(const void *_a, const void *_b)
{
const entity_t *a = (const entity_t *)_a;
const entity_t *b = (const entity_t *)_b;
// all other models are sorted by model then skin
if (a->model == b->model)
return a->skin - b->skin;
else
return a->model - b->model;
}
static void V_SetLightLevel(void)
{
vec3_t shadelight;
// save off light value for server to look at (BIG HACK!)
R_LightPoint(cl.refdef.vieworg, shadelight);
// pick the greatest component, which should be the same
// as the mono value returned by software
if (shadelight[0] > shadelight[1]) {
if (shadelight[0] > shadelight[2]) {
cl.lightlevel = 150.0f * shadelight[0];
} else {
cl.lightlevel = 150.0f * shadelight[2];
}
} else {
if (shadelight[1] > shadelight[2]) {
cl.lightlevel = 150.0f * shadelight[1];
} else {
cl.lightlevel = 150.0f * shadelight[2];
}
}
}
/*
====================
V_CalcFov
====================
*/
float V_CalcFov(float fov_x, float width, float height)
{
float a;
float x;
if (fov_x < 1 || fov_x > 179)
Com_Error(ERR_DROP, "%s: bad fov: %f", __func__, fov_x);
x = width / tan(fov_x * (M_PI / 360));
a = atan(height / x);
a = a * (360 / M_PI);
return a;
}
/*
==================
V_RenderView
==================
*/
void V_RenderView(void)
{
// an invalid frame will just use the exact previous refdef
// we can't use the old frame if the video mode has changed, though...
if (cl.frame.valid) {
V_ClearScene();
// build a refresh entity list and calc cl.sim*
// this also calls CL_CalcViewValues which loads
// v_forward, etc.
CL_AddEntities();
#ifdef _DEBUG
if (cl_testparticles->integer)
V_TestParticles();
if (cl_testentities->integer)
V_TestEntities();
#if USE_DLIGHTS
if (cl_testlights->integer)
V_TestLights();
#endif
if (cl_testblend->integer) {
cl.refdef.blend[0] = 1;
cl.refdef.blend[1] = 0.5f;
cl.refdef.blend[2] = 0.25f;
cl.refdef.blend[3] = 0.5f;
}
#endif
// never let it sit exactly on a node line, because a water plane can
// dissapear when viewed with the eye exactly on it.
// the server protocol only specifies to 1/8 pixel, so add 1/16 in each axis
cl.refdef.vieworg[0] += 1.0f / 16;
cl.refdef.vieworg[1] += 1.0f / 16;
cl.refdef.vieworg[2] += 1.0f / 16;
cl.refdef.x = scr_vrect.x;
cl.refdef.y = scr_vrect.y;
cl.refdef.width = scr_vrect.width;
cl.refdef.height = scr_vrect.height;
// adjust for non-4/3 screens
if (cl_adjustfov->integer) {
cl.refdef.fov_y = cl.fov_y;
cl.refdef.fov_x = V_CalcFov(cl.refdef.fov_y, cl.refdef.height, cl.refdef.width);
} else {
cl.refdef.fov_x = cl.fov_x;
cl.refdef.fov_y = V_CalcFov(cl.refdef.fov_x, cl.refdef.width, cl.refdef.height);
}
cl.refdef.time = cl.time * 0.001f;
if (cl.frame.areabytes) {
cl.refdef.areabits = cl.frame.areabits;
} else {
cl.refdef.areabits = NULL;
}
if (!cl_add_entities->integer)
r_numentities = 0;
if (!cl_add_particles->integer)
r_numparticles = 0;
#if USE_DLIGHTS
if (!cl_add_lights->integer)
r_numdlights = 0;
#endif
if (!cl_add_blend->integer)
Vector4Clear(cl.refdef.blend);
cl.refdef.num_entities = r_numentities;
cl.refdef.entities = r_entities;
cl.refdef.num_particles = r_numparticles;
cl.refdef.particles = r_particles;
#if USE_DLIGHTS
cl.refdef.num_dlights = r_numdlights;
cl.refdef.dlights = r_dlights;
#endif
cl.refdef.lightstyles = r_lightstyles;
cl.refdef.rdflags = cl.frame.ps.rdflags;
// sort entities for better cache locality
qsort(cl.refdef.entities, cl.refdef.num_entities, sizeof(cl.refdef.entities[0]), entitycmpfnc);
}
R_RenderFrame(&cl.refdef);
#ifdef _DEBUG
if (cl_stats->integer)
#if USE_DLIGHTS
Com_Printf("ent:%i lt:%i part:%i\n", r_numentities, r_numdlights, r_numparticles);
#else
Com_Printf("ent:%i part:%i\n", r_numentities, r_numparticles);
#endif
#endif
V_SetLightLevel();
}
/*
=============
V_Viewpos_f
=============
*/
static void V_Viewpos_f(void)
{
Com_Printf("(%.f %.f %.f) : %.f\n", cl.refdef.vieworg[0],
cl.refdef.vieworg[1], cl.refdef.vieworg[2],
cl.refdef.viewangles[YAW]);
}
static const cmdreg_t v_cmds[] = {
{ "gun_next", V_Gun_Next_f },
{ "gun_prev", V_Gun_Prev_f },
{ "gun_model", V_Gun_Model_f },
{ "viewpos", V_Viewpos_f },
{ NULL }
};
static void cl_add_blend_changed(cvar_t *self)
{
CL_UpdateBlendSetting();
}
/*
=============
V_Init
=============
*/
void V_Init(void)
{
Cmd_Register(v_cmds);
#ifdef _DEBUG
cl_testblend = Cvar_Get("cl_testblend", "0", 0);
cl_testparticles = Cvar_Get("cl_testparticles", "0", 0);
cl_testentities = Cvar_Get("cl_testentities", "0", 0);
#if USE_DLIGHTS
cl_testlights = Cvar_Get("cl_testlights", "0", CVAR_CHEAT);
#endif
cl_stats = Cvar_Get("cl_stats", "0", 0);
#endif
#if USE_DLIGHTS
cl_add_lights = Cvar_Get("cl_lights", "1", 0);
cl_show_lights = Cvar_Get("cl_show_lights", "0", 0);
#endif
cl_add_particles = Cvar_Get("cl_particles", "1", 0);
cl_add_entities = Cvar_Get("cl_entities", "1", 0);
cl_add_blend = Cvar_Get("cl_blend", "1", 0);
cl_add_blend->changed = cl_add_blend_changed;
cl_adjustfov = Cvar_Get("cl_adjustfov", "1", 0);
}
void V_Shutdown(void)
{
Cmd_Deregister(v_cmds);
}