【发布时间】:2015-09-12 22:49:22
【问题描述】:
我不知道为什么,但似乎我的 z 轴被窃听了(似乎它的价值翻了一番)
这应该是一个立方体
然而,看起来好像是“更深入”,这似乎是错误的
我的 pr_Matrix:
mat4 mat4::prespective(float fov, float aspectRatio, float near, float far){
mat4 result;
float yScale = 1.0f / tan(toRadians(fov/2.0f));
float xScale = yScale / aspectRatio;
float frustumLength = far - near;
result.elements[0 + 0 * 4] = xScale;
result.elements[1 + 1 * 4] = yScale;
result.elements[2 + 2 * 4] = -(far + near) / frustumLength;
result.elements[3 + 2 * 4] = -1.0f;
result.elements[2 + 3 * 4] = -(2.0f * far * near) / frustumLength;
return result;
}
我的 ml_Matrix:
maths::mat4 &ProjectionMatrix(){
maths::mat4 m_ProjM = maths::mat4::identity();
m_ProjM *= maths::mat4::translation(m_Position);
m_ProjM *= maths::mat4::rotation(m_Rotation.x, maths::vec3(1, 0, 0));
m_ProjM *= maths::mat4::rotation(m_Rotation.y, maths::vec3(0, 1, 0));
m_ProjM *= maths::mat4::rotation(m_Rotation.z, maths::vec3(0, 0, 1));
maths::mat4 scale_matrix = maths::mat4(m_Scale);
scale_matrix.elements[3 + 3 * 4] = 1.0f;
m_ProjM *= scale_matrix;
return m_ProjM;
}
我的 vw_matrix(相机)
void update(){
maths::mat4 newMatrix = maths::mat4::identity();
newMatrix *= maths::mat4::rotation(m_Pitch, maths::vec3(1, 0, 0));
newMatrix *= maths::mat4::rotation(m_Yaw, maths::vec3(0, 1, 0));
newMatrix *= maths::mat4::translation(maths::vec3(-m_Pos.x, -m_Pos.y, -m_Pos.z));
m_ViewMatrix = newMatrix;
}
我在 glsl 代码中的矩阵乘法:
vec4 worldPosition = ml_matrix * vec4(position, 1.0);
vec4 positionRelativeToCamera = vw_matrix * worldPosition;
gl_Position = pr_matrix * positionRelativeToCamera;
编辑:我想我明白了! (一旦我有时间会仔细检查数学)大多数教程(所以,我的代码的来源)使用 mat4::prespective(float fov, float aspectRatio, float near, float far),他们没有说的是“fov”的意思是“fovy”(即“垂直视野”),现在通过这个简单的更改,结果似乎复制了“游戏中通常的 fov”:
float xScale = 1.0f / tan(toRadians(fov/2.0f));
float yScale = xScale * aspectRatio;
感谢您指出这是一个 fov 问题Henk De Boer
【问题讨论】:
-
你在preferive函数中传递什么作为“fov”。
-
这是我的函数调用“mat4 proj = mat4::prespective(80.0f, 16.0f/9.0f, 0.1f, 100.0f);”所以 80.0f
-
试着把它放在更低的地方,应该会有所帮助。说 45-60。
-
这看起来很好,谢谢。但这和我们在游戏中看到的“fov slider”是一样的吗?因为如果是 45-60 似乎相当低
-
是的,但是你会发现,一旦你有了一个实际的场景,80 fov 的结果可能没有立方体看起来那么剧烈。此外,更令人困惑的是,有些游戏将 fov 宣传为 2*therealfov,所以当他们说 fov 为 90 时,实际上是 45。我已将此作为答案发布。
标签: opengl projection perspective z-axis