Jump to content
  • Advertisement
Sign in to follow this  
guvidu

OpenGL Please help me with this function that involves rotation

This topic is 4385 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!