-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraycast.c
268 lines (222 loc) · 7.33 KB
/
raycast.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
#include "raycast.h"
#include <stdlib.h>
#include "q24d8.h"
#include "frame.h"
#include "trig_table.h"
#define MIN(X, Y) (((X) <= (Y)) ? (X) : (Y))
#define MAX(X, Y) (((X) >= (Y)) ? (X) : (Y))
#define checkIntersection( intersection, map ) \
( map[ floorq(intersection.y) ][ floorq(intersection.x) ] )
#define nextIntersection( intersection, delta ) do { \
intersection.x += delta.x; \
intersection.x = MIN( MAX( intersection.x, 0 ), Q24D8_MAX ); \
intersection.y += delta.y; \
intersection.y = MIN( MAX( intersection.y, 0 ), Q24D8_MAX ); \
} while ( 0 )
#define FISHEYE_CORRECTION 1
uint8_t colHeights[128];
uint8_t colColors[128];
uint8_t roofColor = 0x6;
uint8_t floorColor = 0x3;
void drawColumns( FRAME_Frame *frame, RC_Viewport *vp, uint8_t *colHeight, uint8_t *colColor );
void calculateWallHeight( Angle angle, RC_Object *player, RC_Viewport *vp, RC_Map map, RC_Map colorMap, uint8_t *colHeight, uint8_t *colColor );
void RC_initViewport( RC_Viewport *vp, int32_t height, int32_t width, Angle fov, uint32_t wallHeight )
{
Trigval tanVal;
q24d8 distToVp;
vp->height = height;
vp->width = width;
vp->fov = fov;
tanVal = getTanVal( fov / 2 );
distToVp = trigDiv( itoq( vp->height ) / 2, tanVal );
vp->wallXVp = qtoi(multq( itoq( wallHeight ), distToVp ));
//vp->wallXVp = itoq( 100 );
}
void RC_renderBackground( RC_Object *p, RC_Viewport *vp, RC_Map map, RC_Map colorMap, FRAME_Frame *frame )
{
uint32_t col;
uint32_t angleInc;
uint32_t angle;
angleInc = vp->fov / vp->width;
angle = vp->fov / 2;
angle = angleAdd( angle, p->direction );
for ( col = 0; col < vp->width; ++col ) {
calculateWallHeight( angle, p, vp, map, colorMap, &colHeights[col], &colColors[col] );
angle = angleSub( angle, angleInc );
}
drawColumns( frame, vp, colHeights, colColors );
return;
}
void drawColumns( FRAME_Frame *frame, RC_Viewport *vp, uint8_t *colHeight, uint8_t *colColor )
{
uint32_t col;
uint32_t row;
uint32_t colRoofLimit;
uint32_t colFloorLimit;
uint8_t color;
for ( col = 0; col < vp->width; ++col ) {
colRoofLimit = vp->height/2;
colRoofLimit -= colHeight[col]/2;
colFloorLimit = vp->height/2;
colFloorLimit += colHeight[col]/2;
color = colColor[ col ];
for ( row = 0; row < vp->height; ++row ) {
if ( row < colRoofLimit ) {
/* Pain with roof color */
FRAME_setPixel( frame, col, row, roofColor );
}
else if ( row > colFloorLimit ) {
/* Paint with floor color */
FRAME_setPixel( frame, col, row, floorColor );
}
else {
/* Paint with wall color */
FRAME_setPixel( frame, col, row, color );
}
}
}
return;
}
void calculateWallHeight( Angle angle, RC_Object *p, RC_Viewport *vp, RC_Map map, RC_Map colorMap, uint8_t *colHeight, uint8_t *colColor )
{
uint8_t color;
q24d8 height;
q24d8 xDistance;
q24d8 yDistance;
q24d8 distance;
q24d8 correctionAngle;
q24d8 cosCorrection;
Trigval tanVal;
Trigval cosVal;
Trigval sinVal;
RC_Direction dir;
RC_Coordinate yIntersection;
RC_Coordinate xIntersection;
RC_Coordinate yDelta;
RC_Coordinate xDelta;
/* Get trigonometric values */
tanVal = getTanVal( angle );
cosVal = getCosVal( angle );
sinVal = getSinVal( angle );
/*
* Finding box intersections along x-side of boxes and y-side of boxes
*/
/* Find intersection and deltas */
if ( angle < ANGLE_MAX/2 ) {
/* Looking in negative y-direction ( 0 < angle < 180 ) */
/* First find the intersection by flooring to grid */
yIntersection.y = ( p->position.y & 0xFFFFFF00 ) - 1;
yDelta.y = itoq( -1 );
}
else {
/* Looking in positive y-direction */
/* Roofing to grid instead of flooring */
yIntersection.y = ( p->position.y & 0xFFFFFF00 ) + itoq( 1 );
yDelta.y = itoq( 1 );
}
/* Find the x-intersection along the y-axis */
yIntersection.x = p->position.x + trigDiv( ( p->position.y - yIntersection.y ), tanVal );
yIntersection.x = MIN( MAX( yIntersection.x, 0 ), Q24D8_MAX );
/* Find movement along x-direction for each y-delta */
yDelta.x = trigDiv( itoq( 1 ), tanVal );
/* Now finding intersection along x-axis boxes */
if ( ( angle > ANGLE_90 ) && ( angle < ANGLE_270 ) ) {
/* Looking along negative x-direction ( 90 < angle < 270 ) */
/* Finding first intersection by flooring to grid */
xIntersection.x = ( p->position.x & 0xFFFFFF00 ) - 1;
xDelta.x = itoq( -1 );
}
else {
/* Looking in positive x */
/* Roofing to grid instead of flooring */
xIntersection.x = ( p->position.x & 0xFFFFFF00 ) + itoq( 1 );
xDelta.x = itoq( 1 );
}
/* Now find y-intersection along x-axis */
xIntersection.y = p->position.y + trigMult( ( p->position.x - xIntersection.x ), tanVal );
xIntersection.y = MIN( MAX( xIntersection.y, 0 ), Q24D8_MAX );
xDelta.y = trigMult( itoq( 1 ), tanVal );
/*
* Finding map intersections along x-side and y-side
*/
/* Finding direction of current angle */
if ( ( angle < ANGLE_45 ) || ( angle > ANGLE_315 ) ) {
dir = DIR_RIGHT;
}
else if ( angle < ANGLE_135 ) {
dir = DIR_UP;
}
else if ( angle < ANGLE_225 ) {
dir = DIR_LEFT;
}
else {
dir = DIR_DOWN;
}
/* Finding y intersection */
while ( 1 ) {
if ( !checkIntersection( yIntersection, map ) ) {
nextIntersection( yIntersection, yDelta );
}
else {
switch ( dir ) {
case DIR_UP:
case DIR_DOWN:
yDistance = abs( p->position.y - yIntersection.y );
yDistance = trigDiv( yDistance, sinVal );
yDistance = abs( yDistance );
break;
case DIR_RIGHT:
case DIR_LEFT:
default:
yDistance = abs ( p->position.x - yIntersection.x );
yDistance = trigDiv( yDistance, cosVal );
yDistance = abs( yDistance );
break;
}
break;
}
}
/* Finding x intersection */
while ( 1 ) {
if ( !checkIntersection( xIntersection, map ) ) {
nextIntersection( xIntersection, xDelta );
}
else {
switch ( dir ) {
case DIR_UP:
case DIR_DOWN:
xDistance = abs( p->position.y - xIntersection.y );
xDistance = trigDiv( xDistance, sinVal );
xDistance = abs( xDistance );
break;
case DIR_RIGHT:
case DIR_LEFT:
default:
xDistance = abs( p->position.x - xIntersection.x );
xDistance = trigDiv( xDistance, cosVal );
xDistance = abs( xDistance );
break;
}
break;
}
}
/* Min value is actual distance to wall */
if ( xDistance < yDistance ) {
distance = xDistance;
color = colorMap[ floorq( xIntersection.y ) ][ floorq( xIntersection.x ) ];
}
else {
distance = yDistance;
color = colorMap[ floorq( yIntersection.y ) ][ floorq( yIntersection.x ) ];
}
#if FISHEYE_CORRECTION
correctionAngle = angleSub( p->direction, angle );
cosCorrection = getCosVal( correctionAngle );
distance = trigMult( distance, cosCorrection );
#endif
*colColor = color;
height = divq( vp->wallXVp, distance );
height = qtoi( MIN( MAX( height, 0 ), itoq(vp->height) ) );
*colHeight = (uint8_t) height;
return;
}