Skip to content
/ FDF Public

[42-cursus] Draw maps in a interactable 3D mesh using MLX library.

Notifications You must be signed in to change notification settings

esettes/FDF

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fils de Fer

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.


Contents

Introduction

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.

Executing program with this file, we should see something like this: 👈

Challenge

Allowed functions and libraries:

  • open, read, write, close
  • malloc, free
  • perror, strerror
  • exit
  • math library.
  • miniLibX library.
The use of any other function explicitly not allowed its forbidden.

Steps

1. Print a paralell mesh. Bresenham's line algorithm.

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.

...

2. Print isometric mesh.

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.

iso

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;
}

3. Obtain and draw map height.

4. Obtain and parse colors.



Build environment

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"

About

[42-cursus] Draw maps in a interactable 3D mesh using MLX library.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published