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*/
}