fixed seg fault error; still nothing rendes though

This commit is contained in:
2025-12-29 18:24:11 +01:00
parent 73cbac0f66
commit a375b459c3
3 changed files with 34 additions and 17 deletions

View File

@@ -67,7 +67,7 @@ public class Entity
internal void RenderComponents(GL gl, Matrix4X4<float> projectionMatrix, Matrix4X4<float> viewMatrix)
{
// TODO: retrieve the data component "Transform" which every Entity must have (although currently there is no transform component)
var modelMatrix = Matrix4X4<float>.Identity * Matrix4X4.CreateFromQuaternion(Quaternion<float>.Identity) * Matrix4X4.CreateScale(2.0f) * Matrix4X4.CreateTranslation(0, 0, 0f);
var modelMatrix = Matrix4X4<float>.Identity * Matrix4X4.CreateFromQuaternion(Quaternion<float>.Identity) * Matrix4X4.CreateScale(1.0f) * Matrix4X4.CreateTranslation(0, 0, 0f);
foreach (var components in _renderComponents)
{

View File

@@ -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);
}
}

View File

@@ -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<float> projectionMatrix, Matrix4X4<float> viewMatrix, Matrix4X4<float> 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<float>(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<uint>(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<float>(meshData);
gl.BufferData(BufferTargetARB.ArrayBuffer, meshDataSpan, BufferUsageARB.StaticDraw);
gl.BindBuffer(BufferTargetARB.ArrayBuffer, ebo);
var indicesSpan = new ReadOnlySpan<uint>(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));
}
}