我的光线拾取代码有问题。我的代码
我正在使用此代码进行选择计算:
/*-----------------------------------------------------------
Function: GetViewportSystem
Returns:
viewportCoordSystem
Get viewport coordinate system (only for reading)
Forward ray goes through origin
-------------------------------------------------------------*/
ViewportCoordSystem Camera::GetViewportSystem() const
{
ViewportCoordSystem viewportCoord;
viewportCoord.w = this->cameraPos;
viewportCoord.w -= this->lookAt;
viewportCoord.w.Normalize();
viewportCoord.u = MyMath::Vector3::Cross(MyMath::Vector3::UnitY(), viewportCoord.w);
viewportCoord.v = MyMath::Vector3::Cross(viewportCoord.w, viewportCoord.u);
float d = (this->viewport.Height / 2.0f) * (1.0f / tanf(this->viewport.fov / 2.0f));
viewportCoord.origin = this->cameraPos;
viewportCoord.origin -= d * viewportCoord.w;
return viewportCoord;
}
/*-----------------------------------------------------------
Function: MapViewport2Dto3D
Parametrs:
[in] viewportSystem - cameras viewport coordinate system
[in] point - 2D point on image
Returns:
3D mapped point in space
Map 2D image point to 3D space
Info about mapping 2D to 3D: http://meatfighter.com/juggler/
-------------------------------------------------------------*/
MyMath::Vector3 Camera::MapViewport2Dto3D(const ViewportCoordSystem & viewportSystem, const MyMath::Vector2 & point) const
{
MyMath::Vector3 res = viewportSystem.origin;
res += (point.X - this->viewport.Width * 0.5f) * viewportSystem.u;
res += (this->viewport.Height * 0.5f - point.Y) * viewportSystem.v;
return res;
}
选择自己
ViewportCoordSystem vpSystem = this->camera->GetViewportSystem();
MyMath::Vector3 pos = this->camera->MapViewport2Dto3D(vpSystem, MyMath::Vector2(mouseX, mouseY));
this->ray.dir = pos - this->camera->GetPosition();
this->ray.dir.Normalize();
this->ray.origin = this->camera->GetPosition();
使用这条射线,我计算射线 - 球体相交测试。
bool BoundingSphere::RayIntersection(const MyMath::Ray & ray) const
{
MyMath::Vector3 Q = this->sphereCenter - ray.origin;
double c = Q.LengthSquared();
double v = MyMath::Vector3::Dot(Q, ray.dir);
double d = this->sphereRadius * this->sphereRadius - (c - v * v);
if (d < 0.0) return false;
return true;
}
问题是,我的代码工作不正确。如果我使用我的球体并在其中单击,我只得到了一半球体的正确答案。当我移动相机时,它会变得一团糟,并且拾取会在球体之外做出反应。我的世界没有改变(所有世界矩阵都是身份)。只有相机在移动。我正确计算了 OpenGL 窗口中的鼠标位置(左上角有 [0, 0] 并转到 [width, height])。
PS:我在 DirectX 中成功使用此代码进行光线投射/光线追踪。我看不出有什么问题。我的 OpenGL 渲染器使用的是左手系统(它对 OpenGL 来说并不自然,但我希望这样)
编辑: 在可视化光线后,当我向左/向右移动相机时,问题就出现了。光线中心与鼠标位置不一致。