diff --git a/Nebulix/Rendering/Mesh.cs b/Nebulix/Rendering/Mesh.cs index f9a29ad..66f22e1 100644 --- a/Nebulix/Rendering/Mesh.cs +++ b/Nebulix/Rendering/Mesh.cs @@ -30,18 +30,18 @@ namespace Nebulix.Rendering // normals[j] = Vector3D.Zero; // } - return; + // return; for (int i = 0; i < indices.Length; i += 3) { - Vector3D a = vertices[indices[i] - 1]; - Vector3D b = vertices[indices[i + 1] - 1]; - Vector3D c = vertices[indices[i + 2] - 1]; + Vector3D a = vertices[indices[i]]; + Vector3D b = vertices[indices[i + 1]]; + Vector3D c = vertices[indices[i + 2]]; Vector3D 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; } } diff --git a/src/Program.cs b/src/Program.cs index d600cce..767e749 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -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; + public static class Program { private const int Width = 800; @@ -201,11 +203,21 @@ public static class Program _sphereShader.SetVector("viewPosition", _cam.Position); - _sphereShader.SetVector("dirLight.direction", new Vector3D(-0.2f, -1.0f, -0.3f)); - _sphereShader.SetVector("dirLight.ambient", new Vector3D(0.1f)); - _sphereShader.SetVector("dirLight.diffuse", new Vector3D(1f)); - _sphereShader.SetVector("dirLight.specular", new Vector3D(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); } diff --git a/src/Shaders/Sphere/sphere.frag b/src/Shaders/Sphere/sphere.frag index b96d17c..7e7f075 100644 --- a/src/Shaders/Sphere/sphere.frag +++ b/src/Shaders/Sphere/sphere.frag @@ -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); } \ No newline at end of file