Quake II RTX doxygen
1.0 dev
misc.c
Go to the documentation of this file.
1
/*
2
Copyright (C) 1997-2001 Id Software, Inc.
3
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
8
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. See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License along
15
with this program; if not, write to the Free Software Foundation, Inc.,
16
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
*/
18
// r_misc.c
19
20
#include "
sw.h
"
21
22
#define NUM_MIPS 4
23
24
cvar_t *
sw_mipcap
;
25
cvar_t *
sw_mipscale
;
26
27
int
d_minmip
;
28
float
d_scalemip
[
NUM_MIPS
- 1];
29
30
static
const
float
basemip
[
NUM_MIPS
- 1] = {1.0, 0.5 * 0.8, 0.25 * 0.8};
31
32
33
/*
34
=============
35
R_PrintTimes
36
=============
37
*/
38
void
R_PrintTimes
(
void
)
39
{
40
int
r_time2;
41
int
ms;
42
43
r_time2 =
Sys_Milliseconds
();
44
45
ms = r_time2 -
r_time1
;
46
47
Com_Printf(
"%5i ms %3i/%3i/%3i poly %3i surf\n"
,
48
ms,
c_faceclip
,
r_polycount
,
r_drawnpolycount
,
c_surf
);
49
c_surf
= 0;
50
}
51
52
53
/*
54
=============
55
R_PrintAliasStats
56
=============
57
*/
58
void
R_PrintAliasStats
(
void
)
59
{
60
Com_Printf(
"%3i polygon model drawn\n"
,
r_amodels_drawn
);
61
}
62
63
64
65
/*
66
===================
67
R_TransformFrustum
68
===================
69
*/
70
void
R_TransformFrustum
(
void
)
71
{
72
int
i;
73
vec3_t v, v2;
74
75
for
(i = 0; i < 4; i++) {
76
v[0] =
screenedge
[i].normal[2];
77
v[1] = -
screenedge
[i].normal[0];
78
v[2] =
screenedge
[i].normal[1];
79
80
v2[0] = v[1] *
vright
[0] + v[2] *
vup
[0] + v[0] *
vpn
[0];
81
v2[1] = v[1] *
vright
[1] + v[2] *
vup
[1] + v[0] *
vpn
[1];
82
v2[2] = v[1] *
vright
[2] + v[2] *
vup
[2] + v[0] *
vpn
[2];
83
84
VectorCopy(v2,
view_clipplanes
[i].normal);
85
86
view_clipplanes
[i].
dist
= DotProduct(
modelorg
, v2);
87
}
88
}
89
90
91
/*
92
================
93
R_TransformVector
94
================
95
*/
96
void
R_TransformVector
(vec3_t in, vec3_t out)
97
{
98
out[0] = DotProduct(in,
vright
);
99
out[1] = DotProduct(in,
vup
);
100
out[2] = DotProduct(in,
vpn
);
101
}
102
103
#if 0
104
/*
105
================
106
R_TransformPlane
107
================
108
*/
109
void
R_TransformPlane
(cplane_t *p,
float
*normal,
float
*
dist
)
110
{
111
float
d;
112
113
d = DotProduct(
r_origin
, p->normal);
114
*
dist
= p->dist - d;
115
// TODO: when we have rotating entities, this will need to use the view matrix
116
R_TransformVector
(p->normal, normal);
117
}
118
#endif
119
120
/*
121
===============
122
R_SetUpFrustumIndexes
123
===============
124
*/
125
static
void
R_SetUpFrustumIndexes
(
void
)
126
{
127
int
i, j, *pindex;
128
129
pindex =
r_frustum_indexes
;
130
131
for
(i = 0; i < 4; i++) {
132
for
(j = 0; j < 3; j++) {
133
if
(
view_clipplanes
[i].normal[j] < 0) {
134
pindex[j] = j;
135
pindex[j + 3] = j + 3;
136
}
else
{
137
pindex[j] = j + 3;
138
pindex[j + 3] = j;
139
}
140
}
141
142
// FIXME: do just once at start
143
pfrustum_indexes
[i] = pindex;
144
pindex += 6;
145
}
146
}
147
148
/*
149
===============
150
R_ViewChanged
151
152
Called every time the vid structure or r_refdef changes.
153
Guaranteed to be called before the first refresh
154
===============
155
*/
156
static
void
R_ViewChanged
(vrect_t *vr)
157
{
158
float
fov_h, fov_v;
159
int
i;
160
161
r_refdef
.
vrect
= *vr;
162
163
fov_h = 2 * tan((
float
)
r_newrefdef
.fov_x / 360 * M_PI);
164
fov_v = 2 * tan((
float
)
r_newrefdef
.fov_y / 360 * M_PI);
165
166
r_refdef
.
fvrectx
= (float)
r_refdef
.
vrect
.x;
167
r_refdef
.
fvrectx_adj
= (
float
)
r_refdef
.
vrect
.x - 0.5;
168
r_refdef
.
vrect_x_adj_shift20
= (
r_refdef
.
vrect
.x << 20) + (1 << 19) - 1;
169
r_refdef
.
fvrecty
= (float)
r_refdef
.
vrect
.y;
170
r_refdef
.
fvrecty_adj
= (
float
)
r_refdef
.
vrect
.y - 0.5;
171
r_refdef
.
vrectright
=
r_refdef
.
vrect
.x +
r_refdef
.
vrect
.width;
172
r_refdef
.
vrectright_adj_shift20
= (
r_refdef
.
vrectright
<< 20) + (1 << 19) - 1;
173
r_refdef
.
fvrectright
= (float)
r_refdef
.
vrectright
;
174
r_refdef
.
fvrectright_adj
= (
float
)
r_refdef
.
vrectright
- 0.5;
175
r_refdef
.
vrectrightedge
= (float)
r_refdef
.
vrectright
- 0.99;
176
r_refdef
.
vrectbottom
=
r_refdef
.
vrect
.y +
r_refdef
.
vrect
.height;
177
r_refdef
.
fvrectbottom
= (
float
)
r_refdef
.
vrectbottom
;
178
r_refdef
.
fvrectbottom_adj
= (float)
r_refdef
.
vrectbottom
- 0.5;
179
180
// values for perspective projection
181
// if math were exact, the values would range from 0.5 to to range+0.5
182
// hopefully they wll be in the 0.000001 to range+.999999 and truncate
183
// the polygon rasterization will never render in the first row or column
184
// but will definately render in the [range] row and column, so adjust the
185
// buffer origin to get an exact edge to edge fill
186
r_refdef
.
xcenter
= ((
float
)
r_refdef
.
vrect
.width * 0.5) +
r_refdef
.
vrect
.x - 0.5;
187
r_refdef
.
ycenter
= ((float)
r_refdef
.
vrect
.height * 0.5) +
r_refdef
.
vrect
.y - 0.5;
188
r_refdef
.
xscale
=
r_refdef
.
vrect
.width / fov_h;
189
r_refdef
.
xscaleinv
= 1.0 /
r_refdef
.
xscale
;
190
r_refdef
.
yscale
=
r_refdef
.
vrect
.height / fov_v;
191
r_refdef
.
yscaleinv
= 1.0 /
r_refdef
.
yscale
;
192
r_refdef
.
xscaleshrink
= (
r_refdef
.
vrect
.width - 6) / fov_h;
193
r_refdef
.
yscaleshrink
= (
r_refdef
.
vrect
.height - 6) / fov_v;
194
195
r_refdef
.
scale_for_mip
= max(
r_refdef
.
xscale
,
r_refdef
.
yscale
);
196
197
r_refdef
.
pix_min
=
r_refdef
.
vrect
.width / 640;
198
if
(
r_refdef
.
pix_min
< 1)
199
r_refdef
.
pix_min
= 1;
200
201
r_refdef
.
pix_max
= (
int
)((
float
)
r_refdef
.
vrect
.width / (640.0 / 4.0) + 0.5);
202
r_refdef
.
pix_shift
= 8 - (
int
)((
float
)
r_refdef
.
vrect
.width / 640.0 + 0.5);
203
if
(
r_refdef
.
pix_max
< 1)
204
r_refdef
.
pix_max
= 1;
205
206
r_refdef
.
vrectright_particle
=
r_refdef
.
vrectright
-
r_refdef
.
pix_max
;
207
r_refdef
.
vrectbottom_particle
=
r_refdef
.
vrectbottom
-
r_refdef
.
pix_max
;
208
209
// left side clip
210
screenedge
[0].normal[0] = -1.0 / (0.5 * fov_h);
211
screenedge
[0].normal[1] = 0;
212
screenedge
[0].normal[2] = 1;
213
screenedge
[0].type = PLANE_ANYZ;
214
215
// right side clip
216
screenedge
[1].normal[0] = 1.0 / (0.5 * fov_h);
217
screenedge
[1].normal[1] = 0;
218
screenedge
[1].normal[2] = 1;
219
screenedge
[1].type = PLANE_ANYZ;
220
221
// top side clip
222
screenedge
[2].normal[0] = 0;
223
screenedge
[2].normal[1] = -1.0 / (0.5 * fov_v);
224
screenedge
[2].normal[2] = 1;
225
screenedge
[2].type = PLANE_ANYZ;
226
227
// bottom side clip
228
screenedge
[3].normal[0] = 0;
229
screenedge
[3].normal[1] = 1.0 / (0.5 * fov_v);
230
screenedge
[3].normal[2] = 1;
231
screenedge
[3].type = PLANE_ANYZ;
232
233
for
(i = 0; i < 4; i++)
234
VectorNormalize
(
screenedge
[i].normal);
235
}
236
237
238
/*
239
===============
240
R_SetupFrame
241
===============
242
*/
243
void
R_SetupFrame
(
void
)
244
{
245
int
i;
246
vrect_t vrect;
247
248
if
(
r_fullbright
->modified) {
249
r_fullbright
->modified = qfalse;
250
D_FlushCaches
();
// so all lighting changes
251
}
252
253
r_framecount
++;
254
255
256
// build the transformation matrix for the given view angles
257
VectorCopy(
r_newrefdef
.vieworg,
modelorg
);
258
VectorCopy(
r_newrefdef
.vieworg,
r_origin
);
259
260
AngleVectors
(
r_newrefdef
.viewangles,
vpn
,
vright
,
vup
);
261
262
// current viewleaf
263
if
(!(
r_newrefdef
.rdflags & RDF_NOWORLDMODEL)) {
264
r_viewleaf
=
BSP_PointLeaf
(
r_worldmodel
->nodes,
r_origin
);
265
r_viewcluster
=
r_viewleaf
->cluster;
266
}
267
268
if
(
sw_waterwarp
->integer && (
r_newrefdef
.rdflags & RDF_UNDERWATER))
269
r_dowarp
= qtrue;
270
else
271
r_dowarp
= qfalse;
272
273
if
(
r_dowarp
) {
274
// warp into off screen buffer
275
vrect.x = 0;
276
vrect.y = 0;
277
vrect.width =
r_newrefdef
.width <
WARP_WIDTH
?
r_newrefdef
.width :
WARP_WIDTH
;
278
vrect.height =
r_newrefdef
.height <
WARP_HEIGHT
?
r_newrefdef
.height :
WARP_HEIGHT
;
279
280
d_viewbuffer
=
r_warpbuffer
;
281
d_screenrowbytes
=
WARP_WIDTH
*
VID_BYTES
;
282
}
else
{
283
vrect.x =
r_newrefdef
.x;
284
vrect.y =
r_newrefdef
.y;
285
vrect.width =
r_newrefdef
.width;
286
vrect.height =
r_newrefdef
.height;
287
288
d_viewbuffer
= (
void
*)
vid
.
buffer
;
289
d_screenrowbytes
=
vid
.
rowbytes
;
290
}
291
292
R_ViewChanged
(&vrect);
293
294
// start off with just the four screen edge clip planes
295
R_TransformFrustum
();
296
R_SetUpFrustumIndexes
();
297
298
// save base values
299
VectorCopy(
vpn
,
base_vpn
);
300
VectorCopy(
vright
,
base_vright
);
301
VectorCopy(
vup
,
base_vup
);
302
303
// clear frame counts
304
c_faceclip
= 0;
305
r_polycount
= 0;
306
r_drawnpolycount
= 0;
307
r_wholepolycount
= 0;
308
r_amodels_drawn
= 0;
309
r_outofsurfaces
= 0;
310
r_outofedges
= 0;
311
312
// d_setup
313
for
(i = 0; i <
vid
.
height
; i++) {
314
d_spantable
[i] =
d_viewbuffer
+ i *
d_screenrowbytes
;
315
d_zspantable
[i] =
d_pzbuffer
+ i *
d_zwidth
;
316
}
317
318
// clear Z-buffer and color-buffers if we're doing the gallery
319
if
(
r_newrefdef
.rdflags & RDF_NOWORLDMODEL) {
320
memset(
d_pzbuffer
, 0xff,
vid
.
width
*
vid
.
height
*
sizeof
(
d_pzbuffer
[0]));
321
#if 0
322
R_DrawFill8
(
r_newrefdef
.x,
r_newrefdef
.y,
r_newrefdef
.width,
r_newrefdef
.height,
/*(int)sw_clearcolor->value & 0xff*/
0);
323
#endif
324
}
325
326
d_minmip
=
Cvar_ClampInteger
(
sw_mipcap
, 0,
NUM_MIPS
- 1);
327
328
for
(i = 0; i < (
NUM_MIPS
- 1); i++)
329
d_scalemip
[i] =
basemip
[i] *
sw_mipscale
->value;
330
}
331
oldrefdef_t::xcenter
float xcenter
Definition:
sw.h:145
base_vright
vec3_t base_vright
Definition:
main.c:51
d_viewbuffer
pixel_t * d_viewbuffer
Definition:
main.c:125
oldrefdef_t::fvrectright
float fvrectright
Definition:
sw.h:141
oldrefdef_t::fvrectx_adj
float fvrectx_adj
Definition:
sw.h:137
r_origin
vec3_t r_origin
Definition:
main.c:52
r_newrefdef
refdef_t r_newrefdef
Definition:
main.c:28
viddef_t::buffer
pixel_t * buffer
Definition:
sw.h:124
basemip
static const float basemip[NUM_MIPS - 1]
Definition:
misc.c:30
oldrefdef_t::vrectright_adj_shift20
int vrectright_adj_shift20
Definition:
sw.h:139
base_vup
vec3_t base_vup
Definition:
main.c:49
viddef_t::width
int width
Definition:
sw.h:127
R_PrintAliasStats
void R_PrintAliasStats(void)
Definition:
misc.c:58
r_frustum_indexes
int r_frustum_indexes[4 *6]
Definition:
main.c:71
r_viewcluster
int r_viewcluster
Definition:
main.c:74
r_framecount
int r_framecount
Definition:
main.c:64
d_screenrowbytes
int d_screenrowbytes
Definition:
main.c:126
oldrefdef_t::xscaleinv
float xscaleinv
Definition:
sw.h:147
viddef_t::height
int height
Definition:
sw.h:128
r_refdef
oldrefdef_t r_refdef
Definition:
main.c:57
c_surf
int c_surf
Definition:
main.c:42
R_DrawFill8
void(* R_DrawFill8)(int x, int y, int w, int h, int c)
Definition:
refresh.c:422
oldrefdef_t::fvrectright_adj
float fvrectright_adj
Definition:
sw.h:140
oldrefdef_t::xscaleshrink
float xscaleshrink
Definition:
sw.h:148
oldrefdef_t::yscale
float yscale
Definition:
sw.h:146
r_dowarp
qboolean r_dowarp
Definition:
main.c:40
r_amodels_drawn
int r_amodels_drawn
Definition:
alias.c:25
r_viewleaf
mleaf_t * r_viewleaf
Definition:
main.c:73
sw_mipcap
cvar_t * sw_mipcap
Definition:
misc.c:24
oldrefdef_t::vrectrightedge
float vrectrightedge
Definition:
sw.h:134
oldrefdef_t::vrectright_particle
int vrectright_particle
Definition:
sw.h:152
vup
vec3_t vup
Definition:
main.c:49
c_faceclip
int c_faceclip
Definition:
raster.c:35
Sys_Milliseconds
unsigned Sys_Milliseconds(void)
Definition:
system.c:644
BSP_PointLeaf
mleaf_t * BSP_PointLeaf(mnode_t *node, vec3_t p)
Definition:
bsp.c:1439
r_polycount
int r_polycount
Definition:
main.c:66
oldrefdef_t::fvrecty_adj
float fvrecty_adj
Definition:
sw.h:137
r_fullbright
cvar_t * r_fullbright
Definition:
main.c:98
r_outofedges
int r_outofedges
Definition:
main.c:38
WARP_WIDTH
#define WARP_WIDTH
Definition:
sw.h:64
screenedge
cplane_t screenedge[4]
Definition:
main.c:59
viddef_t::rowbytes
int rowbytes
Definition:
sw.h:125
oldrefdef_t::scale_for_mip
float scale_for_mip
Definition:
sw.h:149
oldrefdef_t::pix_min
int pix_min
Definition:
sw.h:153
NUM_MIPS
#define NUM_MIPS
Definition:
misc.c:22
oldrefdef_t::fvrectbottom
float fvrectbottom
Definition:
sw.h:142
vpn
vec3_t vpn
Definition:
main.c:50
d_minmip
int d_minmip
Definition:
misc.c:27
oldrefdef_t::fvrectbottom_adj
float fvrectbottom_adj
Definition:
sw.h:140
sw.h
R_SetUpFrustumIndexes
static void R_SetUpFrustumIndexes(void)
Definition:
misc.c:125
d_scalemip
float d_scalemip[NUM_MIPS - 1]
Definition:
misc.c:28
R_TransformPlane
void R_TransformPlane(cplane_t *p, float *normal, float *dist)
d_spantable
byte * d_spantable[MAXHEIGHT]
Definition:
main.c:130
r_time1
float r_time1
Definition:
main.c:35
AngleVectors
void AngleVectors(vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
Definition:
shared.c:23
d_zwidth
int d_zwidth
Definition:
main.c:129
d_zspantable
short * d_zspantable[MAXHEIGHT]
Definition:
main.c:131
R_TransformFrustum
void R_TransformFrustum(void)
Definition:
misc.c:70
r_outofsurfaces
int r_outofsurfaces
Definition:
main.c:37
oldrefdef_t::xscale
float xscale
Definition:
sw.h:146
areanode_s::dist
float dist
Definition:
world.c:33
modelorg
vec3_t modelorg
Definition:
bsp.c:27
r_drawnpolycount
int r_drawnpolycount
Definition:
main.c:67
VID_BYTES
#define VID_BYTES
Definition:
sw.h:49
r_wholepolycount
int r_wholepolycount
Definition:
main.c:68
r_warpbuffer
byte r_warpbuffer[WARP_WIDTH *WARP_HEIGHT *VID_BYTES]
Definition:
main.c:33
oldrefdef_t::vrect
vrect_t vrect
Definition:
sw.h:132
d_pzbuffer
short * d_pzbuffer
Definition:
main.c:127
oldrefdef_t::yscaleinv
float yscaleinv
Definition:
sw.h:147
WARP_HEIGHT
#define WARP_HEIGHT
Definition:
sw.h:65
D_FlushCaches
void D_FlushCaches(void)
Definition:
surf.c:222
Cvar_ClampInteger
int Cvar_ClampInteger(cvar_t *var, int min, int max)
Definition:
cvar.c:549
sw_waterwarp
cvar_t * sw_waterwarp
Definition:
main.c:85
oldrefdef_t::yscaleshrink
float yscaleshrink
Definition:
sw.h:148
oldrefdef_t::vrectbottom_particle
int vrectbottom_particle
Definition:
sw.h:152
oldrefdef_t::pix_shift
int pix_shift
Definition:
sw.h:153
oldrefdef_t::pix_max
int pix_max
Definition:
sw.h:153
oldrefdef_t::vrectright
int vrectright
Definition:
sw.h:133
oldrefdef_t::vrect_x_adj_shift20
int vrect_x_adj_shift20
Definition:
sw.h:138
R_ViewChanged
static void R_ViewChanged(vrect_t *vr)
Definition:
misc.c:156
oldrefdef_t::fvrectx
float fvrectx
Definition:
sw.h:136
R_TransformVector
void R_TransformVector(vec3_t in, vec3_t out)
Definition:
misc.c:96
clipplane_s::dist
float dist
Definition:
sw.h:195
oldrefdef_t::ycenter
float ycenter
Definition:
sw.h:145
sw_mipscale
cvar_t * sw_mipscale
Definition:
misc.c:25
vid
viddef_t vid
Definition:
main.c:22
int
CONST PIXELFORMATDESCRIPTOR int
Definition:
wgl.c:26
r_worldmodel
bsp_t * r_worldmodel
Definition:
main.c:31
R_SetupFrame
void R_SetupFrame(void)
Definition:
misc.c:243
pfrustum_indexes
int * pfrustum_indexes[4]
Definition:
main.c:70
view_clipplanes
clipplane_t view_clipplanes[4]
Definition:
raster.c:37
base_vpn
vec3_t base_vpn
Definition:
main.c:50
VectorNormalize
vec_t VectorNormalize(vec3_t v)
Definition:
shared.c:55
oldrefdef_t::vrectbottom
int vrectbottom
Definition:
sw.h:133
oldrefdef_t::fvrecty
float fvrecty
Definition:
sw.h:136
vright
vec3_t vright
Definition:
main.c:51
R_PrintTimes
void R_PrintTimes(void)
Definition:
misc.c:38
src
refresh
sw
misc.c
Generated by
1.8.17