add DrawTexture() and DrawBox() functions

This commit is contained in:
Colin McMillen 2023-07-06 21:26:50 -04:00
parent 4e0a15e3a0
commit 536abbf2b4

View File

@ -189,7 +189,7 @@ public class Game : GameWindow {
public Game(GameWindowSettings gwSettings, NativeWindowSettings nwSettings) : base(gwSettings, nwSettings) { } public Game(GameWindowSettings gwSettings, NativeWindowSettings nwSettings) : base(gwSettings, nwSettings) { }
static CameraInfo activeCamera = CameraInfo.NIKON_D7000; static CameraInfo activeCamera = CameraInfo.NIKON_D7000;
static int thumbnailHeight = 100; static int thumbnailHeight = 150;
static int thumbnailWidth = (int) 1.0 * thumbnailHeight * activeCamera.Resolution.X / activeCamera.Resolution.Y; static int thumbnailWidth = (int) 1.0 * thumbnailHeight * activeCamera.Resolution.X / activeCamera.Resolution.Y;
static Texture TEXTURE_WHITE; static Texture TEXTURE_WHITE;
@ -294,34 +294,44 @@ public class Game : GameWindow {
protected override void OnRenderFrame(FrameEventArgs e) { protected override void OnRenderFrame(FrameEventArgs e) {
base.OnRenderFrame(e); base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit); GL.Clear(ClearBufferMask.ColorBufferBit);
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject);
GL.ActiveTexture(TextureUnit.Texture0);
int borderWidth = 2; int borderWidth = 0;
int maxPhotoWidth = windowWidth - thumbnailWidth - borderWidth; int maxPhotoWidth = windowWidth - thumbnailWidth - borderWidth;
Texture active = textures[textureIndex]; Texture active = textures[textureIndex];
// TODO: handle the case where we need to letterbox vertically instead. // TODO: handle the case where we need to letterbox vertically instead.
// TODO: pull these geometry calculations out into an object.
int photoWidth = (int) (1.0 * windowHeight / active.Height * active.Width); int photoWidth = (int) (1.0 * windowHeight / active.Height * active.Width);
int letterboxWidth = (maxPhotoWidth - photoWidth) / 2; int letterboxWidth = (maxPhotoWidth - photoWidth) / 2;
SetVertices(letterboxWidth, 0, photoWidth, windowHeight); DrawTexture(active, letterboxWidth, 0, photoWidth, windowHeight);
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject);
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.DynamicDraw);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, active.Handle);
GL.DrawElements(PrimitiveType.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0);
for (int i = 0; i < textures.Count; i++) { for (int i = 0; i < textures.Count; i++) {
SetVertices(windowWidth - thumbnailWidth, i * thumbnailHeight, thumbnailWidth, thumbnailHeight - borderWidth); // FIXME: make this a rect or something
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject); DrawTexture(textures[i], windowWidth - thumbnailWidth, i * thumbnailHeight, thumbnailWidth, thumbnailHeight - borderWidth);
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.DynamicDraw); if (i == textureIndex) {
GL.ActiveTexture(TextureUnit.Texture0); DrawBox(windowWidth - thumbnailWidth, i * thumbnailHeight, thumbnailWidth, thumbnailHeight - borderWidth, 2);
GL.BindTexture(TextureTarget.Texture2D, textures[i].Handle); }
GL.DrawElements(PrimitiveType.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0);
} }
SwapBuffers(); SwapBuffers();
} }
void DrawTexture(Texture texture, int left, int top, int width, int height) {
SetVertices(left, top, width, height);
GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.DynamicDraw);
GL.BindTexture(TextureTarget.Texture2D, texture.Handle);
GL.DrawElements(PrimitiveType.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0);
}
void DrawBox(int left, int top, int width, int height, int thickness) {
DrawTexture(TEXTURE_WHITE, left, top, width, thickness);
DrawTexture(TEXTURE_WHITE, left, top, thickness, height);
DrawTexture(TEXTURE_WHITE, left, top + height - thickness, width, thickness);
DrawTexture(TEXTURE_WHITE, left + width - thickness, top, thickness, height);
}
protected override void OnResize(ResizeEventArgs e) { protected override void OnResize(ResizeEventArgs e) {
base.OnResize(e); base.OnResize(e);
Console.WriteLine($"OnResize: {e.Width}x{e.Height}"); Console.WriteLine($"OnResize: {e.Width}x{e.Height}");