From c8de182e9fa0c8a2674bf2f13d2ed9f500607ebd Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 4 Feb 2020 03:40:05 +0100 Subject: Norming --- src/render_state.c | 85 ++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 44 deletions(-) (limited to 'src/render_state.c') diff --git a/src/render_state.c b/src/render_state.c index 904e564..09f6f0c 100644 --- a/src/render_state.c +++ b/src/render_state.c @@ -6,13 +6,14 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/15 14:40:14 by cacharle #+# #+# */ -/* Updated: 2020/02/02 17:39:59 by cacharle ### ########.fr */ +/* Updated: 2020/02/04 02:09:43 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" /* +** ray: ** -1 0 1 <-- camera_x ** v v v ** ################ @@ -23,17 +24,8 @@ ** 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: ** delta between each grid unit form the vector percpective ** ** if we have a vector v = [2 3]: @@ -50,20 +42,14 @@ void rstate_ray(t_state *state, t_render_state *rstate) ** 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 = fabs(1 / rstate->ray.x); //vector_norm(rstate->ray) / rstate->ray.x; - rstate->delta.y = fabs(1 / rstate->ray.y); // vector_norm(rstate->ray) / rstate->ray.y; -} - -/* +** +** init probe: ** 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. +** 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; @@ -77,23 +63,29 @@ void rstate_delta(t_render_state *rstate) ** rstate->probe.y *= rstate->delta.y; */ -void rstate_init_probe(t_state *state, t_render_state *rstate) +void rstate_init(t_state *state, t_render_state *rstate, int x) { + double camera_x; + + rstate->x = 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)); + rstate->map_pos = vector_new((int)state->pos.x, (int)state->pos.y); + rstate->delta.x = fabs(1 / rstate->ray.x); + rstate->delta.y = fabs(1 / rstate->ray.y); if (rstate->ray.x < 0) rstate->probe.x = (state->pos.x - rstate->map_pos.x) * rstate->delta.x; else - rstate->probe.x = (rstate->map_pos.x + 1.0 - state->pos.x) * rstate->delta.x; + rstate->probe.x = (rstate->map_pos.x + 1.0 - state->pos.x) + * rstate->delta.x; if (rstate->ray.y < 0) - rstate->probe.y = (state->pos.y - rstate->map_pos.y) * rstate->delta.y; + rstate->probe.y = (state->pos.y - rstate->map_pos.y) + * rstate->delta.y; else - rstate->probe.y = (rstate->map_pos.y + 1.0 - state->pos.y) * rstate->delta.y; - /* 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; */ + rstate->probe.y = (rstate->map_pos.y + 1.0 - state->pos.y) + * rstate->delta.y; + rstate->map_step = vector_new(rstate->ray.x < 0.0 ? -1.0 : 1.0, + rstate->ray.y < 0.0 ? -1.0 : 1.0); } /* @@ -141,21 +133,26 @@ void rstate_next_probe(t_render_state *rstate) ** from a south/north percepective, ** if we had hit it form west/east, we would use the x component instead. */ - -void rstate_perp_dist(t_state *state, t_render_state *rstate) -{ - if (rstate->side == SIDE_WE) - rstate->perp_dist = (rstate->map_pos.x - state->pos.x + (1 - rstate->map_step.x) / 2) / rstate->ray.x; - else - rstate->perp_dist = (rstate->map_pos.y - state->pos.y + (1 - rstate->map_step.y) / 2) / rstate->ray.y; -} - /* ** 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) +void rstate_post(t_state *state, t_render_state *rstate) { - rstate->line_height = (int)((double)state->window.height / rstate->perp_dist); + if (rstate->side == SIDE_WE) + rstate->perp_dist = (rstate->map_pos.x - state->pos.x + + (1 - rstate->map_step.x) / 2) / rstate->ray.x; + else + rstate->perp_dist = (rstate->map_pos.y - state->pos.y + + (1 - rstate->map_step.y) / 2) / rstate->ray.y; + rstate->line_height = (int)((double)state->window.height / + rstate->perp_dist); + rstate->draw_start = state->window.height / 2 - rstate->line_height / 2; + rstate->draw_end = state->window.height / 2 + rstate->line_height / 2; + if (rstate->draw_start < 0) + rstate->draw_start = 0; + if (rstate->draw_end > state->window.height - 1) + rstate->draw_end = state->window.height - 1; + state->z_buffer[rstate->x] = rstate->perp_dist; } -- cgit