From 82bc46fbb3129f5ac52b40271e899b28dfea3df3 Mon Sep 17 00:00:00 2001 From: Zyb3rWolfi Date: Tue, 7 Oct 2025 16:12:24 +0100 Subject: [PATCH] Fixed some bugs --- TheRepo/TheLabs/MyExampleWindow.cs | 14 +++++--------- TheRepo/TheLabs/Shapes/Circle.cs | 20 +++++++++++++------- TheRepo/TheLabs/Shapes/Cylinder.cs | 19 ++++++++++++++----- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/TheRepo/TheLabs/MyExampleWindow.cs b/TheRepo/TheLabs/MyExampleWindow.cs index 4c9bfbf..6f5a0f5 100644 --- a/TheRepo/TheLabs/MyExampleWindow.cs +++ b/TheRepo/TheLabs/MyExampleWindow.cs @@ -71,8 +71,8 @@ namespace TheLabs base.OnRenderFrame(e); // Clear the color buffer and the depth buffer GL.Clear(ClearBufferMask.DepthBufferBit | ClearBufferMask.ColorBufferBit); - // Create the model, view, and projection matrices + Matrix4 model = Matrix4.CreateRotationY(_rotation) * Matrix4.CreateRotationX(_rotation * 0.5f); Matrix4 view = Matrix4.CreateTranslation(0.0f, 0.0f, -3.0f); Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView( MathHelper.DegreesToRadians(45f), @@ -82,14 +82,10 @@ namespace TheLabs ); // Draw the cube - for (int i = 0; i < 3; i++) - { - _cube = new Cube(); - _cube.Position = new Vector3(0.5f, 0.5f * i * 0.5f, 0.0f); - _cube.Draw(_shader, view, projection, _rotation); - } - //_circle.Draw(_shader, model); - //_cylinder.Draw(_shader, model); + //_cube = new Cube(); + //_cube.Draw(_shader, view, projection, _rotation); + //_circle.Draw(_shader, view, projection, model); + //_cylinder.Draw(_shader, model, view, projection); SwapBuffers(); GL.GetError(); } diff --git a/TheRepo/TheLabs/Shapes/Circle.cs b/TheRepo/TheLabs/Shapes/Circle.cs index 9a05f00..e2e79f5 100644 --- a/TheRepo/TheLabs/Shapes/Circle.cs +++ b/TheRepo/TheLabs/Shapes/Circle.cs @@ -9,22 +9,19 @@ public class Circle // Vertex Array Object, Vertex Buffer Object, Element Buffer Object private int _vao; private int _vbo; - private int _ebo; - private int _vertexCount; private readonly float[] _vertices = { }; - private int _maxVert = 100; public Circle(float z = 0f, int segments = 100, Vector4? color = null) { Vector4 col = color ?? new Vector4(0f, 0f, 1f, 1f); for (int i = 0; i <= segments; i++) { - float angle = i * 2.0f * MathF.PI / _maxVert; + float angle = i * 2.0f * MathF.PI / segments; float x = 0.5f * MathF.Cos(angle); float y = 0.5f * MathF.Sin(angle); @@ -71,9 +68,19 @@ public class Circle { return _vertices; } - public void Draw(Shader shader, Matrix4 matrix4) + public void Draw(Shader shader, Matrix4 view, Matrix4 projection, Matrix4 model) { - shader.SetMatrix4("model", matrix4); + //shader.SetMatrix4("model", matrix4); + int modelLoc = GL.GetUniformLocation(shader.Handle, "model"); + int viewLoc = GL.GetUniformLocation(shader.Handle, "view"); + int projLoc = GL.GetUniformLocation(shader.Handle, "projection"); + + // Send the matrices to the shader + GL.UniformMatrix4(modelLoc, false, ref model); + GL.UniformMatrix4(viewLoc, false, ref view); + GL.UniformMatrix4(projLoc, false, ref projection); + + shader.Use(); GL.BindVertexArray(_vao); GL.DrawArrays(PrimitiveType.TriangleFan, 0, _vertices.Length / 7); @@ -82,7 +89,6 @@ public class Circle public void Dispose() { GL.DeleteBuffer(_vbo); - GL.DeleteBuffer(_ebo); GL.DeleteVertexArray(_vao); } } \ No newline at end of file diff --git a/TheRepo/TheLabs/Shapes/Cylinder.cs b/TheRepo/TheLabs/Shapes/Cylinder.cs index fdba6d7..1b232e2 100644 --- a/TheRepo/TheLabs/Shapes/Cylinder.cs +++ b/TheRepo/TheLabs/Shapes/Cylinder.cs @@ -31,7 +31,7 @@ public class Cylinder _vertexCount = _indices.Length; _topCircle = new Circle(0.5f, 100, new Vector4(1f, 1f, 0f, 1f)); _bottomCircle = new Circle(-0.5f, 100, new Vector4(1f, 0f, 0f, 1f)); - GenerateCylinder(101); + GenerateCylinder(100); } @@ -105,14 +105,23 @@ public class Cylinder GL.BindVertexArray(0); } - public void Draw(Shader shader, Matrix4 matrix4) + public void Draw(Shader shader, Matrix4 model, Matrix4 view, Matrix4 projection) { - shader.SetMatrix4("model", matrix4); + shader.SetMatrix4("model", model); + // Set the matrices in the shader + int modelLoc = GL.GetUniformLocation(shader.Handle, "model"); + int viewLoc = GL.GetUniformLocation(shader.Handle, "view"); + int projLoc = GL.GetUniformLocation(shader.Handle, "projection"); + + // Send the matrices to the shader + GL.UniformMatrix4(modelLoc, false, ref model); + GL.UniformMatrix4(viewLoc, false, ref view); + GL.UniformMatrix4(projLoc, false, ref projection); GL.BindVertexArray(_vao); GL.DrawElements(PrimitiveType.Triangles, _indices.Length, DrawElementsType.UnsignedInt, 0); - _topCircle.Draw(shader, matrix4); - _bottomCircle.Draw(shader, matrix4); + _topCircle.Draw(shader, view, projection, model); + _bottomCircle.Draw(shader, view, projection, model); } public void Dispose()