Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenGL ES 2.0 perspective projection -> cube is rectangular
    primarykey
    data
    text
    <p><strong>[SOLVED - See answer in bottom]</strong></p> <p>I'm trying to draw a cube with perspective, using <em>OpenGL ES 2.0 on iOS (iPhone)</em>, but it's appearing as a rectangular shape.</p> <p>From what I've gathered searching the web it seems to be related to the viewport / projection matrix, but I can't seem to put the finger on the actual cause.</p> <p>If I set the viewport to a square measure (width == height) it draws perfectly well (a cube), but if I set it correctly (width = screen_width, height = screen_height) then the cube is drawn as a rectangular shape.</p> <p>Should setting the Projection matrix accordingly with the Viewport make the cube stay a cube?!</p> <p>My code (please let me know if more info is needed):</p> <p><strong>Render method:</strong></p> <pre><code>// viewportSize is SCREEN_WIDTH and SCREEN_HEIGHT // viewportLowerLeft is 0.0 and 0.0 ivec2 size = this-&gt;viewportSize; ivec2 lowerLeft = this-&gt;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()); </code></pre> <p><strong>Matrix operations:</strong></p> <pre><code>static Matrix4&lt;T&gt; 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&lt;T&gt; 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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload