繁体   English   中英

OpenGL ES 2.0透视投影->立方体为矩形

[英]OpenGL ES 2.0 perspective projection -> cube is rectangular

[已解决-请参阅底部的答案]

我正在尝试在iOS(iPhone)上使用OpenGL ES 2.0绘制具有透视图的多维数据集,但它显示为矩形。

从我在网络上搜集的资料来看,它似乎与视口/投影矩阵有关,但我似乎无法真正了解实际原因。

如果将视口设置为正方形尺寸(宽度==高度),则绘制效果很好(一个立方体),但是如果正确设置(宽度= screen_width,高度= screen_height),则该立方体将绘制为矩形。

是否应该使用视口相应地设置投影矩阵以使多维数据集保持为多维数据集?!

我的代码(请让我知道是否需要更多信息):

渲染方法:

// viewportSize is SCREEN_WIDTH and SCREEN_HEIGHT
// viewportLowerLeft is 0.0 and 0.0
ivec2 size = this->viewportSize;
ivec2 lowerLeft = this->viewportLowerLeft;
glViewport(lowerLeft.x, lowerLeft.y, size.x, size.y); // if I put size.x, size.x it draws well


mat4 projectionMatrix = mat4::FOVFrustum(45.0, 0.1, 100.0, size.x / size.y);

glUniformMatrix4fv(uniforms.Projection, 1, 0, projectionMatrix.Pointer());

矩阵运算:

static Matrix4<T> Frustum(T left, T right, T bottom, T top, T near, T far)
{
    T a = 2 * near / (right - left);
    T b = 2 * near / (top - bottom);
    T c = (right + left) / (right - left);
    T d = (top + bottom) / (top - bottom);
    T e = - (far + near) / (far - near);
    T f = -2 * far * near / (far - near);
    Matrix4 m;
    m.x.x = a; m.x.y = 0; m.x.z = 0; m.x.w = 0;
    m.y.x = 0; m.y.y = b; m.y.z = 0; m.y.w = 0;
    m.z.x = c; m.z.y = d; m.z.z = e; m.z.w = -1;
    m.w.x = 0; m.w.y = 0; m.w.z = f; m.w.w = 1;
    return m;
}
static Matrix4<T> FOVFrustum(T fieldOfView, T near, T far, T aspectRatio)
{
    T size = near * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
    return Frustum(-size, size, -size / aspectRatio, size / aspectRatio, near, far);
}

如果您还没有弄清楚,请更改:

return Frustum(-size, size, -size / aspectRatio, size / aspectRatio, near, far);

return Frustum(-size / aspectRatio, size / aspectRatio, -size, size,, near, far);

它应该正确绘制。 (或将比例从siz​​e.x / size.y更改为size.y / size.x)

好的,我发现了问题,size.x和size.y是int,所以除法返回int。

1.0 * size.x / size.y

解决问题。 面容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM