gpt4 book ai didi

c++ - glm::lookAt 给出了 2D 轴的意外结果

转载 作者:行者123 更新时间:2023-12-02 18:14:48 24 4
gpt4 key购买 nike

基本上我有一个局部空间,其中 y 指向下方,x 指向右侧,就像

       |
|
---------> +x
|
| +y

中心位于 (320, 240),因此左上角为 (0,0)。一些窗口系统使用它。

所以我有这个代码

    auto const proj = glm::ortho(0.0f, 640.0f, 0.0f, 480.0f);
auto const view = glm::lookAt(
glm::vec3(320.0f, 240.0f, -1.0f), //eye position
glm::vec3(320.0f, 240.0f, 0.0f), //center
glm::vec3(0.0f, -1.0f, 0.0f) //up
);

我想我需要从 z 的负位置向 z 正方向看 (320,240,0),“向上”方向是负 y。

但是它似乎没有提供正确的结果

    auto const v = glm::vec2(320.0f, 240.0f);   
auto const v2 = glm::vec2(0.0f, 0.0f);
auto const v3 = glm::vec2(640.0f, 480.0f);
auto const result = proj * view * glm::vec4(v, 0.0f, 1.0f); //expects: (0,0), gives (-1,-1)
auto const result2 = proj * view * glm::vec4(v2, 0.0f, 1.0f); //expects: (-1,1), gives (-2,0)
auto const result3 = proj * view * glm::vec4(v3, 0.0f, 1.0f); //expects: (1,-1), gives (0,-2)

最佳答案

这不是 View 和投影的工作原理。 View 矩阵告诉您哪个点应映射到 View 空间中的 [0,0]。看来您尝试将可见区域的中心映射到 [0,0],但随后您使用的投影矩阵假设 [0,0] 是左上角。

由于您首先应用 View 矩阵,因此会给出 view * glm::vec4(v, 0.0f, 1.0f) = [0,0] 的结果。然后将投影矩阵应用到您将 0,0 定义为左上角的位置,因此 proj * [0,0] 将得到 [-1,-1]。

我不是 100% 确定你想要实现什么,但是如果你想使用给定的投影矩阵,那么 View 矩阵必须以可见区域的左上角点的方式变换场景映射到 [0,0]。

您还可以调整项目以使用范围 [-320, 320](分别为 [-240, 240]),并使用 View 矩阵将中心映射到 [0,0]。

关于c++ - glm::lookAt 给出了 2D 轴的意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71747705/

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