rtv1/src/rtv1.h

248 lines
5.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rtv1.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/06 10:11:47 by gbrochar #+# #+# */
/* Updated: 2019/02/22 16:24:26 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef RTV1_H
# define RTV1_H
# include "mlx.h"
# include <math.h>
# include <stdlib.h>
# include "../libft/libft.h"
# define E (double)0.000001
# define TO_RAD (double)57.2957795131
# define SUCCESS 0
# define FAILURE -1
# define TRUE 1
# define FALSE 0
# define PLANE 0
# define SPHERE 1
# define CYLINDER 2
# define CONE 3
# define SPOT 4
# define CAMERA 5
# define SPECULAR 6
# define AMBIENT 7
# define COMMENTARY 8
# define MLX_NOFLAG 0
# define MLX_PTR 1 << 0
# define MLX_WIN 1 << 1
# define MLX_IMG 1 << 2
# define WINX (double)1280
# define WINY (double)720
# define ZOOM (double)1
# define VP_DIST (double)1
# define DEFAULT_AMBIENT (double)0.2
# define DEFAULT_SPEC_RADIUS (double)0.3
# define DEFAULT_SPEC_RATIO (double)1
typedef struct s_vec t_vec;
typedef struct s_ray t_ray;
struct s_vec
{
double x;
double y;
double z;
};
struct s_ray
{
t_vec o;
t_vec d;
};
typedef struct s_color t_color;
struct s_color
{
int r;
int g;
int b;
};
typedef struct s_plane t_plane;
typedef struct s_sphere t_sphere;
typedef struct s_cylinder t_cylinder;
typedef struct s_cone t_cone;
struct s_plane
{
t_vec o;
t_vec d;
};
struct s_sphere
{
t_vec o;
double r;
};
struct s_cylinder
{
t_vec o;
t_vec d;
double r;
};
struct s_cone
{
t_vec o;
t_vec d;
double k;
};
typedef struct s_spot t_spot;
struct s_spot
{
t_vec o;
double i;
};
typedef struct s_camera t_camera;
struct s_camera
{
t_vec o;
t_vec d;
t_vec uv;
t_vec rv;
t_vec ul;
double width;
double height;
double dist;
};
typedef struct s_spot_list t_spot_list;
typedef struct s_obj_list t_obj_list;
struct s_obj_list
{
void *obj;
int type;
t_color c;
double t;
t_obj_list *next;
};
struct s_spot_list
{
t_spot spot;
t_spot_list *next;
};
typedef struct s_mlx
{
void *ptr;
void *win;
void *img;
int *data;
int size_l;
int endian;
int bpp;
int padding;
} t_mlx;
typedef struct s_func
{
double (*intersect_obj[4])(void *, t_ray);
t_vec (*normal_obj[4])(void *, t_ray, double);
} t_func;
typedef struct s_env
{
t_mlx mlx;
t_func func;
t_spot_list *spots;
t_obj_list *objs;
t_camera camera;
double spec_radius;
double spec_ratio;
double ambient;
} t_env;
typedef int (*t_add_elem)(char **, t_env *);
int parse(char *path, t_env *e);
int parse_line(char *line, t_env *e);
void free_parsing(t_env *e);
int lex(char *str);
int set_specular(char **data, t_env *e);
int set_ambient(char **data, t_env *e);
void init_camera(t_camera *camera);
int set_camera(char **data, t_env *e);
void init_spot(t_spot *spot);
int add_spot(char **data, t_env *e);
void init_plane(t_plane *plane, t_color *c);
int add_plane(char **data, t_env *e);
void init_sphere(t_sphere *sphere, t_color *c);
int add_sphere(char **data, t_env *e);
void init_cylinder(t_cylinder *cylinder, t_color *c);
int add_cylinder(char **data, t_env *e);
void init_cone(t_cone *cone, t_color *c);
int add_cone(char **data, t_env *e);
int add_obj_node(t_env *e, void *obj, int type,
t_color c);
int add_spot_node(t_env *e, t_spot spot);
void mlx_free(char flags, t_mlx *mlx);
void put_pixel(t_env *e, int x, int y, t_color c);
int mlx_exit(char flags, t_mlx *mlx);
int init_mlx(t_env *e);
int key_hook(int keycode, void *e_void);
int red_cross_hook(void *e_void);
t_color color(int r, int g, int b);
t_color color_add(t_color c1, t_color c2);
t_color color_mul(t_color c, double scalar);
t_color color_cap(t_color c);
t_vec vec(double x, double y, double z);
t_vec vec_add(t_vec u, t_vec v);
t_vec vec_sub(t_vec u, t_vec v);
t_vec vec_mul(t_vec vec, double scalar);
double vec_length(t_vec vec);
t_vec cross_product(t_vec u, t_vec v);
double dot_product(t_vec u, t_vec v);
int check_direction(t_vec d);
t_vec normalize(t_vec *vec);
t_ray set_ray(t_env *e, int x, int y);
t_vec get_r_vec(t_vec i_ray, t_vec normal);
t_vec rotate(t_vec vec, t_vec axis, double theta);
int solve_quadratic(double params[3], double roots[2]);
double intersect_plane(void *obj, t_ray r);
double intersect_sphere(void *obj, t_ray r);
double intersect_cylinder(void *obj, t_ray r);
double intersect_cone(void *obj, t_ray r);
t_vec normal_plane(void *obj, t_ray r, double t);
t_vec normal_sphere(void *obj, t_ray r, double t);
t_vec normal_cylinder(void *obj, t_ray r, double t);
t_vec normal_cone(void *obj, t_ray r, double t);
void raytrace(t_env *e);
void raytrace_pixel(t_env *e, int x, int y);
t_obj_list *intersect_ray(t_env *e, t_ray r);
t_color phong(t_env *e, t_ray r, t_obj_list *obj);
#endif