rotating a texture works, but only about the origin

This commit is contained in:
Colin McMillen 2023-09-15 11:36:11 -04:00
parent 45027ec8b9
commit 4f3e9f50ed
2 changed files with 28 additions and 17 deletions

View File

@ -105,7 +105,7 @@ public class Photo {
public GpsInfo? Gps = null; public GpsInfo? Gps = null;
public Rectangle CropRectangle = Rectangle.Empty; public Rectangle CropRectangle = Rectangle.Empty;
public Vector2i ViewOffset = Vector2i.Zero; public Vector2i ViewOffset = Vector2i.Zero;
public float Rotation = 0; public float RotationDegrees = 0;
private static long touchCounter = 0; private static long touchCounter = 0;
private Texture texture; private Texture texture;

View File

@ -261,16 +261,16 @@ public class StraightenTool : ITool {
public StraightenTool(Photo photo) { public StraightenTool(Photo photo) {
this.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) { public ToolStatus HandleInput(KeyboardState input, MouseState mouse, Transform transform, Game game, Photo photo, UiGeometry geometry) {
if (input.IsKeyPressed(Keys.Left)) { if (input.IsKeyPressed(Keys.Left)) {
photo.Rotation += 1; photo.RotationDegrees += 5;
} }
if (input.IsKeyPressed(Keys.Right)) { if (input.IsKeyPressed(Keys.Right)) {
photo.Rotation -= 1; photo.RotationDegrees -= 5;
} }
if (input.IsKeyPressed(Keys.Enter)) { if (input.IsKeyPressed(Keys.Enter)) {
@ -278,7 +278,7 @@ public class StraightenTool : ITool {
} }
if (input.IsKeyPressed(Keys.Escape)) { if (input.IsKeyPressed(Keys.Escape)) {
photo.Rotation = initialRotation; photo.RotationDegrees = initialRotation;
return ToolStatus.Canceled; return ToolStatus.Canceled;
} }
@ -286,7 +286,7 @@ public class StraightenTool : ITool {
} }
public string Status() { 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); renderSize.X, renderSize.Y);
activeOffset = new(photoBox.Min.X, photoBox.Min.Y); activeOffset = new(photoBox.Min.X, photoBox.Min.Y);
transform = new Transform(activeScale, activeOffset, activePhoto.Size); transform = new Transform(activeScale, activeOffset, activePhoto.Size);
DrawTexture(active, photoBox); DrawTexture(active, photoBox, Color4.White, activePhoto.RotationDegrees);
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
Texture star = (activePhoto.Rating > i) ? STAR_FILLED : STAR_EMPTY; Texture star = (activePhoto.Rating > i) ? STAR_FILLED : STAR_EMPTY;
DrawTexture(star, geometry.StarBoxes[i].Min.X, geometry.StarBoxes[i].Min.Y); 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) { 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); 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, GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices,
BufferUsageHint.DynamicDraw); BufferUsageHint.DynamicDraw);
GL.BindTexture(TextureTarget.Texture2D, texture.Handle); GL.BindTexture(TextureTarget.Texture2D, texture.Handle);
@ -1136,31 +1140,38 @@ public class Game : GameWindow {
GL.Viewport(0, 0, e.Width, e.Height); 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 // top left
vertices[0] = left; vertices[0] = topLeft.X;
vertices[1] = top; vertices[1] = topLeft.Y;
vertices[2] = 0f; vertices[2] = 0f;
vertices[3] = 0f; vertices[3] = 0f;
vertices[4] = 0f; vertices[4] = 0f;
// top right // top right
vertices[5] = left + width; vertices[5] = topRight.X;
vertices[6] = top; vertices[6] = topRight.Y;
vertices[7] = 0f; vertices[7] = 0f;
vertices[8] = 1f; vertices[8] = 1f;
vertices[9] = 0f; vertices[9] = 0f;
// bottom right // bottom right
vertices[10] = left + width; vertices[10] = bottomRight.X;
vertices[11] = top + height; vertices[11] = bottomRight.Y;
vertices[12] = 0f; vertices[12] = 0f;
vertices[13] = 1f; vertices[13] = 1f;
vertices[14] = 1f; vertices[14] = 1f;
// bottom left // bottom left
vertices[15] = left; vertices[15] = bottomLeft.X;
vertices[16] = top + height; vertices[16] = bottomLeft.Y;
vertices[17] = 0f; vertices[17] = 0f;
vertices[18] = 0f; vertices[18] = 0f;
vertices[19] = 1f; vertices[19] = 1f;