主要实现的功能就是UV贴图,与光照计算。
思路:
1. 一个图片的每一个pixel, 都随机采样n次,然后每一次采样就是相当于发出一条ray,来检测是否碰到物体。
for (s = 0; s < NUM_SAMPLES; s++)
{
xl_samples[s] -= 0.5f;
yl_samples[s] -= 0.5f;
VectorCopy(cam.eye, start);
VectorMA(start, cam.aperature*xl_samples[s], cam_r, start);
VectorMA(start, cam.aperature*yl_samples[s], cam_u, start);
x_samples[s] = cam.d*((((x_samples[s] + 0.5f + c) / ((float)IMAGE_WIDTH)))*(cam.right - cam.left) + cam.left);
y_samples[s] = cam.d*((((y_samples[s] + 0.5f + r) / ((float)IMAGE_HEIGHT)))*(cam.top - cam.bottom) + cam.bottom);
VectorCopy(cam.eye, end);
VectorMA(end, cam.d, cam_f, end);
VectorMA(end, x_samples[s], cam_r, end);
VectorMA(end, y_samples[s], cam_u, end);
VectorSub(end, start, dir);
VectorNormalize(dir);
if (shape_intersect_all(start, dir, t_samples[s], &hit))
{
VectorNegate(dir, eyedir);
VectorMA(start, hit.t, dir, hitpos);
shape_shade(hit.shape, hitpos, eyedir, t_samples[s], color);
VectorAdd(total, color, total);
}
// background
else
{
total[0] += 0.2f;
total[1] += 0.2f;
total[2] += 0.2f;
}
}
2. 如果碰到了物体,就进行计算hitpos上对应的uv值
// v = hitpos
void shape_shade(shape_t *s, vec3 v, vec3 eyedir, float time, vec3 color)
{
typedef void (shadefunc)(shape_t*, vec3, vec3, float, vec3);
static shadefunc *shadefuncs[] = { shape_shade_tri , shape_shade_sphere };
shadefuncs[s->type](s, v, eyedir, time, co