*I say rotating an object on its own axis but I think my term is wrong...what I mean by that is like rotating my object from the objects center and not at the origin
I'm trying to make a program that rotates my object(made by using a matrix of points/vertices) in its *own axis without the use of glRotate or glTranslate What I did was to make a MatrixMultiplication function and a 3 separate functions that rotate the object in the X,Y and Z axes and another function that translates them using matrices and the matrix multiplication function:
void MMMultiplication(float fVector[], float fMatrix[])
{
float tmpFloat;
for(int a = 0; a <= 72; a+=4)//this is 72 because the elements of my object's matrix
is also 72
{
for(int indx2= 0; indx2 < 4; indx2++)
{
tmpFloat = 0.0f;
for(int indx = 0; indx < 4; indx++)
{
tmpFloat = tmpFloat +
(fVector[indx+a] * fMatrix[(indx*4) + indx2]);
}
fVector[indx2+a] = tmpFloat;
}
}
}
and another 3 functions that represents the x y and z rotation matrices as well as a translation matrix.
The program would ask for x,y,z coordinates to translate the object there then you can use the z,x,c keyboard buttons to rotate it in the x,y,z axes respectively and it would rotate the object on its *own axis
What I do is a call a function that draws the object in RenderScene and I use glutKeyboard function to rotate my object in the x,y,z axes...
So far my program would only rotate my object properly in the origin. If I try to translate the oject elsewhere, it ends up "adding" to the translation every time I press z,x, or c... I understand that I need to translate the object to the origin first then translate it back but as I said it ends up translating farther and farther
Anyways here are the codes I used to make it work so far...
void main(int argc, char **argv)
{
cout<<"Input:(x,y,z)";
cin>>arbitrary[0];
cin>>arbitrary[1];
cin>>arbitrary[2];
arbitrary[3] = 1.0f;
TranslateMatrix(initalpos,object,arbitrary[0],arbitrary[1],arbitrary[2]);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(1024,768);
glutCreateWindow("Rotation");
glutReshapeFunc(changeSize);
Lighting(false);
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(inputKey);
glutDisplayFunc(renderScene);
glutTimerFunc(0,Timer,0);
// Enable Depth Testing
glEnable(GL_DEPTH_TEST);
glutMainLoop();
getch();
return;
_getch();
return;
}
void renderScene()
{
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
DrawObject(object);
glPopMatrix();
glutSwapBuffers();
}
void rotatex(float xangle)
{
glPushMatrix();
TranslateVector(object,initalpos,arbitrary[0],arbitrary[1],arbitrary[2]);
RotateX(object, xangle);//this is my x-axis rotation
TranslateMatrix(object,initalpos,arbitrary[0], arbitrary[1], arbitrary[2]);
glPopMatrix();
return;
}
void rotatey(float yangle)
{
glPushMatrix();
TranslateVector(object,initalpos,arbitrary[0],arbitrary[1],arbitrary[2]);
RotateY(object, yangle);//this is my y-axis rotation matrix
TranslateMatrix(object,initalpos,arbitrary[0] ,arbitrary[1],arbitrary[2]);
glPopMatrix();
return;
}
void rotatez(float zangle)
{
glPushMatrix();
TranslateVector(object,initalpos,arbitrary[0],arbitrary[1],arbitrary[2]);
RotateZ(object, zangle);//this is my z-axis rotation matrix matrix
TranslateMatrix(object,initalpos,arbitrary[0],arbitrary[1],arbitrary[2]);
glPopMatrix();
return;
}
void TranslateMatrix
(float fSVector[],float fEVector[], float fX, float fY, float fZ)
{
float fTranslationMatrix[16];
for (int indx = 0; indx < 16; indx++)
{
fTranslationMatrix[indx] = 0;
}
fTranslationMatrix[0] = 1;
fTranslationMatrix[5] = 1;
fTranslationMatrix[10] = 1;
fTranslationMatrix[15] = 1;
fTranslationMatrix[12] = fX;
fTranslationMatrix[13] = fY;
fTranslationMatrix[14] = fZ;
MMMultiplication(fSVector, fTranslationMatrix);
MMMultiplication(fEVector, fTranslationMatrix);
}
void processNormalKeys(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
case 'X':
case 'x':
{
rotatex(0.5f);break;
}
case 'c':
case 'C':
{
rotatey(0.5f);break;
}
case 'Z':
case 'z':
{
rotatez(0.5f);break;
}
}
}
Any ideas?