This code written in C++ is based on Servet Gulnaroglu's repository of cube.c developed in C.
The mathematical foundation behind this process is the concept of rotation matrix in Euclidean space.
The following rotation matrices perform rotations of vectors around the x, y, or z axis in three-dimensional space:
Taking into account an operator
it is possible to calculate the vectors for each axis using matrix multiplication.
A buffer is simply an area of memory that stores temporary data. In this context, it is used to store the visual representation of the scene to be displayed on the screen.
Here, buffer
is an array representing the screen. Each element of this array contains a character that will be printed on the screen. During the rendering process, this buffer is filled with the characters representing the 3D scene before being printed on the screen.
The z-buffer is used to handle the hiding of surfaces. In 3D environments, several objects may be in the same position on the screen, but at different depths. The z-buffer stores the distance (or depth) of each pixel in 3D space.
In this repository, z_buffer
is an array that stores the depth values for each position in the buffer. Before drawing a point on the screen, the value of ooz (inverse of z) is checked and compared to the existing value in the z-buffer to determine if the point is visible based on its distance from the camera.
This variable represents the inverse of the Z coordinate (1/z). It is used to calculate the position on the screen of a point in three-dimensional space based on perspective. By multiplying the x
and y
coordinates by ooz
, the position of the point is adjusted according to its distance from the camera. Its main purpose is to adjust the coordinates projected on the screen based on perspective.
- Replacing memset() with std::fill();
The memset function copies the value of c (converted to an unsigned char) to each of the first n characters in the object to which s points. The memset function returns the value of s.
memset(buffer, background_ASCII_code, WIDTH * HEIGHT);