I have an object on my screen which is presented rotated and panned, But i have 2 problems regarding the z axis rotations. It's a bit tricky to explain so i uploaded 2 videos to describe each problem.
1) Reverse rotation : After rotating the object around the x axis, the z rotations are being reversed and not as it should be.
2) Wrong Z axis rotation : Again, After rotating the object around the x axis, i'm trying to rotate the object around the z axis and the rotation results in a different axis rotations.
I do believe the video's describe the problems well.
EDIT: Second attempt
As kindly explained by the user @Fault, i understood the problems i was facing with. So i tried to solve those by just rotating the projection matrix around the z axis, which seemed to be working, but it doesn't.
Since i am rotating the model's axis, I get this problem (video). the issue appears in the 23'd seconds, while i'm demonstrating how it works. As can been seen, when the z rotation causes a wrong x and y rotations.
I do understand that i must likely have to move these rotations to world view and not model view, the thing is i'm not really sure how to do that.
Here is the relevant code:
Render function:
{
...
CC3GLMatrix *projection = [CC3GLMatrix matrix];
float h = 4.0f * self.frame.size.height / self.frame.size.width;
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:100];
[projection rotateByZ:zRotationEnd];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
[modelView populateFromTranslation:_currentPan];
[modelView rotateBy:_currentRotation];
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);
...
}
X and Y rotations:
- (void) rotateAroundX:(float) x andY:(float) y newRotate:(BOOL) isNewRotate
{
if (isNewRotate) {
rotationStart = CC3VectorMake(0.0, 0.0, 0.0);
}
int rotationDirection = ceil(zRotationEnd/90);
if (rotationDirection % 4 == 2 || rotationDirection % 4 == 3) {
rotationEnd.x = rotationEnd.x - (x-rotationStart.x);
rotationEnd.y = rotationEnd.y - (y-rotationStart.y);
} else {
rotationEnd.x = rotationEnd.x + (x-rotationStart.x);
rotationEnd.y = rotationEnd.y + (y-rotationStart.y);
}
rotationStart.x = x;
rotationStart.y = y;
_currentRotation = CC3VectorMake(rotationEnd.y, rotationEnd.x, 0);
NSLog(@"Current x is: %f y is: %f z is: %f",_currentRotation.x,_currentRotation.y,zRotationEnd);
}
Z rotations:
- (void) rotateAroundZ:(float) z newRotate:(BOOL) isNewRotate
{
if (isNewRotate) {
zRotationStart = 0;
}
zRotationEnd = zRotationEnd - (z - zRotationStart);
zRotationStart = z;
}
And the vertex shader:
attribute vec4 Position;
attribute vec4 SourceColor;
varying vec4 DestinationColor;
uniform mat4 Projection;
uniform mat4 Modelview;
attribute vec2 TexCoordIn;
varying vec2 TexCoordOut;
void main(void) {
DestinationColor = SourceColor;
gl_Position = Projection * Modelview * Position;
TexCoordOut = TexCoordIn;
}
I would really like to get this right, so any help will be appreciated.
Cheers!