Skip to content

Commit

Permalink
everything works, everything passes the norm, added a proper README
Browse files Browse the repository at this point in the history
  • Loading branch information
jwikiera committed Jan 5, 2023
1 parent ffdc29a commit 479a9f4
Show file tree
Hide file tree
Showing 20 changed files with 510 additions and 260 deletions.
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ SOURCES_DIRECTORY := ./sources/
SOURCES_LIST := main.c\
color.c\
parsing.c\
parsing2.c\
drawing.c\
mlx_helpers.c\
draw_line.c\
Expand All @@ -66,15 +67,20 @@ SOURCES_LIST := main.c\
keyhandler.c\
drawing_map.c\
init.c\
arg_handler.c
arg_handler.c\
get_proj_map.c
SOURCES := $(addprefix $(SOURCES_DIRECTORY), $(SOURCES_LIST))
HEADER_LIST := fdf.h
HEADER_FILES := $(addprefix $(INCLUDE_DIR), $(HEADER_LIST))

OBJECTS_DIRECTORY := objects/
OBJECTS_LIST := $(patsubst %.c, %.o, $(SOURCES_LIST))
OBJECTS := $(addprefix $(OBJECTS_DIRECTORY), $(OBJECTS_LIST))

.PHONY: all clean fclean re docker_build docker_run docker_clean

all: $(NAME)

$(OBJECTS_DIRECTORY):
mkdir -p $(OBJECTS_DIRECTORY)
#@echo "$(NAME): $(GREEN)$(OBJECTS_DIRECTORY) was created$(RESET)"
Expand All @@ -98,9 +104,7 @@ $(MINILIBX):
cd $(MINILIBX_DIRECTORY) && $(MAKE)
cp $(MINILIBX) .

all: $(NAME)

$(NAME): $(LIBFT) $(LIBPRINTF) $(MINILIBX) $(OBJECTS_DIRECTORY) $(OBJECTS)
$(NAME): $(LIBFT) $(LIBPRINTF) $(MINILIBX) $(OBJECTS_DIRECTORY) $(OBJECTS) $(HEADER_FILES)
$(CC) $(CFLAGS) $(OBJECTS) $(LIBRARIES) $(INCLUDES) -o $(NAME)

clean:
Expand Down
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,49 @@
# fdf
Isometric projection of 3D maps as wireframe using 42's MiniLibX.
# FDF

![screenshot1](screenshots/screenshot1.png)

FDF is short for ’fil de fer’ in French which means ’wireframe model’. It is a [School 42](https://42.fr/en/homepage/)
project that consists of
representing a map of points in isometric projection, rendered as a wireframe. The only given information are the
heights of the points.

The GitHub repo containing the source and Linux and MacOS builds is available [here](https://github.com/jwikiera/fdf).

The assignment subject can be found [here](https://raw.githubusercontent.com/jwikiera/fdf/master/subject.pdf).

I have rewritten the project in JavaScript. It is entirely contained in the `web/fdf.html` file and also hosted on
[wikiera.ch/fdf.html](https://wikiera.ch/fdf.html).

## Building

The project builds on MacOS Catalina 10.15.7 and Artix Linux 6.0.12. Other configurations were not tested.

MacOS prerequisites: Xcode.

Linux prerequisites: The X server, clang.

Then run `make` inside of the repository.

## Running

`./fdf target_map.fdf`

You can find example maps from `test_maps`.

Navigation:
- Use the `Left` and `Right` arrow keys to rotate the model around its respective y axis.
- Use the `Up` and `Down` arrow keys to rotate the model around its respective x axis.
- Use the `A` and `D` arrow keys to rotate the model around its respective z axis.
- Use the `+` and `-` keys to zoom in or out.
- Use the `P` key to toggle between an orthogonal and perspective projection.
- Use the `W` and `S` keys to increase or decrease the field of view in perspective projection mode.
- Use the `Numpad Arrows` to translate the model.

Note: in the perspective projection, zoom out if the model intersects with the screen, in order to view it entirely.

## License

This project is licensed under the GNU AFFERO GENERAL PUBLIC LICENSE.

![screenshot2](screenshots/screenshot2.png)
![screenshot3](screenshots/screenshot3.png)
Binary file added builds/fdf_linux
Binary file not shown.
34 changes: 23 additions & 11 deletions includes/fdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ typedef struct s_map_struct {
int **map;
int size_x;
int size_y;
int rect_size_x;
int rect_size_y;
int height_mult;
int pos_x;
int pos_y;
int rect_size;
int rect_size_x;
int rect_size_y;
int height_mult;
int pos_x;
int pos_y;
int pos_z;
int min_height;
int max_height;
} t_map_struct;
Expand Down Expand Up @@ -136,28 +138,38 @@ void my_mlx_pixel_put(t_fdf *fdf, int x, int y, int color);

/* drawing */
void draw_line_dda(t_fdf *fdf, const int coords[4], int color);
void draw_line_dda_colors(t_fdf *fdf, const int coords[4], int colors[2]);
void draw_line_dda_colors(t_fdf *fdf,
const int coords[4], int colors[2]);
void fill_screen(t_fdf *fdf, int color);
void draw_square(t_fdf *fdf, const int square_details[4], int color);
void circle_bres(t_fdf *fdf, const int circle_details[3], int color);
void draw_vect(t_fdf *fdf, t_vec3d *vect, int color);
void connect_vects(t_fdf *fdf, t_vec3d *v1, t_vec3d *v2, int color);

void draw_cube(t_fdf *fdf, t_plane3d *plane, int x, int y, int z, int size, int color);
void draw_cube_orthogonal(t_fdf *fdf, t_plane3d *plane, int x, int y, int z, int size, int color);
/*void draw_cube(t_fdf *fdf, t_plane3d *plane, int x, int y,
int z, int size, int color);
void draw_cube_orthogonal(t_fdf *fdf, t_plane3d *plane, int x,
int y, int z, int size, int color);
*/
void draw_map(t_fdf *fdf);

/* other util */
void print_title();
void print_title(void);
void print_map(t_map_struct *map);
t_plane3d *get_screen_plane(int width, int height, int spectator_distance);
t_screen_info *screen_info_init();
t_plane3d *get_screen_plane(int width, int height,
int spectator_distance);
t_screen_info *screen_info_init(void);
void *free_map_gnlstr(t_map_struct *map_struct, char *gnl_str);
void free_map_members(t_map_struct *map_struct);
int handle_keypress(int keycode, t_fdf *fdf);
t_fdf *init_fdf(char *argv[]);
int post_init(t_fdf *fdf, char *argv[]);
int handle_args(int argc, char *argv[]);
int destroy(t_fdf *fdf);
t_vec3d ***get_proj_map(t_fdf *fdf);
void *free_proj_map(t_vec3d ***res, int sizey,
int sizex, int sizelast);

void free_sp(char **split, size_t len);

#endif
9 changes: 6 additions & 3 deletions libs/libft/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,16 @@ SRCS_OWN_LIST := ft_str_starts_ends_with.c\
OBJS_OWN_LIST := $(patsubst %.c, %.o, $(SRCS_OWN_LIST))
OBJS_OWN := $(addprefix $(OBJECTS_DIRECTORY), $(OBJS_OWN_LIST))

INCLUDE_DIR := ./includes/

HEADER_LIST := get_next_line.h\
libft.h
HEADER_FILES := $(addprefix $(INCLUDE_DIR), $(HEADER_LIST))

ALL_SRCS_LIST := $(SRCS_BONUS_LIST) $(SRCS_GNL_LIST) $(SRCS_MANDATORY_LIST) $(SRCS_MATH_LIST) $(SRCS_OWN_LIST) $(SRCS_VEC_LIST)
OBJECTS_LIST := $(patsubst %.c, %.o, $(ALL_SRCS_LIST))
OBJECTS := $(addprefix $(OBJECTS_DIRECTORY), $(OBJECTS_LIST))

INCLUDE_DIR := ./includes/

GNL_BUFFER_FLAG := -D BUFFER_SIZE=1000

INCLUDES := -I$(INCLUDE_DIR)
Expand Down Expand Up @@ -135,7 +138,7 @@ $(OBJECTS_DIRECTORY)%.o : $(OWN_DIR)%.c $(HEADERS)
$(OBJECTS_DIRECTORY)%.o : $(VEC_DIR)%.c $(HEADERS)
$(CC) $(CFLAGS) -c $(INCLUDES) $< -o $@

$(NAME): $(OBJECTS_DIRECTORY) $(OBJECTS)
$(NAME): $(OBJECTS_DIRECTORY) $(OBJECTS) $(HEADER_FILES)
$(LIBC) $(NAME) $(OBJECTS)
$(LIBR) $(NAME)

Expand Down
Binary file added screenshots/screenshot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/screenshot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/screenshot3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion sources/color.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* line.c :+: :+: :+: */
/* color.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jwikiera <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
Expand Down
150 changes: 79 additions & 71 deletions sources/drawing_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,86 +12,94 @@

#include "fdf.h"

void draw_map(t_fdf *fdf)
void draw_y(t_fdf *fdf, t_vec3d ***proj_map)
{
fill_screen(fdf, fdf->screen_info->background_color);

int size = ft_max_int(ft_min_int((fdf->screen_info->height - 600) / fdf->map_struct->size_x, (fdf->screen_info->width - 600) / fdf->map_struct->size_y), 1);
if (fdf->screen_info->projection == orthogonal)
size = ft_max_int(1, size - fdf->screen_info->pos_z);
int base_x = fdf->screen_info->width / 2 - fdf->map_struct->size_y * size / 2 + fdf->screen_info->pos_x;
int base_y = fdf->screen_info->height / 2 - fdf->map_struct->size_x * size / 2 + fdf->screen_info->pos_y;
int depth = fdf->screen_info->screen_depth + 150;

t_vec3d *rotation_center_prev = new_vect3d(fdf->screen_info->rotation_center->x, fdf->screen_info->rotation_center->y, fdf->screen_info->rotation_center->z);
free(fdf->screen_info->rotation_center);
fdf->screen_info->rotation_center = new_vect3d(base_x + fdf->map_struct->size_y * size / 2.0 - size / 2.0, (float)base_y + (float)fdf->map_struct->size_x * (float)size / 2.0 - size / 2.0, depth + fdf->screen_info->pos_z);
t_vec3d *previous_pt;
int i;
int j;

t_vec3d *previous_point_y = 0;
for (int i = 0; i < fdf->map_struct->size_y; i ++) {
for (int j = 0; j < fdf->map_struct->size_x; j ++) {
t_vec3d *pt = new_vect3d(base_x + i * size, base_y + j * size, depth - fdf->map_struct->map[j][i] * fdf->map_struct->height_mult + fdf->screen_info->pos_z);
t_vec3d *pt_rotated = apply_rotation_matrix_to_point(pt, fdf->screen_info);
if (previous_point_y && fdf->screen_info->projection == orthogonal)
{
t_vec3d *proj1 = project_orthogonal(previous_point_y, fdf->screen_info->screen_plane, fdf->screen_info);
t_vec3d *proj2 = project_orthogonal(pt, fdf->screen_info->screen_plane, fdf->screen_info);
if (proj1 && proj2)
connect_vects(fdf, proj1, proj2, 0x00000000);
free_vectors(proj1, proj2, 0, 0);
}
else if (previous_point_y && fdf->screen_info->projection == perspective && pt_rotated->z > fdf->screen_info->screen_depth)
{
t_vec3d *proj1 = project_perspective(previous_point_y, fdf->screen_info->screen_plane, fdf->screen_info);
t_vec3d *proj2 = project_perspective(pt, fdf->screen_info->screen_plane, fdf->screen_info);
if (proj1 && proj2)
connect_vects(fdf, proj1, proj2, 0x00000000);
free_vectors(proj1, proj2, 0, 0);
}
free(previous_point_y);
previous_point_y = new_vect3d(pt->x, pt->y, pt->z);
free(pt);
free(pt_rotated);
i = 0;
while (i < fdf->map_struct->size_x)
{
previous_pt = NULL;
j = 0;
while (j < fdf->map_struct->size_y)
{
if (proj_map[i][j] && previous_pt)
connect_vects(fdf, previous_pt, proj_map[i][j], 0x00000000);
previous_pt = proj_map[i][j];
j ++;
}
if (previous_point_y)
free(previous_point_y);
previous_point_y = 0;
i ++;
}
}

t_vec3d *previous_point_x = 0;
for (int i = 0; i < fdf->map_struct->size_x; ++i) {
for (int j = 0; j < fdf->map_struct->size_y; ++j) {
t_vec3d *pt = new_vect3d(base_x + j * size, base_y + i * size, depth - fdf->map_struct->map[i][j] * fdf->map_struct->height_mult + fdf->screen_info->pos_z);
t_vec3d *pt_rotated = apply_rotation_matrix_to_point(pt, fdf->screen_info);
if (previous_point_x && fdf->screen_info->projection == orthogonal)
{
t_vec3d *proj1 = project_orthogonal(previous_point_x, fdf->screen_info->screen_plane, fdf->screen_info);
t_vec3d *proj2 = project_orthogonal(pt, fdf->screen_info->screen_plane, fdf->screen_info);
if (proj1 && proj2)
connect_vects(fdf, proj1, proj2, 0x00000000);
free_vectors(proj1, proj2, 0, 0);
}
else if (previous_point_x && fdf->screen_info->projection == perspective && pt_rotated->z > fdf->screen_info->screen_depth)
{
t_vec3d *proj1 = project_perspective(previous_point_x, fdf->screen_info->screen_plane, fdf->screen_info);
t_vec3d *proj2 = project_perspective(pt, fdf->screen_info->screen_plane, fdf->screen_info);
if (proj1 && proj2)
connect_vects(fdf, proj1, proj2, 0x00000000);
free_vectors(proj1, proj2, 0, 0);
}
free(previous_point_x);
previous_point_x = new_vect3d(pt->x, pt->y, pt->z);
free(pt);
free(pt_rotated);
void draw_x(t_fdf *fdf, t_vec3d ***proj_map)
{
t_vec3d *previous_pt;
int i;
int j;

i = 0;
while (i < fdf->map_struct->size_y)
{
previous_pt = NULL;
j = 0;
while (j < fdf->map_struct->size_x)
{
if (proj_map[j][i] && previous_pt)
connect_vects(fdf, previous_pt, proj_map[j][i], 0x00000000);
previous_pt = proj_map[j][i];
j ++;
}
if (previous_point_x)
free(previous_point_x);
previous_point_x = 0;
i ++;
}
}

//draw_vect(fdf, fdf->screen_info->rotation_center, 0);
mlx_put_image_to_window(fdf->mlx, fdf->win, fdf->mlx_data->img, 0, 0);
void adapt_size_ortho(t_fdf *fdf)
{
fdf->map_struct->rect_size = ft_max_int(
ft_min_int((fdf->screen_info->height - 600)
/ fdf->map_struct->size_x, (fdf->screen_info->width - 600)
/ fdf->map_struct->size_y), 1);
if (fdf->screen_info->projection == orthogonal)
fdf->map_struct->rect_size = ft_max_int(
1, fdf->map_struct->rect_size
- fdf->screen_info->pos_z);
fdf->map_struct->pos_x = fdf->screen_info->width / 2
- fdf->map_struct->size_y * fdf->map_struct->rect_size
/ 2 + fdf->screen_info->pos_x;
fdf->map_struct->pos_y = fdf->screen_info->height / 2
- fdf->map_struct->size_x * fdf->map_struct->rect_size
/ 2 + fdf->screen_info->pos_y;
fdf->map_struct->pos_z = fdf->screen_info->screen_depth + 150;
}

void draw_map(t_fdf *fdf)
{
t_vec3d *rotation_center_prev;
t_vec3d ***proj_map;

adapt_size_ortho(fdf);
fill_screen(fdf, fdf->screen_info->background_color);
rotation_center_prev = new_vect3d(fdf->screen_info->rotation_center->x,
fdf->screen_info->rotation_center->y,
fdf->screen_info->rotation_center->z);
free(fdf->screen_info->rotation_center);
fdf->screen_info->rotation_center = new_vect3d(fdf->map_struct->pos_x
+ fdf->map_struct->size_y * fdf->map_struct->rect_size / 2.0
- fdf->map_struct->rect_size / 2.0, (float)fdf->map_struct->pos_y
+ (float)fdf->map_struct->size_x * (float)fdf->map_struct->rect_size
/ 2.0 - fdf->map_struct->rect_size / 2.0, fdf->map_struct->pos_z
+ fdf->screen_info->pos_z);
proj_map = get_proj_map(fdf);
if (!proj_map)
return ;
draw_x(fdf, proj_map);
draw_y(fdf, proj_map);
free_proj_map(proj_map, fdf->map_struct->size_x, fdf->map_struct->size_y,
fdf->map_struct->size_y);
mlx_put_image_to_window(fdf->mlx, fdf->win, fdf->mlx_data->img, 0, 0);
free(fdf->screen_info->rotation_center);
fdf->screen_info->rotation_center = rotation_center_prev;
}
Loading

0 comments on commit 479a9f4

Please sign in to comment.