简体   繁体   中英

OpenTK - How to rotate a 2D object

I'm trying to rotate 2D objects in my OpenTK project.

What's happening is that the objects after the rotated object are rotating and I don't want them to do that.

How do I reset the rotation after I've already rotated one object?

      protected override void OnRenderFrame(FrameEventArgs e)
       {
           GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
           GL.MatrixMode(MatrixMode.Projection);
           GL.LoadIdentity();
           GL.Ortho(0, game.Width, game.Height, 0, -1, 4);

           GL.MatrixMode(MatrixMode.Modelview);
           GL.LoadIdentity();

          drawObject();
          GL.Rotate(180, Vector3d.UnitZ);
          drawObject(); // Object I want to rotate
          GL.Rotate(-180, Vector3d.UnitZ);
          drawObject(); // I don't want this object to be rotated, but it does. How do I fix this?
}

Thanks!

In the fixed function pipeline, Rotate operations are not appended. Instead the second one is replacing the first one (as long as there is no PushMatrix operation inbetween). So what you are currently drawing is the first object rotated around 180 degree and the second object around -180 degree.

Back to your question: If you replace the second call to Rotate with LoadIdentity, the current modelmatrix is resetted to an identity matrix, removing all transformations.

drawObject();
GL.Rotate(180, Vector3d.UnitZ);
drawObject();
GL.LoadIdentity();
drawObject();

Note: All transformations are resetted here, if you want to keep translation and scaling you can try GL.Rotate(0, Vector3d.UnitZ);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM