简体   繁体   English

在openGL android中旋转三角形

[英]Rotate triangle in openGL android

I did a simple app based on the sample of getting started at developer.android.com. 我根据developer.android.com的入门示例制作了一个简单的应用程序。 It was working fine, but I changed the rotation logic and now it doesn't show the triangle. 它工作正常,但我更改了旋转逻辑,现在不显示三角形。 I'm not sure I understood the matrix stuff, so I would like if someone could check my code. 我不确定我是否了解矩阵内容,所以我想知道是否有人可以检查我的代码。 This is the SurfaceView method I think may have a problem: 我认为这是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;
}

This is the renderer method: 这是渲染器方法:

@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);
}

The Triangulo class must be right because I didn't changed it since the last time the app was working. Triangulo类必须正确,因为自从上次应用程序运行以来我没有对其进行更改。

Well, i analysed your code thoroughly and there does not seem to be any problem in it. 好吧,我彻底分析了您的代码,似乎没有任何问题。

But, i guess the problem is this : 但是,我想问题是这样的:

When you call 你打电话时

Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -5f, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

You are setting the camera at Z = -5 and looking towards origin ie towards the +ve Z direction. 您将相机设置为Z = -5,并朝原点方向,即+ VE Z方向。

(I guess initially the vertices of the triangle must be in XY plane and hence the triangle is visible.) (我想最初三角形的顶点必须在XY平面中,因此三角形可见)。

Then when you call 那你打电话的时候

Matrix.setRotateM(zRotationMatrix, 0, anguloZ, 0, 0, 1);

It rotates the triangle about Z axis which is fine. 它将三角形绕Z轴旋转很好。

But then you call 但是你打电话

Matrix.setRotateM(xRotationMatrix, 0, anguloX, 1, 0, 0);
Matrix.setRotateM(yRotationMatrix, 0, anguloY, 0, 1, 0);

Here you are rotating the triangle about X and Y axis and since triangles have no thickness, It becomes invisible. 在这里,您围绕X和Y轴旋转三角形,由于三角形没有厚度,因此它不可见。

Possible fixes : 可能的修复:

1) Remove the rotations about X and Y axis and check if it works. 1)取消绕X和Y轴的旋转,并检查其是否有效。

                  OR

2) Put a cube instead of triangle and see if it works. 2)放置一个立方体而不是三角形,然后看是否可行。

                  OR

3) If neither works, give me the full code.I will debug and run it. 3)如果都不行,请给我完整的代码,我将调试并运行它。

Best of Luck 祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM