gpt4 book ai didi

c++ - OpenGL 拾取 - 射线/球体相交错误

转载 作者:行者123 更新时间:2023-11-30 02:53:36 30 4
gpt4 key购买 nike

我的光线拾取代码有问题。我的代码

我正在使用此代码进行选择计算:

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

问题是,我的代码工作不正确。如果我视觉使用我的球体,并在它们内部单击,我只得到一半球体的正确答案。当我移动相机时,它会变得一团糟,并且拾取会在球体外使用react。我的世界没有改变(所有世界矩阵都是身份)。只有相机在移动。我正确计算了 OpenGL 窗口中的鼠标位置(左上角有 [0, 0] 并转到 [width, height])。

PS:我在 DirectX 中成功地使用此代码进行光线转换/光线追踪。我看不出有什么问题。我的 OpenGL 渲染器使用的是左手系统(这对 OpenGL 来说不自然,但我希望这样)

编辑:可视化光线后,问题出现了,当我向左/向右移动相机时。光线中心与鼠标位置不一致。

最佳答案

好的.. 找到了问题...对于任何可能感兴趣的人

那几行不正确

viewportCoord.u = MyMath::Vector3::Cross(MyMath::Vector3::UnitY(), viewportCoord.w);
viewportCoord.v = MyMath::Vector3::Cross(viewportCoord.w, viewportCoord.u);

工作解决方案是

viewportCoord.u = MyMath::Vector3::Cross(viewportCoord.w, MyMath::Vector3::UnitY());
viewportCoord.u.Normalize();
viewportCoord.v = MyMath::Vector3::Cross(viewportCoord.u, viewportCoord.w);
viewportCoord.v.Normalize();

关于c++ - OpenGL 拾取 - 射线/球体相交错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17839469/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com