3

I am trying to rotate an object about one of its side and already tried the common approach as found on the forums:

translate(-P); rotate(); translate(P); 

In OpenGL (reversing the order of translations/rotations), I used the following code:

glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glPushMatrix(); glTranslatef(-50, 50, 0); glRotatef(rotationCoord, 0, 1, 0); glTranslatef(50, -50, 0); glBegin(GL_QUADS); glVertex3f(-50.0, 50.0, 0); glVertex3f(50.0, 50.0, 0); glVertex3f(50.0, -50.0, 0); glVertex3f(-50.0, -50.0, 0); glEnd(); glPopMatrix(); 

However, the rectangle that I am drawing doesn't seem to be rotating around one side as pivot. (trying to set the left side as the pivot point and rotate around it). I made a screen capture vid to show what kind of rotation I am getting right now. Here's the video:

http://youtu.be/VgEZ_rsG3xU

How do I set the pivot for this object so that it rotated around that point?

9
  • The choice of pivot is odd. The y-axis translation component has no effect, since you're rotating in the xz-plane. Is this what you intended? Commented Jul 13, 2013 at 13:03
  • @MarceloCantos : I didn't intend to rotate the around the Y-axis for now. Just X-axis. But the left hadn side of the rectangle seems to be moving whereas the pivot should be stationary. right? Commented Jul 13, 2013 at 13:06
  • glRotatef(…, 0, 1, 0) is a rotation around the y-axis (aka xz-plane), not the x-axis. Commented Jul 13, 2013 at 13:08
  • @MarceloCantos : Oops. I had to mention in the above comment that rotaiton has to be only around Y-axis for now, not X-axis. Apologies for the confusion. That said, with rotatio around Y-axis as well as it is happening now, the pivot should be stationary about which I am not sure why. Commented Jul 13, 2013 at 13:11
  • Try reversing the order: T(P) R() T(-P). This is clutching at straws, but it would be good to at least eliminate the possibility. Commented Jul 13, 2013 at 13:30

1 Answer 1

3

The problem comes from the

glTranslatef(0, 0, -10.0); 

you're calling after the rotation. That means the (0,0,-10) is applied to the object coordinates (+-50,+-50,0) before they are transformed by anything else, so the quad is offset before it's being rotated.

Here's the code I used for testing, maybe you get something out of it:

#include <glm/glm.hpp> #include <glm/ext.hpp> using namespace glm; void display(void) { glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GLfloat aspect = (float)g_Width / g_Height; mat4 eyeToCamera = perspective(45.f, aspect, g_nearPlane, g_farPlane); // projection mat4 cameraToWorld = lookAt(vec3(0, 0, -150), vec3(), vec3(0, 1, 0)); // view vec3 pivot(50, -50, 0); mat4 worldToObject = translate(pivot) * rotate(rotationAngle, vec3(0, 1, 0)) * translate(-pivot); // model glLoadMatrixf(value_ptr(eyeToCamera * cameraToWorld * worldToObject)); glBegin(GL_QUADS); glVertex3f(-50.0, 50.0, 0); glVertex3f(50.0, 50.0, 0); glVertex3f(50.0, -50.0, 0); glVertex3f(-50.0, -50.0, 0); glEnd(); glutSwapBuffers(); } 

Note that I don't use any matrix stack functions, and I only set the GL_MODELVIEW matrix.

Sign up to request clarification or add additional context in comments.

4 Comments

I needed to translate the translate the back by -10 first because I have the projection matrix applied as above (which is needed for one of my subsequent operations that is to come later). I tried reducing the z value of the rectangle to change how far the rectangle is drawn and to see if that is creating a problem with the rotation. But even if the rectangle is at -0.1, I still don't get a fixed pivot. Because of OpenGL's post-multiplication, that translate by -10 has to be there after rotation so as to make the rectangle fully visible inside the perspective projection.
I am not sure why that translate by -10 is causing or how should I go around for fixed pivot with a rectangle still being shown in my perspective projection.
@user1240679 Put the glTranslatef(0, 0, -10.0) call before the rotation, i.e. directly after calling glPushMatrix above. That way the translation is applied after the object is rotated.
Superb! Thanks. A lil off-topic doubt to be cleared here. I have the vector positions of a rectangle (which is rotated in the real world - two Z vertices at Z=0, other two at Z=200). I want to get the rotation matrix from these vertices and apply it as I did above in my post (but instead of glRotatef will be using glMultMatrix). The question is here btw: stackoverflow.com/questions/17585424/… If you could just give a read to comments discussion below that would give the context. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.