If you want to learn more about IT topics, I invite you to join my Patreon channel and visit my website: IA Notes
According to computer hope, a ray casting is the most basic form of raytracing in CGI (computer-generated imagery). Unlike other forms of ray tracing, where rays originate grom a light source and bounce off objects to arrive at the observer, in ray casting, they are cast directly from the viewpoint. When cast rays intersect an object, the object's color and brightness at that point determines the value of one pixel in the final image.
A direction vector represents the orientation of the observer extending forward. A camera plane, perpendicular to the direction vector and representing the shape of the final rendered image, is also required.
-
Clone cub3D repository:
git clone https://github.com/pgomez-a/cub3D.git && cd cub3D
-
Run make to create the game:
make
-
Execute cub3D with the map path you want to play:
./cub3D maps/map5.cub
The first game built on a ray casting engine to achieve massive success was Wolfenstein 3D. The game provided and interactive 3D world, displaying it at a higher frame rate and with a more advanced level of photorealism than previous games.
Moder video games have advanced significantly since Wolfenstein 3D, with faster CPUs, more memory, ant the availability of GPUs to accelerate the computation of 3D graphics. However, most modern video game 3D engines still use an advanced form of raycasting, because it remains the most efficient methos to render a 3D image.
MinilibX is the graphic library that 42 allows us to use to made our cub3D project. With it, we have some of the most basics tasks we can use to draw pixels on a screen or to make an image before putting it into that screen. Mlx provides us a call to the creation of screens, a drawing tool and a system to manage events. Some of the most useful functions are:
- mlx_init: initializes the MLX library. Is the first function we have call before using the rest of the functions. Will return NULL in case of a failed initialization.
- mlx_clear_window: clears the current window.
- mlx_get_color_value: get the color value accordingly from a int.
- mlx_pixel_put: puts a pixel on the screen.
- mlx_new_image: create a new mlx image.
- mlx_get_data_addr: gets the memory address of the given image.
- mlx_put_image_to_window: puts an image to the given window.
- mlx_loop_hook: hook into the loop.
- mlx_loop: loop over the given mlx pointer.
- mlx_xmp_file_to_image: converts an xmp file to a new image instance.
Clue: mlx_pixel_put writes directly over the screen without caring about any render frame. It sound good to use a temporary buffer.
Colors are represented in an int format. The color type we will use is the TRUE COLOR, also known as TRGB. To define a color, we initialize as: 0xTTRRGGBB, where each character means the following:
- T: transparency --> 0xFF000000
- R: red --> 0x00FF0000
- G: green --> 0x0000FF00
- B: blue --> 0x000000FF/li>
- Bits per pixel: (bpp) is the number of different colors in an image based on the color depth.
- Line-length: image width size.
- Endian: a term that describes how a sequence of bytes is sorted in computer memory. The adjectives "big" and "small" are used to specify which value is stored first. Big Endian stores the most significant value first, and Small Endian stores the least significant value first.
To understand how colors are made, we need to know how to work with bits (binary digits) in C. The operators we are going to use are the same we can find when it comes to working with Boolean algebra. These operators are:
- & AND: multiplication operation.
- I OR: addition operation.
- ^ XOR: if the states are different the return is 1, otherwise is 0.
- ~ NOT: inverts the values of the bits.
- >>: bits are shifted from left to right.
- <<: bits are shifted from right to left.
AND | OR | XOR | NOT |
---|---|---|---|
0 & 0 --> 0 | 0 | 0 --> 0 | 0 ^ 0 --> 0 | ~0 --> 1 |
0 & 1 --> 0 | 0 | 1 --> 1 | 0 ^ 1 --> 1 | ~1 --> 0 |
1 & 0 --> 0 | 1 | 0 --> 1 | 1 ^ 0 --> 1 | |
1 & 1 --> 1 | 1 | 1 --> 1 | 1 ^ 1 --> 0 |
Before understanding how events work, we must know the concept of hooking. Hooking is a set of techniques used to modify the behaviour of operating systems, applications or another software through function calls and events. The software that controls this is called the HOOK.
All hooks from mlx are functions that are called when an event is found:
- KeyPress/KeyRelease: information about a key that is pressed or released.
- ButtonPress/ButtonRelease: information about a button that is pressed or released.
- MotionNotify: information about a moving mouse.
To do the cub3D you will have to read many articles about lights, colors, rays, mathematics, trigonometry, 3D games, raycasting, raytracing, etc. The good thing is that you will end up knowing a lot of things that are amazing and that you have never thought about. From here on, I recommend you to read as much as you can while coding your cub. Don't be ashamed to ask me any questions you have and don't feel bad if you have to use a mathematic function that you don't fully understand how it works. But make sure you know what you are doing while having fun coding :)