I am currently creating a 3D cube in open gl es 2.0 and wish to rotate it by touch movements. Initially the cube is rendered well as a 3D cube and it appears fine like http://imageshack.us/photo/my-images/64/originalnw.png/.

However when i rotate it, it gets elongated sometimes and doesnt really appear well. It gets flattened out and after sometime, it disappears after some rotations. Like this http://imageshack.us/photo/my-images/24/56821607.png/.


Can some one help me with this problem?

This is what I am doing.

Code :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
 
if([allTouches count] == 1) {
    lastTouchPosition = [[touches anyObject] locationInView:self];
} else if([allTouches count] == 2) {
    UITouch *t1 = [[allTouches allObjects] objectAtIndex:0];
    UITouch *t2 = [[allTouches allObjects] objectAtIndex:1];
 
    CGPoint p1 = [t1 locationInView:self];
    CGPoint p2 = [t2 locationInView:self];
 
    float x = p1.x - p2.x;
    float y = p1.y - p2.y;
 
    lastPinchDistance = sqrtf(x * x + y * y);
}
}
 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{    
CGPoint currentTouchPosition = [[touches anyObject] locationInView:self];    
        NSSet *allTouches = [event allTouches];
        if([allTouches count] == 1) 
        {        
            float xMovement = lastTouchPosition.x - currentTouchPosition.x;
            float yMovement = lastTouchPosition.y - currentTouchPosition.y;
            lastTouchPosition = currentTouchPosition;
            [self rotateCubeAroundX:yMovement andY:xMovement];
        }
        else if([allTouches count] == 2) 
        {
            UITouch *t1 = [[allTouches allObjects] objectAtIndex:0];
            UITouch *t2 = [[allTouches allObjects] objectAtIndex:1];
            CGPoint p1 = [t1 locationInView:self];
            CGPoint p2 = [t2 locationInView:self];
            float x = p1.x - p2.x;
            float y = p1.y - p2.y;
 
            float currPinchDistance = sqrtf(x * x + y * y);
 
            float zoomDistance = lastPinchDistance - currPinchDistance;
 
            lastZoomDistance = lastZoomDistance - (zoomDistance / 100);
 
            lastPinchDistance = currPinchDistance;
        }
}

Code :
- (void)rotateCubeAroundX:(float)x andY:(float)y {
GLfloat totalXRotation = x * M_PI / 180.0f;
GLfloat totalYRotation = y * M_PI / 180.0f;
 Matrix4x4Utils:: applyRotation(rotationMatrix, totalXRotation, totalYRotation, 0.0);
}

In the initwithframe function, I set the rotation and projection matrix to identity initially and then set the projection of the scene.

In my drawFrame function, I set my cube vertices and its normals. Then i set the mvp matrix to identity. Then I multiply the mvp matrix with the rotation and projection matrices and I pass it to the shader. The inital drawing is fine, but on rotation using touch, it gets flattened out. Could someone help me, as to why is this happening?