Mesh can now be rendered and therefore also the sphere
All checks were successful
Gitea Actions Demo / Scan the project (push) Successful in 17s
All checks were successful
Gitea Actions Demo / Scan the project (push) Successful in 17s
This commit is contained in:
parent
cc3493627d
commit
d5f3a34a59
|
@ -6,13 +6,13 @@ namespace Nebulix.Rendering
|
||||||
public sealed class Mesh
|
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; } }
|
||||||
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 uint vao = 0, vbo = 0, ebo = 0;
|
||||||
private bool regenerate = true;
|
private bool regenerate = true;
|
||||||
|
|
||||||
private Vector3D<float>[] vertices = [];
|
private Vector3D<float>[] vertices = [];
|
||||||
private nuint[] indices = [];
|
private uint[] indices = [];
|
||||||
private Vector3D<float>[] normals = [];
|
private Vector3D<float>[] normals = [];
|
||||||
|
|
||||||
public void Clear()
|
public void Clear()
|
||||||
|
@ -50,12 +50,12 @@ namespace Nebulix.Rendering
|
||||||
/// Binds the necessary buffers and draws the mesh. Does not use any Shaders
|
/// Binds the necessary buffers and draws the mesh. Does not use any Shaders
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="gl"></param>
|
/// <param name="gl"></param>
|
||||||
public void Render(GL gl)
|
public unsafe void Render(GL gl)
|
||||||
{
|
{
|
||||||
if (regenerate) Generate(gl);
|
if (regenerate) Generate(gl);
|
||||||
|
|
||||||
gl.BindVertexArray(vao);
|
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)
|
private unsafe void Generate(GL gl)
|
||||||
|
@ -71,16 +71,20 @@ namespace Nebulix.Rendering
|
||||||
if(ebo == 0)
|
if(ebo == 0)
|
||||||
ebo = gl.GenBuffer();
|
ebo = gl.GenBuffer();
|
||||||
|
|
||||||
List<float> meshData = new(vertices.Length * 3 + normals.Length * 3);
|
float[] meshData = new float[vertices.Length * 3 + normals.Length * 3];
|
||||||
meshData.AddRange(vertices.ExtractComponents());
|
vertices.ExtractComponents().CopyTo(meshData, 0);
|
||||||
//meshData.AddRange(normals.ExtractComponents());
|
//meshData.AddRange(normals.ExtractComponents());
|
||||||
ReadOnlySpan<float> data = new(meshData.ToArray());
|
|
||||||
gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo);
|
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.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
|
// vertices
|
||||||
gl.EnableVertexAttribArray(0);
|
gl.EnableVertexAttribArray(0);
|
||||||
|
|
|
@ -32,7 +32,6 @@ public static class Program
|
||||||
private static Vector2 _lastMousePosition;
|
private static Vector2 _lastMousePosition;
|
||||||
private static uint _vao, _vbo;
|
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)
|
||||||
{
|
{
|
||||||
|
@ -144,24 +143,6 @@ 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)
|
||||||
|
@ -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);
|
_gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
|
||||||
|
|
||||||
|
@ -207,21 +188,18 @@ public static class Program
|
||||||
_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(0, 0, 0);
|
modelMatrix = Matrix4x4.CreateTranslation(1, 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);
|
|
||||||
// f.GetMesh().Render(_gl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void OnKeyDown(IKeyboard keyboard, Key key, int keyCode)
|
private static void OnKeyDown(IKeyboard keyboard, Key key, int keyCode)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#version 330 core
|
#version 330 core
|
||||||
|
|
||||||
out vec4 colour;
|
out vec4 FragColour;
|
||||||
|
|
||||||
in vec3 FragPos;
|
in vec3 FragPos;
|
||||||
//in vec3 Normal;
|
//in vec3 Normal;
|
||||||
|
@ -8,8 +8,5 @@ in vec3 FragPos;
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
vec3 col = vec3(1.0, 0.5, 0.2) * FragPos;
|
vec3 col = vec3(1.0, 0.5, 0.2) * FragPos;
|
||||||
//vec3 col = texture(tex, TexCoords);
|
FragColour = vec4(col, 1);
|
||||||
//colour = vec4(texture(tex, TexCoords).rgb, 1.0);
|
|
||||||
// colour = vec4(col, 1);
|
|
||||||
colour = vec4(1);
|
|
||||||
}
|
}
|
|
@ -6,9 +6,19 @@ using Silk.NET.OpenGL;
|
||||||
namespace Engine_silk.NET
|
namespace Engine_silk.NET
|
||||||
{
|
{
|
||||||
// also maybe make an ISphere and call this class "CubeSphere"?
|
// 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 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()
|
public void CreateSphere()
|
||||||
{
|
{
|
||||||
Vector3D<float>[] directions =
|
Vector3D<float>[] directions =
|
||||||
|
@ -63,7 +73,7 @@ namespace Engine_silk.NET
|
||||||
var vertices = new Vector3D<float>[_resolution * _resolution];
|
var vertices = new Vector3D<float>[_resolution * _resolution];
|
||||||
// _resolution - 1 because the vertices index starts at 0
|
// _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
|
// * 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;
|
int triangleIndex = 0;
|
||||||
|
|
||||||
uint i;
|
uint i;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user