Sign in to follow this  
itay390

opengl -problem at rotating invidual object

Recommended Posts

i have a problem, when i try to rotate a single object out of many, only one rotates, but along with the rotation it should move. as it moves - it also changing his direction of movement. like as the pivot was located in its start point - 0,0 so the pivot is not located locally in the center of the quad, like i want it to be, but somewhere else. here is my main loop : while (1) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glPushMatrix(); glTranslatef(camerax,cameray,cameraz); graphics.handlecommands(); graphics.idle(); glPopMatrix(); SDL_GL_SwapBuffers( ); SDL_Delay(10); } graphics.handlecommands(); is a function that moves objects , following a line, using 2d vector calculations - it just changing their x1,x2,y1,y2 quad coordinates. graphics.idle() is the function that draws all the quads. graphicsclass::idle() { int counter = 0; while (counter != quad.size()) { glBindTexture(GL_TEXTURE_2D, quad[counter].texture); glRotatef(quad[counter].thisangle, 0, 0, 1); glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex3f(quad[counter].x1, quad[counter].y2, quad[counter].z); glTexCoord2f(1,0); glVertex3f(quad[counter].x2, quad[counter].y2, quad[counter].z); glTexCoord2f(1,1); glVertex3f( quad[counter].x2, quad[counter].y1, quad[counter].z); glTexCoord2f(0,1); glVertex3f(quad[counter].x1, quad[counter].y1, quad[counter].z); glEnd(); ++counter; } } glrotatef is supposing to rotate the object if needed to. but .. you know my problem. so how can i fix it?

Share this post


Link to post
Share on other sites
I don't know if I entirely understand what you mean...Do you want things to orbit around a certain point? If so, just translate to the point that the object is orbiting about, rotate about the vector that is normal to the plane of orbit, translate to the radial distance of the orbit, and draw your object.

Share this post


Link to post
Share on other sites
Quote:
Original post by CrimsonSun
I don't know if I entirely understand what you mean...Do you want things to orbit around a certain point? If so, just translate to the point that the object is orbiting about, rotate about the vector that is normal to the plane of orbit, translate to the radial distance of the orbit, and draw your object.


my object is rotating around the point (0,0) and i want it to rotate around itself. the center of my quad is ((quad[counter].x1+quad[counter].x2)/2),((quad[counter].y1+quad[counter].y2)/2), quad[counter].z
translating before rotating to this point is a disaster because it screws up even more. so your method didn't work

Share this post


Link to post
Share on other sites
I think that your problem might be two-fold.

First, try translating first to the negation of the centre of the current quad, then rotating, and then translating back to the position of the quad.

Second, encase the section within the loop "while (counter != quad.size())" in a pushMatrix()/popPatrix() pair to isolate the changes that you make to one quad from other geometry - such as the next quad. Currently I would expect your rotations to be concatenated, so that the rotation of a given quad is its own rotation plus the rotation of every quad before it.

Thus I would suggest:

while (counter != quad.size())
{
glPushMatrix() //Save the current state of the transformation matrix.
glBindTexture(GL_TEXTURE_2D, quad[counter].texture);
//Move the quad back to its position after the rotation has been performed.
// I have used the coordinates that you gave for the centre of your quads.
glTranslatef(((quad[counter].x1+quad[counter].x2)/2),
((quad[counter].y1+quad[counter].y2)/2),
quad[counter].z);
glRotatef(quad[counter].thisangle, 0, 0, 1);
//Move the quad to the origin.
// Note the negation of each element.
glTranslatef(-(quad[counter].x1+quad[counter].x2)/2),
-((quad[counter].y1+quad[counter].y2)/2),
-quad[counter].z);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f(quad[counter].x1, quad[counter].y2, quad[counter].z);
glTexCoord2f(1,0);
glVertex3f(quad[counter].x2, quad[counter].y2, quad[counter].z);
glTexCoord2f(1,1);
glVertex3f( quad[counter].x2, quad[counter].y1, quad[counter].z);
glTexCoord2f(0,1);
glVertex3f(quad[counter].x1, quad[counter].y1, quad[counter].z);
glEnd();
glPopMatrix(); //Load the saved transformation matrix so that the changes that
// we made for this quad don't affect anything else.
++counter;
}

Share this post


Link to post
Share on other sites
So as I understand, your quads are rotating around the world origin and you want them to revolve around their individual z axes?

If this is the case, then let me revise... If you're displaying multiple objects, you need to preserve your current world state while also altering all of your quads individually. You can push and pop matrices onto the stack to accomplish this...


while(counter != quad.size() ){

glBindTexture(GL_TEXTURE_2D, quad[counter].texture);

glPushMatrix();

glTranslatef( (quad[counter].x1 + quad[counter.x2) / 2.0,
(quad[counter].y1 + quad[counter.y2) / 2.0,
0);
glRotatef(quad[counter].thisangle, 0, 0, 1);

glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f(quad[counter].x1, quad[counter].y2, quad[counter].z);
glTexCoord2f(1,0);
glVertex3f(quad[counter].x2, quad[counter].y2, quad[counter].z);
glTexCoord2f(1,1);
glVertex3f( quad[counter].x2, quad[counter].y1, quad[counter].z);
glTexCoord2f(0,1);
glVertex3f(quad[counter].x1, quad[counter].y1, quad[counter].z);
glEnd();

glPopMatrix();
++counter;
}



EDIT: Let me actually explain what this does. You have a nice neat stack of matrices in your OpenGL implementation (even if you don't know it). When you glPushMatrix, you're essentially copying the current matrix into another matrix and putting this copy on top of the stack. Now any points you render are going to be multiplied by this new matrix. Any glRotates or glTranslates you apply here are going to affect the topmost stack only. When you glPopMatrix, you are taking that copy and throwing it away, leaving the original matrix on top of the stack unaltered.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this