Please help me with this function that involves rotation

Started by
5 comments, last by guvidu 17 years, 8 months ago
So - i have like 20 cubes in my scene - and i need to make a function that will only rotate the cubes with X == 1 around a poin. How can i make that? The cubes are created with this function: void COpenGL::DrawCubes(float CenterX, float CenterY, float CenterZ, float dimension) { glPushMatrix(); glBindTexture(GL_TEXTURE_2D, texture[0]); glBegin(GL_QUADS); /***************************FRONT FACE****************************************/ glTexCoord2f( 1.0f, 1.0f); glVertex3f(CenterX + dimension/2, CenterY + dimension/2, CenterZ + dimension/2); glTexCoord2f( 0.0f, 1.0f); glVertex3f(CenterX - dimension/2, CenterY + dimension/2, CenterZ + dimension/2); glTexCoord2f( 0.0f, 0.0f); glVertex3f(CenterX - dimension/2, CenterY - dimension/2, CenterZ + dimension/2); glTexCoord2f( 1.0f, 0.0f); glVertex3f(CenterX + dimension/2 , CenterY - dimension/2, CenterZ + dimension/2); glEnd(); /****************************RIGHT FACE****************************************/ glBindTexture(GL_TEXTURE_2D, texture[1]); glBegin(GL_QUADS); glTexCoord2f( 1.0f, 1.0f); glVertex3f(CenterX + dimension/2, CenterY + dimension/2, CenterZ - dimension/2); glTexCoord2f( 0.0f, 1.0f); glVertex3f(CenterX + dimension/2, CenterY + dimension/2, CenterZ + dimension/2); glTexCoord2f( 0.0f, 0.0f); glVertex3f(CenterX + dimension/2, CenterY - dimension/2, CenterZ + dimension/2); glTexCoord2f( 1.0f, 0.0f); glVertex3f(CenterX + dimension/2, CenterY - dimension/2, CenterZ - dimension/2); glEnd(); /****************************BACK FACE****************************************/ glBindTexture(GL_TEXTURE_2D, texture[2]); glBegin(GL_QUADS); glTexCoord2f( 1.0f, 1.0f); glVertex3f(CenterX + dimension/2, CenterY + dimension/2, CenterZ - dimension/2); glTexCoord2f( 0.0f, 1.0f); glVertex3f(CenterX - dimension/2, CenterY + dimension/2, CenterZ - dimension/2); glTexCoord2f( 0.0f, 0.0f); glVertex3f(CenterX - dimension/2, CenterY - dimension/2, CenterZ - dimension/2); glTexCoord2f( 1.0f, 0.0f); glVertex3f(CenterX + dimension/2 , CenterY - dimension/2, CenterZ - dimension/2); glEnd(); /****************************LEFT FACE****************************************/ glBindTexture(GL_TEXTURE_2D, texture[3]); glBegin(GL_QUADS); glTexCoord2f( 1.0f, 1.0f); glVertex3f(CenterX - dimension/2, CenterY + dimension/2, CenterZ - dimension/2); glTexCoord2f( 0.0f, 1.0f); glVertex3f(CenterX - dimension/2, CenterY + dimension/2, CenterZ + dimension/2); glTexCoord2f( 0.0f, 0.0f); glVertex3f(CenterX - dimension/2, CenterY - dimension/2, CenterZ + dimension/2); glTexCoord2f( 1.0f, 0.0f); glVertex3f(CenterX - dimension/2, CenterY - dimension/2, CenterZ - dimension/2); glEnd(); /****************************TOP FACE****************************************/ glBindTexture(GL_TEXTURE_2D, texture[4]); glBegin(GL_QUADS); glTexCoord2f( 1.0f, 1.0f); glVertex3f(CenterX + dimension/2, CenterY + dimension/2, CenterZ - dimension/2); glTexCoord2f( 0.0f, 1.0f); glVertex3f(CenterX - dimension/2, CenterY + dimension/2, CenterZ - dimension/2); glTexCoord2f( 0.0f, 0.0f); glVertex3f(CenterX - dimension/2, CenterY + dimension/2, CenterZ + dimension/2); glTexCoord2f( 1.0f, 0.0f); glVertex3f(CenterX + dimension/2, CenterY + dimension/2, CenterZ + dimension/2); glEnd(); /****************************TOP FACE****************************************/ glBindTexture(GL_TEXTURE_2D, texture[5]); glBegin(GL_QUADS); glTexCoord2f( 1.0f, 1.0f); glVertex3f(CenterX + dimension/2, CenterY - dimension/2, CenterZ - dimension/2); glTexCoord2f( 0.0f, 1.0f); glVertex3f(CenterX - dimension/2, CenterY - dimension/2, CenterZ - dimension/2); glTexCoord2f( 0.0f, 0.0f); glVertex3f(CenterX - dimension/2, CenterY - dimension/2, CenterZ + dimension/2); glTexCoord2f( 1.0f, 0.0f); glVertex3f(CenterX + dimension/2, CenterY - dimension/2, CenterZ + dimension/2); glEnd(); glPopMatrix(); } Hope to hear from you guys soon - Thanks!
Advertisement
Push an adequate transformation matrix to apply the rotation to all your objects. Check your favourite OpenGL reference for more details about transformation in OpenGL and the corresponding API calls.
Thanks for answering me. But can you be more specific. I know how to make a rotation - i just couldnt find anywhere (reference, tutorial or something) on how to rotate only the object with specific coordinates and how after rotation to save those coordinates.
The method is to:
1° Push the adequate matrix
2° Display the cube which you want to rotate.
3° Pop the matrix

This way, the matrix, as long as it is pushed, will modify the coordinates of any rendered vertices.

If you want to save the transform for later use, you can create a display list that does the above, and OpenGL would then precompute the new coordinates once and for all instead of transforming them every frame.
If i understood corectly you are telling me to do something like this

glPushMatrix();
glRotatef(...);
DrawCube(...);
glPopMatrix();

and anyway - i need to rotate multiple cubes - and the cubes arent always the same

i only know about them that they have the same X coordinate. Y and Z are diferent.

i cant to somenthing like this if(x==1)glRotatef(...); becouse in this case i have to put DrawCube() befoure if() statemant - and then the rotation it wont be done.

Thanks for your help! If you have any other ideea please - im desperate.
float x = GetTheValue();if (fabs(x-1)<0.01) glRotatef(...);DrawCube(x,...);


Replace GetTheValue by whatever means you have of obtaining the x-coordinate of your cube before calling DrawCube.
Thanks for your help - i try that function - doesnt realy help me.


To explain it better please look below

------
/ (cube1) (point around i want to rotate) (cube1 after the rotation)
(cube2) (cube2 after the rotation)
(cube3) (cube3 after the rotation)

ive make the rotation now i want to save the coordinates couse i want to do
something like this nex

(cube2) (point) (cube2 after the rotation)
(cube4) (cube4 after the rotation)
(cube5) (cube5...................)

Thanks for the help!

This topic is closed to new replies.

Advertisement