letterbox the image so that the aspect ratio is right

This commit is contained in:
Colin McMillen 2023-06-29 22:52:52 -04:00
parent a3360b1420
commit 0625040304

View File

@ -278,13 +278,18 @@ public class Game : GameWindow {
int thumbnailWidth = 150; int thumbnailWidth = 150;
int thumbnailHeight = 100; int thumbnailHeight = 100;
int borderWidth = 2; int borderWidth = 2;
int photoWidth = windowWidth - thumbnailWidth - borderWidth; int maxPhotoWidth = windowWidth - thumbnailWidth - borderWidth;
SetVertices(0, 0, photoWidth, windowHeight); Texture active = textures[textureIndex];
// TODO: handle the case where we need to letterbox vertically instead.
int photoWidth = (int) (1.0 * windowHeight / active.Height * active.Width);
int letterboxWidth = (maxPhotoWidth - photoWidth) / 2;
SetVertices(letterboxWidth, 0, photoWidth + letterboxWidth, windowHeight);
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject); GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject);
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.DynamicDraw); GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.DynamicDraw);
GL.ActiveTexture(TextureUnit.Texture0); GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, textures[textureIndex].Handle); GL.BindTexture(TextureTarget.Texture2D, active.Handle);
shader.Use(); shader.Use();
GL.DrawElements(PrimitiveType.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0); GL.DrawElements(PrimitiveType.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0);