我根据developer.android.com上的入门示例做了一个简单的应用程序。它工作正常,但我改变了旋转逻辑,现在它没有显示三角形。我不确定我是否理解矩阵的东西,所以如果有人能检查我的代码我想。 这是我认为可能有问题的SurfaceView方法:
@Override
public boolean onTouchEvent(MotionEvent e){
float x = e.getX();
float y = e.getY();
switch(e.getAction()){
case MotionEvent.ACTION_MOVE:
float rotateX = 0.0f;
float rotateY = 0.0f;
float rotateZ = 0.0f;
//na coluna da esquerda, rotação no eixo Z
if(x < getWidth() / 3){
rotateZ = (y - previousY);
}
//na coluna do meio, rotação no eixo X
if(getWidth()/3 < x && x < 2*getWidth()/3){
rotateX = (y - previousY);
}
//na coluna da direita, rotação no eixo Z invertido
if(x > getWidth() / 3){
rotateZ = - (y - previousY);
}
//na linha superior, rotação no eixo Z
if(y < getHeight() / 3){
rotateZ = (x - previousX);
}
//na linha do meio, rotação no eixo Y
if(getHeight()/3 < y && y < 2*getHeight()/3){
rotateY = (x - previousY);
}
//na linha inferior, rotação no eixo Z invertido
if(y > 2*getHeight() / 3){
rotateZ = - (x - previousX);
}
mRenderer.setAngulo(mRenderer.getAnguloX() + (rotateX) * TOUCH_SCALE_FACTOR,
mRenderer.getAnguloY() + (rotateY) * TOUCH_SCALE_FACTOR,
mRenderer.getAnguloZ() + (rotateZ) * TOUCH_SCALE_FACTOR);
requestRender();
}
previousX = x;
previousY = y;
return true;
}
这是渲染器方法:
@Override
public void onDrawFrame(GL10 unused) {
//desenha o fundo
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
//define a posição da camera
//Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -5f, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
//cria uma matrix para a rotação de cada angulo
Matrix.setRotateM(xRotationMatrix, 0, anguloX, 1, 0, 0);
Matrix.setRotateM(yRotationMatrix, 0, anguloY, 0, 1, 0);
Matrix.setRotateM(zRotationMatrix, 0, anguloZ, 0, 0, 1);
float[] mMatrix = new float[16];
//aplica todas as rotações na matrix scratch
Matrix.multiplyMM(mMatrix, 0, xRotationMatrix, 0, mMatrix, 0);
Matrix.multiplyMM(mMatrix, 0, yRotationMatrix, 0, mMatrix, 0);
Matrix.multiplyMM(mMatrix, 0, zRotationMatrix, 0, mMatrix, 0);
// Calcula a view e depois a projeção
Matrix.multiplyMM(resultingMatrix, 0, mViewMatrix, 0, mMatrix, 0);
Matrix.multiplyMM(resultingMatrix, 0, mProjectionMatrix, 0, resultingMatrix, 0);
// Draw shape
triangulo.draw(resultingMatrix);
}
Triangulo类必须是正确的,因为自上次应用程序运行以来我没有更改它。
答案 0 :(得分:0)
嗯,我彻底分析了你的代码,似乎没有任何问题。
但是,我想问题是这样的:
致电时
Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -5f, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
您正在将相机设置为Z = -5并朝向原点,即朝向+ ve Z方向。
(我猜最初三角形的顶点必须在XY平面内,因此三角形是可见的。)
然后当你打电话
Matrix.setRotateM(zRotationMatrix, 0, anguloZ, 0, 0, 1);
它围绕Z轴旋转三角形,这很好。
但是你打电话
Matrix.setRotateM(xRotationMatrix, 0, anguloX, 1, 0, 0);
Matrix.setRotateM(yRotationMatrix, 0, anguloY, 0, 1, 0);
这里是围绕X和Y轴旋转三角形,因为三角形没有厚度, 它变得不可见。
可能的修复:
1)移除绕X和Y轴的旋转并检查它是否有效。
OR
2)放一个立方体而不是三角形,看它是否有效。
OR
3)如果两者都不起作用,请给我完整的代码。我将调试并运行它。
最好的运气