Spotlight still in the works

This commit is contained in:
Hurlu 2019-03-20 19:01:16 +09:00
parent 87b2b8c28d
commit 1f558e3f1c
5 changed files with 36 additions and 21 deletions

View File

@ -5,12 +5,14 @@ Light::Light(glm::vec3 intensity_, glm::vec4 location_) : location(location_), i
type = BASE; 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_) 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; type = SPOT;
direction = lookAtPosition_;
} }

View File

@ -13,7 +13,9 @@ public:
}; };
Light(glm::vec3 intensity, glm::vec4 location); 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(); ~Light();
bool activated = true; bool activated = true;
@ -23,6 +25,7 @@ public:
float spot_cutoff; float spot_cutoff;
float spot_inner_cutoff; float spot_inner_cutoff;
float spot_exponent; float spot_exponent;
glm::vec4 lookAt;
glm::vec3 direction; glm::vec3 direction;
}; };

View File

@ -102,7 +102,8 @@ void MyGlWindow::shaderSetup()
void MyGlWindow::lightSetup() void MyGlWindow::lightSetup()
{ {
//Showcase lights //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 //Party lights
/*_scnctx.lights.emplace("Light1", Light(glm::vec3(0.0f, 0.5f, 0.5f), glm::vec4(10, 10, 0, 1))); /*_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; Dataset moddata;
moddata.checkeredFloor(20, 20, glm::vec3(0.1, 0.1, 0.1), glm::vec3(0.7, 0.7, 0.7)); 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"]->addTranslation(glm::vec4(0, -0.05, 0, 1));
meshes["Floor"]->cullMode = BACK; meshes["Floor"]->cullMode = BACK;
moddata.simpleCube(); /*moddata.simpleCube();
meshes.emplace("Cube", new Mesh(moddata, shaders["BrickBaseLight"])); meshes.emplace("Cube", new Mesh(moddata, shaders["BrickBaseLight"]));
meshes["Cube"]->addTranslation(glm::vec4(0, 1, 0, 1)); meshes["Cube"]->addTranslation(glm::vec4(0, 1, 0, 1));
@ -137,7 +138,7 @@ void MyGlWindow::setup()
meshes["TeapotSilhouette"]->cullMode = FRONT; meshes["TeapotSilhouette"]->cullMode = FRONT;
meshes.emplace("Teapot", new Mesh("teapot.obj", shaders["Toon"])); meshes.emplace("Teapot", new Mesh("teapot.obj", shaders["Toon"]));
meshes["Teapot"]->addTranslation(glm::vec4(5, 0, 3, 1)); 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.viewMatrix = view;
_scnctx.projectionMatrix = projection; _scnctx.projectionMatrix = projection;
_scnctx.adjustSpots();
for (auto it = meshes.begin(); it != meshes.end(); it++) for (auto it = meshes.begin(); it != meshes.end(); it++)
(*it).second->draw(_scnctx); (*it).second->draw(_scnctx);
} }

View File

@ -18,4 +18,16 @@ struct SceneContext
glm::mat3x3 normalMatrix; glm::mat3x3 normalMatrix;
std::map<std::string, Texture> textures; std::map<std::string, Texture> 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);
}
}
}
}; };

View File

@ -44,26 +44,21 @@ void main()
//vec3 R = normalize(reflect(-L, N)); //vec3 R = normalize(reflect(-L, N));
vec3 H = normalize(V + L); vec3 H = normalize(V + L);
float SpotAttenuation = 1.0; float SpotAttenuation = 1.0;
vec spotDot = dot(Light[i].SpotCutoff, Light[i].SpotInnerCutoff); float spotDot = dot(-L, normalize(Light[i].SpotDirection));
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.
if (spotDot > cos(radians(Light[i].SpotInnerCutoff)))
if (theta < Light[i].SpotInnerCutoff)
{
SpotAttenuation = 1.0; //No change in the attenuation
}
else
{ {
//Fragment is outside the Cone //Fragment is outside the Cone
//Interpolate between cos(Innercutoff )and cos(cutoff) with cos(theta) //Interpolate between cos(Innercutoff )and cos(cutoff) with cos(theta)
float Spot = smoothstep(cos(Light[i].SpotCutoff),cos(Light[i].SpotInnerCutoff),cos(theta)); float spotValue = smoothstep(cos(radians(Light[i].SpotCutoff)),
SpotAttenuation = pow(Spot, Light[i].SpotExponent); cos(radians(Light[i].SpotInnerCutoff)),
spotDot);
SpotAttenuation = pow(spotValue, Light[i].SpotExponent);
} }
attenuation *= SpotAttenuation; attenuation *= SpotAttenuation;
vec3 ambient = Ka * Light[i].Intensity * attenuation;
vec3 diffuse = Kd * Light[i].Intensity * max(dot(L, N), 0.0) * 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; vec3 specular = Ks * Light[i].Intensity * pow(max(dot(H, N), 0.0), Shininess) * attenuation;