fix #4 Sphere can be rendered with normals now. Calculation fo normals is however not working
All checks were successful
Gitea Actions Demo / Scan the project (push) Successful in 17s

This commit is contained in:
2024-01-04 21:19:55 +01:00
parent d5f3a34a59
commit 551ae51b45
7 changed files with 76 additions and 19 deletions

View File

@ -5,7 +5,8 @@ namespace Nebulix.Rendering
{
public sealed class Mesh
{
public Vector3D<float>[] Vertices { get => vertices; set { vertices = value; regenerate = true; } }
public Vector3D<float>[] Vertices { get => vertices;
set { vertices = value; regenerate = true; normals = new Vector3D<float>[vertices.Length];} }
public uint[] Indices { get => indices; set { indices = value; regenerate = true; } }
private uint vao = 0, vbo = 0, ebo = 0;
@ -23,11 +24,11 @@ namespace Nebulix.Rendering
public void CalculateNormals()
{
normals = new Vector3D<float>[vertices.Length];
for (int j = 0; j < vertices.Length; j++)
{
normals[j] = Vector3D<float>.Zero;
}
// 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)
@ -72,12 +73,23 @@ namespace Nebulix.Rendering
ebo = gl.GenBuffer();
float[] meshData = new float[vertices.Length * 3 + normals.Length * 3];
vertices.ExtractComponents().CopyTo(meshData, 0);
//meshData.AddRange(normals.ExtractComponents());
for (int i = 0, insert = 0; i < vertices.Length; i++)
{
meshData[insert] = vertices[i].X;
meshData[insert + 1] = vertices[i].Y;
meshData[insert + 2] = vertices[i].Z;
meshData[insert + 3] = normals[i].X;
meshData[insert + 4] = normals[i].Y;
meshData[insert + 5] = normals[i].Z;
insert += 6;
}
// extractedVertices.CopyTo(meshData, 0);
// extractedNormals.CopyTo(meshData, extractedVertices.Length);
gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo);
fixed(void* data = &meshData[0])
{
gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(meshData.Length * 3 * sizeof(float)), data, BufferUsageARB.StaticDraw);
gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(meshData.Length * sizeof(float)), data, BufferUsageARB.StaticDraw);
}
gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo);
@ -88,10 +100,10 @@ namespace Nebulix.Rendering
// vertices
gl.EnableVertexAttribArray(0);
gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), null);
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.EnableVertexAttribArray(1);
gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), (void*)(3 * sizeof(float)));
}
}
}

View File

@ -35,6 +35,11 @@ public class Shader
_glContext.Uniform1(_glContext.GetUniformLocation(_shaderProgramId, name), value);
}
public void SetVector(string name, Vector3D<float> value)
{
_glContext.Uniform3(_glContext.GetUniformLocation(_shaderProgramId, name), value.X, value.Y, value.Z);
}
public unsafe void SetMatrix(string name, Matrix4x4 matrix)
{
_glContext.UniformMatrix4(_glContext.GetUniformLocation(_shaderProgramId, name), 1, false, (float*) &matrix);