Made normal calculations to work and added diffuse lighting (TODO: get rid of seams)
All checks were successful
Gitea Actions Demo / Scan the project (push) Successful in 51s

This commit is contained in:
Daniel 2024-01-23 12:22:37 +01:00
parent 8f1fadd473
commit 5de28c7d7c
3 changed files with 56 additions and 12 deletions

View File

@ -30,18 +30,18 @@ namespace Nebulix.Rendering
// normals[j] = Vector3D<float>.Zero;
// }
return;
// return;
for (int i = 0; i < indices.Length; i += 3)
{
Vector3D<float> a = vertices[indices[i] - 1];
Vector3D<float> b = vertices[indices[i + 1] - 1];
Vector3D<float> c = vertices[indices[i + 2] - 1];
Vector3D<float> a = vertices[indices[i]];
Vector3D<float> b = vertices[indices[i + 1]];
Vector3D<float> c = vertices[indices[i + 2]];
Vector3D<float> normal = Vector3D.Cross(b-a, c-a);
normal = Vector3D.Normalize(normal);
normals[indices[i] - 1] = normal;
normals[indices[i + 1] - 1] = normal;
normals[indices[i + 2] - 1] = normal;
normals[indices[i]] = normal;
normals[indices[i + 1]] = normal;
normals[indices[i + 2]] = normal;
}
}

View File

@ -19,6 +19,8 @@ using System.Numerics;
// Next to do will be cleaning this up by creating a shader class and maybe even an engine class
// IEngine -> IEngine.Create(WindowOptions) -> return OpenGLEngine/VulkanEngine maybe?
using Vector3 = Silk.NET.Maths.Vector3D<float>;
public static class Program
{
private const int Width = 800;
@ -201,10 +203,20 @@ public static class Program
_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));
_sphereShader.SetVector("dirLight.direction", new Vector3(-0.2f, -1.0f, -0.3f));
_sphereShader.SetVector("dirLight.ambient", new Vector3(0.1f));
_sphereShader.SetVector("dirLight.diffuse", new Vector3(1f));
_sphereShader.SetVector("dirLight.specular", new Vector3(0.5f));
_sphereShader.SetVector("pointLight.position", new Vector3(0.0f, 2.0f, 0.0f));
_sphereShader.SetFloat("pointLight.constant", 1.0f);
_sphereShader.SetFloat("pointLight.linear", 0.09f);
_sphereShader.SetFloat("pointLight.quadratic", 0.032f);
_sphereShader.SetVector("pointLight.ambient", new Vector3(1.0f) * 0.1f);
_sphereShader.SetVector("pointLight.diffuse", new Vector3(1.0f));
_sphereShader.SetVector("pointLight.specular", new Vector3(1.0f));
sphere.RenderSphere(_gl);
}

View File

@ -12,9 +12,21 @@ struct DirectionalLight {
vec3 diffuse;
vec3 specular;
};
struct PointLight {
vec3 position;
float constant;
float linear;
float quadratic;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
uniform vec3 viewPosition;
uniform DirectionalLight dirLight;
uniform PointLight pointLight;
vec3 CalculateDirectionalLight(DirectionalLight light, vec3 normal, vec3 viewDir)
{
@ -31,6 +43,25 @@ vec3 CalculateDirectionalLight(DirectionalLight light, vec3 normal, vec3 viewDir
return ambient + diffuse + specular;
}
vec3 CalculatePointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
vec3 lightDir = normalize(light.position - fragPos);
float diff = max(dot(normal, lightDir), 0.0);
vec3 reflectionDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectionDir), 0.0), 32);
float distance = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
vec3 ambientLight = light.ambient * attenuation;// * texture(material.diffuseMap, TexCoords).rgb;
vec3 diffuseLight = light.diffuse * diff * attenuation;// * texture(material.diffuseMap, TexCoords).rgb;
vec3 specularLight = light.specular * spec * attenuation;// * texture(material.specularMap, TexCoords).rgb;
return ambientLight + diffuseLight + specularLight;
}
void main()
{
@ -38,6 +69,7 @@ void main()
vec3 viewDir = normalize(viewPosition - FragPos);
// vec3 col = vec3(1.0, 0.5, 0.2) * FragPos;
vec3 col = CalculateDirectionalLight(dirLight, normal, viewDir);
vec3 col = vec3(0);//CalculateDirectionalLight(dirLight, normal, viewDir);
col += CalculatePointLight(pointLight, normal, FragPos, viewDir);
FragColour = vec4(col, 1);
}