0
votes

i expose my problem! I'm rotate the cube recalculating the individual coordinates of each vertex at each rotation and I must say I'm getting excellent results. If the cube rotates along a single axis everything to perfection, however, rotates along two or three axes vertices ranging widening causing the cube after a bit of time has stratospheric size. Below is the code for the rotation, behind which I think is hiding problem. The multiplication of the components of the individual vertices for the respective row of the rotation matrix make the vertex detached from its trajectory, but I do not understand why.

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

    }

Thanks to all!!

1
Why would you even rotate like that? Why not just use the glRotate functions?vallentin
because i need to manipulate rotation matrix directly. I've already done the version with glRotate, now i need this version..ExEcUtIvE
@ExEcUtIvE: You can load your own transform matrices using glMultMatrix or glLoadMatrix. Don't perform transformations on the CPU. Harness the computing power of the GPU, which easily outperforms the CPU doing this. Also have a look at shaders.datenwolf

1 Answers

0
votes

I've find the solution, i've not normalized the vector (rX,rY,rZ). After normalization all work perfect!

@datenwolf: thanks for reply, i've already done the same operation on GPU, but i want execute it on CPU for an other question!