Mesh can now be rendered and therefore also the sphere
All checks were successful
Gitea Actions Demo / Scan the project (push) Successful in 17s

This commit is contained in:
Daniel 2024-01-03 21:47:37 +01:00
parent cc3493627d
commit d5f3a34a59
4 changed files with 45 additions and 56 deletions

View File

@ -6,13 +6,13 @@ namespace Nebulix.Rendering
public sealed class Mesh
{
public Vector3D<float>[] Vertices { get => vertices; set { vertices = value; regenerate = true; } }
public nuint[] Indices { get => indices; set { indices = value; regenerate = true; } }
public uint[] Indices { get => indices; set { indices = value; regenerate = true; } }
private uint vao = 0, vbo = 0, ebo = 0;
private bool regenerate = true;
private Vector3D<float>[] vertices = [];
private nuint[] indices = [];
private uint[] indices = [];
private Vector3D<float>[] normals = [];
public void Clear()
@ -50,12 +50,12 @@ namespace Nebulix.Rendering
/// Binds the necessary buffers and draws the mesh. Does not use any Shaders
/// </summary>
/// <param name="gl"></param>
public void Render(GL gl)
public unsafe void Render(GL gl)
{
if (regenerate) Generate(gl);
gl.BindVertexArray(vao);
gl.DrawElements(PrimitiveType.Triangles, (uint)indices.Length, DrawElementsType.UnsignedInt, 0);
gl.DrawElements(PrimitiveType.Triangles, (uint)indices.Length, DrawElementsType.UnsignedInt, null);
}
private unsafe void Generate(GL gl)
@ -71,16 +71,20 @@ namespace Nebulix.Rendering
if(ebo == 0)
ebo = gl.GenBuffer();
List<float> meshData = new(vertices.Length * 3 + normals.Length * 3);
meshData.AddRange(vertices.ExtractComponents());
float[] meshData = new float[vertices.Length * 3 + normals.Length * 3];
vertices.ExtractComponents().CopyTo(meshData, 0);
//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);
fixed(void* data = &meshData[0])
{
gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(meshData.Length * 3 * sizeof(float)), data, BufferUsageARB.StaticDraw);
}
ReadOnlySpan<nuint> indicesData = new(indices);
gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo);
gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indicesData.Length * sizeof(nuint)), indicesData, BufferUsageARB.StaticDraw);
fixed(void* indicesData = &indices[0])
{
gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indices.Length * sizeof(uint)), indicesData, BufferUsageARB.StaticDraw);
}
// vertices
gl.EnableVertexAttribArray(0);

View File

@ -32,7 +32,6 @@ public static class Program
private static Vector2 _lastMousePosition;
private static uint _vao, _vbo;
private static Sphere sphere;
private static Mesh testMesh;
public static void Main(string[] args)
{
@ -144,24 +143,6 @@ public static class Program
sphere.CreateSphere();
_sphereShader =
new Nebulix.Rendering.Shader(_gl, "./Shaders/Sphere/sphere.vert", "./Shaders/Sphere/sphere.frag");
testMesh = new Mesh
{
Vertices =
[
new Vector3D<float>(0.5f, 0.5f, 0.0f),
new Vector3D<float>(0.5f, -0.5f, 0.0f),
new Vector3D<float>(-0.5f, -0.5f, 0.0f),
new Vector3D<float>(-0.5f, 0.5f, 0.5f)
],
Indices =
[
0u, 1u, 3u,
1u, 2u, 3u
]
};
testMesh.CalculateNormals();
}
private static void OnUpdate(double deltaTime)
@ -190,7 +171,7 @@ public static class Program
}
}
private static void OnRender(double deltaTime)
private static unsafe void OnRender(double deltaTime)
{
_gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
@ -207,21 +188,18 @@ public static class Program
_shader.SetInt("tex", 0);
_texture.Bind();
// _gl.BindVertexArray(_vao);
// _gl.DrawArrays(PrimitiveType.Triangles, 0, 36);
_gl.BindVertexArray(_vao);
_gl.DrawArrays(PrimitiveType.Triangles, 0, 36);
// Sphere
modelMatrix = Matrix4x4.CreateTranslation(0, 0, 0);
modelMatrix = Matrix4x4.CreateTranslation(1, 0, 0);
_sphereShader.Use();
_sphereShader.SetMatrix("modelMatrix", modelMatrix);
_sphereShader.SetMatrix("projectionMatrix", projectionMatrix);
_sphereShader.SetMatrix("viewMatrix", viewMatrix);
// sphere.RenderSphere(_gl);
testMesh.Render(_gl);
// var f = new Face(-Vector3D<float>.UnitX, 10);
// f.GetMesh().Render(_gl);
sphere.RenderSphere(_gl);
}
private static void OnKeyDown(IKeyboard keyboard, Key key, int keyCode)

View File

@ -1,6 +1,6 @@
#version 330 core
out vec4 colour;
out vec4 FragColour;
in vec3 FragPos;
//in vec3 Normal;
@ -8,8 +8,5 @@ in vec3 FragPos;
void main()
{
vec3 col = vec3(1.0, 0.5, 0.2) * FragPos;
//vec3 col = texture(tex, TexCoords);
//colour = vec4(texture(tex, TexCoords).rgb, 1.0);
// colour = vec4(col, 1);
colour = vec4(1);
FragColour = vec4(col, 1);
}

View File

@ -6,9 +6,19 @@ using Silk.NET.OpenGL;
namespace Engine_silk.NET
{
// also maybe make an ISphere and call this class "CubeSphere"?
public class Sphere(uint resolution)
public class Sphere
{
private readonly Face[] sphereFaces = new Face[6];
private readonly uint resolution;
public Sphere(uint resolution)
{
if (resolution < 2)
throw new ArgumentOutOfRangeException(nameof(resolution), resolution,
"Resolution must be greater than 1");
this.resolution = resolution;
}
public void CreateSphere()
{
Vector3D<float>[] directions =
@ -63,7 +73,7 @@ namespace Engine_silk.NET
var vertices = new Vector3D<float>[_resolution * _resolution];
// _resolution - 1 because the vertices index starts at 0
// * 6 because each triangle needs 3 points and each small quad has 2 triangles 3*2 = 6
var indices = new nuint[(_resolution - 1) * (_resolution - 1) * 6];
var indices = new uint[(_resolution - 1) * (_resolution - 1) * 6];
int triangleIndex = 0;
uint i;