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