Android OpenGL旋转矩阵变形

时间:2013-02-18 08:55:17

标签: android opengl-es matrix rotation

在我的渲染器中,我有:

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”被篡改。 它计算View * Projection * Origin矩阵:

...
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维度)几何减小 旋转90度时最大为0,旋转180度时放大到原始值。

有谁知道,这可能是什么原因?

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

1 个答案:

答案 0 :(得分:0)

可以避免没有像http://tutorialrandom.blogspot.com/2012/08/how-to-rotate-in-3d-using-opengl-proper.html

这样的四元数的万向节锁定

听起来你的模型视图矩阵中的某些内容出错了,尝试像上面的链接一样进行旋转,它可以解决你的问题