This project is about creating a simplified graphic wireframe (fils de fer) representation of a relief landscape linking various points(x, y, z) via segments.
Here I learn how to place points in space, how to join them with segments and how to observe the scene from a particular point of view.
It works with the graphic library miniLibX. This library was developed internally and includes the minimum necessary to open a window, light a pixel and deal with events linked to this window: keyboard and mouse. In this case I've used the MLX42 library, developed by Codam's colleagues (42), similar to the original but updated and documented.
The coordinates of the landscape which must be represented are stored in a file passed as a parameter to the program.
Example file parameter: 👈
$>cat 42.fdf
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 10 10 0 0 10 10 0 0 0 10 10 10 10 10 0 0 0
0 0 10 10 0 0 10 10 0 0 0 0 0 0 0 10 10 0 0
0 0 10 10 0 0 10 10 0 0 0 0 0 0 0 10 10 0 0
0 0 10 10 10 10 10 10 0 0 0 0 10 10 10 10 0 0 0
0 0 0 10 10 10 10 10 0 0 0 10 10 0 0 0 0 0 0
0 0 0 0 0 0 10 10 0 0 0 10 10 0 0 0 0 0 0
0 0 0 0 0 0 10 10 0 0 0 10 10 10 10 10 10 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
$>
Each number corresponds to a point in space.
Horizontal position corresponds to axis(x).
Vertical position corresponds to ordinate(y).
The value corresponds to altitude.
Allowed functions and libraries:
- open, read, write, close
- malloc, free
- perror, strerror
- exit
- math library.
- miniLibX library.
The Bresenham's algorithm consists of, given an initial and a final coordinate, it obtains all the intermediate coordinates from one point to the other, until forming a line. This algorithm whit pixel_put() function allows me draw the wireframe.
After several Bresenham's discarded algorithms, in the end this was that allowed me the scalability of the program.
...
The minilibX is not intended to represent 3D graphics, so the depth(z-axis) is obtained by modifying x and y coordinates. My biggest problem when I started with isometric view was dealing with sines and cosines, since I had never worked with it.
void isometric(t_fdf *fdf, t_vec2 *start, t_vec2 *end, t_depth depth)
{
float z_zoom;
z_zoom = 0.5;
start->x = (start->x - start->y) * (cos(45) * 1);
start->y = (start->x + start->y) * (sin(45) * z_zoom) - (depth.z * z_zoom
* (fdf->control.zoom * 0.2));
end->x = (end->x - end->y) * (cos(45) * 1);
end->y = (end->x + end->y) * (sin(45) * z_zoom) - (depth.z1 * z_zoom
* (fdf->control.zoom * 0.2));
(void)fdf;
}
Mount temporary container which compiles program and exits.
docker run --rm --mount type=bind,source=/home/,target=/home/ fdf:1.0 bash -c "cd $(makefile_dir) && make re"