diff options
author | talha <talha@talhaamir.xyz> | 2024-03-13 22:54:43 +0500 |
---|---|---|
committer | talha <talha@talhaamir.xyz> | 2024-03-13 22:54:43 +0500 |
commit | 2a1557aa2b4d406c92703f60f2d34283fed5fcfb (patch) | |
tree | ed0690693f97a21a8d8495d9e7154cc490f23a05 /source/shaders | |
parent | 20f2625f9832504661774b8a9f6661188fd97716 (diff) |
Did some work on materials, refactored a few things
Diffstat (limited to 'source/shaders')
-rw-r--r-- | source/shaders/light_source.fs.glsl | 5 | ||||
-rw-r--r-- | source/shaders/light_source.vs.glsl | 10 | ||||
-rw-r--r-- | source/shaders/light_subject.fs.glsl | 46 | ||||
-rw-r--r-- | source/shaders/light_subject.vs.glsl | 18 |
4 files changed, 79 insertions, 0 deletions
diff --git a/source/shaders/light_source.fs.glsl b/source/shaders/light_source.fs.glsl new file mode 100644 index 0000000..9e834cb --- /dev/null +++ b/source/shaders/light_source.fs.glsl @@ -0,0 +1,5 @@ +#version 330 core +out vec4 FragColor; +void main() { + FragColor = vec4(1.0); +} diff --git a/source/shaders/light_source.vs.glsl b/source/shaders/light_source.vs.glsl new file mode 100644 index 0000000..bf372d5 --- /dev/null +++ b/source/shaders/light_source.vs.glsl @@ -0,0 +1,10 @@ +#version 330 core +layout(location = 0) in vec3 position; + +uniform mat4 Model; +uniform mat4 View; +uniform mat4 Projection; + +void main() { + gl_Position = Projection * View * Model * vec4(position.x, position.y, position.z, 1.0); +} diff --git a/source/shaders/light_subject.fs.glsl b/source/shaders/light_subject.fs.glsl new file mode 100644 index 0000000..54bf700 --- /dev/null +++ b/source/shaders/light_subject.fs.glsl @@ -0,0 +1,46 @@ +#version 330 core +struct Material { + vec3 ambient; + vec3 diffuse; + vec3 specular; + float shininess; +}; + +struct Light { + vec3 ambient; + vec3 diffuse; + vec3 specular; + + vec3 position; +}; + +in vec3 fragNormal; +in vec3 worldPosition; + +uniform Material material; +uniform Light light; +uniform vec3 cameraPosition; +uniform vec3 lightColor; +uniform sampler2D smilingTexture; +uniform sampler2D containerTexture; + +out vec4 FragColor; + +void main() { + vec3 ambientLight = light.ambient * material.ambient; + +// @note: Diffuse calculations + vec3 lightDir = normalize(light.position - worldPosition); + float diffuseStrength = max(dot(lightDir, fragNormal), 0.0); + vec3 diffuseLight = light.diffuse * (diffuseStrength * material.diffuse); + +// @note: Specular calculations + vec3 viewDir = normalize(cameraPosition - worldPosition); + vec3 reflectDir = reflect(-lightDir, fragNormal); + float specularity = max(dot(viewDir, reflectDir), 0.0); + float shinePower = pow(specularity, material.shininess); + vec3 specularLight = light.specular * (shinePower * material.specular); + + vec3 color = ambientLight + diffuseLight + specularLight; + FragColor = vec4(color, 1.0); +} diff --git a/source/shaders/light_subject.vs.glsl b/source/shaders/light_subject.vs.glsl new file mode 100644 index 0000000..327e896 --- /dev/null +++ b/source/shaders/light_subject.vs.glsl @@ -0,0 +1,18 @@ +#version 330 core + +layout(location = 0) in vec3 position; +layout(location = 1) in vec3 normal; + +uniform mat4 Model; +uniform mat4 View; +uniform mat4 Projection; + +out vec3 fragNormal; +out vec3 worldPosition; + +void main() { + gl_Position = Projection * View * Model * vec4(position, 1.0); + worldPosition = vec3(Model * vec4(position, 1.0)); + fragNormal = mat3(transpose(inverse(Model))) * normal; + fragNormal = normalize(normal); +} |