我使用 OpenGL 创建了一个立方体,但目前它可以自由旋转,所以它可以向任何方向旋转。
我如何对其进行编码,使其仅向上、向下、向左和向右旋转?
这是我的旋转代码:
+ (void)applyRotation:(GLfloat *)m x:(GLfloat)x y:(GLfloat)y z:(GLfloat)z {
GLfloat tempMatrix[16];
if(x != 0) {
GLfloat c = cosf(x);
GLfloat s = sinf(x);
[self applyIdentity:tempMatrix];
tempMatrix[5] = c;
tempMatrix[6] = -s;
tempMatrix[9] = s;
tempMatrix[10] = c;
[self multiplyMatrix:tempMatrix by:m giving:m];
}
if(y != 0) {
GLfloat c = cosf(y);
GLfloat s = sinf(y);
[self applyIdentity:tempMatrix];
tempMatrix[0] = c;
tempMatrix[2] = s;
tempMatrix[8] = -s;
tempMatrix[10] = c;
[self multiplyMatrix:tempMatrix by:m giving:m];
}
if(z != 0) {
GLfloat c = cosf(z);
GLfloat s = sinf(z);
[self applyIdentity:tempMatrix];
tempMatrix[0] = c;
tempMatrix[1] = -s;
tempMatrix[4] = s;
tempMatrix[5] = c;
[self multiplyMatrix:tempMatrix by:m giving:m];
}
}