PDA

View Full Version : Rotating around pivot point.



JonekSharp
03-04-2018, 07:42 AM
Good evening.

I've succesfuly rotated a rectangle around left-top corner (0, 0) using GL.Rotate and GL.Translate, but rotating around pivot kinda eludes me.

I'm quite keen on theoretical aspects of rotating around pivot point, I know I have to:
- Rotate around default 0, 0 (left-top corner)
- Move it to right and up by the pivot point position - for example: by half the width and height if pivot point was at the center

The problem is, rectangle changes it's size after rotation (which I know can also be calculated using sin/cos) - but I've heard that there is a better solution using matrixes and Matrix.Rotate function.

Looking forward to any suggestions - if it helps, I'm using C# OpenTK.

GClements
03-04-2018, 11:42 AM
I've succesfuly rotated a rectangle around left-top corner (0, 0) using GL.Rotate and GL.Translate, but rotating around pivot kinda eludes me.

To rotate about the point <x,y,z>:


GL.Translate(x,y,z);
GL.Rotate(...);
GL.Translate(-x,-y,-z);




The problem is, rectangle changes it's size after rotation

That suggests that your projection matrix doesn't match the aspect ratio of the viewport.

The viewport transformation maps the signed unit cube (<-1,-1,-1> to <1,1,1>) to the viewport. If the viewport isn't square, that's going to introduce non-uniform scaling. Typically the projection matrix is set to counteract this. For an orthographic projection, you can fold the projection transformation into the model-view matrix. But if you do that with a perspective projection, it tends to mess up the lighting calculations (which is why a separate projection matrix is normally used).

JonekSharp
03-04-2018, 01:39 PM
To rotate about the point <x,y,z>:


GL.Translate(x,y,z);
GL.Rotate(...);
GL.Translate(-x,-y,-z);



That suggests that your projection matrix doesn't match the aspect ratio of the viewport.

The viewport transformation maps the signed unit cube (<-1,-1,-1> to <1,1,1>) to the viewport. If the viewport isn't square, that's going to introduce non-uniform scaling. Typically the projection matrix is set to counteract this. For an orthographic projection, you can fold the projection transformation into the model-view matrix. But if you do that with a perspective projection, it tends to mess up the lighting calculations (which is why a separate projection matrix is normally used).

I'm not new to programming, but I have little to no knowledge about OpenGL.
I'm not sure if viewport is the issue here, I'm quite sure that the fact I'm using ortho with non-normaliized vectors might be the issue.

Take a look (hope it's readable enough):
https://i.imgur.com/SF4zVSk.png

Oh, and probably the most important code ran inside OnLoad function.

GL.Enable(EnableCap.Texture2D);
GL.Enable(EnableCap.Blend);
GL.Enable(EnableCap.DepthTest);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.DstAlpha);

GL.Viewport(0, 0, Width, Height);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(0, Width, Height, 0, 0, 1);