added ability to render a mesh
This commit is contained in:
45
src/EngineSharp.Core/EngineSharp.Core/Rendering/Mesh.cs
Normal file
45
src/EngineSharp.Core/EngineSharp.Core/Rendering/Mesh.cs
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
using Silk.NET.Maths;
|
||||||
|
|
||||||
|
namespace EngineSharp.Core.Rendering;
|
||||||
|
|
||||||
|
public class Mesh
|
||||||
|
{
|
||||||
|
public Vector3D<float>[] Vertices { get; set; }
|
||||||
|
public Vector3D<float>[] Normals { get; set; }
|
||||||
|
public uint[] Indices { get; set; }
|
||||||
|
|
||||||
|
// TODO: Add uv textures but make them nullable; MeshRenderer should then only send uv data to shader if specified
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Overwrites the values of <see cref="Normals"/>
|
||||||
|
/// </summary>
|
||||||
|
public void CalculateNormals()
|
||||||
|
{
|
||||||
|
if (Normals.Length != Vertices.Length)
|
||||||
|
{
|
||||||
|
Normals = new Vector3D<float>[Vertices.Length];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < Indices.Length; i += 3)
|
||||||
|
{
|
||||||
|
var i0 = Indices[i];
|
||||||
|
var i1 = Indices[i+1];
|
||||||
|
var i2 = Indices[i+2];
|
||||||
|
var v0 = Vertices[i0];
|
||||||
|
var v1 = Vertices[i1];
|
||||||
|
var v2 = Vertices[i2];
|
||||||
|
|
||||||
|
var normal = Vector3D.Cross(v1-v0, v2-v0);
|
||||||
|
// Commenting this out, will result in the normals being weighted based on the triangle area
|
||||||
|
normal = Vector3D.Normalize(normal);
|
||||||
|
Normals[i0] += normal;
|
||||||
|
Normals[i1] += normal;
|
||||||
|
Normals[i2] += normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < Normals.Length; i++)
|
||||||
|
{
|
||||||
|
Normals[i] = Vector3D.Normalize(Normals[i]); // smoothing for shared vertices
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
using Silk.NET.OpenGL;
|
||||||
|
|
||||||
|
namespace EngineSharp.Core.Rendering;
|
||||||
|
|
||||||
|
public class MeshRenderer
|
||||||
|
{
|
||||||
|
public Mesh Mesh { get; set; }
|
||||||
|
|
||||||
|
private uint vao, vbo, ebo;
|
||||||
|
|
||||||
|
internal void Render(GL gl)
|
||||||
|
{
|
||||||
|
GenerateRenderableMesh(gl);
|
||||||
|
gl.BindVertexArray(vao);
|
||||||
|
gl.DrawElements(PrimitiveType.Triangles, (uint)Mesh.Indices.Length, DrawElementsType.UnsignedInt, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateRenderableMesh(GL gl)
|
||||||
|
{
|
||||||
|
if(vao == 0) { vao = gl.CreateVertexArray(); }
|
||||||
|
gl.BindVertexArray(vao);
|
||||||
|
|
||||||
|
if(vbo == 0) { vbo = gl.GenBuffer(); }
|
||||||
|
if(ebo == 0) { ebo = gl.GenBuffer(); }
|
||||||
|
|
||||||
|
var meshData = new float[Mesh.Vertices.Length * 3 + Mesh.Indices.Length * 3];
|
||||||
|
for (int i = 0, insert = 0; i < Mesh.Vertices.Length; i++, insert += 6)
|
||||||
|
{
|
||||||
|
meshData[insert] = Mesh.Vertices[i].X;
|
||||||
|
meshData[insert + 1] = Mesh.Vertices[i].Y;
|
||||||
|
meshData[insert + 2] = Mesh.Vertices[i].Z;
|
||||||
|
|
||||||
|
meshData[insert + 3] = Mesh.Normals[i].X;
|
||||||
|
meshData[insert + 4] = Mesh.Normals[i].Y;
|
||||||
|
meshData[insert + 5] = Mesh.Normals[i].Z;
|
||||||
|
}
|
||||||
|
|
||||||
|
gl.BindBuffer(BufferTargetARB.ArrayBuffer, vbo);
|
||||||
|
var meshDataSpan = new ReadOnlySpan<float>(meshData);
|
||||||
|
gl.BufferData(BufferTargetARB.ArrayBuffer, meshDataSpan, BufferUsageARB.StaticDraw);
|
||||||
|
|
||||||
|
gl.BindBuffer(BufferTargetARB.ArrayBuffer, ebo);
|
||||||
|
var indicesSpan = new ReadOnlySpan<uint>(Mesh.Indices);
|
||||||
|
gl.BufferData(BufferTargetARB.ArrayBuffer, indicesSpan, BufferUsageARB.StaticDraw);
|
||||||
|
|
||||||
|
// vertices
|
||||||
|
gl.EnableVertexAttribArray(0);
|
||||||
|
gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), 0);
|
||||||
|
// normals
|
||||||
|
gl.EnableVertexAttribArray(1);
|
||||||
|
gl.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), 3*sizeof(float));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user