From 4f3e9f50ed762ac38e14f356c93b378c4db246d9 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Fri, 15 Sep 2023 11:36:11 -0400 Subject: [PATCH] rotating a texture works, but only about the origin --- Photo.cs | 2 +- Program.cs | 43 +++++++++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/Photo.cs b/Photo.cs index 793ec5f..80e6fd2 100644 --- a/Photo.cs +++ b/Photo.cs @@ -105,7 +105,7 @@ public class Photo { public GpsInfo? Gps = null; public Rectangle CropRectangle = Rectangle.Empty; public Vector2i ViewOffset = Vector2i.Zero; - public float Rotation = 0; + public float RotationDegrees = 0; private static long touchCounter = 0; private Texture texture; diff --git a/Program.cs b/Program.cs index 24f1a67..10399ca 100644 --- a/Program.cs +++ b/Program.cs @@ -261,16 +261,16 @@ public class StraightenTool : ITool { public StraightenTool(Photo photo) { this.photo = photo; - initialRotation = photo.Rotation; + initialRotation = photo.RotationDegrees; } public ToolStatus HandleInput(KeyboardState input, MouseState mouse, Transform transform, Game game, Photo photo, UiGeometry geometry) { if (input.IsKeyPressed(Keys.Left)) { - photo.Rotation += 1; + photo.RotationDegrees += 5; } if (input.IsKeyPressed(Keys.Right)) { - photo.Rotation -= 1; + photo.RotationDegrees -= 5; } if (input.IsKeyPressed(Keys.Enter)) { @@ -278,7 +278,7 @@ public class StraightenTool : ITool { } if (input.IsKeyPressed(Keys.Escape)) { - photo.Rotation = initialRotation; + photo.RotationDegrees = initialRotation; return ToolStatus.Canceled; } @@ -286,7 +286,7 @@ public class StraightenTool : ITool { } public string Status() { - return String.Format("[straighten] {0}", photo.Rotation); + return String.Format("[straighten] {0}", photo.RotationDegrees); } } @@ -977,7 +977,7 @@ public class Game : GameWindow { renderSize.X, renderSize.Y); activeOffset = new(photoBox.Min.X, photoBox.Min.Y); transform = new Transform(activeScale, activeOffset, activePhoto.Size); - DrawTexture(active, photoBox); + DrawTexture(active, photoBox, Color4.White, activePhoto.RotationDegrees); for (int i = 0; i < 5; i++) { Texture star = (activePhoto.Rating > i) ? STAR_FILLED : STAR_EMPTY; DrawTexture(star, geometry.StarBoxes[i].Min.X, geometry.StarBoxes[i].Min.Y); @@ -1088,8 +1088,12 @@ public class Game : GameWindow { } public void DrawTexture(Texture texture, Box2i box, Color4 color) { + DrawTexture(texture, box, color, 0); + } + + public void DrawTexture(Texture texture, Box2i box, Color4 color, float rotationDegrees) { GL.Uniform4(shader.GetUniformLocation("color"), color); - SetVertices(box.Min.X, box.Min.Y, box.Size.X, box.Size.Y); + SetVertices(box.Min.X, box.Min.Y, box.Size.X, box.Size.Y, rotationDegrees); GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.DynamicDraw); GL.BindTexture(TextureTarget.Texture2D, texture.Handle); @@ -1136,31 +1140,38 @@ public class Game : GameWindow { GL.Viewport(0, 0, e.Width, e.Height); } - private void SetVertices(float left, float top, float width, float height) { + private void SetVertices(float left, float top, float width, float height, float rotationDegrees) { + Matrix2 transform = Matrix2.CreateRotation(MathHelper.DegreesToRadians(rotationDegrees)); + + Vector2 topLeft = new Vector2(left, top) * transform; + Vector2 topRight = new Vector2(left + width, top) * transform; + Vector2 bottomRight = new Vector2(left + width, top + height) * transform; + Vector2 bottomLeft = new Vector2(left, top + height) * transform; + // top left - vertices[0] = left; - vertices[1] = top; + vertices[0] = topLeft.X; + vertices[1] = topLeft.Y; vertices[2] = 0f; vertices[3] = 0f; vertices[4] = 0f; // top right - vertices[5] = left + width; - vertices[6] = top; + vertices[5] = topRight.X; + vertices[6] = topRight.Y; vertices[7] = 0f; vertices[8] = 1f; vertices[9] = 0f; // bottom right - vertices[10] = left + width; - vertices[11] = top + height; + vertices[10] = bottomRight.X; + vertices[11] = bottomRight.Y; vertices[12] = 0f; vertices[13] = 1f; vertices[14] = 1f; // bottom left - vertices[15] = left; - vertices[16] = top + height; + vertices[15] = bottomLeft.X; + vertices[16] = bottomLeft.Y; vertices[17] = 0f; vertices[18] = 0f; vertices[19] = 1f;