pgomez-a / cub3D

This project will allow us to explore the ray-casting technique. Our goal is to establish a dynamic view inside a maze, where we will have to find the exit.

Home Page:https://www.linkedin.com/in/pgomez-a/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cub3D

cub3d

If you want to learn more about IT topics, I invite you to join my Patreon channel and visit my website: IA Notes

What is raycasting?

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.

How to run cub3D?

  1. Clone cub3D repository:

    git clone https://github.com/pgomez-a/cub3D.git && cd cub3D
    
  2. Run make to create the game:

    make
    
  3. Execute cub3D with the map path you want to play:

    ./cub3D maps/map5.cub
    

Curiosities of ray casting

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.
map in 2D map player in 2D map ray in 2D map animated square

What is MinilibX?

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.
These are the ones that I consider the most important. However, you will have to RFM if you want to prevent some problems in the future. Take in mind that, if you want to do a good project, you first have to understand the tools your are going to work with. And because I know that the manual is a bit quiet, I suggest you to read some of these pages to get a better idea of the things you can do with mlx:
Clue: mlx_pixel_put writes directly over the screen without caring about any render frame. It sound good to use a temporary buffer.

How does the color work?

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>
Now that we know how colors are represented, it might be nice to know how to manipulate them in case we want to change some of the values of a color. For example, if all the walls in a room are white, it might be a good idea to change the transparency of some of the walls to differentiate them. The variables that we will use to manipulate the colors are:

events gif

  • 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.

Bitwise Operations

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

Events

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.
The X-Window & MacOSX graphic systems are bidirectional. On the one hand, the program sends commands to the screen to display pixels, images, etc. On the other hand, it can obtain information from the mouse and keyboard linked to the screen. Finally, the program receives events from the mouse and keyboard.

map

The technique of castin rays

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 :)

world

About

This project will allow us to explore the ray-casting technique. Our goal is to establish a dynamic view inside a maze, where we will have to find the exit.

https://www.linkedin.com/in/pgomez-a/

License:MIT License


Languages

Language:C 79.7%Language:Swift 12.6%Language:Roff 5.8%Language:Makefile 2.0%