improved phong lighting

This commit is contained in:
Robert 2021-01-28 01:07:19 +01:00
parent 6f46112f14
commit 5c51de37a6
4 changed files with 39 additions and 37 deletions

View file

@ -272,9 +272,10 @@ int main(int argc, char** argv)
shader->SetUniformTexture("texture1", crate, 0); shader->SetUniformTexture("texture1", crate, 0);
shader->SetUniformTexture("texture2", opengl, 1); shader->SetUniformTexture("texture2", opengl, 1);
shader->SetUniform("ambientColor", "ambientStrength", ambient); shader->SetUniform("light.ambient", "light.ambientStrength", ambient);
shader->SetUniform3fv("lightPos", 1, glm::value_ptr(lightSource.GetPosition())); shader->SetUniform3fv("light.position", 1, glm::value_ptr(lightSource.GetPosition()));
shader->SetUniform("lightColor", pointLight.color, true); shader->SetUniform("light.diffuse", pointLight.diffusionColor, true);
shader->SetUniform("light.specular", pointLight.specularColor, true);
shader->SetUniform3fv("viewPos", 1, glm::value_ptr(camera.GetPosition())); shader->SetUniform3fv("viewPos", 1, glm::value_ptr(camera.GetPosition()));
@ -297,11 +298,12 @@ int main(int argc, char** argv)
lightSourceShader->SetUniformMatrix4fv("model", 1, GL_FALSE, glm::value_ptr(lightSource.GetMatrix(true))); lightSourceShader->SetUniformMatrix4fv("model", 1, GL_FALSE, glm::value_ptr(lightSource.GetMatrix(true)));
lightSourceShader->SetUniformMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(camera.GetMatrix())); lightSourceShader->SetUniformMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(camera.GetMatrix()));
lightSourceShader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(camera.GetProjection())); lightSourceShader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(camera.GetProjection()));
lightSourceShader->SetUniform("color", pointLight.color, true); lightSourceShader->SetUniform("color", pointLight.diffusionColor, true);
lightSource.Render(); lightSource.Render();
ImGui::Begin("Test"); ImGui::Begin("Controls");
ImGui::SetNextItemOpen(true, ImGuiCond_Once);
if(ImGui::CollapsingHeader("Scene")); if(ImGui::CollapsingHeader("Scene"));
{ {
ImGui::ColorEdit3("Background color", &bgColor.r); ImGui::ColorEdit3("Background color", &bgColor.r);
@ -310,10 +312,10 @@ int main(int argc, char** argv)
ImGui::SliderFloat("zFar", &camera.zFar, 2.0f, 100.0f); ImGui::SliderFloat("zFar", &camera.zFar, 2.0f, 100.0f);
} }
ImGui::SetNextItemOpen(true); ImGui::SetNextItemOpen(true, ImGuiCond_Once);
if(ImGui::CollapsingHeader("Lighting")) if(ImGui::CollapsingHeader("Lighting"))
{ {
ImGui::SetNextItemOpen(true); ImGui::SetNextItemOpen(true, ImGuiCond_Once);
if (ImGui::TreeNode("Ambient")) if (ImGui::TreeNode("Ambient"))
{ {
ImGui::ColorEdit3("Color", &ambient.color.r); ImGui::ColorEdit3("Color", &ambient.color.r);
@ -323,27 +325,24 @@ int main(int argc, char** argv)
ImGui::Separator(); ImGui::Separator();
} }
ImGui::SetNextItemOpen(true); ImGui::SetNextItemOpen(true, ImGuiCond_Once);
if (ImGui::TreeNode("Point")) if (ImGui::TreeNode("Point"))
{ {
ImGui::ColorEdit3("Color", &pointLight.color.r); ImGui::ColorEdit3("Diffusion", &pointLight.diffusionColor.r);
ImGui::ColorEdit3("Specular", &pointLight.specularColor.r);
ImGui::SliderFloat3("Position", pointLight.GetPositionPointer(), -5.0f, 5.0f); ImGui::SliderFloat3("Position", pointLight.GetPositionPointer(), -5.0f, 5.0f);
ImGui::TreePop(); ImGui::TreePop();
ImGui::Separator();
} }
}
ImGui::SetNextItemOpen(true); ImGui::SetNextItemOpen(true, ImGuiCond_Once);
if (ImGui::TreeNode("Cube Material")) if (ImGui::CollapsingHeader("Cube Material"))
{ {
ImGui::ColorEdit3("Ambient", &(cubeMaterial->ambient.r)); ImGui::ColorEdit3("Ambient", &(cubeMaterial->ambient.r));
ImGui::ColorEdit3("Diffuse", &(cubeMaterial->diffuse.r)); ImGui::ColorEdit3("Diffuse", &(cubeMaterial->diffuse.r));
ImGui::ColorEdit3("Specular", &(cubeMaterial->specular.r)); ImGui::ColorEdit3("Specular", &(cubeMaterial->specular.r));
ImGui::SliderFloat("Shininess", &(cubeMaterial->shininess), 1.0f, 256.0f); ImGui::SliderFloat("Shininess", &(cubeMaterial->shininess), 1.0f, 256.0f);
ImGui::TreePop();
ImGui::Separator();
}
} }
ImGui::End(); ImGui::End();

View file

@ -5,6 +5,13 @@ struct Material
float shininess; float shininess;
}; };
struct Light
{
vec3 position;
vec3 ambient, diffuse, specular;
float ambientStrength;
};
in vec2 oUV; in vec2 oUV;
in vec3 oNormal; in vec3 oNormal;
in vec3 oFragPos; in vec3 oFragPos;
@ -14,34 +21,29 @@ out vec4 FragColor;
uniform sampler2D texture1; uniform sampler2D texture1;
uniform sampler2D texture2; uniform sampler2D texture2;
uniform float ambientStrength;
uniform vec3 ambientColor;
uniform vec3 lightPos;
uniform vec3 lightColor;
uniform vec3 viewPos; uniform vec3 viewPos;
uniform Material material; uniform Material material;
uniform Light light;
void main() void main()
{ {
// Ambient light // Ambient light
vec3 ambient = ambientColor * ambientStrength * material.ambient; vec3 ambient = light.ambient * light.ambientStrength * material.ambient;
vec3 norm = normalize(oNormal); vec3 norm = normalize(oNormal);
// Diffuse light // Diffuse light
vec3 lightDir = normalize(lightPos - oFragPos); vec3 lightDir = normalize(light.position - oFragPos);
float diff = max(dot(norm, lightDir), 0.0); float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = (diff * material.diffuse) * lightColor; vec3 diffuse = (diff * material.diffuse) * light.diffuse;
// Specular light // Specular light
vec3 viewDir = normalize(viewPos - oFragPos); vec3 viewDir = normalize(viewPos - oFragPos);
vec3 reflectDir = reflect(-lightDir, norm); vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = (material.specular * spec) * lightColor; vec3 specular = (material.specular * spec) * light.specular;
vec4 objColor = mix(texture(texture1, oUV), texture(texture2, oUV), 0.2); vec4 objColor = mix(texture(texture1, oUV), texture(texture2, oUV), 0.2);

View file

@ -13,7 +13,7 @@ namespace oglu
{ {
public: public:
PointLight(); PointLight();
PointLight(const glm::vec3& position, const Color& color); PointLight(const glm::vec3& position, const Color& diffusionColor = Color::White, const Color& specularColor = Color::White);
PointLight(const PointLight& other); PointLight(const PointLight& other);
~PointLight(); ~PointLight();
@ -23,7 +23,8 @@ namespace oglu
float* GetPositionPointer(); float* GetPositionPointer();
public: public:
Color color; Color diffusionColor;
Color specularColor;
private: private:

View file

@ -5,18 +5,18 @@
namespace oglu namespace oglu
{ {
PointLight::PointLight() : PointLight::PointLight() :
position(new glm::vec3(0.0f)), color(oglu::Color::White) position(new glm::vec3(0.0f)), diffusionColor(oglu::Color::White), specularColor(oglu::Color::White)
{ {
} }
PointLight::PointLight(const glm::vec3& position, const Color& color) : PointLight::PointLight(const glm::vec3& position, const Color& diffusionColor, const Color& specularColor) :
position(new glm::vec3(0.0f)), color(color) position(new glm::vec3(0.0f)), diffusionColor(diffusionColor), specularColor(specularColor)
{ {
memcpy(this->position, &position, sizeof(glm::vec3)); memcpy(this->position, &position, sizeof(glm::vec3));
} }
PointLight::PointLight(const PointLight& other) : PointLight::PointLight(const PointLight& other) :
position(new glm::vec3(0.0f)), color(other.color) position(new glm::vec3(0.0f)), diffusionColor(other.diffusionColor), specularColor(other.specularColor)
{ {
memcpy(this->position, position, sizeof(glm::vec3)); memcpy(this->position, position, sizeof(glm::vec3));
} }