学习光线追踪(10)---三角形

本文介绍了一个三角形类的实现过程,重点在于如何判断光线是否与三角形相交。通过计算三角形所在平面与光线的交汇点,进一步判断该点是否位于三角形内部,采用向量叉积和角度计算两种方法进行验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0.简介

在OpenGL或者D3D中,都有一个最基本的图元,就是三角形,三角形可以组成任意形状,并且计算起来也容易,为了能让我们绘制出更多的形状,这次来实现一个三角形类。

1.三角形判断

三角形在空间中形成了一个平面,所以,三角形一定在某个平面上,所以先判断光线是否能打到扫三角形所在平面,如果光线打到了三角形所在平面,那么再计算打在平面上的点是否在三角形内。判断光线是否打在平面上已经在上一节平面中实现了,现在要实现的判断点在三角形内,这里使用的方法是,将三角形三个点和待判断点形成三个向量,两两计算叉积,三个叉积方向一样就说明点在三角形内。另一种就是判断三个向量的角度之和,效果最后都一样。

2.实现

Ray Triangle::intersect(Ray ray)
{
	Ray result(ray.direction, ray.position, ray.intensity, ray.color, nullptr);
	
	if ((result = Plane::intersect(ray)).polygon == nullptr)
		return Ray(ray.direction, ray.position, ray.intensity, vec3(0, 0, 0), nullptr);

	//判断是否在三角形内
	vec3 a = normalize((wA - result.end));
	vec3 b = normalize((wB - result.end));
	vec3 c = normalize((wC - result.end));
	//在三角形内部
	//double angle = (acos(dot(a, b))+ acos(dot(b, c))+ acos(dot(c, a)))* R_ANGLE;
	//if (fabs(angle-360.0)<1)
	//{
	//	return result;
	//}
	vec3 sa = normalize(cross(a, b));
	vec3 sb = normalize(cross(b, c));
	vec3 sc = normalize(cross(c, a));
	if(dot(sa,sb) >0.999 && dot(sb,sc) >0.999 && dot(sc,sa) >0.999)
		return result;
	return Ray(ray.direction, ray.position, ray.intensity, vec3(0, 0, 0), nullptr);
}

3.结果

三角形

4.源码

release 0.05

For those that do not know me: My name is Jacco Bikker, also known as 'Phantom'. I work as '3D tech guy' at Overloaded, a company that develops and distributes games for mobile phones. I specialize at 3D Symbian games, which require highly optimized fixed-point, non-HW-accelerated 3D engines, crammed into 250Kb installers. So basically I'm having fun. As software rendering used to be my spare time activity, I was looking for something else. I tried some AI, which was great fun, and recently I dove into a huge pile of research papers on raytracing and related topics; such as global illumination, image based lighting, photon maps and so on. One document especially grabbed my attention. It's titled: "State-of-the-Art in Interactive Ray Tracing", and was written by Wald & Slusallek. I highly recommend this paper. Basically, it summarizes recent efforts to improve the speed of raytracing, and adds a couple of tricks too. But it starts with a list of benefits of raytracing over rasterization-based algorithms. And one of those benefits is that when you go to extremes, raytracing is actually faster than rasterizing. And they prove it: Imagine a huge scene, consisting of, say, 50 million triangles. Toss it at a recent GeForce with enough memory to store all those triangles, and write down the frame rate. It will be in the vicinity of 2-5. If it isn't, double the triangle count. Now, raytrace the same scene. These guys report 8 frames per second on a dual PIII/800. Make that a quad PIII/800 and the speed doubles. Raytracing scales linearly with processing power, but only logarithmically with scene complexity. Now that I got your attention, I would like to move on to the intended contents of this crash course in raytracing.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值