Added phong lighting
This commit is contained in:
parent
839df891ab
commit
6f46112f14
7 changed files with 90 additions and 11 deletions
|
@ -183,6 +183,7 @@ int main(int argc, char** argv)
|
|||
|
||||
// Make a square
|
||||
oglu::VertexArray cubeDefault = oglu::MakeVertexArray(vertices, sizeof(vertices), nullptr, 0, topology, sizeof(topology));
|
||||
oglu::SharedMaterial cubeMaterial(new oglu::Material);
|
||||
oglu::Object cubes[10] = {
|
||||
oglu::Object(cubeDefault),
|
||||
oglu::Object(cubeDefault),
|
||||
|
@ -218,6 +219,9 @@ int main(int argc, char** argv)
|
|||
cubes[8].SetRotation(150.0f, 20.0f, -150.0f);
|
||||
cubes[9].SetRotation(-130.0f, 10.0f, -150.0f);
|
||||
|
||||
for (oglu::Object& cube : cubes)
|
||||
cube.material = cubeMaterial;
|
||||
|
||||
oglu::Object lightSource(cubeDefault);
|
||||
lightSource.SetScale(glm::vec3(0.1f));
|
||||
|
||||
|
@ -254,6 +258,7 @@ int main(int argc, char** argv)
|
|||
|
||||
oglu::Color bgColor = oglu::Color::Black;
|
||||
lightSource.SetPosition(1.0f, 1.0f, -1.0f);
|
||||
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
processInput(window);
|
||||
|
@ -271,6 +276,8 @@ int main(int argc, char** argv)
|
|||
shader->SetUniform3fv("lightPos", 1, glm::value_ptr(lightSource.GetPosition()));
|
||||
shader->SetUniform("lightColor", pointLight.color, true);
|
||||
|
||||
shader->SetUniform3fv("viewPos", 1, glm::value_ptr(camera.GetPosition()));
|
||||
|
||||
shader->SetUniformMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(camera.GetMatrix()));
|
||||
shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(camera.GetProjection()));
|
||||
|
||||
|
@ -278,6 +285,11 @@ int main(int argc, char** argv)
|
|||
{
|
||||
shader->SetUniform("model", cube);
|
||||
shader->SetUniformMatrix3fv("normal", 1, GL_FALSE, glm::value_ptr(cube.GetNormalMatrix()));
|
||||
|
||||
shader->SetUniform("material.ambient", cube.material->ambient, true);
|
||||
shader->SetUniform("material.diffuse", cube.material->diffuse, true);
|
||||
shader->SetUniform("material.specular", cube.material->specular, true);
|
||||
shader->SetUniform("material.shininess", cube.material->shininess);
|
||||
cube.Render();
|
||||
}
|
||||
|
||||
|
@ -320,6 +332,18 @@ int main(int argc, char** argv)
|
|||
ImGui::TreePop();
|
||||
ImGui::Separator();
|
||||
}
|
||||
|
||||
ImGui::SetNextItemOpen(true);
|
||||
if (ImGui::TreeNode("Cube Material"))
|
||||
{
|
||||
ImGui::ColorEdit3("Ambient", &(cubeMaterial->ambient.r));
|
||||
ImGui::ColorEdit3("Diffuse", &(cubeMaterial->diffuse.r));
|
||||
ImGui::ColorEdit3("Specular", &(cubeMaterial->specular.r));
|
||||
ImGui::SliderFloat("Shininess", &(cubeMaterial->shininess), 1.0f, 256.0f);
|
||||
|
||||
ImGui::TreePop();
|
||||
ImGui::Separator();
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::End();
|
||||
|
|
|
@ -1,4 +1,10 @@
|
|||
#version 330 core
|
||||
struct Material
|
||||
{
|
||||
vec3 ambient, diffuse, specular;
|
||||
float shininess;
|
||||
};
|
||||
|
||||
in vec2 oUV;
|
||||
in vec3 oNormal;
|
||||
in vec3 oFragPos;
|
||||
|
@ -14,18 +20,30 @@ uniform vec3 ambientColor;
|
|||
uniform vec3 lightPos;
|
||||
uniform vec3 lightColor;
|
||||
|
||||
uniform vec3 viewPos;
|
||||
|
||||
uniform Material material;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 ambient = ambientColor * ambientStrength;
|
||||
// Ambient light
|
||||
vec3 ambient = ambientColor * ambientStrength * material.ambient;
|
||||
|
||||
vec3 norm = normalize(oNormal);
|
||||
// Diffuse light
|
||||
vec3 lightDir = normalize(lightPos - oFragPos);
|
||||
|
||||
float diff = max(dot(norm, lightDir), 0.0) * 2.0;
|
||||
diff *= min(1.0 / pow(length(lightPos - oFragPos), 2), 2.0);
|
||||
|
||||
vec3 diffuse = diff * lightColor;
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = (diff * material.diffuse) * lightColor;
|
||||
|
||||
// Specular light
|
||||
vec3 viewDir = normalize(viewPos - oFragPos);
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||
vec3 specular = (material.specular * spec) * lightColor;
|
||||
|
||||
vec4 objColor = mix(texture(texture1, oUV), texture(texture2, oUV), 0.2);
|
||||
|
||||
FragColor = vec4(ambient + diffuse, 1.0) * objColor;
|
||||
FragColor = vec4(ambient + diffuse + specular, 1.0) * objColor;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue