Implemented basic materials in to the fragment shader..
This commit is contained in:
parent
fc3806e73d
commit
59bd1ece6b
39
shader.fs
39
shader.fs
@ -4,9 +4,25 @@ out vec4 FragColor;
|
|||||||
in vec3 Normal;
|
in vec3 Normal;
|
||||||
in vec3 FragPos;
|
in vec3 FragPos;
|
||||||
|
|
||||||
uniform vec3 objectColor;
|
struct Material {
|
||||||
uniform vec3 lightColor;
|
vec3 ambient;
|
||||||
uniform vec3 lightPos;
|
vec3 diffuse;
|
||||||
|
vec3 specular;
|
||||||
|
float shininess;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Light{
|
||||||
|
vec3 position;
|
||||||
|
|
||||||
|
vec3 ambient;
|
||||||
|
vec3 diffuse;
|
||||||
|
vec3 specular;
|
||||||
|
};
|
||||||
|
|
||||||
|
uniform Light light;
|
||||||
|
uniform Material material;
|
||||||
|
|
||||||
|
|
||||||
uniform vec3 viewPos;
|
uniform vec3 viewPos;
|
||||||
|
|
||||||
|
|
||||||
@ -14,28 +30,25 @@ uniform vec3 viewPos;
|
|||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
// ambient lighting calculation
|
// ambient lighting calculation
|
||||||
float ambientStrength = 0.1;
|
vec3 ambient = light.ambient * material.ambient;
|
||||||
vec3 ambient = ambientStrength * lightColor;
|
|
||||||
|
|
||||||
// diffuse lighting calculation
|
// diffuse lighting calculation
|
||||||
vec3 norm = normalize(Normal);
|
vec3 norm = normalize(Normal);
|
||||||
vec3 lightDir = normalize(lightPos - FragPos);
|
vec3 lightDir = normalize(light.position - FragPos);
|
||||||
|
|
||||||
float diff = max(dot(norm, lightDir), 0.0);
|
float diff = max(dot(norm, lightDir), 0.0);
|
||||||
vec3 diffuse = diff * lightColor;
|
vec3 diffuse = light.diffuse * ( diff * material.diffuse);
|
||||||
|
|
||||||
// specular lighting
|
// specular lighting
|
||||||
float specularStrength = 0.5;
|
|
||||||
float shininess = 32;
|
|
||||||
vec3 viewDir = normalize(viewPos - FragPos);
|
vec3 viewDir = normalize(viewPos - FragPos);
|
||||||
vec3 reflectDir = reflect(-lightDir, norm);
|
vec3 reflectDir = reflect(-lightDir, norm);
|
||||||
|
|
||||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess);
|
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||||
vec3 specular = specularStrength * spec * lightColor;
|
vec3 specular = light.specular * ( spec * material.specular);
|
||||||
|
|
||||||
|
|
||||||
// Calculate the result;
|
// Calculate the result;
|
||||||
vec3 result = (ambient + diffuse + specular) * objectColor;
|
vec3 result = ambient + diffuse + specular ;
|
||||||
|
|
||||||
FragColor = vec4(result, 1.0);
|
FragColor = vec4(result, 1.0);
|
||||||
}
|
}
|
28
src/main.cpp
28
src/main.cpp
@ -196,9 +196,31 @@ while(!glfwWindowShouldClose(window))
|
|||||||
|
|
||||||
// draw cubes
|
// draw cubes
|
||||||
shader.use();
|
shader.use();
|
||||||
shader.setVec3("objectColor", glm::vec3( 1.0f, 0.5, 0.31f));
|
|
||||||
shader.setVec3("lightColor",glm::vec3( 1.0f, 1.0f, 1.0f));
|
shader.setVec3("material.ambient", glm::vec3(1.0f, 0.5f, 0.31f));
|
||||||
shader.setVec3("lightPos", lightpos);
|
shader.setVec3("material.diffuse", glm::vec3(1.0f, 0.5f, 0.31f));
|
||||||
|
shader.setVec3("material.specular", glm::vec3(0.5, 0.5f, 0.5f));
|
||||||
|
shader.setFloat("material.shininess", 32.0f);
|
||||||
|
|
||||||
|
|
||||||
|
glm::vec3 lightColor;
|
||||||
|
lightColor.x = sin(glfwGetTime() * 2.0f);
|
||||||
|
lightColor.y = sin(glfwGetTime() * 0.7f);
|
||||||
|
lightColor.z = sin(glfwGetTime() * 1.3f);
|
||||||
|
|
||||||
|
glm::vec3 diffuseColor = lightColor * glm::vec3(0.5f);
|
||||||
|
glm::vec3 ambientColor = diffuseColor * glm::vec3(0.2f);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
shader.setVec3("light.position", lightpos);
|
||||||
|
|
||||||
|
shader.setVec3("light.ambient", ambientColor);
|
||||||
|
shader.setVec3("light.diffuse", diffuseColor);
|
||||||
|
shader.setVec3("light.specular", glm::vec3(1.0f, 1.0f, 1.0f));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
shader.setVec3("viewPos", camera.Position);
|
shader.setVec3("viewPos", camera.Position);
|
||||||
|
|
||||||
shader.setMat4("view", view);
|
shader.setMat4("view", view);
|
||||||
|
Loading…
Reference in New Issue
Block a user