I have a problem with writing an alternative function for glRotatef in pure C. I need to implement function which arguments are: list of points, angle and rotate vector. Function must return list of points after rotation. My function looks like this:
void rotate(float * current, float a, float x, float y, float z)
{
float sina = sinf(a);
float cosa = cosf(a);
float rotateMatrix[9] = //creating rotate matrix
{
x*x*(1-cosa) + cosa, x*y*(1-cosa) - z*sina, x*z*(1-cosa) + y*sina,
y*x*(1-cosa) + z*sina, y*y*(1-cosa) + cosa, y*z*(1-cosa) - x*sina,
z*x*(1-cosa) - y*sina, z*y*(1-cosa) + x*sina, z*z*(1-cosa) + cosa
};
float *resultVertexList = current; //temporary help
int i;
for(i=0;current[i] != 0;i++) //multiplying CURRENT_MATRIX * ROTATE_MATRIX
{
int currentVertex = (i/3) * 3;
int rotateColumn = i%3;
resultVertexList[i] =
current[currentVertex] * rotateMatrix[rotateColumn] +
current[currentVertex+1] * rotateMatrix[rotateColumn+3] +
current[currentVertex+2] * rotateMatrix[rotateColumn+6];
}
current = resultVertexList;
}
I'm calling it like here: rotate(current, M_PI/10, 0, 1, 0);
After that, I take current
list of points and simply draw them with openGL. For testing I tried to rotate list of points representing cube. It rotates, but with every one call of rotate
function it shrinks. I have no idea why. See some screenshots:
After many callings of rotate
function it shrinks to one point.
What am I doing wrong?