9 static cgm_vec3 cur_col, cur_spec;
10 static float cur_shin;
11 static struct image *cur_tex;
13 void rt_init(struct rtscene *scn)
15 scn->obj = darr_alloc(0, sizeof *scn->obj);
17 scn->lt = darr_alloc(0, sizeof *scn->lt);
20 cgm_vcons(&cur_col, 1, 1, 1);
21 cgm_vcons(&cur_spec, 0, 0, 0);
26 void rt_destroy(struct rtscene *scn)
30 memset(scn, 0, sizeof *scn);
34 void rt_color(float r, float g, float b)
36 cgm_vcons(&cur_col, r, g, b);
39 void rt_specular(float r, float g, float b)
41 cgm_vcons(&cur_spec, r, g, b);
44 void rt_shininess(float s)
49 static union rtobject *add_object(struct rtscene *scn, enum rt_obj_type type)
53 obj = calloc_nf(1, sizeof *obj);
56 obj->x.mtl.kd = cur_col;
57 obj->x.mtl.ks = cur_spec;
58 obj->x.mtl.shin = cur_shin;
59 obj->x.mtl.tex = cur_tex;
61 darr_push(scn->obj, &obj);
62 scn->num_obj = darr_size(scn->obj);
66 union rtobject *rt_add_sphere(struct rtscene *scn, float x, float y, float z, float r)
68 union rtobject *obj = add_object(scn, RT_SPH);
69 cgm_vcons(&obj->s.p, x, y, z);
74 union rtobject *rt_add_plane(struct rtscene *scn, float nx, float ny, float nz, float d)
76 union rtobject *obj = add_object(scn, RT_PLANE);
77 cgm_vcons(&obj->p.n, nx, ny, nz);
82 struct rtlight *rt_add_light(struct rtscene *scn, float x, float y, float z)
84 struct rtlight *lt = calloc_nf(1, sizeof *lt);
86 cgm_vcons(<->p, x, y, z);
89 darr_push(scn->lt, <);
90 scn->num_lt = darr_size(scn->lt);
95 /* color is initialized to black */
96 static void shade(struct rayhit *hit, struct rtscene *scn, int lvl, cgm_vec3 *color)
99 float ndotl, vdotr, spec;
103 struct rtmaterial *mtl = &hit->obj->x.mtl;
105 sray.origin = hit->p;
106 cgm_vnormalize(&hit->n);
107 cgm_vnormalize(&hit->ray->dir);
109 for(i=0; i<scn->num_lt; i++) {
112 cgm_vsub(&sray.dir, &sray.origin);
114 if(ray_scene(&sray, scn, 1.0f, 0)) continue;
116 cgm_vnormalize(&sray.dir);
117 ndotl = cgm_vdot(&sray.dir, &hit->n);
118 if(ndotl < 0.0f) ndotl = 0.0f;
120 rdir = hit->ray->dir;
121 cgm_vreflect(&rdir, &hit->n);
122 vdotr = cgm_vdot(&sray.dir, &rdir);
123 if(vdotr < 0.0f) vdotr = 0.0f;
124 spec = pow(vdotr, mtl->shin);
126 color->x += (mtl->kd.x * ndotl + mtl->ks.x * spec) * lt->color.x;
127 color->y += (mtl->kd.y * ndotl + mtl->ks.y * spec) * lt->color.y;
128 color->z += (mtl->kd.z * ndotl + mtl->ks.z * spec) * lt->color.z;
132 int ray_trace(cgm_ray *ray, struct rtscene *scn, int lvl, cgm_vec3 *color)
136 color->x = color->y = color->z = 0.0f;
137 if(!ray_scene(ray, scn, FLT_MAX, &hit)) {
141 shade(&hit, scn, lvl, color);
146 int ray_scene(cgm_ray *ray, struct rtscene *scn, float maxt, struct rayhit *hit)
151 struct rayhit hit0 = {FLT_MAX};
153 /* find nearest hit */
154 for(i=0; i<scn->num_obj; i++) {
155 if(ray_object(ray, scn->obj[i], maxt, hit) && hit->t < hit0.t) {
166 for(i=0; i<scn->num_obj; i++) {
167 if(ray_object(ray, scn->obj[i], maxt, 0)) {
176 int ray_object(cgm_ray *ray, union rtobject *obj, float maxt, struct rayhit *hit)
180 return ray_sphere(ray, &obj->s, maxt, hit);
182 return ray_plane(ray, &obj->p, maxt, hit);
189 #define SQ(x) ((x) * (x))
190 int ray_sphere(cgm_ray *ray, struct rtsphere *sph, float maxt, struct rayhit *hit)
192 float a, a2, b, c, d, sqrt_d, t1, t2;
194 a = SQ(ray->dir.x) + SQ(ray->dir.y) + SQ(ray->dir.z);
195 b = 2.0f * ray->dir.x * (ray->origin.x - sph->p.x) +
196 2.0f * ray->dir.y * (ray->origin.y - sph->p.y) +
197 2.0f * ray->dir.z * (ray->origin.z - sph->p.z);
198 c = SQ(sph->p.x) + SQ(sph->p.y) + SQ(sph->p.z) +
199 SQ(ray->origin.x) + SQ(ray->origin.y) + SQ(ray->origin.z) +
200 2.0f * (-sph->p.x * ray->origin.x - sph->p.y * ray->origin.y - sph->p.z * ray->origin.z) -
203 if((d = SQ(b) - 4.0f * a * c) < 0.0f) return 0;
207 t1 = (-b + sqrt_d) / a2;
208 t2 = (-b - sqrt_d) / a2;
210 if((t1 < 1e-5f && t2 < 1e-5f) || (t1 > maxt && t2 > maxt)) {
218 } else if(t2 < 1e-5f) {
221 t = t1 < t2 ? t1 : t2;
225 cgm_raypos(&hit->p, ray, t);
227 hit->n.x = hit->p.x - sph->p.x;
228 hit->n.y = hit->p.y - sph->p.y;
229 hit->n.z = hit->p.z - sph->p.z;
231 hit->obj = (union rtobject*)sph;
236 int ray_plane(cgm_ray *ray, struct rtplane *plane, float maxt, struct rayhit *hit)
241 ndotdir = cgm_vdot(&plane->n, &ray->dir);
242 if(fabs(ndotdir) < 1e-5) {
246 vo.x = plane->n.x * plane->d - ray->origin.x;
247 vo.y = plane->n.y * plane->d - ray->origin.y;
248 vo.z = plane->n.z * plane->d - ray->origin.z;
249 t = cgm_vdot(&plane->n, &vo) / ndotdir;
251 if(t < 1e-5 || t > maxt) return 0;
255 cgm_raypos(&hit->p, ray, t);
258 hit->obj = (union rtobject*)plane;