construct shader & WHITE_TEXTURE at Game constructor time

This commit is contained in:
Colin McMillen 2023-07-07 23:29:51 -04:00
parent 72e65b912c
commit 30ac76f4c0

View File

@ -4,9 +4,6 @@ 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 // https://docs.sixlabors.com/api/ImageSharp/SixLabors.ImageSharp.Image.html
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Image = SixLabors.ImageSharp.Image; using Image = SixLabors.ImageSharp.Image;
@ -24,9 +21,13 @@ public class CameraInfo {
} }
public class Shader : IDisposable { public class Shader : IDisposable {
int Handle; public int Handle;
private bool init = false;
public Shader() { public Shader() {}
public void Init() {
init = true;
int VertexShader; int VertexShader;
int FragmentShader; int FragmentShader;
@ -99,6 +100,9 @@ void main() {
} }
public void Use() { public void Use() {
if (!init) {
Console.WriteLine("Shader.Use(): must call Init() first");
}
GL.UseProgram(Handle); GL.UseProgram(Handle);
} }
@ -218,7 +222,8 @@ public static class Util {
public class Game : GameWindow { public class Game : GameWindow {
public Game(GameWindowSettings gwSettings, NativeWindowSettings nwSettings) : base(gwSettings, nwSettings) { } public Game(GameWindowSettings gwSettings, NativeWindowSettings nwSettings) : base(gwSettings, nwSettings) { }
static Texture TEXTURE_WHITE; private static Image<Rgba32> white1x1 = new(1, 1, new Rgba32(255, 255, 255));
private static Texture TEXTURE_WHITE = new Texture(white1x1);
UiGeometry geometry = new(); UiGeometry geometry = new();
@ -236,7 +241,7 @@ public class Game : GameWindow {
int VertexArrayObject; int VertexArrayObject;
List<Texture> textures = new(); List<Texture> textures = new();
int textureIndex = 0; int textureIndex = 0;
Shader shader; Shader shader = new Shader();
Matrix4 projection; Matrix4 projection;
protected override void OnUpdateFrame(FrameEventArgs e) { protected override void OnUpdateFrame(FrameEventArgs e) {
@ -276,7 +281,7 @@ public class Game : GameWindow {
GL.BindBuffer(BufferTarget.ElementArrayBuffer, ElementBufferObject); GL.BindBuffer(BufferTarget.ElementArrayBuffer, ElementBufferObject);
GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Length * sizeof(uint), indices, BufferUsageHint.DynamicDraw); GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Length * sizeof(uint), indices, BufferUsageHint.DynamicDraw);
shader = new Shader(); shader.Init();
shader.Use(); shader.Use();
// Because there's 5 floats between the start of the first vertex and the start of the second, // Because there's 5 floats between the start of the first vertex and the start of the second,
@ -294,8 +299,7 @@ public class Game : GameWindow {
GL.VertexAttribPointer(texCoordLocation, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float)); GL.VertexAttribPointer(texCoordLocation, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float));
// Create a blank white texture. // Create a blank white texture.
Image<Rgba32> white1x1 = new(1, 1, new Rgba32(255, 255, 255));
TEXTURE_WHITE = new Texture(white1x1);
// Load textures from JPEGs. // Load textures from JPEGs.
string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\06\27\"); string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\06\27\");