use a Vector2i for Texture size

This commit is contained in:
Colin McMillen 2023-07-07 23:41:32 -04:00
parent 30f21943a8
commit ec2989a9b0

View File

@ -3,8 +3,8 @@ using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.GraphicsLibraryFramework;
// https://docs.sixlabors.com/api/ImageSharp/SixLabors.ImageSharp.Image.html
using System.Runtime.CompilerServices;
// https://docs.sixlabors.com/api/ImageSharp/SixLabors.ImageSharp.Image.html
using Image = SixLabors.ImageSharp.Image;
namespace SemiColinGames;
@ -138,26 +138,23 @@ void main() {
public class Texture : IDisposable {
public int Handle;
// FIXME: use a Vector2i for dimensions.
public int Width;
public int Height;
public Vector2i Size;
public Texture(Image<Rgba32> image) {
Width = image.Width;
Height = image.Height;
Console.WriteLine($"image loaded: {Width}x{Height}");
Size = new Vector2i(image.Width, image.Height);
Console.WriteLine($"image loaded: {Size}");
//foreach (IExifValue exif in image.Metadata.ExifProfile.Values) {
// Console.WriteLine($"{exif.Tag} : {exif.GetValue()}");
//}
byte[] pixelBytes = new byte[Width * Height * Unsafe.SizeOf<Rgba32>()];
byte[] pixelBytes = new byte[Size.X * Size.Y * Unsafe.SizeOf<Rgba32>()];
image.CopyPixelDataTo(pixelBytes);
image.Dispose();
Handle = GL.GenTexture();
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, Handle);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, image.Width, image.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, pixelBytes);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, Size.X, Size.Y, 0, PixelFormat.Rgba, PixelType.UnsignedByte, pixelBytes);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) TextureMinFilter.LinearMipmapLinear); // FIXME: is this right?
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) TextureMagFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) TextureWrapMode.ClampToBorder);
@ -321,11 +318,11 @@ public class Game : GameWindow {
Texture active = textures[textureIndex];
// FIXME: make a function for scaling & centering one box on another.
double scaleX = 1.0 * geometry.PhotoBox.Size.X / active.Width;
double scaleY = 1.0 * geometry.PhotoBox.Size.Y / active.Height;
double scaleX = 1.0 * geometry.PhotoBox.Size.X / active.Size.X;
double scaleY = 1.0 * geometry.PhotoBox.Size.Y / active.Size.Y;
double scale = Math.Min(scaleX, scaleY);
int renderWidth = (int) (active.Width * scale);
int renderHeight = (int) (active.Height * scale);
int renderWidth = (int) (active.Size.X * scale);
int renderHeight = (int) (active.Size.Y * scale);
Box2i photoBox = Util.makeBox((int) geometry.PhotoBox.Center.X - renderWidth / 2, (int) geometry.PhotoBox.Center.Y - renderHeight / 2, renderWidth, renderHeight);
DrawTexture(active, photoBox);
for (int i = 0; i < textures.Count; i++) {