fixed error on windows machines; removed some unsafe code; did a little cleanup

This commit is contained in:
2026-01-06 10:50:24 +01:00
parent 9365e82cbf
commit 9242fe5b4c
3 changed files with 25 additions and 37 deletions

View File

@@ -13,7 +13,7 @@ internal class OpenGLEngine : Engine
{
private readonly IWindow _window;
private readonly Dictionary<string, Scene> _scenes;
private GL _gl = null!; // because between constructing the engine and OnLoad being called nothing should call these fields. Therefore, we do "!" to get rid of warnings
private GL _gl = null!; // "= null!" because between constructing the engine and OnLoad being called nothing should access these fields anyway
private InputService _inputService = null!;
private PerspectiveCamera _camera = null!;
private Scene? _currentScene;
@@ -58,7 +58,8 @@ internal class OpenGLEngine : Engine
var shader = new Shader(vertexShaderFile, fragmentShaderFile);
shader.Initialize(_gl);
_shaders.Add(vertexShaderFile, shader);
var osInvariantVertexShaderFile = vertexShaderFile.Replace("\\", "/");
_shaders.Add(osInvariantVertexShaderFile, shader);
}
var computeShaderFiles = Directory.GetFiles("./assets/shaders", "*.comp");
@@ -67,7 +68,8 @@ internal class OpenGLEngine : Engine
var shader = new ComputeShader(computeShaderFile);
shader.Initialize(_gl);
_computeShaders.Add(computeShaderFile, shader);
var osInvariantComputeShaderFile = computeShaderFile.Replace("\\", "/");
_computeShaders.Add(osInvariantComputeShaderFile, shader);
}
}

View File

@@ -27,7 +27,6 @@ internal abstract class Engine : IEngine
internal Scene CreateScene(string sceneName, bool isDefaultScene)
{
if (isDefaultScene && !string.IsNullOrEmpty(_defaultSceneName))
{
throw new InvalidOperationException(

View File

@@ -1,16 +1,15 @@
using System;
using EngineSharp.Core.ECS;
using EngineSharp.Core.ECS;
using Silk.NET.Maths;
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)
// TODO: Make IDisposable and delete the buffers and vertex arrays on dispose
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
private uint vao, vbo, ebo;
private uint? _vao, _vbo, _ebo;
internal override void Render(GL gl, Matrix4X4<float> projectionMatrix, Matrix4X4<float> viewMatrix, Matrix4X4<float> modelMatrix)
{
@@ -24,11 +23,11 @@ public class MeshRenderer : RenderComponent
private void GenerateRenderableMesh(GL gl, Matrix4X4<float> projectionMatrix, Matrix4X4<float> viewMatrix, Matrix4X4<float> modelMatrix)
{
if(vao == 0) { vao = gl.CreateVertexArray(); }
gl.BindVertexArray(vao);
_vao ??= gl.CreateVertexArray();
gl.BindVertexArray(_vao.Value);
if(vbo == 0) { vbo = gl.GenBuffer(); }
if(ebo == 0) { ebo = gl.GenBuffer(); }
_vbo ??= gl.GenBuffer();
_ebo ??= gl.GenBuffer();
var meshData = new float[Mesh.Vertices.Length * 3 + Mesh.Normals.Length * 3];
for (int i = 0, insert = 0; i < Mesh.Vertices.Length; i++, insert += 6)
@@ -42,31 +41,19 @@ public class MeshRenderer : RenderComponent
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.Value);
var meshDataSpan = new ReadOnlySpan<float>(meshData);
gl.BufferData(BufferTargetARB.ArrayBuffer, meshDataSpan, BufferUsageARB.StaticDraw);
gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, _ebo.Value);
var indicesSpan = new ReadOnlySpan<uint>(Mesh.Indices);
gl.BufferData(BufferTargetARB.ElementArrayBuffer, 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));
}
}