From 1f558e3f1c610df8e201f1a05666d04bac66247b Mon Sep 17 00:00:00 2001 From: Hurlu Date: Wed, 20 Mar 2019 19:01:16 +0900 Subject: [PATCH] Spotlight still in the works --- BaseGLProject/Light.cpp | 6 ++++-- BaseGLProject/Light.h | 7 +++++-- BaseGLProject/MyGLWindow.cpp | 11 +++++++---- BaseGLProject/SceneContext.h | 12 ++++++++++++ BaseGLProject/spotlight.frag | 21 ++++++++------------- 5 files changed, 36 insertions(+), 21 deletions(-) diff --git a/BaseGLProject/Light.cpp b/BaseGLProject/Light.cpp index 36fa349..d1f2601 100644 --- a/BaseGLProject/Light.cpp +++ b/BaseGLProject/Light.cpp @@ -5,12 +5,14 @@ Light::Light(glm::vec3 intensity_, glm::vec4 location_) : location(location_), i type = BASE; } -Light::Light(glm::vec3 intensity_, glm::vec4 location_, float spot_cutoff_, float spot_inner_cutoff_, float spot_exponent_, glm::vec3 direction_): +Light::Light(glm::vec3 intensity_, glm::vec4 location_, float spot_cutoff_, float spot_inner_cutoff_, float spot_exponent_, + glm::vec4 lookAtPosition_): location(location_), intensity(intensity_), spot_cutoff(spot_cutoff_), spot_inner_cutoff(spot_inner_cutoff_) - ,spot_exponent(spot_exponent_), direction(direction_) + ,spot_exponent(spot_exponent_) { type = SPOT; + direction = lookAtPosition_; } diff --git a/BaseGLProject/Light.h b/BaseGLProject/Light.h index c0dbdd8..742dc8b 100644 --- a/BaseGLProject/Light.h +++ b/BaseGLProject/Light.h @@ -13,7 +13,9 @@ public: }; Light(glm::vec3 intensity, glm::vec4 location); - Light(glm::vec3 intensity, glm::vec4 location, float spot_cutoff, float spot_inner_cutoff, float spot_exponent, glm::vec3 direction); + Light(glm::vec3 intensity, glm::vec4 location, + float spot_cutoff, float spot_inner_cutoff, + float spot_exponent, glm::vec4 lookAt); ~Light(); bool activated = true; @@ -23,6 +25,7 @@ public: float spot_cutoff; float spot_inner_cutoff; float spot_exponent; - glm::vec3 direction; + glm::vec4 lookAt; + glm::vec3 direction; }; diff --git a/BaseGLProject/MyGLWindow.cpp b/BaseGLProject/MyGLWindow.cpp index b16a542..7d54b7f 100644 --- a/BaseGLProject/MyGLWindow.cpp +++ b/BaseGLProject/MyGLWindow.cpp @@ -102,7 +102,8 @@ void MyGlWindow::shaderSetup() void MyGlWindow::lightSetup() { //Showcase lights - _scnctx.lights.emplace("Light1", Light(glm::vec3(0.8f), glm::vec4(3, 10, 5, 1))); + _scnctx.lights.emplace("Spotlight1", Light(glm::vec3(0.8f), glm::vec4(3, 10, 5, 1), + 24, 12, 2, glm::vec4(0, 1, 0, 1))); //Party lights /*_scnctx.lights.emplace("Light1", Light(glm::vec3(0.0f, 0.5f, 0.5f), glm::vec4(10, 10, 0, 1))); @@ -120,11 +121,11 @@ void MyGlWindow::setup() Dataset moddata; moddata.checkeredFloor(20, 20, glm::vec3(0.1, 0.1, 0.1), glm::vec3(0.7, 0.7, 0.7)); - meshes.emplace("Floor", new Mesh(moddata, shaders["Simple"])); + meshes.emplace("Floor", new Mesh(moddata, shaders["SpotLight"])); meshes["Floor"]->addTranslation(glm::vec4(0, -0.05, 0, 1)); meshes["Floor"]->cullMode = BACK; - moddata.simpleCube(); + /*moddata.simpleCube(); meshes.emplace("Cube", new Mesh(moddata, shaders["BrickBaseLight"])); meshes["Cube"]->addTranslation(glm::vec4(0, 1, 0, 1)); @@ -137,7 +138,7 @@ void MyGlWindow::setup() meshes["TeapotSilhouette"]->cullMode = FRONT; meshes.emplace("Teapot", new Mesh("teapot.obj", shaders["Toon"])); meshes["Teapot"]->addTranslation(glm::vec4(5, 0, 3, 1)); - meshes["Teapot"]->cullMode = BACK; + meshes["Teapot"]->cullMode = BACK;*/ } @@ -157,6 +158,8 @@ void MyGlWindow::draw() _scnctx.viewMatrix = view; _scnctx.projectionMatrix = projection; + + _scnctx.adjustSpots(); for (auto it = meshes.begin(); it != meshes.end(); it++) (*it).second->draw(_scnctx); } diff --git a/BaseGLProject/SceneContext.h b/BaseGLProject/SceneContext.h index 4e2d1a0..6f4847a 100644 --- a/BaseGLProject/SceneContext.h +++ b/BaseGLProject/SceneContext.h @@ -18,4 +18,16 @@ struct SceneContext glm::mat3x3 normalMatrix; std::map textures; + + void adjustSpots() + { + for (auto it : lights) + { + if (it.second.type == Light::SPOT) + { + it.second.location = it.second.location * viewMatrix; + it.second.direction = (viewMatrix * it.second.lookAt - it.second.location * viewMatrix); + } + } + } }; \ No newline at end of file diff --git a/BaseGLProject/spotlight.frag b/BaseGLProject/spotlight.frag index c12e2a5..dbb1653 100644 --- a/BaseGLProject/spotlight.frag +++ b/BaseGLProject/spotlight.frag @@ -43,27 +43,22 @@ void main() vec3 V = normalize(-pos); //vec3 R = normalize(reflect(-L, N)); vec3 H = normalize(V + L); - float SpotAttenuation = 1.0; - vec spotDot = dot(Light[i].SpotCutoff, Light[i].SpotInnerCutoff); - float theta = dot(L, normalize(Light[i].SpotDirection)); //Angle between the light direction and the fragment-light vector. - float phi = ; // Angle between the light direction and the inner cutoff. + float SpotAttenuation = 1.0; + float spotDot = dot(-L, normalize(Light[i].SpotDirection)); - - - if (theta < Light[i].SpotInnerCutoff) - { - SpotAttenuation = 1.0; //No change in the attenuation - } - else + if (spotDot > cos(radians(Light[i].SpotInnerCutoff))) { //Fragment is outside the Cone //Interpolate between cos(Innercutoff )and cos(cutoff) with cos(theta) - float Spot = smoothstep(cos(Light[i].SpotCutoff),cos(Light[i].SpotInnerCutoff),cos(theta)); - SpotAttenuation = pow(Spot, Light[i].SpotExponent); + float spotValue = smoothstep(cos(radians(Light[i].SpotCutoff)), + cos(radians(Light[i].SpotInnerCutoff)), + spotDot); + SpotAttenuation = pow(spotValue, Light[i].SpotExponent); } attenuation *= SpotAttenuation; + vec3 ambient = Ka * Light[i].Intensity * attenuation; vec3 diffuse = Kd * Light[i].Intensity * max(dot(L, N), 0.0) * attenuation; vec3 specular = Ks * Light[i].Intensity * pow(max(dot(H, N), 0.0), Shininess) * attenuation;