我对光线跟踪完全不熟悉,并且显示圆柱体时出现问题。我已经实现了代码来查找一个球体,并且一直按照我在这里找到的教程:http://woo4.me/wootracer/cylinder-intersection/来添加一个简单的圆柱体(无限启动)。我编写了以下代码,但无论我给出的圆柱体是什么坐标,都会在图像上获得坚固的平面。谁能给我一个关于我哪里出错的提示?我已经搜索了谷歌三天,并通过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]};
答案 0 :(得分:0)