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