Nope, putting the rotate after all those drawing caused nothing to rotate at all
Do you know someone really good at this? plz plz help :''(
Rotate with respect to a distant point
Fier i think your last example was abit closer. The order is in reverse when you are doing matrix stuff. Like if you want something to rotate about an axis but its position is xyz then the order is translate rotate draw. but if you want it to move to a position, then rotate, its rotate translate draw then pop your matrix. So just re order your last code posting and in theory it should work, sorry not at my workstation rightnow to test it. now for how you translate, that should be rotation point + the distance you are rotating around.
If for example you are doing a planet and star: star is at 1,0,0; distance to rotate around is 10; call would be glTranslate(star.x + 10, 0, 0); now call your draw function; When you do all these matrix opperations you are changing the world coordinates briefly. But as far as your model is concerned, or the planet in our example, it is being drawn the same as if you just called your draw function by itself.
Now the way all this would work as a rotating object, since your actual position of the planet is not moving, is to rotate in your loop. You would have to change the angle for each iteration. so on 1st pass angle is 5 2nd pass angle is 10 etc. I hope this helps, if not, I''m kiinda stumped.
If for example you are doing a planet and star: star is at 1,0,0; distance to rotate around is 10; call would be glTranslate(star.x + 10, 0, 0); now call your draw function; When you do all these matrix opperations you are changing the world coordinates briefly. But as far as your model is concerned, or the planet in our example, it is being drawn the same as if you just called your draw function by itself.
Now the way all this would work as a rotating object, since your actual position of the planet is not moving, is to rotate in your loop. You would have to change the angle for each iteration. so on 1st pass angle is 5 2nd pass angle is 10 etc. I hope this helps, if not, I''m kiinda stumped.
You have to translate everything such that the point to rotate about goes to the origin. If your point is at (1,1,1), do a Translatef(-1,-1,-1), rotate, and Translate(1,1,1) again.
--nico
--nico
What you are doing is rotating a location vector about the world origin. To simulate what you are currently doing try this: (image that your fist is a sphere or whatever object you want) now stand up, make a fist and extend your arm. Now spin yourself around and around in a circle with your arm extended while at the same time retracting and extending your fist to and from your chest (bend you elbow). (Dumb example~)
WHAT YOU ARE DOING:
Given a world origin <0,0,0> rotate an object X around the Z axis. Object X is at location L <10, 10, 10>.
1. Translate Object X to the Origin.
2. Rotate The Object X Location Vector using a Z axis rotation matrix.
3. Translate Object X back to the Origin.
EXPLANATION:
The key here is that you are currently rotating the objects Location Vector and not the actual vertices or points that make up the object. ****What you want to do is rotate the actual point or vertices that make up the object**** NOT the point that represents the objects location.
WHAT YOU NEED TO DO:
1. Translate all vertices of Object X to the origin.
2. Rotate all vertices of Object X accordingly.
3. Translate all vertices of Object X back their Location.
See what I''m saying?
WHAT YOU ARE DOING:
Given a world origin <0,0,0> rotate an object X around the Z axis. Object X is at location L <10, 10, 10>.
1. Translate Object X to the Origin.
2. Rotate The Object X Location Vector using a Z axis rotation matrix.
3. Translate Object X back to the Origin.
EXPLANATION:
The key here is that you are currently rotating the objects Location Vector and not the actual vertices or points that make up the object. ****What you want to do is rotate the actual point or vertices that make up the object**** NOT the point that represents the objects location.
WHAT YOU NEED TO DO:
1. Translate all vertices of Object X to the origin.
2. Rotate all vertices of Object X accordingly.
3. Translate all vertices of Object X back their Location.
See what I''m saying?
The code below is a replacement DrawGLScene function for Nehe''s Lesson 2. It does exactly what we''re describing.
int DrawGLScene (){ static float s_fRot; // current rotation; // clear buffers glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // reset modelview matrix glLoadIdentity (); // SETUP CAMERA POSITION AND ORIENTATION // move the camera back 30 units so we can see everything glTranslatef (0.0f, 0.0f, -30.0f); // tilt the camera so that we''re not looking straight down on the scene glRotatef (-60.0f, 1.0f, 0.0f, 0.0f); // START DRAWING THE SCENE // this push-pop block represents the scene''s position, // the centre object being at 1,1,1 -- just because :) glPushMatrix (); glTranslatef (1.0f, 1.0f, 1.0f); // draw the centre object glBegin (GL_TRIANGLES); glVertex3f ( 0.0f, 1.0f, 0.0f); glVertex3f (-1.0f, -1.0f, 0.0f); glVertex3f ( 1.0f, -1.0f, 0.0f); glEnd (); // DRAW THE ORBITTING OBJECT // This push-pop block represents the orbiting object''s matrix // relative to the centre object. The orbiting object is 10 // units away from the centre. glPushMatrix (); // rotate the matrix glRotatef (s_fRot, 0.0f, 0.0f, 1.0f); // The orbiting object has an orbit of 10 units, so // translate 10 points along the x-axis. glTranslatef (10.0f, 0.0f, 0.0f); // draw another triangle -- this is the orbiting object glBegin (GL_TRIANGLES); glVertex3f ( 0.0f, 1.0f, 0.0f); glVertex3f (-1.0f, -1.0f, 0.0f); glVertex3f ( 1.0f, -1.0f, 0.0f); glEnd (); glPopMatrix (); glPopMatrix (); // increment the rotation s_fRot += 0.01f; // make sure the rotation never goes beyond 360° if (s_fRot >= 360.0f) s_fRot -= 360.0f; return TRUE;}
quote:Original post by _the_phantom_
which is what i said in the 2nd post in this thread...
Indeed, but Fier still seems to be having some difficulty. As such, it''s probably best to demonstrate with working code that does exactly what we''re describing.
First of all, I sincerely appreciate everyone''s help. Especially Ostsol, for showing me the acutal code. I understand what Ostsol and _the_phantom_ saying. I also believe you guys are doing the right way. Nevertheless, I need to figure out how to change the way object is rotating around a point in DIFFERENT DIRECTION, instead of just axis.
In the code Ostel post.
**************
// rotate the matrix
glRotatef (s_fRot, 0.0f, 0.0f, 1.0f); <----how to manipulate this one
// The orbiting object has an orbit of 10 units, so
// translate 10 points along the x-axis.
glTranslatef (10.0f, 0.0f, 0.0f);
******************************
This rotation would only made the object(say sphere) rotate around the centre point in centre point''s Z axis. That''s not enough, the requirement wants me to rotate around in point in any direction, not just distant point''s axis. Say change above code''s Translatef(10,0,0) to Translate(10,10,0), we''ll see the object is sphere is rotating around (1,10,1) on y=10 plane instead of the acutal centre point (1,1,1). This is not gonan work. Try those code and you''ll see what I am saying.
I think the real hard part is how to use that glRotate() function to define the direction the object can rotate. I don''t know how, I tried combine different axis, it doesn''t work out right. Really stuck now.
Nico, I have no idea what should I do that, please elaborated.
Anonymous Poster, my object sphere is define by the midpoint, which is the point that define the position. So what you saying has no effect on what I am doing. Thanks for help though.
In the code Ostel post.
**************
// rotate the matrix
glRotatef (s_fRot, 0.0f, 0.0f, 1.0f); <----how to manipulate this one
// The orbiting object has an orbit of 10 units, so
// translate 10 points along the x-axis.
glTranslatef (10.0f, 0.0f, 0.0f);
******************************
This rotation would only made the object(say sphere) rotate around the centre point in centre point''s Z axis. That''s not enough, the requirement wants me to rotate around in point in any direction, not just distant point''s axis. Say change above code''s Translatef(10,0,0) to Translate(10,10,0), we''ll see the object is sphere is rotating around (1,10,1) on y=10 plane instead of the acutal centre point (1,1,1). This is not gonan work. Try those code and you''ll see what I am saying.
I think the real hard part is how to use that glRotate() function to define the direction the object can rotate. I don''t know how, I tried combine different axis, it doesn''t work out right. Really stuck now.
Nico, I have no idea what should I do that, please elaborated.
Anonymous Poster, my object sphere is define by the midpoint, which is the point that define the position. So what you saying has no effect on what I am doing. Thanks for help though.
glRotatef takes four numbers, an angle, and then a normalized vector to rotate about.
So change it to glRotatef(angle, arbitraryVectorX, arbitraryVectorY, arbitraryVectorZ).
Remember to normalize it, though... so divide each component (x, y and z) of the vector by the length of the vector (square root(x^2+y^2+z^2) ).
So change it to glRotatef(angle, arbitraryVectorX, arbitraryVectorY, arbitraryVectorZ).
Remember to normalize it, though... so divide each component (x, y and z) of the vector by the length of the vector (square root(x^2+y^2+z^2) ).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement