Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Rotating around pivot point.

  1. #1
    Newbie Newbie
    Join Date
    Mar 2018
    Posts
    2

    Rotating around pivot point.

    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.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,003
    Quote Originally Posted by JonekSharp View Post
    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>:
    Code :
    GL.Translate(x,y,z);
    GL.Rotate(...);
    GL.Translate(-x,-y,-z);

    Quote Originally Posted by JonekSharp View Post
    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).

  3. #3
    Newbie Newbie
    Join Date
    Mar 2018
    Posts
    2
    Quote Originally Posted by GClements View Post
    To rotate about the point <x,y,z>:
    Code :
    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.
    Code :
                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);
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	lwXToLOmOFOHctrTqvuULKoXBfOqJL.jpg 
Views:	36 
Size:	5.4 KB 
ID:	2680  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •