Example 5 : Texturing

主要实现的功能就是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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值