【发布时间】:2022-01-11 07:29:03
【问题描述】:
当相机四处移动时,为什么我的起始光线仍然停留在原点 0、0、0,即使相机位置已经更新?
如果我启动程序并且我的相机位置默认为 0、0、0,它工作正常。但是一旦我将相机移动到右侧并单击更多,线条仍然来自 0 0 0什么时候应该从相机所在的地方开始。我做错了什么吗?我已经检查以确保它们在主循环中得到更新。我使用了以下引用的代码片段:
picking in 3D with ray-tracing using NinevehGL or OpenGL i-phone
// 1. Get mouse coordinates then normalize
float x = (2.0f * lastX) / width - 1.0f;
float y = 1.0f - (2.0f * lastY) / height;
// 2. Move from clip space to world space
glm::mat4 inverseWorldMatrix = glm::inverse(proj * view);
glm::vec4 near_vec = glm::vec4(x, y, -1.0f, 1.0f);
glm::vec4 far_vec = glm::vec4(x, y, 1.0f, 1.0f);
glm::vec4 startRay = inverseWorldMatrix * near_vec;
glm::vec4 endRay = inverseWorldMatrix * far_vec;
// perspective divide
startR /= startR.w;
endR /= endR.w;
glm::vec3 direction = glm::vec3(endR - startR);
// start the ray points from the camera position
glm::vec3 startPos = glm::vec3(camera.GetPosition());
glm::vec3 endPos = glm::vec3(startPos + direction * someLength);
第一个屏幕截图我点击了一些光线,第二个我将相机向右移动并点击了一些,但初始的起始光线仍然在 0、0、0。我正在寻找的是光线来相机位置在第三张图像中的任何位置,即红色光线抱歉造成混乱,红线应该射出并射入远处而不是向上。
// and these are my matrices
// projection
glm::mat4 proj = glm::perspective(glm::radians(camera.GetFov()), (float)width / height, 0.1f, 100.0f);
// view
glm::mat4 view = camera.GetViewMatrix(); // This returns glm::lookAt(this->Position, this->Position + this->Front, this->Up);
// model
glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f));
【问题讨论】: