Tried rendering the sphere without success
All checks were successful
Gitea Actions Demo / Scan the project (push) Successful in 29s

This commit is contained in:
2024-01-02 16:02:24 +01:00
parent 8470304859
commit cf14c9a79e
9 changed files with 135 additions and 53 deletions

View File

@ -28,6 +28,12 @@
<None Update="shader.vert">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Shaders\Sphere\sphere.frag">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Shaders\Sphere\sphere.vert">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>

View File

@ -24,12 +24,13 @@ public static class Program
private static IWindow _window;
private static GL _gl;
private static Nebulix.Rendering.Shader _shader;
private static Nebulix.Rendering.Shader _shader, _sphereShader;
private static Texture2D _texture;
private static Camera _cam;
private static Vector2 _lastMousePosition;
private static uint _vao, _vbo, _ebo;
private static Sphere sphere;
public static void Main(string[] args)
{
@ -46,6 +47,7 @@ public static class Program
_window.Run();
}
private static unsafe void OnLoad()
{
@ -157,6 +159,13 @@ public static class Program
_gl.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, 8 * sizeof(float), (void*)(6 * sizeof(float)));
_texture = new Texture2D(_gl, "images/container.png", ImageFormat.RGBA);
// Sphere
sphere = new Sphere(10);
sphere.CreateSphere();
_sphereShader =
new Nebulix.Rendering.Shader(_gl, "./Shaders/Sphere/sphere.vert", "./Shaders/Sphere/sphere.frag");
}
private static void OnUpdate(double deltaTime)
@ -193,18 +202,28 @@ public static class Program
var modelMatrix = Matrix4x4.CreateRotationY(Maths.Convert.ToRadians(difference)) * Matrix4x4.CreateRotationX(Maths.Convert.ToRadians(difference));
var viewMatrix = _cam.ViewMatrix;
var projectionMatrix = Matrix4X4.CreatePerspectiveFieldOfView(Maths.Convert.ToRadians(_cam.Fov), Width / (float)Height, 0.1f, 100.0f);
// _shader.Use();
// _shader.SetMatrix("modelMatrix", modelMatrix);
// _shader.SetMatrix("projectionMatrix", projectionMatrix);
// _shader.SetMatrix("viewMatrix", viewMatrix);
//
// _shader.SetInt("tex", 0);
// _texture.Bind();
//
// _gl.BindVertexArray(_vao);
// _gl.DrawArrays(PrimitiveType.Triangles, 0, 36);
_gl.BindVertexArray(_vao);
// Sphere
// modelMatrix = Matrix4x4.CreateTranslation(1, 0, 1);
_sphereShader.Use();
_sphereShader.SetMatrix("modelMatrix", modelMatrix);
_sphereShader.SetMatrix("projectionMatrix", projectionMatrix);
_sphereShader.SetMatrix("viewMatrix", viewMatrix);
sphere.RenderSphere(_gl);
_shader.Use();
_shader.SetMatrix("modelMatrix", modelMatrix);
_shader.SetMatrix("projectionMatrix", projectionMatrix);
_shader.SetMatrix("viewMatrix", viewMatrix);
_shader.SetInt("tex", 0);
_texture.Bind();
_gl.DrawArrays(PrimitiveType.Triangles, 0, 36);
// var f = new Face(-Vector3D<float>.UnitX, 10);
// f.GetMesh().Render(_gl);
}
private static void OnKeyDown(IKeyboard keyboard, Key key, int keyCode)

View File

@ -0,0 +1,15 @@
#version 330 core
out vec4 colour;
in vec3 FragPos;
//in vec3 Normal;
void main()
{
vec3 col = vec3(1.0, 0.5, 0.2) * FragPos;
//vec3 col = texture(tex, TexCoords);
//colour = vec4(texture(tex, TexCoords).rgb, 1.0);
// colour = vec4(col, 1);
colour = vec4(1);
}

View File

@ -0,0 +1,21 @@
#version 330 core
layout (location = 0) in vec3 aPosition;
layout (location = 1) in vec3 normalVector;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
out vec3 FragPos;
//out vec3 Normal;
void main()
{
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
// Normal = mat3(transpose(inverse(modelMatrix))) * normalVector;
FragPos = vec3(modelMatrix * pos);
gl_Position = projectionMatrix * viewMatrix * modelMatrix * pos;
}

View File

@ -12,52 +12,58 @@ namespace Engine_silk.NET
public void CreateSphere()
{
Vector3D<float>[] directions =
{
[
Vector3D<float>.UnitZ, -Vector3D<float>.UnitZ,
Vector3D<float>.UnitY, -Vector3D<float>.UnitY,
Vector3D<float>.UnitX, -Vector3D<float>.UnitX
};
];
for (int i = 0; i < sphereFaces.Length; i++)
{
// TODO: Refactor so that a face gets a mesh it can only change the contents of (should improve performance)
sphereFaces[i] = new Face(directions[i], resolution);
sphereFaces[i] = new Face(directions[i], new Mesh(), resolution);
sphereFaces[i].GenerateMesh();
}
}
/// <summary>
/// Renders the mesh. No Shader is specified and <see cref="CreateSphere"/> needs to be called beforehand
/// </summary>
public void RenderSphere(GL gl) // Will not be needed
{
// Use default shader etc. to render the sphere
for (int i = 0; i < 6; i++)
{
Mesh m = sphereFaces[i].GetMesh();
m.Use(gl);
sphereFaces[i].Mesh.Render(gl);
}
}
}
internal record struct Face
{
public Mesh Mesh => _mesh;
private readonly Mesh _mesh;
private readonly Vector3D<float> _localX;
private readonly Vector3D<float> _localY;
private readonly Vector3D<float> _localUp;
private readonly uint _resolution;
public Face(Vector3D<float> localUp, uint resolution)
public Face(Vector3D<float> localUp, Mesh mesh, uint resolution)
{
_mesh = mesh;
_localX = new(localUp.Y, localUp.Z, localUp.X);
_localY = Vector3D.Cross(localUp, _localX);
_localUp = localUp;
_resolution = resolution;
}
internal Mesh GetMesh()
internal void GenerateMesh()
{
var vertices = new Vector3D<float>[_resolution * _resolution];
// _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
nuint[] indices = new nuint[(_resolution - 1) * (_resolution - 1) * 6];
var indices = new nuint[(_resolution - 1) * (_resolution - 1) * 6];
int triangleIndex = 0;
uint i;
@ -86,11 +92,10 @@ namespace Engine_silk.NET
}
}
Mesh m = new Mesh();
m.Vertices = vertices.ExtractComponents();
m.Indices = indices;
return m;
_mesh.Clear();
_mesh.Vertices = vertices;
_mesh.Indices = indices;
_mesh.CalculateNormals();
}
}
}