-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.c
237 lines (193 loc) · 4.7 KB
/
render.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
#include <math.h>
//#include "rdata.h"
#include "fenton.h"
#include "map.h"
#include "appio.h"
#include "vec.h"
#include "render.h"
static const double fov_x = 90.0;
struct camera_s camera;
unsigned int r_framenum;
int r_gspan_debug = 0;
void
R_Init (void)
{
camera.fov_x = DEG2RAD(fov_x);
Vec_Clear (camera.pos);
Vec_Clear (camera.angles);
R_CalcViewXForm ();
r_framenum = 0;
}
void
R_Shutdown (void)
{
R_Span_Cleanup ();
}
void
R_CameraSizeChanged (int w, int h)
{
camera.center_x = w / 2.0;
camera.center_y = h / 2.0;
camera.dist = (w / 2.0) / tan(camera.fov_x / 2.0);
camera.fov_y = 2.0 * atan((h / 2.0) / camera.dist);
}
void
R_CalcViewXForm (void)
{
double v[3];
/* make transformation matrix */
Vec_Copy (camera.angles, v);
Vec_Scale (v, -1.0);
Vec_AnglesMatrix (v, camera.xform, "xyz");
/* get view vectors */
Vec_Copy (camera.xform[0], camera.left);
Vec_Copy (camera.xform[1], camera.up);
Vec_Copy (camera.xform[2], camera.forward);
}
/*
* At the start of each frame render we classify each viewplane by
* the direction of its normal. This classification tells which bbox
* corner the plane points to. This is used to check for bboxes that
* are fully in front of a viewplane (trivially accept nodes and reduce
* clip checks). The opposite bbox corner is used to check when the bbox
* is fully behind the plane (trivially rejected nodes/leafs terminate
* tree traversal for that node).
*/
/* indices of a bbox, taken as a straight array of 6 values */
#define MINX 0
#define MINY 1
#define MINZ 2
#define MAXX 3
#define MAXY 4
#define MAXZ 5
static const int _bboxmap[8][3] = {
{ MINX, MINY, MINZ },
{ MAXX, MINY, MINZ },
{ MINX, MAXY, MINZ },
{ MAXX, MAXY, MINZ },
{ MINX, MINY, MAXZ },
{ MAXX, MINY, MAXZ },
{ MINX, MAXY, MAXZ },
{ MAXX, MAXY, MAXZ },
};
#undef MINX
#undef MINY
#undef MINZ
#undef MAXX
#undef MAXY
#undef MAXZ
static void
CalcAcceptReject (struct viewplane_s *p)
{
int planeclass = ((p->normal[0] < 0.0) << 0) +
((p->normal[1] < 0.0) << 1) +
((p->normal[2] < 0.0) << 2);
/* The fully-accept corner. If this corner is in front of a
* plane then the whole bbox (and its contents) are in front of
* the plane.
*/
p->accept[0] = _bboxmap[planeclass][0];
p->accept[1] = _bboxmap[planeclass][1];
p->accept[2] = _bboxmap[planeclass][2];
/* The fully-reject corner. If this corner is behind a plane
* then the whole bbox (and its contents) are behind the plane.
*/
p->reject[0] = _bboxmap[7 - planeclass][0];
p->reject[1] = _bboxmap[7 - planeclass][1];
p->reject[2] = _bboxmap[7 - planeclass][2];
}
static void
CalcViewPlanes (void)
{
double cam2world[3][3];
double v[3];
struct viewplane_s *p;
double ang;
//TODO: move planes inward a quarter-pixel to help w/ roundoff?
/* view to world transformation matrix */
Vec_AnglesMatrix (camera.angles, cam2world, "zyx");
p = &camera.vplanes[VPLANE_LEFT];
ang = (camera.fov_x / 2.0);
v[0] = -cos (ang);
v[1] = 0.0;
v[2] = sin (ang);
Vec_Transform (cam2world, v, p->normal);
p->dist = Vec_Dot (p->normal, camera.pos);
CalcAcceptReject (p);
p = &camera.vplanes[VPLANE_RIGHT];
ang = (camera.fov_x / 2.0);
v[0] = cos (ang);
v[1] = 0.0;
v[2] = sin (ang);
Vec_Transform (cam2world, v, p->normal);
p->dist = Vec_Dot (p->normal, camera.pos);
CalcAcceptReject (p);
p = &camera.vplanes[VPLANE_TOP];
ang = (camera.fov_y / 2.0);
v[0] = 0.0;
v[1] = -cos (ang);
v[2] = sin (ang);
Vec_Transform (cam2world, v, p->normal);
p->dist = Vec_Dot (p->normal, camera.pos);
CalcAcceptReject (p);
p = &camera.vplanes[VPLANE_BOTTOM];
ang = (camera.fov_y / 2.0);
v[0] = 0.0;
v[1] = cos (ang);
v[2] = sin (ang);
Vec_Transform (cam2world, v, p->normal);
p->dist = Vec_Dot (p->normal, camera.pos);
CalcAcceptReject (p);
}
void
R_Refresh (void)
{
R_Clear ();
CalcViewPlanes ();
R_DrawWorld ();
if (1)
{
int i;
for (i = 0; i < 100; i++)
{
R_3DPoint2(i*0.1,0,0,video.red);
R_3DPoint2(0,i*0.1,0,video.green);
R_3DPoint2(0,0,i*0.1,video.blue);
}
}
// R_DrawLine(50,22,277,189,0x00ffffff); // white
// R_DrawLine(50,30,277,197,0x000000ff); // B - bits 0-7
// R_DrawLine(50,38,277,205,0x0000ff00); // G - bits 8-15
// R_DrawLine(50,46,277,213,0x00ff0000); // R - bits 16-23
#if 0
{
int i;
for (i = 0; i < map.num_vertices; i++)
{
R_3DPoint(map.vertices[i].xyz,0xffffffff);
}
for (i = 0; i < map.num_edges; i++)
{
R_3DLine ( map.vertices[map.edges[i].v[0]].xyz,
map.vertices[map.edges[i].v[1]].xyz,
0xffffffff );
}
}
#endif
if (r_gspan_debug)
R_Span_DrawGSpans ();
r_framenum++;
}
void
R_Die (const char *msg)
{
F_Error ("%s\npos: %f %f %f\nangles: %f %f %f",
msg,
camera.pos[0],
camera.pos[1],
camera.pos[2],
camera.angles[0],
camera.angles[1],
camera.angles[2]
);
}