Ray Tracing Rendering System
Basic System
Image *img = NULL;
View *view = NULL;
Light *lights = NULL;
OBJECT *objs = NULL;
Matrix4 mclip, mdpy;
RContext *rc;
main(int argc, char **argv)
{
Color c; int u, v;
Ray r; Inode *l;
read_scene();
init_render();
for (v = view->sc_min.y; v < view->sc_max.y; v += 1) {
for (u = view->sc_min.x; u < view->sc_max.x; u += 1) {
if (l = ray_cast(objs, (r = ray_transform(ray_view(u, v), mclip))))
c = point_shade(ray_point(r, l->t), l->n,
view->center, rc, lights, l->m);
else
c = BG_COL;
inode_free(l);
img_puti(img, u, v, col_dpymap(c));
}
}
img_write(img,"stdout",0);
exit(0);
}
Ray ray_view(int u, int v)
{
Vector4 w = v4_m4mult(v4_make((Real)(u),(Real)(v),view->sc_max.z,1), mdpy);
return ray_make(v3_v4conv(v4_m4mult(v4_make(0, 0, 1, 0), mdpy)),
v3_unit(v3_make(w.x, w.y, w.z)));
}
void init_render(void)
{
if (view == NULL)
view = initview();
mclip = m4_m4prod(view->Vinv, view->Cinv);
mdpy = m4_m4prod(view->Pinv,view->Sinv);
if (img == NULL)
img = img_init(view->sc_max.x, view->sc_max.y, 0);
if (lights == NULL)
LIST_INSERT(lights, light_ambient(0.5), Light);
rc = NEWSTRUCT(RContext);
}
Input-Output
Input
scene{
camera = view {
from = {0, 0, -3}, to = {0, 0, 0}, roll = 0, fov = 60
},
light = dist_light {direction = {0, 1, 1} },
object = csgobj{
material = plastic { },
shape = csg_prim{ sphere { center = {0, 0, 0}}}},
};
Output
Image File (stdout)
Extension Options
-
Include global illumination with recursive ray tracing.
-
Include a ray tracing acceleration method.
-
Include polygons.
Copyright © 1997 Luiz Velho