Post

FdF

My FdF notes.

FdF

FdF

This will just be a thought collection or code snippet storage. Not a detailed walkthrough or example of FdF.

This project involves creating a simple wireframe model of a landscape.

Overview

This project is about creating a simple wireframe model representation of a 3D landscape by linking various points (x, y, z) thanks to line segments (edges).

Rendering

Your program has to represent the model in isometric projection. The coordinates of the landscape are stored in a .fdf file, provided as a command line parameter to your program. Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
$>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
$>

this should result in something like this: FdF example

Graphic management

Your program has to display the image in a window.

  • Window management must remain smooth (e.g., switching to another window, minimizing, etc.).
  • Pressing ESC must close the window and quit the program in a clean way.
  • Clicking on the cross on the window’s frame must close the window and quit the program in a clean way.
  • The use of the images of the MiniLibX library is mandatory

Map parsing

FdF example For the map parsing a file with all height and colour data needs to be parsed. I’ve done this in multiple steps.

  1. Read the entire map into char *file_buffer.
  2. Split char *filebuffer into char **line_array by splitting on the \n character.
  3. Split char **line_array into char ***char_array by splitting on the spaces. This will create a 2d string array with a string for each map coordinate.
  4. Create a 2d struct array t_coord **pix_array according to the dimensions of char ***char_array.
  5. fill in the height and colour in the t_coord **pix_array according to the content of char ***char_array.

Math

Rotating

The following Rotation matrix is applied to the point cloud created to rotate the points around the corresponding axis. After this a translated point-map is made and passed on to draw lines in between these points.

\[{\displaystyle {\begin{alignedat}{1}R_{x}(\theta )&={\begin{bmatrix}1&0&0\\0&\cos \theta &-\sin \theta \\[3pt]0&\sin \theta &\cos \theta \\[3pt]\end{bmatrix}}\\[6pt]R_{y}(\theta )&={\begin{bmatrix}\cos \theta &0&\sin \theta \\[3pt]0&1&0\\[3pt]-\sin \theta &0&\cos \theta \\\end{bmatrix}}\\[6pt]R_{z}(\theta )&={\begin{bmatrix}\cos \theta &-\sin \theta &0\\[3pt]\sin \theta &\cos \theta &0\\[3pt]0&0&1\\\end{bmatrix}}\end{alignedat}}}\]

Drawing lines

Bresenham’s line algorithm is used for the calculation do draw pixels on a line. This is a expansion on the basic y=mx+b formula.

Rotation matrix

MLX42 documentation

This post is licensed under CC BY 4.0 by the author.

Trending Tags