Tried rendering the sphere without success
All checks were successful
Gitea Actions Demo / Scan the project (push) Successful in 29s

This commit is contained in:
2024-01-02 16:02:24 +01:00
parent 8470304859
commit cf14c9a79e
9 changed files with 135 additions and 53 deletions

View File

@ -1,18 +1,19 @@
using Silk.NET.OpenGL;
using Silk.NET.Maths;
using Silk.NET.OpenGL;
namespace Nebulix.Rendering
{
public sealed class Mesh
{
public float[] Vertices { get => vertices; set { vertices = value; regenerate = true; } }
public Vector3D<float>[] Vertices { get => vertices; set { vertices = value; regenerate = true; } }
public nuint[] Indices { get => indices; set { indices = value; regenerate = true; } }
private uint vao = 0, vbo = 0, ebo = 0;
private bool regenerate = true;
private float[] vertices = [];
private Vector3D<float>[] vertices = [];
private nuint[] indices = [];
private float[] normals = [];
private Vector3D<float>[] normals = [];
public void Clear()
{
@ -22,21 +23,41 @@ namespace Nebulix.Rendering
public void CalculateNormals()
{
throw new NotImplementedException();
normals = new Vector3D<float>[vertices.Length];
for (int j = 0; j < vertices.Length; j++)
{
normals[j] = Vector3D<float>.Zero;
}
return;
for (int i = 0; i < indices.Length; i += 3)
{
Vector3D<float> a = vertices[indices[i] - 1];
Vector3D<float> b = vertices[indices[i + 1] - 1];
Vector3D<float> c = vertices[indices[i + 2] - 1];
Vector3D<float> normal = Vector3D.Cross(b-a, c-a);
normal = Vector3D.Normalize(normal);
normals[indices[i] - 1] = normal;
normals[indices[i + 1] - 1] = normal;
normals[indices[i + 2] - 1] = normal;
}
}
// getting called by "Engine" which currently is in another assembly, meaning I probably need to make this public
// needs to be change for the real engine
public void Use(GL gl)
public void Render(GL gl)
{
if (regenerate) Generate(gl);
gl.BindVertexArray(vao);
gl.DrawElements(PrimitiveType.Triangles, (uint)vertices.Length * 3, DrawElementsType.UnsignedInt, 0);
}
private unsafe void Generate(GL gl)
{
regenerate = false;
if(vao == 0)
vao = gl.CreateVertexArray();
if(vbo == 0)
@ -46,23 +67,23 @@ namespace Nebulix.Rendering
gl.BindVertexArray(vao);
// TODO: meshData needs to also contain uv coords if I decide to add some
List<float> meshData = new(vertices.Length + normals.Length);
meshData.AddRange(vertices);
meshData.AddRange(normals);
List<float> meshData = new(vertices.Length * 3 + normals.Length * 3);
meshData.AddRange(vertices.ExtractComponents());
//meshData.AddRange(normals.ExtractComponents());
ReadOnlySpan<float> data = new(meshData.ToArray());
gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo);
gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(data.Length * sizeof(float)), data, BufferUsageARB.StaticDraw);
ReadOnlySpan<nuint> indicesData = new(indices);
gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo);
gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indicesData.Length * sizeof(uint)), indicesData, BufferUsageARB.StaticDraw);
gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indicesData.Length * sizeof(nuint)), indicesData, BufferUsageARB.StaticDraw);
// vertices
gl.EnableVertexAttribArray(0);
gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), (void*)0);
gl.EnableVertexAttribArray(1);
gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), (void*)(3 * sizeof(float)));
gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), null);
// normals
//gl.EnableVertexAttribArray(1);
//gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), (void*)(3 * sizeof(float)));
}
}
}

View File

@ -4,6 +4,7 @@ using System.Numerics;
namespace Nebulix.Rendering;
// TODO: make IDisposable
public class Shader
{
private readonly GL _glContext;
@ -21,7 +22,7 @@ public class Shader
_shaderProgramId = CreateProgram(vertexShader, fragmentShader);
}
public void Use() { _glContext.UseProgram(_shaderProgramId); }
public void Use() => _glContext.UseProgram(_shaderProgramId);
#region Set uniforms
public void SetInt(string name, int value)

View File

@ -11,17 +11,14 @@ namespace Nebulix.Rendering;
[Serializable]
public class ShaderCompileException : Exception
{
protected ShaderType _shaderType;
protected ShaderType ShaderType;
public ShaderCompileException(ShaderType shaderType) : base("Unable to compile shader.") { _shaderType = shaderType; }
public ShaderCompileException(ShaderType shaderType, string message) : base(message) { _shaderType = shaderType; }
public ShaderCompileException(ShaderType shaderType, string message, Exception inner) : base(message, inner) { _shaderType = shaderType; }
protected ShaderCompileException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
public ShaderCompileException(ShaderType shaderType) : base("Unable to compile shader.") { ShaderType = shaderType; }
public ShaderCompileException(ShaderType shaderType, string message) : base(message) { ShaderType = shaderType; }
public ShaderCompileException(ShaderType shaderType, string message, Exception inner) : base(message, inner) { ShaderType = shaderType; }
public override string ToString()
{
return $"Type of Shader: '{_shaderType}'" + "\n" + Message;
return $"Type of Shader: '{ShaderType}'" + "\n" + Message;
}
}

View File

@ -13,7 +13,4 @@ public class ShaderLinkException : Exception
public ShaderLinkException() : base("Error occured while trying to link a shader.") { }
public ShaderLinkException(string message) : base(message) { }
public ShaderLinkException(string message, Exception inner) : base(message, inner) { }
protected ShaderLinkException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}