fix #4 Sphere can be rendered with normals now. Calculation fo normals is however not working
All checks were successful
Gitea Actions Demo / Scan the project (push) Successful in 17s

This commit is contained in:
2024-01-04 21:19:55 +01:00
parent d5f3a34a59
commit 551ae51b45
7 changed files with 76 additions and 19 deletions

View File

@ -14,6 +14,7 @@ public enum MovementDirection
public class Camera
{
public Vector3D<float> Position => _position;
private readonly Vector3D<float> _worldUp;
private Vector3D<float> _position;
private Vector3D<float> _front;

View File

@ -29,7 +29,7 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Shaders\Sphere\sphere.frag">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Shaders\Sphere\sphere.vert">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

View File

@ -171,7 +171,7 @@ public static class Program
}
}
private static unsafe void OnRender(double deltaTime)
private static void OnRender(double deltaTime)
{
_gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
@ -198,6 +198,13 @@ public static class Program
_sphereShader.SetMatrix("modelMatrix", modelMatrix);
_sphereShader.SetMatrix("projectionMatrix", projectionMatrix);
_sphereShader.SetMatrix("viewMatrix", viewMatrix);
_sphereShader.SetVector("viewPosition", _cam.Position);
_sphereShader.SetVector("dirLight.direction", new Vector3D<float>(-0.2f, -1.0f, -0.3f));
_sphereShader.SetVector("dirLight.ambient", new Vector3D<float>(0.1f));
_sphereShader.SetVector("dirLight.diffuse", new Vector3D<float>(1f));
_sphereShader.SetVector("dirLight.specular", new Vector3D<float>(0.5f));
sphere.RenderSphere(_gl);
}

View File

@ -3,10 +3,41 @@
out vec4 FragColour;
in vec3 FragPos;
//in vec3 Normal;
in vec3 Normal;
struct DirectionalLight {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
uniform vec3 viewPosition;
uniform DirectionalLight dirLight;
vec3 CalculateDirectionalLight(DirectionalLight light, vec3 normal, vec3 viewDir)
{
vec3 lightDir = normalize(-light.direction);
float diff = max(dot(normal, lightDir), 0.0);
vec3 reflectionDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectionDir), 0.0), 32);
vec3 ambient = light.ambient;// * vec3(texture(material.diffuseMap, TexCoords));
vec3 diffuse = light.diffuse * diff;// * vec3(texture(material.diffuseMap, TexCoords));
vec3 specular = light.specular * spec;// * vec3(texture(material.specularMap, TexCoords));
return ambient + diffuse + specular;
}
void main()
{
vec3 col = vec3(1.0, 0.5, 0.2) * FragPos;
vec3 normal = normalize(Normal);
vec3 viewDir = normalize(viewPosition - FragPos);
// vec3 col = vec3(1.0, 0.5, 0.2) * FragPos;
vec3 col = CalculateDirectionalLight(dirLight, normal, viewDir);
FragColour = vec4(col, 1);
}

View File

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