another try at fixing the mesh class (maybe mesh class is not the problem after all?)
All checks were successful
Gitea Actions Demo / Scan the project (push) Successful in 20s

This commit is contained in:
Daniel 2024-01-03 00:22:58 +01:00
parent 049d8cff7e
commit cc3493627d
4 changed files with 49 additions and 46 deletions

View File

@ -28,6 +28,7 @@ namespace Nebulix
result[resultIdx] = array[i].X; result[resultIdx] = array[i].X;
result[resultIdx + 1] = array[i].Y; result[resultIdx + 1] = array[i].Y;
result[resultIdx + 2] = array[i].Z; result[resultIdx + 2] = array[i].Z;
resultIdx += 3;
} }
return result; return result;

View File

@ -46,12 +46,16 @@ namespace Nebulix.Rendering
// getting called by "Engine" which currently is in another assembly, meaning I probably need to make this public // 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 // needs to be change for the real engine
/// <summary>
/// Binds the necessary buffers and draws the mesh. Does not use any Shaders
/// </summary>
/// <param name="gl"></param>
public void Render(GL gl) public void Render(GL gl)
{ {
if (regenerate) Generate(gl); if (regenerate) Generate(gl);
gl.BindVertexArray(vao); gl.BindVertexArray(vao);
gl.DrawElements(PrimitiveType.Triangles, (uint)vertices.Length * 3, DrawElementsType.UnsignedInt, 0); gl.DrawElements(PrimitiveType.Triangles, (uint)indices.Length, DrawElementsType.UnsignedInt, 0);
} }
private unsafe void Generate(GL gl) private unsafe void Generate(GL gl)
@ -60,13 +64,13 @@ namespace Nebulix.Rendering
if(vao == 0) if(vao == 0)
vao = gl.CreateVertexArray(); vao = gl.CreateVertexArray();
gl.BindVertexArray(vao);
if(vbo == 0) if(vbo == 0)
vbo = gl.GenBuffer(); vbo = gl.GenBuffer();
if(ebo == 0) if(ebo == 0)
ebo = gl.GenBuffer(); ebo = gl.GenBuffer();
gl.BindVertexArray(vao);
List<float> meshData = new(vertices.Length * 3 + normals.Length * 3); List<float> meshData = new(vertices.Length * 3 + normals.Length * 3);
meshData.AddRange(vertices.ExtractComponents()); meshData.AddRange(vertices.ExtractComponents());
//meshData.AddRange(normals.ExtractComponents()); //meshData.AddRange(normals.ExtractComponents());
@ -77,7 +81,7 @@ namespace Nebulix.Rendering
ReadOnlySpan<nuint> indicesData = new(indices); ReadOnlySpan<nuint> indicesData = new(indices);
gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo); gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ebo);
gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indicesData.Length * sizeof(nuint)), indicesData, BufferUsageARB.StaticDraw); gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indicesData.Length * sizeof(nuint)), indicesData, BufferUsageARB.StaticDraw);
// vertices // vertices
gl.EnableVertexAttribArray(0); gl.EnableVertexAttribArray(0);
gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), null); gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), null);

View File

@ -1,4 +1,6 @@
namespace Engine_silk.NET; using Nebulix;
namespace Engine_silk.NET;
using Engine_silk.NET.Textures; using Engine_silk.NET.Textures;
using Nebulix.InputSystem; using Nebulix.InputSystem;
@ -28,9 +30,9 @@ public static class Program
private static Texture2D _texture; private static Texture2D _texture;
private static Camera _cam; private static Camera _cam;
private static Vector2 _lastMousePosition; private static Vector2 _lastMousePosition;
private static uint _vao, _vbo, _ebo; private static uint _vao, _vbo;
private static Sphere sphere; private static Sphere sphere;
private static Mesh testMesh;
public static void Main(string[] args) public static void Main(string[] args)
{ {
@ -54,7 +56,7 @@ public static class Program
_gl = _window.CreateOpenGL(); _gl = _window.CreateOpenGL();
_gl.ClearColor(Color.CornflowerBlue); _gl.ClearColor(Color.CornflowerBlue);
_gl.Enable(EnableCap.DepthTest); _gl.Enable(EnableCap.DepthTest);
//_gl.PolygonMode(GLEnum.FrontAndBack, GLEnum.Fill); // _gl.PolygonMode(GLEnum.FrontAndBack, GLEnum.Fill);
_cam = new Camera(new(0.0f, 0.0f, 3.0f)); _cam = new Camera(new(0.0f, 0.0f, 3.0f));
IInputContext input = _window.CreateInput(); IInputContext input = _window.CreateInput();
@ -118,37 +120,13 @@ public static class Program
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f
}; };
ReadOnlySpan<float> verticesData = new(vertices); ReadOnlySpan<float> verticesData = new(vertices);
//uint[] indices =
//{
// 0u, 1u, 3u,
// 1u, 2u, 3u,
// 4u, 5u, 7u,
// 5u, 6u, 7u,
// 3u, 2u, 7u,
// 7u, 2u, 6u,
// 0u, 4u, 5u,
// 0u, 5u, 1u,
// 0u, 3u, 7u,
// 0u, 7u, 4u,
// 1u, 6u, 5u,
// 1u, 2u, 6u
//};
_vbo = _gl.GenBuffer(); _vbo = _gl.GenBuffer();
_gl.BindBuffer(BufferTargetARB.ArrayBuffer, _vbo); _gl.BindBuffer(BufferTargetARB.ArrayBuffer, _vbo);
_gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(verticesData.Length * sizeof(float)), verticesData, BufferUsageARB.StaticDraw); _gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(verticesData.Length * sizeof(float)), verticesData, BufferUsageARB.StaticDraw);
//_ebo = _gl.GenBuffer();
//_gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, _ebo);
//fixed (uint* buffer = indices)
// _gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indices.Length * sizeof(uint)), buffer, BufferUsageARB.StaticDraw);
_shader = new Nebulix.Rendering.Shader(_gl, "shader.vert", "shader.frag"); _shader = new Nebulix.Rendering.Shader(_gl, "shader.vert", "shader.frag");
_gl.EnableVertexAttribArray(0); _gl.EnableVertexAttribArray(0);
@ -166,6 +144,24 @@ public static class Program
sphere.CreateSphere(); sphere.CreateSphere();
_sphereShader = _sphereShader =
new Nebulix.Rendering.Shader(_gl, "./Shaders/Sphere/sphere.vert", "./Shaders/Sphere/sphere.frag"); 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) private static void OnUpdate(double deltaTime)
@ -203,24 +199,26 @@ public static class Program
var viewMatrix = _cam.ViewMatrix; var viewMatrix = _cam.ViewMatrix;
var projectionMatrix = Matrix4X4.CreatePerspectiveFieldOfView(Maths.Convert.ToRadians(_cam.Fov), Width / (float)Height, 0.1f, 100.0f); var projectionMatrix = Matrix4X4.CreatePerspectiveFieldOfView(Maths.Convert.ToRadians(_cam.Fov), Width / (float)Height, 0.1f, 100.0f);
// _shader.Use(); _shader.Use();
// _shader.SetMatrix("modelMatrix", modelMatrix); _shader.SetMatrix("modelMatrix", modelMatrix);
// _shader.SetMatrix("projectionMatrix", projectionMatrix); _shader.SetMatrix("projectionMatrix", projectionMatrix);
// _shader.SetMatrix("viewMatrix", viewMatrix); _shader.SetMatrix("viewMatrix", viewMatrix);
//
// _shader.SetInt("tex", 0); _shader.SetInt("tex", 0);
// _texture.Bind(); _texture.Bind();
//
// _gl.BindVertexArray(_vao); // _gl.BindVertexArray(_vao);
// _gl.DrawArrays(PrimitiveType.Triangles, 0, 36); // _gl.DrawArrays(PrimitiveType.Triangles, 0, 36);
// Sphere // Sphere
// modelMatrix = Matrix4x4.CreateTranslation(1, 0, 1); modelMatrix = Matrix4x4.CreateTranslation(0, 0, 0);
_sphereShader.Use(); _sphereShader.Use();
_sphereShader.SetMatrix("modelMatrix", modelMatrix); _sphereShader.SetMatrix("modelMatrix", modelMatrix);
_sphereShader.SetMatrix("projectionMatrix", projectionMatrix); _sphereShader.SetMatrix("projectionMatrix", projectionMatrix);
_sphereShader.SetMatrix("viewMatrix", viewMatrix); _sphereShader.SetMatrix("viewMatrix", viewMatrix);
sphere.RenderSphere(_gl);
// sphere.RenderSphere(_gl);
testMesh.Render(_gl);
// var f = new Face(-Vector3D<float>.UnitX, 10); // var f = new Face(-Vector3D<float>.UnitX, 10);
// f.GetMesh().Render(_gl); // f.GetMesh().Render(_gl);

View File

@ -1,7 +1,8 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPosition; layout (location = 0) in vec3 aPosition;
layout (location = 1) in vec3 normalVector; //layout (location = 1) in vec3 normalVector;
uniform mat4 modelMatrix; uniform mat4 modelMatrix;
uniform mat4 viewMatrix; uniform mat4 viewMatrix;
@ -13,7 +14,6 @@ out vec3 FragPos;
void main() void main()
{ {
vec4 pos = vec4(aPosition, 1.0); vec4 pos = vec4(aPosition, 1.0);
// TODO: calculate the inverse of the model matrix beforehand since "inverse()" is very costly to calculate for every vertex // TODO: calculate the inverse of the model matrix beforehand since "inverse()" is very costly to calculate for every vertex
// Normal = mat3(transpose(inverse(modelMatrix))) * normalVector; // Normal = mat3(transpose(inverse(modelMatrix))) * normalVector;
FragPos = vec3(modelMatrix * pos); FragPos = vec3(modelMatrix * pos);