Skip to content

Commit

Permalink
Added Blinn-Phong
Browse files Browse the repository at this point in the history
  • Loading branch information
denyskryvytskyi committed May 1, 2024
1 parent 9ae04f3 commit 8686262
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct SpotLight {
uniform vec3 u_ViewPos;
uniform Material u_Material;

uniform bool u_BlinnPhong;
uniform bool u_DirLightEnabled;
uniform int u_ActivePointLightsAmount;
uniform int u_ActiveSpotLightsAmount;
Expand Down Expand Up @@ -123,7 +124,16 @@ vec3 CalcDirLight(DirLight light, vec3 diffuseMap, float specularMap, vec3 emiss
vec3 diffuse = light.diffuse * diff * diffuseMap;

// specular
float spec = pow(max(dot(viewDir, reflectDir), 0.0), u_Material.shininess);
float spec = 0.0;
if (u_BlinnPhong)
{
vec3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(normal, halfwayDir), 0.0), u_Material.shininess);
}
else
{
spec = pow(max(dot(viewDir, reflectDir), 0.0), u_Material.shininess);
}
vec3 specular = light.specular * spec * specularMap * u_Material.specularColor;

// emission
Expand All @@ -145,7 +155,16 @@ vec3 CalcPointLight(PointLight light, vec3 diffuseMap, float specularMap, vec3 e
vec3 diffuse = light.diffuse * diff * diffuseMap;

// specular
float spec = pow(max(dot(viewDir, reflectDir), 0.0), u_Material.shininess);
float spec = 0.0;
if (u_BlinnPhong)
{
vec3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(normal, halfwayDir), 0.0), u_Material.shininess);
}
else
{
spec = pow(max(dot(viewDir, reflectDir), 0.0), u_Material.shininess);
}
vec3 specular = light.specular * spec * specularMap * u_Material.specularColor;

// emission
Expand Down Expand Up @@ -175,7 +194,16 @@ vec3 CalcSpotLight(SpotLight light, vec3 diffuseMap, float specularMap, vec3 emi
vec3 diffuse = light.diffuse * diff * diffuseMap;

// specular
float spec = pow(max(dot(viewDir, reflectDir), 0.0), u_Material.shininess);
float spec = 0.0;
if (u_BlinnPhong)
{
vec3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(normal, halfwayDir), 0.0), u_Material.shininess);
}
else
{
spec = pow(max(dot(viewDir, reflectDir), 0.0), u_Material.shininess);
}
vec3 specular = light.specular * spec * specularMap * u_Material.specularColor;

// emission
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions Engine/src/Editor/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void Editor::OnImGuiRender()
bool isVSync = window->IsVSync();
bool isFullScreen = window->IsFullScreen();
bool isMSAAEnabled = renderer.IsMSAAEnabled();
bool isBlinnPhongEnabled = renderer.IsBlinnPhongEnabled();

// ============ Settings panel ============
ImGui::Begin("Settings");
Expand All @@ -49,6 +50,10 @@ void Editor::OnImGuiRender()
if (ImGui::Checkbox("MSAA", &isMSAAEnabled)) {
renderer.EnableMSAA(isMSAAEnabled);
}

if (ImGui::Checkbox("Blinn-Phong", &isBlinnPhongEnabled)) {
renderer.EnableBlinnPhong(isBlinnPhongEnabled);
}
ImGui::End();

// ============ Environment ============
Expand Down
6 changes: 6 additions & 0 deletions Engine/src/Renderer/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "RHI/RendererAPI.h"
#include "RHI/Shader.h"

#include "Scene/Components/LightComponent.h"

namespace elv {

class Mesh;
Expand Down Expand Up @@ -41,10 +43,14 @@ class Renderer {
void EnableMSAA(bool enabled);
bool IsMSAAEnabled() const { return m_isMSAAEnabled; }

void EnableBlinnPhong(const bool enabled) { m_isBlinnPhongEnabled = enabled; }
bool IsBlinnPhongEnabled() const { return m_isBlinnPhongEnabled; }

static RendererAPI::API GetAPI() { return RendererAPI::GetAPI(); }

private:
bool m_isMSAAEnabled { true };
bool m_isBlinnPhongEnabled { false };

SceneData m_sceneData;
lia::vec4 m_clearColor { 0.2f, 0.2f, 0.2f, 1.0f };
Expand Down
3 changes: 2 additions & 1 deletion Engine/src/Scene/Systems/RenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace elv {
void RenderSystem::OnInit()
{
m_shader = ShaderManager::Load("mesh_phong", "mesh_phong.vert", "mesh_phong.frag");
m_shader = ShaderManager::Load("object_render", "object_render.vert", "object_render.frag");
m_debugLightMesh = gMeshLibrary.GetMesh("sphere");
m_lightShader = ShaderManager::Load("light_debug", "light_debug.vert", "light_debug.frag");
}
Expand All @@ -35,6 +35,7 @@ void RenderSystem::OnRender(float dt)

m_shader->Bind();
m_shader->SetVector3f("u_ViewPos", camera.GetPosition());
m_shader->SetInteger("u_BlinnPhong", renderer.IsBlinnPhongEnabled());

// ================== LIGHT UNIFORMS ===========================

Expand Down

0 comments on commit 8686262

Please sign in to comment.