From 9bdb641096bc42591fdbcd2acde42b3a7d53ba84 Mon Sep 17 00:00:00 2001 From: zyb3rwolfi Date: Thu, 13 Nov 2025 02:28:09 +0000 Subject: [PATCH] Finished refractor --- TheRepo/TheLabs/Mesh.cs | 3 +-- TheRepo/TheLabs/MyExampleWindow.cs | 30 ++++++++++++++---------------- TheRepo/TheLabs/RenderObject.cs | 13 ++++++++----- TheRepo/TheLabs/ShapeFactory.cs | 6 +++--- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/TheRepo/TheLabs/Mesh.cs b/TheRepo/TheLabs/Mesh.cs index d2c868b..27ad6f5 100644 --- a/TheRepo/TheLabs/Mesh.cs +++ b/TheRepo/TheLabs/Mesh.cs @@ -49,14 +49,13 @@ public class Mesh : IDisposable { // layout(location = 0) in shader: vec3 position var stride = 5 * sizeof(float); - GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, stride, 0); + GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, stride, 0 ); GL.EnableVertexAttribArray(0); // layout(location = 1) in shader: vec2 texCoord GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, stride, 3 * sizeof(float)); GL.EnableVertexAttribArray(1); } - // ... (you could add more layouts) GL.BindVertexArray(0); } diff --git a/TheRepo/TheLabs/MyExampleWindow.cs b/TheRepo/TheLabs/MyExampleWindow.cs index 28855bb..ec44233 100644 --- a/TheRepo/TheLabs/MyExampleWindow.cs +++ b/TheRepo/TheLabs/MyExampleWindow.cs @@ -36,28 +36,19 @@ namespace TheLabs { // This is called when the window is created and is where we can set up OpenGL resources. base.OnLoad(); - string shaderVertPath = "Shaders/shader.vert"; - string texturePath = "Textures/placeholder.png"; - - if (!File.Exists(shaderVertPath)) - { - throw new FileNotFoundException($"The shader file was not found at: {Path.GetFullPath(shaderVertPath)}"); - } - if (!File.Exists(texturePath)) - { - throw new FileNotFoundException($"The texture file was not found at: {Path.GetFullPath(texturePath)}"); - } + //GL.Disable(EnableCap.CullFace); + //GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line); // Set The background color to a nice blue. GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f); GL.Enable(EnableCap.DepthTest); _shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag"); - _cubeMesh = ShapeFactory.CreateTexturedCube(); _cubeTexture = new Texture("Textures/placeholder.png"); - _cubeObject = new RenderObject(_cubeMesh, _shader, _cubeTexture); - _cubeObject.Position = new Vector3(-0.75f, 0.0f, 0.0f); + _cubeObject.Position = new Vector3(0.0f, 0.0f, -1.0f); + _cubeObject.Scale = new Vector3(0.5f, 0.5f, 0.5f); + } @@ -72,9 +63,16 @@ namespace TheLabs // --- Set up Camera --- Matrix4 view = Matrix4.CreateTranslation(0.0f, 0.0f, -3.0f); + float aspectRatio = (float)Size.X / Size.Y; + + // 2. Add a safety check for divide-by-zero + if (Size.Y == 0 || Size.X == 0) + { + aspectRatio = 1.0f; // Default to 1:1 if window is minimized + } Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView( MathHelper.DegreesToRadians(45f), - Size.X / (float)Size.Y, + aspectRatio, 0.1f, 100.0f ); @@ -87,7 +85,7 @@ namespace TheLabs protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); - _rotation += (float)e.Time; + _rotation += (float)e.Time ; var input = KeyboardState; if (input.IsKeyDown(Keys.Escape)) diff --git a/TheRepo/TheLabs/RenderObject.cs b/TheRepo/TheLabs/RenderObject.cs index 74d8eb3..1ef5d38 100644 --- a/TheRepo/TheLabs/RenderObject.cs +++ b/TheRepo/TheLabs/RenderObject.cs @@ -28,13 +28,16 @@ public class RenderObject Shader.SetInt("uTexture", 0); // 2. Bind Texture Texture.Use(); // Assumes you have a Texture class - // 3. Calculate Model Matrix Matrix4 model = Matrix4.CreateScale(Scale) * Matrix4.CreateFromQuaternion(Rotation) * Matrix4.CreateTranslation(Position); - // 4. Set Uniforms - Shader.SetMatrix4("model", model); - Shader.SetMatrix4("view", view); - Shader.SetMatrix4("projection", projection); + int modelLoc = GL.GetUniformLocation(Shader.Handle, "model"); + GL.UniformMatrix4(modelLoc, false, ref model); // Try TRUE here + + int viewLoc = GL.GetUniformLocation(Shader.Handle, "view"); + GL.UniformMatrix4(viewLoc, false, ref view); // Try TRUE here + + int projLoc = GL.GetUniformLocation(Shader.Handle, "projection"); + GL.UniformMatrix4(projLoc, false, ref projection); // Try TRUE here // 5. Tell the Mesh to draw itself Mesh.Draw(); diff --git a/TheRepo/TheLabs/ShapeFactory.cs b/TheRepo/TheLabs/ShapeFactory.cs index 9093274..a148011 100644 --- a/TheRepo/TheLabs/ShapeFactory.cs +++ b/TheRepo/TheLabs/ShapeFactory.cs @@ -44,7 +44,7 @@ public static class ShapeFactory uint[] indices = { // Front face - 0, 1, 2, + 0, 2, 1, 0, 2, 3, // Back face 4, 5, 6, @@ -53,13 +53,13 @@ public static class ShapeFactory 8, 9, 10, 8, 10, 11, // Right face - 12, 13, 14, + 12, 14, 13, 12, 14, 15, // Top face 16, 17, 18, 16, 18, 19, // Bottom face - 20, 21, 22, + 20, 22, 21, 20, 22, 23 };