From 86c7bdee944ce2f60d63124e9ddb76198ddbc676 Mon Sep 17 00:00:00 2001 From: jvech Date: Sun, 11 Sep 2022 13:54:51 -0500 Subject: learn: 14. Materials learnopengl book --- shaders/color.fsh | 44 ++++++++++++++++++++++++++++++++++++++++++++ shaders/color.vsh | 17 +++++++++++++++++ shaders/fragment.fsh | 9 --------- shaders/lightsource.fsh | 7 +++++++ shaders/lightsource.vsh | 10 ++++++++++ shaders/vertex.vsh | 13 ------------- 6 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 shaders/color.fsh create mode 100644 shaders/color.vsh delete mode 100644 shaders/fragment.fsh create mode 100644 shaders/lightsource.fsh create mode 100644 shaders/lightsource.vsh delete mode 100644 shaders/vertex.vsh (limited to 'shaders') diff --git a/shaders/color.fsh b/shaders/color.fsh new file mode 100644 index 0000000..4989d73 --- /dev/null +++ b/shaders/color.fsh @@ -0,0 +1,44 @@ +# version 330 core + +struct Material { + vec3 ambient; + vec3 diffuse; + vec3 specular; + float shininess; +}; + +struct Light { + vec3 position; + vec3 ambient; + vec3 diffuse; + vec3 specular; +}; + +in vec3 Normal; +in vec3 FragPos; + +out vec4 FragColor; + +uniform vec3 viewPos; +uniform Material material; +uniform Light light; + +void main() +{ + // ambient + vec3 ambient = material.ambient * light.ambient; + + // diffuse + vec3 norm = normalize(Normal); + vec3 lightDir = normalize(light.position - FragPos); + float diff = max(dot(norm, lightDir), 0.0); + vec3 diffuse = light.diffuse * diff * material.diffuse; + + // specular + vec3 viewDir = normalize(viewPos - FragPos); + vec3 reflectDir = reflect(-lightDir, norm); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); + vec3 specular = light.specular * spec * material.specular; + + FragColor = vec4((specular + ambient + diffuse), 1.0f); +} diff --git a/shaders/color.vsh b/shaders/color.vsh new file mode 100644 index 0000000..ff878be --- /dev/null +++ b/shaders/color.vsh @@ -0,0 +1,17 @@ +# version 330 core + +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aNormal; + +uniform mat4 model, view, proj; +uniform mat4 rotNormals; + +out vec3 FragPos; +out vec3 Normal; + +void main() +{ + gl_Position = proj * view * model * vec4(aPos, 1.0f); + FragPos = vec3(model * vec4(aPos, 1.0)); + Normal = vec3(rotNormals * vec4(aNormal, 1.0)); +} diff --git a/shaders/fragment.fsh b/shaders/fragment.fsh deleted file mode 100644 index 2d87daa..0000000 --- a/shaders/fragment.fsh +++ /dev/null @@ -1,9 +0,0 @@ -# version 330 core - -in vec3 vertexColor; -out vec4 FragColor; - -void main() -{ - FragColor = vec4(vertexColor, 0.0f); -} diff --git a/shaders/lightsource.fsh b/shaders/lightsource.fsh new file mode 100644 index 0000000..1bd96c6 --- /dev/null +++ b/shaders/lightsource.fsh @@ -0,0 +1,7 @@ +#version 330 core +out vec4 FragColor; + +void main() +{ + FragColor = vec4(1.0); +} diff --git a/shaders/lightsource.vsh b/shaders/lightsource.vsh new file mode 100644 index 0000000..3525c32 --- /dev/null +++ b/shaders/lightsource.vsh @@ -0,0 +1,10 @@ +# version 330 core + +layout (location = 0) in vec3 aPos; +uniform mat4 model, view, proj; + + +void main() +{ + gl_Position = proj * view * model * vec4(aPos, 1.0f); +} diff --git a/shaders/vertex.vsh b/shaders/vertex.vsh deleted file mode 100644 index 987dc3b..0000000 --- a/shaders/vertex.vsh +++ /dev/null @@ -1,13 +0,0 @@ -# version 330 core - -layout (location = 0) in vec3 aPos; -layout (location = 1) in vec3 aColor; -uniform mat4 model, view, proj; - -out vec3 vertexColor; - -void main() -{ - gl_Position = proj * view * model * vec4(aPos, 1.0f); - vertexColor = aColor; -} -- cgit v1.2.3-70-g09d2