Materials and light properties
This commit is contained in:
@@ -7,7 +7,21 @@ struct Material {
|
|||||||
sampler2D u_texture_diffuse1;
|
sampler2D u_texture_diffuse1;
|
||||||
sampler2D u_texture_specular0;
|
sampler2D u_texture_specular0;
|
||||||
sampler2D u_texture_specular1;
|
sampler2D u_texture_specular1;
|
||||||
|
vec3 ambient;
|
||||||
|
vec3 diffuse;
|
||||||
|
vec3 specular;
|
||||||
|
float shininess;
|
||||||
};
|
};
|
||||||
|
uniform Material material;
|
||||||
|
|
||||||
|
struct Light {
|
||||||
|
vec3 position;
|
||||||
|
|
||||||
|
vec3 ambient;
|
||||||
|
vec3 diffuse;
|
||||||
|
vec3 specular;
|
||||||
|
};
|
||||||
|
uniform Light light;
|
||||||
|
|
||||||
in vec3 v_normal;
|
in vec3 v_normal;
|
||||||
in vec4 v_color;
|
in vec4 v_color;
|
||||||
@@ -15,35 +29,28 @@ in vec2 v_texCoord;
|
|||||||
|
|
||||||
in vec3 v_fragmentPosition;
|
in vec3 v_fragmentPosition;
|
||||||
|
|
||||||
uniform vec3 u_objectColor;
|
|
||||||
|
|
||||||
uniform vec3 u_lightPosition;
|
|
||||||
uniform vec3 u_lightColor;
|
|
||||||
|
|
||||||
uniform mat3 u_normalMatrix;
|
uniform mat3 u_normalMatrix;
|
||||||
|
|
||||||
uniform vec3 u_viewPosition;
|
uniform vec3 u_viewPosition;
|
||||||
|
|
||||||
uniform Material material;
|
|
||||||
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
float ambientStrength = 0.1f;
|
|
||||||
float specularStrength = 0.5;
|
float specularStrength = 0.5;
|
||||||
|
|
||||||
vec3 ambient = ambientStrength * u_lightColor;
|
vec3 ambient = light.ambient * material.ambient;
|
||||||
|
|
||||||
// Diffuse lighting
|
// Diffuse lighting
|
||||||
vec3 normal = normalize(u_normalMatrix * v_normal);
|
vec3 normal = normalize(u_normalMatrix * v_normal);
|
||||||
vec3 lightDir = normalize(u_lightPosition - v_fragmentPosition);
|
vec3 lightDir = normalize(light.position - v_fragmentPosition);
|
||||||
float diff = max(dot(normal, lightDir), 0.0f);
|
float diff = max(dot(normal, lightDir), 0.0f);
|
||||||
vec3 diffuse = diff * u_lightColor;
|
vec3 diffuse = light.diffuse * (diff * material.diffuse);
|
||||||
|
|
||||||
// Specular lighting
|
// Specular lighting
|
||||||
vec3 viewDir = normalize(u_viewPosition - v_fragmentPosition);
|
vec3 viewDir = normalize(u_viewPosition - v_fragmentPosition);
|
||||||
vec3 reflectDir = reflect(-lightDir, normal);
|
vec3 reflectDir = reflect(-lightDir, normal);
|
||||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
|
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||||
vec3 specular = specularStrength * spec * u_lightColor;
|
vec3 specular = light.specular * (spec * material.specular);
|
||||||
|
|
||||||
//f_color = v_color;
|
//f_color = v_color;
|
||||||
//vec4 texColor1 = texture(material.u_texture_diffuse0, v_texCoord);
|
//vec4 texColor1 = texture(material.u_texture_diffuse0, v_texCoord);
|
||||||
@@ -51,5 +58,5 @@ void main() {
|
|||||||
//f_color = texColor1;
|
//f_color = texColor1;
|
||||||
|
|
||||||
|
|
||||||
f_color = vec4((ambient + diffuse + specular) * u_objectColor, 1.0f);
|
f_color = vec4((ambient + diffuse + specular), 1.0f);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,12 +49,12 @@ void Controller::run() {
|
|||||||
|
|
||||||
std::vector<Entity> scene;
|
std::vector<Entity> scene;
|
||||||
|
|
||||||
Model model_backpack("res/models/backpack.obj");
|
//Model model_backpack("res/models/backpack.obj");
|
||||||
Model model_cube("res/models/cube.obj");
|
Model model_cube("res/models/cube.obj");
|
||||||
Model model_sphere("res/models/sphere.obj");
|
Model model_sphere("res/models/sphere.obj");
|
||||||
|
|
||||||
//Entity backpack1(&model_backpack, &shaderProgram);
|
//Entity backpack1(&model_cube, &shaderProgram);
|
||||||
Entity cube(&model_backpack, &shaderProgram);
|
Entity cube(&model_cube, &shaderProgram);
|
||||||
Entity sphere(&model_sphere, &shaderProgram);
|
Entity sphere(&model_sphere, &shaderProgram);
|
||||||
Entity lightSource(&model_cube, &lightProgram);
|
Entity lightSource(&model_cube, &lightProgram);
|
||||||
|
|
||||||
@@ -63,10 +63,20 @@ void Controller::run() {
|
|||||||
lightSource.translate(glm::vec3(-5.0f, 1.0f, 0.0f));
|
lightSource.translate(glm::vec3(-5.0f, 1.0f, 0.0f));
|
||||||
lightSource.scale(0.2f);
|
lightSource.scale(0.2f);
|
||||||
|
|
||||||
|
glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
|
||||||
|
glm::vec3 diffuseColor = lightColor * glm::vec3(1.0f);
|
||||||
|
glm::vec3 ambientColor = diffuseColor * glm::vec3(0.1f);
|
||||||
|
|
||||||
shaderProgram.bind();
|
shaderProgram.bind();
|
||||||
shaderProgram.setUniform("u_lightPosition", lightSource.getPosition());
|
shaderProgram.setUniform("light.position", lightSource.getPosition());
|
||||||
shaderProgram.setUniform("u_objectColor", glm::vec3(1.0f, 0.5f, 0.31f));
|
shaderProgram.setUniform("light.ambient", ambientColor);
|
||||||
shaderProgram.setUniform("u_lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
|
shaderProgram.setUniform("light.diffuse", diffuseColor);
|
||||||
|
shaderProgram.setUniform("light.specular", glm::vec3(1.0f, 1.0f, 1.0f));
|
||||||
|
|
||||||
|
shaderProgram.setUniform("material.ambient", glm::vec3(1.0f, 0.5f, 0.31f));
|
||||||
|
shaderProgram.setUniform("material.diffuse", glm::vec3(1.0f, 0.5f, 0.31f));
|
||||||
|
shaderProgram.setUniform("material.specular", glm::vec3(0.5f, 0.5f, 0.5f));
|
||||||
|
shaderProgram.setUniform("material.shininess", 32.0f);
|
||||||
shaderProgram.unbind();
|
shaderProgram.unbind();
|
||||||
|
|
||||||
scene.push_back(lightSource);
|
scene.push_back(lightSource);
|
||||||
|
|||||||
Reference in New Issue
Block a user