首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >X以外的绕轴旋转使三维物体平坦

X以外的绕轴旋转使三维物体平坦
EN

Stack Overflow用户
提问于 2022-02-27 21:32:59
回答 1查看 90关注 0票数 -1

在我的程序中,我从GLTF文件中加载一个多维数据集,并使用我自己实现的数学对象(如向量、矩阵和四元数)来显示它。

当我试图旋转立方体时,我的程序就是这样画的:

X轮:

而Y轮调:

正如你所看到的,如果我的立方体是平的,那么Y旋转看起来就像。

我试着找出罪魁祸首,但现在还没找到。但我百分之百肯定:

  • ,它不是GLTF加载程序的错误,因为我的测试显示立方体的数据是正确加载的,

  • ,这不是我的数学模块错误,因为我的测试再次告诉我,计算是正确的
  • ,只有当我试图围绕Y和Z轴旋转时才会发生。X轴旋转,不知何故没有这个问题,

我唯一不确定的是着色器。但是我的着色代码是最简单的:

代码语言:javascript
复制
#version 330 core

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;

uniform mat4 projection;
uniform mat4 view;

out vec3 fragNormal;
out float distance;

void main()
{
    gl_Position = projection * view * vec4(position, 1.0);
    vec4 tmp = view * vec4(position, 1.0);
    distance = tmp.z + 10.0;
    fragNormal = normal;
}
代码语言:javascript
复制
#version 330 core
in vec3 fragNormal;
in float distance;

out vec4 FragColor;

void main()
{
    FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f) * distance;
}

我真的不知道我还能去哪儿看看。另外,我希望为我的程序的其余部分提供我的代码,但在这一点上,很显然不可能把它放在这篇文章中,因为我的程序有很多行代码。

那么,这个错误是如何发生的呢?

编辑:

我的投影矩阵是fov=45°、near=0.1、far=100.0的标准透视图:

代码语言:javascript
复制
|1.29996 0 0 0 |
|0 2.41421 0 0 |
|0 0 -1.002 -0.2002 |
|0 0 -1 0 |

视图矩阵改变:它是一个平移矩阵(x=0,y=0,z=-10)乘以(从右)与一个旋转矩阵(在下面的列表绕Y轴在一定程度上)

代码语言:javascript
复制
| 0.5 0 0 0 |
| 0 1 0 0 |
| -0.866026 0 0.5 -10 |
| 0 0 0 1 |

旋转是从四元数取来的,但也可以直接从旋转矩阵中计算。这个错误与我如何计算旋转矩阵无关。

EN

回答 1

Stack Overflow用户

发布于 2022-02-28 21:09:20

我已经找到了错误所在:矩阵类的乘法方法。不知何故,这个代码:

代码语言:javascript
复制
mat4 operator* (const mat4& other) const {
    T _c11 = c11 * other.c11 + c12 * other.c21 + c13 * other.c31 + c14 * other.c41;
    T _c12 = c11 * other.c12 + c12 * other.c22 + c13 * other.c32 + c14 * other.c42;
    T _c13 = c11 * other.c13 + c12 * other.c23 + c13 * other.c33 + c14 * other.c43;
    T _c14 = c11 * other.c14 + c12 * other.c24 + c13 * other.c34 + c14 * other.c44;
    
    T _c21 = c21 * other.c11 + c22 * other.c21 + c23 * other.c31 + c24 * other.c41;
    T _c22 = c21 * other.c12 + c22 * other.c22 + c23 * other.c32 + c24 * other.c42;
    T _c23 = c21 * other.c13 + c22 * other.c23 + c23 * other.c33 + c24 * other.c43;
    T _c24 = c21 * other.c14 + c22 * other.c24 + c23 * other.c34 + c24 * other.c44;
    
    T _c31 = c31 * other.c11 + c32 * other.c21 + c33 * other.c31 + c34 * other.c41;
    T _c32 = c31 * other.c12 + c32 * other.c22 + c33 * other.c32 + c34 * other.c42;
    T _c33 = c31 * other.c13 + c32 * other.c23 + c33 * other.c33 + c34 * other.c43;
    T _c34 = c31 * other.c14 + c32 * other.c24 + c33 * other.c34 + c34 * other.c44;
    
    T _c41 = c41 * other.c11 + c42 * other.c21 + c43 * other.c31 + c44 * other.c41;
    T _c42 = c41 * other.c12 + c42 * other.c22 + c43 * other.c32 + c44 * other.c42;
    T _c43 = c41 * other.c13 + c42 * other.c23 + c43 * other.c33 + c44 * other.c43;
    T _c44 = c41 * other.c14 + c42 * other.c24 + c43 * other.c34 + c44 * other.c44;
    
    return mat4{_c11, c12, c13, _c14, 
                _c21, _c22, _c23, _c24, 
                _c31, _c32, _c33, _c34,
                _c41, _c42, _c43, _c44};
}

不是这样的代码:

代码语言:javascript
复制
mat4 operator* (const mat4& other) const {
    mat4<T> result;
    result.c11 = c11 * other.c11 + c12 * other.c21 + c13 * other.c31 + c14 * other.c41;
    result.c12 = c11 * other.c12 + c12 * other.c22 + c13 * other.c32 + c14 * other.c42;
    result.c13 = c11 * other.c13 + c12 * other.c23 + c13 * other.c33 + c14 * other.c43;
    result.c14 = c11 * other.c14 + c12 * other.c24 + c13 * other.c34 + c14 * other.c44;
    
    result.c21 = c21 * other.c11 + c22 * other.c21 + c23 * other.c31 + c24 * other.c41;
    result.c22 = c21 * other.c12 + c22 * other.c22 + c23 * other.c32 + c24 * other.c42;
    result.c23 = c21 * other.c13 + c22 * other.c23 + c23 * other.c33 + c24 * other.c43;
    result.c24 = c21 * other.c14 + c22 * other.c24 + c23 * other.c34 + c24 * other.c44;
    
    result.c31 = c31 * other.c11 + c32 * other.c21 + c33 * other.c31 + c34 * other.c41;
    result.c32 = c31 * other.c12 + c32 * other.c22 + c33 * other.c32 + c34 * other.c42;
    result.c33 = c31 * other.c13 + c32 * other.c23 + c33 * other.c33 + c34 * other.c43;
    result.c34 = c31 * other.c14 + c32 * other.c24 + c33 * other.c34 + c34 * other.c44;
    
    result.c41 = c41 * other.c11 + c42 * other.c21 + c43 * other.c31 + c44 * other.c41;
    result.c42 = c41 * other.c12 + c42 * other.c22 + c43 * other.c32 + c44 * other.c42;
    result.c43 = c41 * other.c13 + c42 * other.c23 + c43 * other.c33 + c44 * other.c43;
    result.c44 = c41 * other.c14 + c42 * other.c24 + c43 * other.c34 + c44 * other.c44;
    
    return result;
}

可能我遇到了一些编译器实现细节。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71288630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档