0

我暴露了我的问题!我正在旋转立方体,在每次旋转时重新计算每个顶点的单独坐标,我必须说我得到了很好的结果。如果立方体沿单轴旋转,一切都完美,但是,沿两个或三个轴旋转顶点范围变宽,导致立方体在一段时间后具有平流层大小。下面是旋转的代码,我认为这是隐藏的问题。旋转矩阵的各个行的各个顶点的分量相乘使顶点与其轨迹分离,但我不明白为什么。

    //for each cube
    for(contatoreOnDraw=0;contatoreOnDraw<numberOfCube;contatoreOnDraw++)
    {

        x=contatoreOnDraw*3;
        y=(contatoreOnDraw*3)+1;
        z=(contatoreOnDraw*3)+2;
        gl.glPushMatrix();
        gl.glTranslatef(translation[row][x], translation[row][y], translation[row][z]);
        float angle=2;


        //Rotation matrix 3x3
        c =(float) Math.cos(angle*(Math.PI/180));
        s =(float) Math.sin(angle*(Math.PI/180));
        rotation[0][0] = (rX*rX*(1-c)) + c;
        rotation[0][1] = (rX*rY*(1-c))-rZ*s;
        rotation[0][2] = (rX*rZ*(1-c))+rY*s;
        rotation[1][0] = (rY*rX*(1-c))+rZ*s;
        rotation[1][1] = (rY*rY*(1-c)) + c;
        rotation[1][2] = (rY*rZ*(1-c))-rX*s;
        rotation[2][0] = (rX*rZ*(1-c))-rY*s;
        rotation[2][1] = (rY*rZ*(1-c))+rX*s;
        rotation[2][2] = (rZ*rZ*(1-c)) + c;

        //Updating each vertex component
        for(int i=0;i<70;i=i+3)
        {
            vX_tmp=(rotation[0][0]*cubes[contatoreOnDraw].getVertices(i))+(rotation[0][1]*cubes[contatoreOnDraw].getVertices(i+1))+(rotation[0][2]*cubes[contatoreOnDraw].getVertices(i+2));
            vY_tmp=(rotation[1][0]*cubes[contatoreOnDraw].getVertices(i))+(rotation[1][1]*cubes[contatoreOnDraw].getVertices(i+1))+(rotation[1][2]*cubes[contatoreOnDraw].getVertices(i+2));
            vZ_tmp=(rotation[2][0]*cubes[contatoreOnDraw].getVertices(i))+(rotation[2][1]*cubes[contatoreOnDraw].getVertices(i+1))+(rotation[2][2]*cubes[contatoreOnDraw].getVertices(i+2));
            cubes[contatoreOnDraw].setVertices(i, vX_tmp);
            cubes[contatoreOnDraw].setVertices(i+1, vY_tmp);
            cubes[contatoreOnDraw].setVertices(i+2, vZ_tmp);

        }
        cubes[contatoreOnDraw].draw(gl);
        gl.glPopMatrix();

    }

谢谢大家!!

4

1 回答 1

0

我找到了解决方案,我没有标准化向量(rX,rY,rZ)。标准化后一切工作完美!

@datenwolf:感谢您的回复,我已经在 GPU 上完成了相同的操作,但我想在 CPU 上执行另一个问题!

于 2013-08-17T23:19:22.347 回答