Skip to content

Commit

Permalink
chore: improve clarity on cub3d post
Browse files Browse the repository at this point in the history
  • Loading branch information
herbievine committed May 20, 2024
2 parents 09b552f + ac72c5c commit ad72435
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions posts/42-a-comprehensive-guide-to-cub3d.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ Here is a graphical example which hopefully illustrates the idea a bit better.

![Example of ray casting (bird’s eye view)](/assets/posts/ray-casting-example.png)

If you want a more in-depth explanation into the math behind all this, I highly suggest watching [this short YouTube video](https://www.youtube.com/watch?v=gYRrGTC7GtA) from 3DSage.

## Implementation Details

Although I’ve been referring to ray casting as the sole culprit to this project, it’s not the only piece of the puzzle, and you will later find out it’s the easiest!
Although I’ve been referring to ray casting as the sole culprit to this project, it’s not the only piece of the puzzle! You will have to implement a lot of other functionalities, such as handling image buffers and texture colouring, to create a fully functional 3D environment.

### Setting Up the Environment

Expand Down Expand Up @@ -75,7 +73,7 @@ This gives us the distance the ray must travel to reach the next grid line in ea

#### Step 3: Calculating the Step and Initial Side Distances

Now we need to calculate the `stepX`, `stepY` and the initial side distances for the ray. The `stepx` and `stepy` variables determine the direction in which the ray moves through the grid.
Now we need to calculate the `step_x`, `step_y` and the initial side distances for the ray. The `step_x` and `step_y` variables determine the direction in which the ray moves through the grid.

The `side_dist_x` and `side_dist_y` variables represent initially the distance the ray must travel from its current position to the next grid line in the x or y direction. Later these variables will be updated with the delta distance as the ray moves through the grid.

Expand Down Expand Up @@ -204,7 +202,7 @@ typedef struct s_data
} t_data
```

So imagine you have a texture of `64x64`, the size of a `texture_buffer` will be `sizeof(int) * 64 * 64`. To access a pixel, you can use the following formula: `texture_buffer[y * 64 + x]`. This skips `y` rows by multiplying the width of the texture, and then adds `x` to get the pixel.
So imagine you have a texture of `64x64`, the size of a `texture_buffer[n]` will be `sizeof(int) * 64 * 64`. To access a pixel, you can use the following formula: `texture_buffer[n][y * 64 + x]`. This skips `y` rows by multiplying the width of the texture, and then adds `x` to get the pixel.

To get a pixel value from a MLX image pointer, you need to use the `mlx_get_data_addr` function, and you can access a pixel like this: `img->addr[y * img->width + x]`. I recommend reading [the docs](https://harm-smits.github.io/42docs/libs/minilibx/prototypes.html#mlx_get_data_addr) for this function to understand how and why it works.

Expand Down

0 comments on commit ad72435

Please sign in to comment.