Ex02: Minor improvements
This commit is contained in:
parent
47ca6ee0c6
commit
ad84fe083d
1 changed files with 11 additions and 14 deletions
|
@ -192,31 +192,28 @@ std::optional<glm::vec3> Ex02::RT::Internal::rayToSphere(glm::vec3 rayPos,
|
||||||
glm::vec3 spherePos,
|
glm::vec3 spherePos,
|
||||||
float sphereRadius) {
|
float sphereRadius) {
|
||||||
// check if there is collision
|
// check if there is collision
|
||||||
glm::vec3 tempVec = rayPos - spherePos;
|
const glm::vec3 tempVec = rayPos - spherePos;
|
||||||
float temp = rayDirUnit.x * tempVec.x + rayDirUnit.y * tempVec.y +
|
float temp = rayDirUnit.x * tempVec.x + rayDirUnit.y * tempVec.y +
|
||||||
rayDirUnit.z * tempVec.z;
|
rayDirUnit.z * tempVec.z;
|
||||||
float delta = temp * temp;
|
float delta =
|
||||||
temp = tempVec.x * tempVec.x + tempVec.y * tempVec.y + tempVec.z * tempVec.z -
|
temp * temp - (tempVec.x * tempVec.x + tempVec.y * tempVec.y +
|
||||||
sphereRadius * sphereRadius;
|
tempVec.z * tempVec.z - sphereRadius * sphereRadius);
|
||||||
delta -= temp;
|
|
||||||
|
|
||||||
if (delta < 0.0F) {
|
if (delta < 0.0F) {
|
||||||
return {};
|
return {};
|
||||||
} else {
|
} else {
|
||||||
temp = rayDirUnit.x * tempVec.x + rayDirUnit.y * tempVec.y +
|
temp = rayDirUnit.x * tempVec.x + rayDirUnit.y * tempVec.y +
|
||||||
rayDirUnit.z * tempVec.z;
|
rayDirUnit.z * tempVec.z;
|
||||||
float dist = -temp - std::sqrt(delta);
|
const float temp2 = std::sqrt(delta);
|
||||||
float dist2 = -temp + std::sqrt(delta);
|
const float dist = -temp - temp2;
|
||||||
float min = dist > dist2 ? dist2 : dist;
|
const float dist2 = -temp + temp2;
|
||||||
float max = dist > dist2 ? dist : dist2;
|
if (dist < 0.0F) {
|
||||||
if (min < 0.0F) {
|
if (dist2 < 0.0F) {
|
||||||
if (max < 0.0F) {
|
|
||||||
return {};
|
return {};
|
||||||
} else {
|
} else {
|
||||||
return {rayPos + rayDirUnit * max};
|
return {rayPos + rayDirUnit * dist2};
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return {rayPos + rayDirUnit * min};
|
return {rayPos + rayDirUnit * dist};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue