[港大-Computer Graphic作业]Assign3- Ray Tracing

So many bugs in this assignment.

1. Type conversion

int divider = 5;
int dividend = 4;
float quotient = dividend / divider;

The result is 0 rather than 0.8 which is expected. The reason is that division between two integers is handled in integer layer rather than float layer. Which means C/C++ doesn't convert the integers to floating point. The CPU just divides it and discards fraction. So we should convert either divider or dividend to float, and then do the division.

int divider = 5;
int dividend = 4;
float quotient = (float) dividend / divider;

2. Vector Normalization

Given the ray equation

P = S + t * D 

where P = point on the ray, S = start point, D = direction vector, t = parameter

When we calculate the intersection point of a ray and a plane, the direction vector should not be modified even though it may not be a normalized vector.

For example, the function 

bool  IntersectTriangle(V3 rayStart,V3 rayDir, V3 v0, V3 v1,V3 v2, float& t,V3& intersection)

Because we calculate intersection point in this formula 

V3 intersection = rayStart + t * rayDir

So if rayDir is modified during this function, the intersection point will get wrong. And we can always get the correct answer no matter rayDir is normalized or not.

In fact, adding a constant constraint to parameters makes it safer and more reliable.

bool IntersectTriangle(const V3 rayStart,const V3 rayDir, const V3 v0, const V3 v1,const V3 v2, float& t,V3& intersection)

3. Threshold

Error is inevitable in floating point calculation. Here error means the distance between calculated result and theoretical result, which is always small and negligible. But this kind of error emerges during floating point comparison and mess up control flow.

Say, vector A parallels to vector B. While  A.dot(B) may not be exactly 0 but a value extremely close to it, 0.00001 for example. 

Considering this reason, the following code checking whether A parallels to vector B may not work (and the error probability is a little high).

So a threshold is needed. We tolerate some small errors.

/*Wrong, may not work*/
if(A.dot(B) == 0){
  /*A parallels to B*/

}

/*Correct, set a threshold*/
if(A.dot(B) < 0.00001 && A.dot(B) > 0.00001){
    /*A parallels to B*/
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值