Updated structure and took first steps for a better architecture
Some checks failed
Gitea Actions Demo / Scan the project (push) Failing after 5s
Some checks failed
Gitea Actions Demo / Scan the project (push) Failing after 5s
This commit is contained in:
@ -32,6 +32,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="images\" />
|
||||
<Folder Include="Shaders\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
using Engine_silk.NET.Textures;
|
||||
using Nebulix.InputSystem;
|
||||
using Nebulix.Rendering;
|
||||
using Silk.NET.Input;
|
||||
using Silk.NET.Maths;
|
||||
using Silk.NET.OpenGL;
|
||||
@ -23,7 +24,7 @@ public static class Program
|
||||
|
||||
private static IWindow _window;
|
||||
private static GL _gl;
|
||||
private static Shaders.Shader _shader;
|
||||
private static Nebulix.Rendering.Shader _shader;
|
||||
private static Texture2D _texture;
|
||||
private static Camera _cam;
|
||||
private static Vector2 _lastMousePosition;
|
||||
@ -147,7 +148,7 @@ public static class Program
|
||||
//fixed (uint* buffer = indices)
|
||||
// _gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(indices.Length * sizeof(uint)), buffer, BufferUsageARB.StaticDraw);
|
||||
|
||||
_shader = new Shaders.Shader(_gl, "shader.vert", "shader.frag");
|
||||
_shader = new Nebulix.Rendering.Shader(_gl, "shader.vert", "shader.frag");
|
||||
|
||||
_gl.EnableVertexAttribArray(0);
|
||||
_gl.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 8 * sizeof(float), (void*)0);
|
||||
|
@ -1,94 +0,0 @@
|
||||
using Silk.NET.Maths;
|
||||
using Silk.NET.OpenGL;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Engine_silk.NET.Shaders;
|
||||
|
||||
public class Shader
|
||||
{
|
||||
private readonly GL _glContext;
|
||||
private readonly uint _shaderProgramId;
|
||||
|
||||
public Shader(GL openGLContext, string pathToVertexShader, string pathToFragmentShader)
|
||||
{
|
||||
_glContext = openGLContext;
|
||||
var vertexCode = File.ReadAllText(pathToVertexShader);
|
||||
var fragmentCode = File.ReadAllText(pathToFragmentShader);
|
||||
|
||||
var vertexShader = CompileShader(vertexCode, ShaderType.VertexShader);
|
||||
var fragmentShader = CompileShader(fragmentCode, ShaderType.FragmentShader);
|
||||
|
||||
_shaderProgramId = CreateProgram(vertexShader, fragmentShader);
|
||||
}
|
||||
|
||||
public void Use() { _glContext.UseProgram(_shaderProgramId); }
|
||||
|
||||
#region Set uniforms
|
||||
public void SetInt(string name, int value)
|
||||
{
|
||||
_glContext.Uniform1(_glContext.GetUniformLocation(_shaderProgramId, name), value);
|
||||
}
|
||||
|
||||
public void SetFloat(string name, float value)
|
||||
{
|
||||
_glContext.Uniform1(_glContext.GetUniformLocation(_shaderProgramId, name), value);
|
||||
}
|
||||
|
||||
public unsafe void SetMatrix(string name, Matrix4x4 matrix)
|
||||
{
|
||||
_glContext.UniformMatrix4(_glContext.GetUniformLocation(_shaderProgramId, name), 1, false, (float*) &matrix);
|
||||
}
|
||||
public unsafe void SetMatrix(string name, Matrix4X4<float> matrix)
|
||||
{
|
||||
_glContext.UniformMatrix4(_glContext.GetUniformLocation(_shaderProgramId, name), 1, false, (float*) &matrix);
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Compiles the given shadercode.
|
||||
/// </summary>
|
||||
/// <param name="shaderCode">The shadercode</param>
|
||||
/// <param name="shaderType">The type of shader to compile</param>
|
||||
/// <returns>Returns the id of the compiled shader.</returns>
|
||||
/// <exception cref="ShaderCompileException"></exception>
|
||||
protected uint CompileShader(string shaderCode, ShaderType shaderType)
|
||||
{
|
||||
uint shader = _glContext.CreateShader(shaderType);
|
||||
_glContext.ShaderSource(shader, shaderCode);
|
||||
_glContext.CompileShader(shader);
|
||||
|
||||
_glContext.GetShader(shader, ShaderParameterName.CompileStatus, out int status);
|
||||
|
||||
if (status != (int)GLEnum.True)
|
||||
throw new ShaderCompileException(shaderType, $"Failed to compile shader with message: \n {_glContext.GetShaderInfoLog(shader)}");
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a shader program and links the vertex and fragment shader together.
|
||||
/// </summary>
|
||||
/// <returns>Returns the id of the created shader program</returns>
|
||||
/// <exception cref="ShaderLinkException"></exception>
|
||||
protected uint CreateProgram(uint vertexShader, uint fragmentShader)
|
||||
{
|
||||
uint program = _glContext.CreateProgram();
|
||||
_glContext.AttachShader(program, vertexShader);
|
||||
_glContext.AttachShader(program, fragmentShader);
|
||||
_glContext.LinkProgram(program);
|
||||
|
||||
_glContext.GetProgram(program, ProgramPropertyARB.LinkStatus, out int lStatus);
|
||||
if (lStatus != (int)GLEnum.True)
|
||||
throw new ShaderLinkException("Error occured while trying to link a shader with message: \n" + _glContext.GetProgramInfoLog(program));
|
||||
|
||||
// should be done right after compiling and linking
|
||||
_glContext.DetachShader(program, vertexShader);
|
||||
_glContext.DetachShader(program, fragmentShader);
|
||||
_glContext.DeleteShader(vertexShader);
|
||||
_glContext.DeleteShader(fragmentShader);
|
||||
|
||||
return program;
|
||||
}
|
||||
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
using Silk.NET.OpenGL;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Engine_silk.NET.Shaders;
|
||||
|
||||
|
||||
[Serializable]
|
||||
public class ShaderCompileException : Exception
|
||||
{
|
||||
protected ShaderType _shaderType;
|
||||
|
||||
public ShaderCompileException(ShaderType shaderType) : base("Unable to compile shader.") { _shaderType = shaderType; }
|
||||
public ShaderCompileException(ShaderType shaderType, string message) : base(message) { _shaderType = shaderType; }
|
||||
public ShaderCompileException(ShaderType shaderType, string message, Exception inner) : base(message, inner) { _shaderType = shaderType; }
|
||||
protected ShaderCompileException(
|
||||
System.Runtime.Serialization.SerializationInfo info,
|
||||
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"Type of Shader: '{_shaderType}'" + "\n" + Message;
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Engine_silk.NET.Shaders;
|
||||
|
||||
|
||||
[Serializable]
|
||||
public class ShaderLinkException : Exception
|
||||
{
|
||||
public ShaderLinkException() : base("Error occured while trying to link a shader.") { }
|
||||
public ShaderLinkException(string message) : base(message) { }
|
||||
public ShaderLinkException(string message, Exception inner) : base(message, inner) { }
|
||||
protected ShaderLinkException(
|
||||
System.Runtime.Serialization.SerializationInfo info,
|
||||
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using Silk.NET.Maths;
|
||||
using Nebulix.Rendering;
|
||||
using Silk.NET.Maths;
|
||||
using Silk.NET.OpenGL;
|
||||
|
||||
namespace Engine_silk.NET
|
||||
@ -43,35 +44,40 @@ namespace Engine_silk.NET
|
||||
return new ReadOnlySpan<float>(finalArray);
|
||||
}
|
||||
|
||||
/// <param name="normal">The unit vector in which the side should point. E.g. Vector.Up</param>
|
||||
private (float[], float[]) GetSide(Vector3D<float> normal)
|
||||
|
||||
[Obsolete("Use Sphere.GetFace(Vector3D<float>) instead")]
|
||||
private (float[], float[]) GetSide(Vector3D<float> localUp)
|
||||
{
|
||||
//float upperBound = radius, lowerBound = -radius;
|
||||
|
||||
float stepSize = 2f / resolution;
|
||||
|
||||
List<float> vertices = new((int)(3 * resolution * resolution * 2)); // resolution * resolution == number of rows/columns; 3 * ... == each vertex has 3 positions; 2 * ... == vertex also needs normal
|
||||
|
||||
Vector3D<float> position = normal;
|
||||
for (int row = 0; row <= resolution; row++)
|
||||
Vector3D<float> position = localUp;
|
||||
for (uint y = 0; y <= resolution; y++)
|
||||
{
|
||||
for (int col = 0; col <= resolution; col++)
|
||||
for (uint x = 0; x <= resolution; x++)
|
||||
{
|
||||
if (normal.X != 0)
|
||||
uint i = x + y * resolution;
|
||||
Vector2D<float> percent = new Vector2D<float>(x, y) / (resolution - 1);
|
||||
//Vector3D<float> pointOnUnitCube = localUp + (percent.X - 0.5f) * stepSize *
|
||||
|
||||
if (localUp.X != 0)
|
||||
{
|
||||
position.Y = row * stepSize - 1;
|
||||
position.Z = col * stepSize - 1;
|
||||
position.Y = x * stepSize - 1;
|
||||
position.Z = y * stepSize - 1;
|
||||
}
|
||||
else if (normal.Y != 0)
|
||||
else if (localUp.Y != 0)
|
||||
{
|
||||
position.X = row * stepSize - 1;
|
||||
position.Z = col * stepSize - 1;
|
||||
position.X = x * stepSize - 1;
|
||||
position.Z = y * stepSize - 1;
|
||||
}
|
||||
else if (normal.Z != 0)
|
||||
else if (localUp.Z != 0)
|
||||
{
|
||||
position.X = row * stepSize - 1;
|
||||
position.Y = col * stepSize - 1;
|
||||
position.X = x * stepSize - 1;
|
||||
position.Y = y * stepSize - 1;
|
||||
}
|
||||
vertices.AddRange([position.X, position.Y, position.Z, normal.X, normal.Y, normal.Z]);
|
||||
vertices.AddRange([position.X, position.Y, position.Z, localUp.X, localUp.Y, localUp.Z]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -97,26 +103,21 @@ namespace Engine_silk.NET
|
||||
return (vertices.ToArray(), []);
|
||||
}
|
||||
|
||||
// top left origin, stepsizeX, stepsizeY (maybe index of first vertex?)
|
||||
// https://youtu.be/QN39W020LqU?si=a66D1Lnic1vNaC6l&t=89
|
||||
private Face Quad(uint resolution, uint row, uint column)
|
||||
// https://github.com/SebLague/Procedural-Planets/blob/master/Procedural%20Planet%20E01/Assets/TerrainFace.cs
|
||||
/// <param name="localUp">The unit vector in which the side should point. E.g. Vector.UnitX</param>
|
||||
public Mesh GetFace(Vector3D<float> localUp)
|
||||
{
|
||||
// TODO: change coordinates according to the direction the quad should be looking
|
||||
// https://catlikecoding.com/unity/tutorials/procedural-meshes/cube-sphere/
|
||||
float[] vertices = [
|
||||
1.0f, 1.0f, 0.0f, // top right
|
||||
1.0f, -1.0f, 0.0f, // bottom right
|
||||
-1.0f, -1.0f, 0.0f, // bottom left
|
||||
-1.0f, 1.0f, 0.0f // top left
|
||||
];
|
||||
uint[] indices = [
|
||||
0, 1, 3, // first triangle
|
||||
1, 2, 3 // second triangle
|
||||
];
|
||||
|
||||
return new Face(vertices, indices);
|
||||
// this all probably needs to be rewritten to use the "Face" record below and split the stuff better
|
||||
return new Mesh();
|
||||
}
|
||||
}
|
||||
|
||||
internal record struct Face(float[] Vertices, uint[] Indices);
|
||||
internal record struct Face
|
||||
{
|
||||
public Face()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user