To do
- Move
parse_utils.c
from utils
folder to parser
folder
- Move
normal.c
from utils
to renderer
folder
- Create
world_print
to debug all fields at once, hence removing any loose prints like these:
void init_viewport(t_world *w)
{
...
vec3_print(vec3_cross(r->camera.normal, UPGUIDE));
vec3_print(r->camera.normal);
printf("Right ");
vec3_print(r->right);
printf("Up ");
vec3_print(r->up);
printf("wview: %f\n", r->wview);
printf("hview: %f\n", r->hview);
printf("------------------------------\n");
}
- Replace own macros in
main
with X11
ones:
int main(int argc, char **argv)
{
...
mlx_hook(world->disp.win, ON_KEYPRESS, KEYPRESS_MASK, on_keypress, world);
mlx_hook(world->disp.win, ON_CLOSE, CLOSE_MASK, quit, world);
...
return (0);
}
- Replace
nc_bzero
call in render
and world_hit
functions with simple instructions, since it might be causing the program to run slower:
int render(t_world *w)
{
...
coords.y = -1;
while (++coords.y < HEIGHT)
{
coords.x = -1;
while (++coords.x < WIDTH)
{
...
nc_bzero(&closest, sizeof(t_intersection));
...
}
}
mlx_put_image_to_window(w->disp.mlx, w->disp.win, w->disp.img, 0, 0);
return (0);
}
bool world_hit(t_vector *shapes, t_ray *ray, t_intersection *closest)
{
t_shape *shape;
t_intersection tmp;
...
nc_bzero(&tmp, sizeof(t_intersection));
while(++i < shapes->size)
{
...
}
...
- Find a way to unite these intersection-related pieces of code from the
intersects
function and the rest of the intersection functions:
bool intersects(t_shape *shape, t_ray *ray, t_intersection *inter)
{
...
if (shape->type == SPHERE)
hit = sphere_intersect(&shape->data.sp, ray, inter);
else if (shape->type == PLANE)
hit = plane_intersect(&shape->data.pl, ray, inter);
else if (shape->type == CYLINDER)
hit = cylinder_intersect(&shape->data.cy, ray, inter);
if (hit)
{
inter->ray = *ray;
inter->shape = shape;
inter->point = ray_at(ray, inter->t);
inter->normal = vec3_normalize(shape_normal(inter, ray));
}
return (hit);
}
bool sphere_intersect(t_sphere *sp, t_ray *ray, t_intersection *inter)
{
...
if (quadformula(&equation) > 0 && equation.t1 > EPSILON)
{
inter->t = equation.t1;
inter->color = sp->color;
return (true);
}
...
}
bool plane_intersect(t_plane *pl, t_ray *ray, t_intersection *inter)
{
...
if (t > EPSILON)
{
inter->t = t;
inter->color = pl->color;
return (true);
}
...
}
bool cylinder_intersect(t_cylinder *cy, t_ray *ray, t_intersection *inter)
{
...
if (t > 0.0f)
{
inter->t = t;
inter->color = cy->color;
return (true);
}
...
}
- Apply
EPSILON
correction on shape values on parsing-time to avoid complicated expressions like this:
bool plane_intersect(t_plane *pl, t_ray *ray, t_intersection *inter)
{
...
if (vec3_dot(ray->direction, vec3_add(pl->normal, vec3_new(EPSILON, EPSILON, EPSILON))) != 0.0)
{
...
numerator = vec3_dot(co, vec3_add(pl->normal, vec3_new(EPSILON, EPSILON, EPSILON)));
denominator = vec3_dot(ray->direction, vec3_add(pl->normal, vec3_new(EPSILON, EPSILON, EPSILON)));
...
}
}
- Transfer constant error messages on
read_map
, get_filesize
, init_graphics
, main
and to macros.h
:
char **read_map(t_world *world, char *filename)
{
...
message(world, "Failed allocation on read_map.");
...
message(world, "Error opening file.");
}
int get_filesize(t_world *world, char *filename)
{
...
message(world, "Error opening file.");
...
}
void init_graphics(t_world *w)
{
...
message(r, "Failed allocation on mlx pointer\n");
...
message(r, "Failed allocation on window pointer\n");
}
int main(int argc, char **argv)
{
...
message(NULL, "Usage: ./miniRT <scene>.rt");
...
- Merge calculation of quadratic and linear equations (used across the shapes different intersection functions) by applying the following logic:
Given ax^2 + bx + c = 0
if a is 0:
Use bx + c = 0 <=> x = -c/b
else:
use ax^2 + bx + c = 0 <=> quadratic formula
- Create a new plane function that returns a new
t_plane
with parameters which are not strings, in order to avoid duplicate code in cylinder.c
:
double cap_intersection(t_cylinder *cy, t_ray *ray, t_vec3 cap)
{
t_vec3 co;
t_vec3 vec;
double numerator;
double denominator;
double t;
t = -1;
vec = vec3_sub(cy->normal, cap);
if (vec3_dot(ray->direction, vec) != 0.0)
{
co = vec3_sub(ray->origin, cap);
numerator = vec3_dot(co, vec3_add(cy->normal, \
vec3_new(EPSILON, EPSILON, EPSILON)));
denominator = vec3_dot(ray->direction, vec3_add(cy->normal, \
vec3_new(EPSILON, EPSILON, EPSILON)));
t = -(numerator / denominator);
return (t);
}
return (t);
}
- Normalize functions parameters for all shapes, just like the
t_cylinder
:
t_cylinder cylinder_new(char **tokens);
t_plane plane_new(char **point, char **normal, char **color);
t_sphere sphere_new(char **center, char *diameter, char **color);
- Change
world_to_viewport
function name, as it is misleading