簡體   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