#include "fdf.h" void draw_image(t_fdf *fdf) { int i; int j; fdf->rad_x = fdf->rot_x * M_PI / 180; fdf->rad_y = fdf->rot_y * M_PI / 180; i = -1; while (++i < fdf->img_size_y * fdf->img_sizel) *(unsigned int *)(fdf->img_addr + i) = 0; j = -1; while (++j < fdf->map_height) { i = -1; while (++i < fdf->map_width) draw_grid(fdf, i, j); } mlx_put_image_to_window(fdf->mlx_ptr, fdf->win_ptr, fdf->img_ptr, 0, 0); position_state(fdf); } void draw_grid(t_fdf *fdf, int i, int j) { int *point_start; int *point_end; point_start = new_coordinates(fdf, i, j); point_end = NULL; if (i + 1 < fdf->map_width) point_end = new_coordinates(fdf, i + 1, j); draw_lines(fdf, point_start, point_end); if (j + 1 < fdf->map_height) { free(point_end); point_end = new_coordinates(fdf, i, j + 1); } draw_lines(fdf, point_start, point_end); free(point_start); free(point_end); ft_putchar_fd('\n',1); } // return the position of the point X // in percentage of the total height of the map : // z_min---start---point---end----z_max // 0%--------s%-----P-------e%-----100% // line: ------------- // ex. . . . . . ---e // --- . // px. . . -p- . // ---. . // s-- . . // py ey // return value is 0 <= X <= 100 // - z_total : z_max - z_min // - start : start // - line : end - start // - p_numerator : px + py // - p_denominator : ex + ey int color_percent(t_fdf *fdf, int *p_start, int *p_end, int p_numerator, int p_denominator) { int z_total; int start; int line; int percent; z_total = fdf->z_amplitude; line = ft_abs(p_end[2] - p_start[2]); line *= 100; if (p_start[2] < p_end[2]) { start = p_start[2] - fdf->min_z; start *= 100; } else { start = p_end[2] - fdf->min_z; start *= 100; p_numerator = p_denominator - p_numerator; } percent = (start + line * p_numerator / p_denominator) / z_total; /* if (percent > 100) { ft_putchar_fd('[',1); ft_putnbr_fd(z_total,1); ft_putchar_fd(',',1); ft_putnbr_fd(start,1); ft_putchar_fd(',',1); ft_putnbr_fd(line,1); ft_putchar_fd(',',1); ft_putnbr_fd(p_numerator,1); ft_putchar_fd(',',1); ft_putnbr_fd(p_denominator,1); ft_putchar_fd(',',1); ft_putnbr_fd(percent,1); ft_putchar_fd(']',1); } */ /* start end min max z_total start line p_numerator p_denominator percent ( 1, -1, -1, 1)[ 2, 200, 200, 16, 61, 126] ( -1, 1, -1, 1)[ 2, 0, 200, -8, -7, 114] percent = (start + line * p_numerator / p_denominator) / z_total; */ return (percent); } void draw_lines(t_fdf *fdf, int *start, int *end) { int dx; int dy; int i; int j; int z; if (end) { dx = end[0] - start[0]; dy = end[1] - start[1]; /* ft_putchar_fd('(',1); ft_putnbr_fd(start[2],1); ft_putchar_fd(',',1); ft_putnbr_fd(end[2],1); ft_putchar_fd(',',1); ft_putnbr_fd(fdf->min_z,1); ft_putchar_fd(',',1); ft_putnbr_fd(fdf->max_z,1); ft_putchar_fd(')',1); */ i = 0; j = 0; while (ft_abs(i) <= ft_abs(dx) && ft_abs(j) <= ft_abs(dy)) { z = color_percent(fdf, start, end, ft_abs(i) + ft_abs(j), ft_abs(dx) + ft_abs(dy)); draw_color_pixel(fdf, start[0] + i, start[1] + j, z); if (!ft_abs(dx) || ft_abs(j) < ft_abs(i * dy / dx)) j += ft_sign(dy); else i += ft_sign(dx); } } } void draw_color_pixel(t_fdf *fdf, int new_x, int new_y, int z) { int color; color = COLOR_START; /* int color_start; int color_end; color_start = COLOR_START; color_end = COLOR_END; int color_r; int color_g; int color_b; */ color = color ^ (((0xff / fdf->z_amplitude) * (z - fdf->min_z)) << 16); color = color ^ (((0xff / fdf->z_amplitude) * (z - fdf->min_z)) << 8); color = color ^ (((0xff / fdf->z_amplitude) * (z - fdf->min_z)) << 0); /* int color; color = COLOR_START; z -= (z * fdf->zoom) / fdf->offset; z /= fdf->altitude; if (z > fdf->min_z && fdf->z_amplitude) { color = color ^ (((0xff / fdf->z_amplitude) * (z - fdf->min_z)) << 16); color = color ^ (((0xff / fdf->z_amplitude) * (z - fdf->min_z)) << 0); } */ /* int color; int height; color = COLOR_START; height = z; if (height != 0) height /= fdf->altitude; height -= height / fdf->offset; if (height > fdf->min_z && fdf->z_amplitude) { color = color ^ (((0xff) * (height - fdf->min_z)) << 16); color = color ^ (((0xff) * (height - fdf->min_z)) << 0); // color = color ^ (((0xff / fdf->z_amplitude) * (height - fdf->min_z)) << 16); // color = color ^ (((0xff / fdf->z_amplitude) * (height - fdf->min_z)) << 0); } */ draw_pixel(fdf, new_x, new_y, color); } void draw_pixel(t_fdf *fdf, int x, int y, int color) { int position; int xmax; int ymax; xmax = fdf->img_sizel / (fdf->img_bpp / 8); ymax = fdf->img_size_y; if (x < 0 || y < 0 || x > xmax || y > ymax) return ; position = y * fdf->img_sizel + x * fdf->img_bpp / 8; *(unsigned int *)(fdf->img_addr + position) = color; }