我对光线追踪完全陌生,并且在显示圆柱体时遇到了问题。我已经实现了查找球体的代码,并一直按照我在这里找到的教程进行操作:http ://woo4.me/wootracer/cylinder-intersection/添加一个简单的圆柱体(无限开始)。我编写了以下代码,但无论我给圆柱体提供什么坐标,都会在图像上获得一个坚实的平面。谁能给我提示我哪里出错了?我已经在 Google 上搜索了三天并通过 Stack Overflow 进行了搜索,并尝试了论坛和教程中建议的多种方法,但都无济于事,希望这里的人能看到我哪里出错了??提前致谢!!
virtual double findIntersection(Ray ray){
Vect rayOrigin = ray.getRayOrigin();
double rayOriginX = rayOrigin.getVectX();
double rayOriginY = rayOrigin.getVectY();
double rayOriginZ = rayOrigin.getVectZ();
Vect rayDirection = ray.getRayDirection();
double rayDirectionX = rayDirection.getVectX();
double rayDirectionY = rayDirection.getVectY();
double rayDirectionZ = rayDirection.getVectZ();
//a=d x2 + d y2 b = ex d x + e y d y c = ex2 + e y2−1
double a = rayDirectionX * rayDirectionX + rayDirectionY * rayDirectionY;
double b = 2* rayOriginX * rayDirectionX + 2* rayOriginY * rayDirectionY;
double c = rayOriginX * rayOriginX + rayOriginY * rayOriginY - 1;
double discriminant = b*b - 4 * a*c;
if (discriminant > 0){
//ray intersects
double root1 = ((-1 * b - sqrt(discriminant)) / 2*a - 0.0001);
if (root1 > 0){
return root1; //first root is the smallest positive value
}
else{
double root2 = ((sqrt(discriminant) - b) / 2*a - 0.0001);
return root2; //the second root is the smallest positive value
}
}
else{
//ray misses the Cylinder
return -1;
}
}
![enter image description here][1]};