的Android的OpenGL旋转矩阵变形

问题描述:

在我的渲染器,我有:的Android的OpenGL旋转矩阵变形

private float[] MATRIX_VIEW = new float[16]; 
private float[] MATRIX_PROJECTION = new float[16]; 
private float[] MATRIX_VP = new float[16]; 

在 “onSurfaceChanged”:

... 
//Set projection 
Matrix.orthoM(
    MATRIX_PROJECTION,0, 
    -hDim,hDim, 
    -vDim,vDim, 
    1,100 
); 
//Set View 
Matrix.setLookAtM(
    MATRIX_VIEW,0, 
    cameraPosition[0], 
    cameraPosition[1], 
    cameraPosition[2], 
    cameraFacing[0], 
    cameraFacing[1], 
    cameraFacing[2], 
    cameraHook[0], 
    cameraHook[1], 
    cameraHOOK[2] 
); 
//SetView*Projection 
Matrix.multiplyMM(
    MATRIX_VP,0, 
    MATRIX_PROJECTION,0, 
    MATRIX_VIEW,0 
); 
... 

我有一个形状的许多克隆, 所以我写了 “ShapeSet_Set” 级搭配:

...  
private float[] MATRIX_ORIGIN = new float[16]; 
private float[] MATRIX_VPO = new float[16]; 

private float[] MATRIX_SCALE = new float[16]; 
... 

/* 
origin matrix is group of all shapes center position 
scale matrix represents zoom 
*/ 

和 “ShapeSet_Element” 有:

...  
private float[] MATRIX_POSITION = new float[16]; 
private float[] MATRIX_ROTATION = new float[16]; 

private float[] MATRIX_ALL = new float[16]; 
... 

/* 
position matrix is position of shape relative to origin position 
rotation matrix is shape rotation arond its center 
all matrix is matrix which will be passed to shader 
*/ 

在“DrawFrame”的渲染器中,ShapeSet_Set的“onDrawFrame”被卡住。 它计算查看*投影*产地矩阵:

... 
Matrix.multiplyMM(
    MATRIX_VPO,0, 
    RENDERER.getVPMatrix(),0, 
    MATRIX_ORIGIN,0 
); 
... 

,并呼吁各ShapeSet_Element的 “onDrawFrame”,其中包含:

... 
//Get View*Projection*Origin matrix 
System.arraycopy(
    SET.getVPOMatrix(),0, 
    MATRIX_ALL,0, 
    16 
); 
//Apply zoom 
Matrix.multiplyMM(
    MATRIX_ALL,0, 
    MATRIX_ALL,0, 
    SET.getScaleMatrix(),0 
); 
//Apply element's position 
Matrix.multiplyMM(
    MATRIX_ALL,0, 
    MATRIX_ALL,0, 
    MATRIX_POSITION,0 
); 
//Apply element's rotation 
Matrix.multiplyMM(
    MATRIX_ALL,0, 
    MATRIX_ALL,0, 
    MATRIX_ROTATION,0 
); 
... 

一切工作正常,直到我申请转动。 这是方法来设置元件的旋转:

public void setRotation(float xDeg,float yDeg,float zDeg) 
{ 
    //Set new rotation values to open up actual values 
    rotation_X = xDeg; 
    rotation_Y = yDeg; 
    rotation_Z = zDeg; 
    //Set new rotation matrix 
    Matrix.setIdentityM(MATRIX_ROTATION,0); 
    //Rotate around x axis 
    Matrix.rotateM(
    MATRIX_ROTATION,0, 
    rotation_X,0, 
    1,0,0 
); 
    //Rotate around y axis 
    Matrix.rotateM(
    MATRIX_ROTATION,0, 
    -rotation_Y,0, 
    0,1,0 
); 
    //Rotate around z axis 
    Matrix.rotateM(
    MATRIX_ROTATION,0, 
    rotation_Z,0, 
    0,0,-1 
); 
} 

当元件被围绕x轴或y轴旋转时,它工作得很好,但... 当围绕z轴的,元件的高度(原始y尺寸)旋转是geometricaly减少 直到0,旋转90度时旋转180度时放大到原始值。

有谁知道,有什么可能导致这种情况?

如果这是由错误的矩阵乘法或错误的旋转矩阵设置完成的,我无法解决。

+0

这是更好地使用处理涉及多个轴的旋转时的四元数。还防止万向节锁。 – kineticfocus 2013-02-18 08:59:22

+0

你可以更具体一些,我现在没有任何关于这种方法。 – user1993006 2013-02-18 09:05:25

+0

我尝试过四元数。围绕x轴和y轴的旋转工作正常,紧靠z轴变形仍然存在。 – user1993006 2013-02-18 14:54:31

可避免万向节锁定没有这样http://tutorialrandom.blogspot.com/2012/08/how-to-rotate-in-3d-using-opengl-proper.html

这听起来像你的模型视图矩阵东西四元是怎么了,尝试做旋转如上面的链接,它可能会解决你的问题