diff --git a/src/EngineSharp.Core/EngineSharp.Core/ECS/Entity.cs b/src/EngineSharp.Core/EngineSharp.Core/ECS/Entity.cs index 529487a..050ffa4 100644 --- a/src/EngineSharp.Core/EngineSharp.Core/ECS/Entity.cs +++ b/src/EngineSharp.Core/EngineSharp.Core/ECS/Entity.cs @@ -67,7 +67,7 @@ public class Entity internal void RenderComponents(GL gl, Matrix4X4 projectionMatrix, Matrix4X4 viewMatrix) { // TODO: retrieve the data component "Transform" which every Entity must have (although currently there is no transform component) - var modelMatrix = Matrix4X4.Identity * Matrix4X4.CreateFromQuaternion(Quaternion.Identity) * Matrix4X4.CreateScale(2.0f) * Matrix4X4.CreateTranslation(0, 0, 0f); + var modelMatrix = Matrix4X4.Identity * Matrix4X4.CreateFromQuaternion(Quaternion.Identity) * Matrix4X4.CreateScale(1.0f) * Matrix4X4.CreateTranslation(0, 0, 0f); foreach (var components in _renderComponents) { diff --git a/src/EngineSharp.Core/EngineSharp.Core/OpenGLEngine.cs b/src/EngineSharp.Core/EngineSharp.Core/OpenGLEngine.cs index 63ac0ac..2f80cd2 100644 --- a/src/EngineSharp.Core/EngineSharp.Core/OpenGLEngine.cs +++ b/src/EngineSharp.Core/EngineSharp.Core/OpenGLEngine.cs @@ -24,7 +24,7 @@ internal class OpenGLEngine : Engine _window.Load += OnLoad; _window.Update += OnUpdate; _window.Render += OnRender; - _window.Resize += OnResize; + _window.FramebufferResize += OnResize; _scenes = []; } @@ -125,5 +125,6 @@ internal class OpenGLEngine : Engine // when using an entity component system the camera should probably read the aspect ratio from a central location or should listen to the "Resize" event rather than the engine updating the aspect on the camera. // Especially if more than one camera should be able to exist simultaneously _camera.UpdateAspect(newDimensions); + _gl.Viewport(newDimensions); } } \ No newline at end of file diff --git a/src/EngineSharp.Core/EngineSharp.Core/Rendering/MeshRenderer.cs b/src/EngineSharp.Core/EngineSharp.Core/Rendering/MeshRenderer.cs index ce830be..9178c01 100644 --- a/src/EngineSharp.Core/EngineSharp.Core/Rendering/MeshRenderer.cs +++ b/src/EngineSharp.Core/EngineSharp.Core/Rendering/MeshRenderer.cs @@ -5,6 +5,7 @@ using Silk.NET.OpenGL; namespace EngineSharp.Core.Rendering; +// TODO: Make IDisposable and delete the buffers and vertex arrays on dispose (and make vao, vbo, ebo nullable for cleaner code) public class MeshRenderer : RenderComponent { public required Mesh Mesh { get; set; } // in the future this might be an array, or alternatively, a mesh can have submeshes. we will see @@ -15,7 +16,10 @@ public class MeshRenderer : RenderComponent { GenerateRenderableMesh(gl, projectionMatrix, viewMatrix, modelMatrix); Mesh.PrepareForRendering(projectionMatrix, viewMatrix, modelMatrix); - gl.DrawElements(PrimitiveType.Triangles, (uint)Mesh.Indices.Length, DrawElementsType.UnsignedInt, 0); + unsafe + { + gl.DrawElements(PrimitiveType.Triangles, (uint)Mesh.Indices.Length, DrawElementsType.UnsignedInt, null); + } } private void GenerateRenderableMesh(GL gl, Matrix4X4 projectionMatrix, Matrix4X4 viewMatrix, Matrix4X4 modelMatrix) @@ -37,20 +41,32 @@ public class MeshRenderer : RenderComponent meshData[insert + 4] = Mesh.Normals[i].Y; meshData[insert + 5] = Mesh.Normals[i].Z; } + + unsafe + { + gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo); + // var meshDataSpan = new ReadOnlySpan(meshData); + // gl.BufferData(BufferTargetARB.ArrayBuffer, meshDataSpan, BufferUsageARB.StaticDraw); // TODO: maybe try the unsafe approach and see if that works? + fixed (void* data = &meshData[0]) + { + gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(meshData.Length * sizeof(float)), data, BufferUsageARB.StaticDraw); + } + + gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo); + // var indicesSpan = new ReadOnlySpan(Mesh.Indices); + // gl.BufferData(BufferTargetARB.ArrayBuffer, indicesSpan, BufferUsageARB.StaticDraw); + fixed (void* indices = &Mesh.Indices[0]) + { + gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(Mesh.Indices.Length * sizeof(float)), indices, BufferUsageARB.StaticDraw); + } + + // vertices + gl.EnableVertexAttribArray(0); + gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), null); + // normals + gl.EnableVertexAttribArray(1); + gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), (void*)(3 * sizeof(float))); + } - gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo); - var meshDataSpan = new ReadOnlySpan(meshData); - gl.BufferData(BufferTargetARB.ArrayBuffer, meshDataSpan, BufferUsageARB.StaticDraw); - - gl.BindBuffer(BufferTargetARB.ArrayBuffer, ebo); - var indicesSpan = new ReadOnlySpan(Mesh.Indices); - gl.BufferData(BufferTargetARB.ArrayBuffer, indicesSpan, BufferUsageARB.StaticDraw); - - // vertices - gl.EnableVertexAttribArray(0); - gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), 0); - // normals - gl.EnableVertexAttribArray(1); - gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), 3*sizeof(float)); } } \ No newline at end of file