通过重新定位每个顶点来旋转立方体

时间:2013-08-17 10:55:13

标签: java opengl

我暴露了我的问题! 我在每次旋转时旋转立方体重新计算每个顶点的各个坐标,我必须说 我的成绩非常好。如果立方体沿着单个轴旋转,那么一切都要完美, 沿着两个或三个轴的顶点旋转,范围加宽,在一点点之后导致立方体 时间有平流层大小。 下面是轮换的代码,我认为隐藏在其后面 问题。 旋转矩阵的相应行的各个顶点的分量的乘法 使顶点脱离其轨迹,但我不明白为什么。

    //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();

    }

感谢所有!!

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,我没有规范化矢量(rX,rY,rZ)。 正常化后,所有工作都完美无缺!

@datenwolf: 感谢回复,我已经在GPU上完成了相同的操作,但我想在CPU上执行它以获得其他问题!