# How convert object coordinate to world coordinate space for rotation

Dear Friends

I am trying to rotate my object with respect to x and y axes, in world space, with a single command as

`GL.Rotate(rotationAngle, rotationAxis.X, rotationAxis.Y, rotationAxis.Z);`

where rotationAngle is a double and rotationAxis is a Vector in OpenTK.

How can I achieve it ? How can I convert my object space to world space ?

I am trying like the code snippet below.

``````////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Vector vector = new Vector();
Vector va = vector.GetMouseVector(m_old_X, m_old_Y, Width, Height);
Vector vb = vector.GetMouseVector(e.X, e.Y, Width, Height);
rotationAngle = Math.Acos(Math.Min(1.0f, vector.DotProduct(va, vb)));
rotationAxis = vector.CrossProduct(va, vb);

int[] viewport = new int[4];
Matrix4d modelViewMatrix, projectionMatrix;
GL.GetInteger(GetPName.Viewport, viewport);
GL.GetDouble(GetPName.ModelviewMatrix, out modelViewMatrix);
GL.GetDouble(GetPName.ProjectionMatrix, out projectionMatrix);

modelViewMatrix.Invert();

Vector4d newvec;
newvec.X = rotationAxis.X;
newvec.Y = rotationAxis.Y;
newvec.Z = rotationAxis.Z;
newvec.W = 1;

Vector4d axisNew = new Vector4d();
axisNew.X = modelViewMatrix.M11 * newvec.X + modelViewMatrix.M12 * newvec.Y + modelViewMatrix.M13 * newvec.Z + modelViewMatrix.M14 * newvec.W;
axisNew.Y = modelViewMatrix.M21 * newvec.X + modelViewMatrix.M22 * newvec.Y + modelViewMatrix.M23 * newvec.Z + modelViewMatrix.M24 * newvec.W;
axisNew.Z = modelViewMatrix.M31 * newvec.X + modelViewMatrix.M32 * newvec.Y + modelViewMatrix.M33 * newvec.Z + modelViewMatrix.M34 * newvec.W;
axisNew.W = modelViewMatrix.M41 * newvec.X + modelViewMatrix.M42 * newvec.Y + modelViewMatrix.M43 * newvec.Z + modelViewMatrix.M44 * newvec.W;

rotationAxis.X = axisNew.X / axisNew.W;
rotationAxis.Y = axisNew.Y / axisNew.W;
rotationAxis.Z = axisNew.Z / axisNew.W;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
``````

Any help will be highly appreciated. Thanks in advance, I am using c# & OpenTK.

The model-view matrix transforms from object space to eye space. You wouldn’t invert it unless you want to convert to object space. Also: OpenGL itself doesn’t have any concept of world space. So I’m wondering if what you’re actually asking is how to convert eye space to object space.

If you’re transforming a direction, the W component should be zero so that any translation component isn’t added to it. Or you can just ignore the right-most column of the matrix (the M?4 entries, which are multiplied by W).

If you want to rotate about a specific point (other than the origin), you’ll need to extract the centre position and perform a `glTranslate`/`glRotate`/`glTranslate` sequence. E.g.

``````glTranslatef(center.x, center.y, center.z);
glRotatef(angle, axis.x, axis.y, axis.z);
glTranslatef(-center.x, -center.y, -center.z);
``````

will rotate about the line parallel to `axis` and which passes through `center`.

The rotation centre should be transformed in a similar manner to the axis, except its W component should be equal to 1 (as it’s a position rather than a direction). If you want to transform the origin (0,0,0,1), you can just use the right-most column of the matrix (the first three columns will be multiplied by zero, the fourth by one).

You can ignore the W coordinate. The model-view matrix should never contain any projective transformation (the fourth row will be [0,0,0,1]) as that breaks the lighting calculations. This is why the projection matrix is a separate matrix.

Aside from the fact that W should be zero (assuming that the original vector has W=0), `glRotate` normalises the axis, so the scale doesn’t matter.

Thanks for your reply. In the above code then how can I compute the centre of my object and Axis, I am computing my angle and axis like below.

``````    Vector vector = new Vector();
Vector va = vector.GetMouseVector(m_old_X, m_old_Y, Width, Height);
Vector vb = vector.GetMouseVector(e.X, e.Y, Width, Height);
rotationAngle = Math.Acos(Math.Min(1.0f, vector.DotProduct(va, vb)));
rotationAxis = vector.CrossProduct(va, vb);
``````

And now if I try to rotate this rotationAngle with respect to the rotationAxis, it is not working, I need to implement an arcball rotation, wit single GL.Rotate command. My cube domain is
X : -length to length
Y : -height to height
Z : -width to width
So my center is originally 0

But there is Pan associated with it, so my center is changing, how can I retain my object position and implement an arcball rotation. Please help me with some code snippet.

Thanks a lot.

Is this what you’re looking for?

Exactly I am looking for this. but I sought a code snippet of the implementation for opentk c#, or at least in opengl c++.? Whichever is possible.

It is so thankful even for this. But it will be good if I can get a working code snippet.

Thanks a lot.