So I am trying to draw and rotate a cube using GLKit on an iPad. My problem is that as the cube rotates it seems as the aspect ratio changes with it, squeezing it as it rotates. To be more precise, the face of the cube on the starting position has a ratio of 1/2 (width/height). Lets say A is the base of the cube and B is the height(we do not care about the depth right now, although the effect I am explaining occurs there as well). So at the starting position A/B=1/2. As it turns clockwise it gets squeezed, side A decreases and side B increases, so after a 90 degrees clockwise rotation(anticlockwise has the same effect) the ratio A/B is 2/1.
Some code...
The cube vertices:
Code :+ (void)initialize { if (!initialized) { vertices[0] = GLKVector3Make(-0.5, -0.5, 0.5); // Left bottom front vertices[1] = GLKVector3Make( 0.5, -0.5, 0.5); // Right bottom front vertices[2] = GLKVector3Make( 0.5, 0.5, 0.5); // Right top front vertices[3] = GLKVector3Make(-0.5, 0.5, 0.5); // Left top front vertices[4] = GLKVector3Make(-0.5, -0.5, -0.5); // Left bottom back vertices[5] = GLKVector3Make( 0.5, -0.5, -0.5); // Right bottom back vertices[6] = GLKVector3Make( 0.5, 0.5, -0.5); // Right top back vertices[7] = GLKVector3Make(-0.5, 0.5, -0.5); // Left top back colors[0] = GLKVector4Make(1.0, 0.0, 0.0, 1.0); // Red colors[1] = GLKVector4Make(0.0, 1.0, 0.0, 1.0); // Green colors[2] = GLKVector4Make(0.0, 0.0, 1.0, 1.0); // Blue colors[3] = GLKVector4Make(0.0, 0.0, 0.0, 1.0); // Black colors[4] = GLKVector4Make(0.0, 0.0, 1.0, 1.0); // Blue colors[5] = GLKVector4Make(0.0, 0.0, 0.0, 1.0); // Black colors[6] = GLKVector4Make(1.0, 0.0, 0.0, 1.0); // Red colors[7] = GLKVector4Make(0.0, 1.0, 0.0, 1.0); // Green int vertexIndices[36] = { // Front 0, 1, 2, 0, 2, 3, // Right 1, 5, 6, 1, 6, 2, // Back 5, 4, 7, 5, 7, 6, // Left 4, 0, 3, 4, 3, 7, // Top 3, 2, 6, 3, 6, 7, // Bottom 4, 5, 1, 4, 1, 0, }; for (int i = 0; i < 36; i++) { triangleVertices[i] = vertices[vertexIndices[i]]; triangleColors[i] = colors[vertexIndices[i]]; } effect = [[GLKBaseEffect alloc] init]; } }
and the method that draws the cube in every frame:
Code :- (void)draw { GLKQuaternion glkq=GLKQuaternionMake(-_quat.y,_quat.x,_quat.z,_quat.w); GLKMatrix4 rotationMatrix=GLKMatrix4MakeWithQuaternion (glkq); GLKMatrix4 scaleMatrix = GLKMatrix4MakeScale(0.5, 1. ,0.3); GLKMatrix4 translateMatrix = GLKMatrix4MakeTranslation(0., 0., 0.); GLKMatrix4 modelMatrix =GLKMatrix4Multiply(translateMatrix,GLKMatrix4Multiply(scaleMatrix,rotationMatrix)); GLKMatrix4 viewMatrix = GLKMatrix4MakeLookAt(0, 0, 5, 0, 0, 0, 0, 1 , 0); effect.transform.modelviewMatrix = GLKMatrix4Multiply(viewMatrix, modelMatrix); effect.transform.projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(45.0f), 3./4., 3, -5); [effect prepareToDraw]; glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glEnableVertexAttribArray(GLKVertexAttribPosition); glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, triangleVertices); glEnableVertexAttribArray(GLKVertexAttribColor); glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, 0, triangleColors); glDrawArrays(GL_TRIANGLES, 0, 36); glDisableVertexAttribArray(GLKVertexAttribPosition); glDisableVertexAttribArray(GLKVertexAttribColor); }
I cannot figure out what I am doing wrong here though I am under the impression that it is the call to
that creates a wrong perspective. Anyhow if you could point me out what might be going wrong I would be grateful.Code :effect.transform.projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(45.0f), 3./4., 3, -5);