学习光线追踪(9)---平面

0.简介

之前绘制的都是球体,现在我们来尝试绘制平面。

1.平面

平面表示方式有不少,这里用点法式,一个法向量和一个顶点。

class Plane : public Polygon
{
public:
	Plane();
	Plane(vec3 _normal, float _normal_distance, Material _m) :normal(_normal), normal_distance(_normal_distance) { m = _m; }
	//平面法向量
	vec3 normal;
	//法向量上长度
	float normal_distance;
	//计算光线碰撞
	virtual Ray intersect(Ray ray);
	vec3 getNormal(vec3 _vector) { return normal; };
	~Plane();
};

然后就是要实现光线碰撞到平面的算法。

这里有一点要注意,就是光线从物体A出发的时候,是不可能直接打到物体A上的,所以需要判断一下光源的位置,不然反射效果会出错。取光线出发点,然后与平面上一点成为向量,之后与平面法向量计算夹角,夹角90度说明光源在平面上。

Ray Plane::intersect(Ray ray)
{
	Ray result(ray.direction, ray.position, ray.intensity,vec3(0,0,0), nullptr);
	//判断光源位置
	if ((dot(normalize(ray.position - (normal * normal_distance)), normal)) <= 0.0001)
		return result;
	//Ray result(ray.direction, ray.position, ray.intensity, ray.color, nullptr);
	result.distance = FLT_MAX;
	float light_d = dot(((normal * normal_distance) - ray.position),normal) / dot(ray.direction , normal);
	if (light_d <= 0)
		return result;
	result.normal = normal;
	result.distance = light_d;
	result.end = ray.getEndPoint(light_d);
	result.polygon = this;
	return result;
}

这样一来就能光到平面所返回的信息了。

2.效果

平面效果

3.源码

release0.04

发布了64 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/ARTELE/article/details/103772055