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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render_state.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/15 14:40:14 by cacharle #+# #+# */
/* Updated: 2020/01/17 14:48:15 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
/*
** -1 0 1 <-- camera_x
** v v v
** ################
** # | # <-- screen
** # | #
** ################
**
** camera_x is the x column from the camera percpective
** scaling the plane vector and adding it to the direction vector
** to create a vector in the *direction* of the column x.
*/
void rstate_ray(t_state *state, t_render_state *rstate)
{
double camera_x;
camera_x = 2.0 * (double)rstate->x / (double)state->window.width - 1.0;
rstate->ray = vector_add(state->dir, vector_scale(state->plane, camera_x));
}
/*
** delta between each grid unit form the vector percpective
**
** if we have a vector v = [2 3]:
** dx = |v| / v_1
** = sqrt(v_1^2 + v_2^2) / v_1
** = (v_1^2 + v_2^2) / v_1^2
** = 1 + v_2^2 / v_1^2
** Same thing for dy
** dy = |v| / v_2
** = sqrt(v_1^2 + v_2^2) / v_1
** = (v_1^2 + v_2^2) / v_2^2
** = v_1^2 / v_2^2 + 1
**
** This can be simplified (for some obscure reason):
** dx = |1 / v_1|
** dy = |1 / v_2|
*/
void rstate_delta(t_render_state *rstate)
{
rstate->delta.x = vector_norm(rstate->ray) / rstate->ray.x;
rstate->delta.y = vector_norm(rstate->ray) / rstate->ray.y;
}
/*
** first delta between player position and first grid unit
**
** current x and y are the perpendicular distance to the nearest wall,
** we multiply them by their corresponding delta.
** 0 <= perpendicular distance <= 1 is a ratio, how much of the full delta we need to take.
**
** if (rstate->ray.x < 0)
** rstate->probe.x = state->pos.x - rstate->map_pos.x;
** else
** rstate->probe.x = fabs(state->pos.x - rstate->map_pos.x + 1.0);
** if (rstate->ray.y < 0)
** rstate->probe.y = state->pos.y - rstate->map_pos.y;
** else
** rstate->probe.y = fabs(state->pos.y - rstate->map_pos.y + 1.0);
** rstate->probe.x *= rstate->delta.x;
** rstate->probe.y *= rstate->delta.y;
*/
void rstate_init_probe(t_state *state, t_render_state *rstate)
{
rstate->probe = VECTOR_SUB(state->pos, rstate->map_pos);
if (rstate->ray.x > 0)
rstate->probe.x += 1.0;
if (rstate->ray.y > 0)
rstate->probe.y += 1.0;
rstate->probe.x *= rstate->delta.x;
rstate->probe.y *= rstate->delta.y;
}
/*
** Move the probe to it's next iteration by advancing to the nearest square unit
** in the x or y direction.
** This advance is represented both with the
** player/ray percpective and the map pecpective.
*/
void rstate_next_probe(t_render_state *rstate)
{
if (rstate->probe.x < rstate->probe.y)
{
rstate->probe.x += rstate->delta.x;
rstate->map_pos.x += rstate->map_step.x;
}
else
{
rstate->probe.y += rstate->delta.y;
rstate->map_pos.y += rstate->map_step.y;
}
}
/*
** perpendicular distance between the wall hit and the camera plane.
** We don't use euclidean distance because it would cause a fisheye effect.
**
** ====================X========== wall
** | /|
** | <------ / | -----+
** | / | |
** plane | / | <- perpendicular distance
** | | / |
** v | / |
** <-------^----/----------------- camera plane
** | /
** dir -> | /
** | / <- euclidean distance
** |/
** x <- pos
**
** In this case the perpendicular distance (p) is the difference
** of the y-coord of the hit point and the y-coord of the pos + dir vector.
** We use the y component because we hit the wall
** from a south/north percepective,
** if we had hit it form west/east, we would use the x component instead.
*/
double rstate_perp_dist(t_state *state, t_render_state *rstate)
{
double dist;
/* printf("ray [%f %f]\n", rstate->ray.x, rstate->ray.y); */
dist = 1.0;
if (rstate->side == SIDE_NS)
{
dist = rstate->map_pos.y - state->pos.y;
dist += rstate->map_step.y == 1 ? 1 : 0;
dist /= rstate->ray.y;
}
else if (rstate->side == SIDE_WE)
{
dist = rstate->map_pos.x - state->pos.x;
dist += rstate->map_step.x == 1 ? 1 : 0;
dist /= rstate->ray.x;
}
return dist;
/* if (rstate->side == SIDE_NS) */
/* return fabs(rstate->map_pos.y - state->pos.y + state->dir.y); */
/* else if (rstate->side == SIDE_WE) */
/* return fabs(rstate->map_pos.x - state->pos.x + state->dir.x); */
/* return (1.0); */
}
/*
** 0 <= 1 / perp_dist <= 1
** height * (1 / perp_dist) is how much of the screen height do we take
*/
void rstate_line_height(t_state *state, t_render_state *rstate)
{
/* double perp = rstate_perp_dist(state, rstate); */
/* printf("perp %f\n", perp); */
rstate->line_height = (int)((double)state->window.height / rstate_perp_dist(state, rstate));
}
t_image *get_tex(t_state *state, t_render_state *rstate)
{
if (rstate->side == SIDE_NS)
{
if (rstate->probe.y < state->pos.y)
return (state->textures + TEX_NORTH);
else
return (state->textures + TEX_SOUTH);
}
else if (rstate->side == SIDE_WE)
{
if (rstate->probe.x < state->pos.x)
return (state->textures + TEX_WEST);
else
return (state->textures + TEX_EAST);
}
return (NULL);
}
/*
** Since we're drawing each column, all the texels we want to draw on the window
** are on a single column of the texture.
** First we find the x-coord relative to the wall we hit
*/
/* int get_tex_x() */
/* { */
/* //calculate value of wall_x */
/* double wall_x; //where exactly the wall was hit */
/* if (side == 0) wall_x = state->pos.y + perp_dist * ray.y; */
/* else wall_x = state->pos.x + perp_dist * ray.x; */
/* wall_x -= floor(wall_x); */
/* //x coordinate on the texture */
/* int tex_x = (int)(wall_x * (double)texWidth); */
/* if(side == 0 && ray.x > 0) tex_x = texture_width - tex_x - 1; */
/* if(side == 1 && ray.y < 0) tex_x = texture_width - tex_x - 1; */
/* return (tex_x); */
/* } */
|