Load lights from Json file and fix a bug in pointshadows
This commit is contained in:
@@ -109,8 +109,7 @@ void Controller::run()
|
||||
}
|
||||
static glm::vec3 lightColor = glm::vec3(1.f);
|
||||
static float intensity = 20.f;
|
||||
world->updatePointLight(0, true, world->getEntityByName("light")->getPosition(), lightColor * intensity);
|
||||
world->updateDirectionalLight(true, glm::vec3(-0.2f, -1.0f, -0.3f), lightColor * 0.25f);
|
||||
world->updatePointLight(0, true, world->getEntityByName("light")->getPosition(), lightColor, intensity);
|
||||
getShaderProgramByName("lightProgram")->bind();
|
||||
getShaderProgramByName("lightProgram")->setUniform("v_lightColor", lightColor * 100.0f);
|
||||
getShaderProgramByName("lightProgram")->unbind();
|
||||
@@ -145,7 +144,7 @@ void Controller::run()
|
||||
pp_framebuffer->render();
|
||||
|
||||
#ifdef _DEBUG
|
||||
renderImGui(world, &world->getPointLights()[0], &lightColor, &rotateEntity, &rotateLightSource, getShaderProgramByName("postProcessingProgram"), &intensity, &drawShadows);
|
||||
renderImGui(world, world->getPointLights()[0], &lightColor, &rotateEntity, &rotateLightSource, getShaderProgramByName("postProcessingProgram"), &intensity, &drawShadows);
|
||||
#endif
|
||||
|
||||
glfwSwapBuffers(gameWindow->getGLFWwindow());
|
||||
|
||||
@@ -130,6 +130,83 @@ std::vector<ShaderProgram *> JsonParser::getShaderPrograms()
|
||||
return temp_shaderPrograms;
|
||||
}
|
||||
|
||||
std::vector<Light*> JsonParser::getLights(ShaderProgram* shaderProgram)
|
||||
{
|
||||
std::vector<Light*> temp_lights;
|
||||
glm::vec3 light_direction = {1.0f, 0.0f, 0.0f};
|
||||
glm::vec3 light_position = {};
|
||||
glm::vec3 light_color = {1.0f, 1.0f, 1.0f};
|
||||
float light_intensity = 10.0f;
|
||||
|
||||
const Json::Value directionalLightsJson = root["directionalLight"];
|
||||
|
||||
const Json::Value directionJson = directionalLightsJson["direction"];
|
||||
Json::Value colorJson = directionalLightsJson["color"];
|
||||
Json::Value intensityJson = directionalLightsJson["intensity"];
|
||||
|
||||
if(!intensityJson.empty()) {
|
||||
light_intensity = intensityJson.asFloat();
|
||||
}
|
||||
if(!directionJson.empty()) {
|
||||
light_direction.x = directionJson[0].asFloat();
|
||||
light_direction.y = directionJson[1].asFloat();
|
||||
light_direction.z = directionJson[2].asFloat();
|
||||
}
|
||||
if(!colorJson.empty()) {
|
||||
light_color.x = colorJson[0].asFloat();
|
||||
light_color.y = colorJson[1].asFloat();
|
||||
light_color.z = colorJson[2].asFloat();
|
||||
}
|
||||
|
||||
DirectionalLight *current_directionalLight = new DirectionalLight(light_direction, light_color, light_intensity, shaderProgram);
|
||||
current_directionalLight->setActive(true);
|
||||
temp_lights.push_back(current_directionalLight);
|
||||
|
||||
// Pointlights
|
||||
const Json::Value pointLightsJson = root["pointLights"];
|
||||
|
||||
|
||||
unsigned int index = 0;
|
||||
for (; index < pointLightsJson.size(); index++) {
|
||||
PointLight *current_pointLight;
|
||||
|
||||
const Json::Value positionJson = pointLightsJson[index]["position"];
|
||||
colorJson = pointLightsJson[index]["color"];
|
||||
intensityJson = pointLightsJson[index]["intensity"];
|
||||
|
||||
if(!intensityJson.empty()) {
|
||||
light_intensity = intensityJson.asFloat();
|
||||
}
|
||||
if(!positionJson.empty()) {
|
||||
light_position.x = positionJson[0].asFloat();
|
||||
light_position.y = positionJson[1].asFloat();
|
||||
light_position.z = positionJson[2].asFloat();
|
||||
}
|
||||
if(!colorJson.empty()) {
|
||||
light_color.x = colorJson[0].asFloat();
|
||||
light_color.y = colorJson[1].asFloat();
|
||||
light_color.z = colorJson[2].asFloat();
|
||||
}
|
||||
|
||||
current_pointLight = new PointLight(light_position, light_color, light_intensity, shaderProgram);
|
||||
current_pointLight->setActive(true);
|
||||
temp_lights.push_back(current_pointLight);
|
||||
}
|
||||
|
||||
// In case there aren't enough PointLights defined in the Json file
|
||||
for(; NUM_POINT_LIGHTS - index > 0; index++) {
|
||||
const glm::vec3 default_position(0.0f);
|
||||
const glm::vec3 default_color(1.0f);
|
||||
const float default_intensity = 10.0f;
|
||||
PointLight *current_pointLight = new PointLight(default_position, default_color, default_intensity, shaderProgram);
|
||||
current_pointLight->setActive(false);
|
||||
temp_lights.push_back(current_pointLight);
|
||||
}
|
||||
|
||||
return temp_lights;
|
||||
}
|
||||
|
||||
|
||||
Skybox *JsonParser::getSkybox(Model *cubeModel, ShaderProgram *skyboxProgram)
|
||||
{
|
||||
Skybox* temp_skybox;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "Model.h"
|
||||
#include "Entity.h"
|
||||
#include "ShaderProgram.h"
|
||||
#include "Light.h"
|
||||
|
||||
#include <jsoncpp/json/json.h>
|
||||
#include <string>
|
||||
@@ -16,6 +17,7 @@ public:
|
||||
|
||||
std::vector<Model*> getModels();
|
||||
std::vector<Entity*> getEntities(std::vector<Model*> &models, std::vector<ShaderProgram*> shaderPrograms);
|
||||
std::vector<Light*> getLights(ShaderProgram* shaderProgram);
|
||||
Skybox *getSkybox(Model *cubeModel, ShaderProgram *skyboxProgram);
|
||||
|
||||
std::vector<ShaderProgram*> getShaderPrograms();
|
||||
|
||||
@@ -2,8 +2,22 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
uint32_t Light::id_counter = 0;
|
||||
|
||||
// Light
|
||||
|
||||
Light::Light(glm::vec3 color, float intensity, ShaderProgram* shaderProgram) :
|
||||
shaderProgram(shaderProgram),
|
||||
intensity(intensity)
|
||||
{
|
||||
id = id_counter++;
|
||||
lightColor = color * intensity;
|
||||
diffuseColor = lightColor * glm::vec3(1.0f);
|
||||
ambientColor = diffuseColor * glm::vec3(0.002f);
|
||||
specularColor = lightColor * glm::vec3(1.0f);
|
||||
}
|
||||
|
||||
|
||||
glm::vec3 Light::getColor()
|
||||
{
|
||||
return lightColor;
|
||||
@@ -17,13 +31,19 @@ void Light::setShaderProgram(ShaderProgram* shaderProgram)
|
||||
|
||||
void Light::setColor(glm::vec3 color)
|
||||
{
|
||||
lightColor = color;
|
||||
lightColor = color * intensity;
|
||||
diffuseColor = lightColor * glm::vec3(1.0f);
|
||||
ambientColor = diffuseColor * glm::vec3(0.002f);
|
||||
specularColor = lightColor * glm::vec3(1.0f);
|
||||
update();
|
||||
}
|
||||
|
||||
void Light::setIntensity(float intensity)
|
||||
{
|
||||
this->intensity = intensity;
|
||||
}
|
||||
|
||||
|
||||
void Light::setActive(bool active)
|
||||
{
|
||||
isActive = active;
|
||||
@@ -32,8 +52,9 @@ void Light::setActive(bool active)
|
||||
|
||||
// PointLight
|
||||
|
||||
PointLight::PointLight(ShaderProgram *shaderProgram) :
|
||||
Light(shaderProgram)
|
||||
PointLight::PointLight(glm::vec3 position, glm::vec3 color, float intensity, ShaderProgram *shaderProgram) :
|
||||
Light(color, intensity, shaderProgram),
|
||||
position(position)
|
||||
{
|
||||
// Empty
|
||||
}
|
||||
@@ -54,7 +75,7 @@ void PointLight::update()
|
||||
|
||||
std::string PointLight::getStructMemberName()
|
||||
{
|
||||
std::string temp = "u_pointLight[" + std::to_string(lightId) + "].";
|
||||
std::string temp = "u_pointLight[" + std::to_string(id) + "].";
|
||||
return temp;
|
||||
}
|
||||
|
||||
@@ -74,15 +95,11 @@ void PointLight::setParameters(float K_q)
|
||||
this->K_q = K_q;
|
||||
}
|
||||
|
||||
void PointLight::setId(unsigned int id)
|
||||
{
|
||||
this->lightId = id;
|
||||
}
|
||||
|
||||
// DirectionalLight
|
||||
|
||||
DirectionalLight::DirectionalLight(ShaderProgram *shaderProgram) :
|
||||
Light(shaderProgram)
|
||||
DirectionalLight::DirectionalLight(glm::vec3 direction, glm::vec3 color, float intensity, ShaderProgram *shaderProgram) :
|
||||
Light(color, intensity, shaderProgram),
|
||||
direction(direction)
|
||||
{
|
||||
// Empty
|
||||
}
|
||||
|
||||
19
src/Light.h
19
src/Light.h
@@ -4,7 +4,7 @@
|
||||
|
||||
#include "ShaderProgram.h"
|
||||
|
||||
#define NUM_POINT_LIGHTS 16
|
||||
#define NUM_POINT_LIGHTS 4
|
||||
|
||||
class Light
|
||||
{
|
||||
@@ -15,19 +15,25 @@ public:
|
||||
|
||||
void setActive(bool active);
|
||||
void setColor(glm::vec3 color);
|
||||
void setIntensity(float intensity);
|
||||
void setShaderProgram(ShaderProgram *shaderProgram);
|
||||
|
||||
glm::vec3 getColor();
|
||||
|
||||
protected:
|
||||
Light(ShaderProgram *shaderProgram) : shaderProgram(shaderProgram) {}
|
||||
Light(glm::vec3 color, float intensity, ShaderProgram *shaderProgram);
|
||||
|
||||
protected:
|
||||
ShaderProgram *shaderProgram;
|
||||
|
||||
bool isActive = false;
|
||||
uint32_t id;
|
||||
static uint32_t id_counter;
|
||||
|
||||
bool isActive;
|
||||
bool shouldCastShadow = true;
|
||||
|
||||
float intensity;
|
||||
|
||||
// Color
|
||||
glm::vec3 lightColor;
|
||||
glm::vec3 diffuseColor;
|
||||
@@ -38,12 +44,11 @@ protected:
|
||||
class PointLight : public Light
|
||||
{
|
||||
public:
|
||||
PointLight(ShaderProgram *shaderProgram);
|
||||
PointLight(glm::vec3 position, glm::vec3 color, float intensity, ShaderProgram *shaderProgram);
|
||||
~PointLight() = default;
|
||||
|
||||
void setPosition(glm::vec3 position);
|
||||
void setParameters(float K_q);
|
||||
void setId(unsigned int id);
|
||||
|
||||
glm::vec3 getPosition();
|
||||
|
||||
@@ -52,8 +57,6 @@ private:
|
||||
std::string getStructMemberName();
|
||||
|
||||
private:
|
||||
unsigned int lightId;
|
||||
|
||||
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
|
||||
float K_q = 0.032f;
|
||||
@@ -62,7 +65,7 @@ private:
|
||||
class DirectionalLight : public Light
|
||||
{
|
||||
public:
|
||||
DirectionalLight(ShaderProgram *shaderProgram);
|
||||
DirectionalLight(glm::vec3 direction, glm::vec3 color, float intensity, ShaderProgram *shaderProgram);
|
||||
|
||||
void setDirection(glm::vec3 direction);
|
||||
|
||||
|
||||
@@ -6,16 +6,8 @@
|
||||
|
||||
World::World(std::vector<ShaderProgram*> shaderPrograms) :
|
||||
shaderProgram(Controller::getShaderProgramByName(shaderPrograms, "defaultProgram")),
|
||||
directionalLight(shaderProgram),
|
||||
depthMapDirectionalFBO(DEPTHMAP_NORMAL, SHADOW_RES)
|
||||
{
|
||||
// PointLights
|
||||
for (unsigned int i = 0; i < NUM_POINT_LIGHTS; i++) {
|
||||
PointLight new_pointLight(shaderProgram);
|
||||
new_pointLight.setId(i);
|
||||
pointLights.push_back(new_pointLight);
|
||||
}
|
||||
|
||||
// Create 4 depthMaps
|
||||
for (int i = 0; i < 4; i++) {
|
||||
DepthMap *temp_depthMap = new DepthMap(DEPTHMAP_CUBEMAP, SHADOW_RES);
|
||||
@@ -27,10 +19,13 @@ World::World(std::vector<ShaderProgram*> shaderPrograms) :
|
||||
shaderProgram->setUniform("u_material.shininess", 100.0f);
|
||||
shaderProgram->unbind();
|
||||
|
||||
JsonParser worldParser("res/models.json");
|
||||
models = worldParser.getModels();
|
||||
entities = worldParser.getEntities(models, shaderPrograms);
|
||||
skybox = worldParser.getSkybox(getModelByName("cube"), Controller::getShaderProgramByName(shaderPrograms, "skyboxProgram"));
|
||||
JsonParser modelParser("res/models.json");
|
||||
models = modelParser.getModels();
|
||||
entities = modelParser.getEntities(models, shaderPrograms);
|
||||
skybox = modelParser.getSkybox(getModelByName("cube"), Controller::getShaderProgramByName(shaderPrograms, "skyboxProgram"));
|
||||
|
||||
JsonParser lightParser("res/lights.json");
|
||||
lights = lightParser.getLights(shaderProgram);
|
||||
}
|
||||
|
||||
World::~World()
|
||||
@@ -74,19 +69,21 @@ void World::clearEntities()
|
||||
}
|
||||
}
|
||||
|
||||
void World::updatePointLight(unsigned int lightId, bool active, glm::vec3 position, glm::vec3 color)
|
||||
void World::updatePointLight(unsigned int lightId, bool active, glm::vec3 position, glm::vec3 color, float intensity)
|
||||
{
|
||||
pointLights[lightId].setActive(active);
|
||||
pointLights[lightId].setPosition(position);
|
||||
pointLights[lightId].setColor(color);
|
||||
std::vector<PointLight*> pointLights = getPointLights();
|
||||
pointLights[lightId]->setActive(active);
|
||||
pointLights[lightId]->setPosition(position);
|
||||
pointLights[lightId]->setIntensity(intensity);
|
||||
pointLights[lightId]->setColor(color);
|
||||
}
|
||||
|
||||
void World::updateDirectionalLight(bool active, glm::vec3 direction, glm::vec3 color)
|
||||
{
|
||||
directionalLight.setActive(active);
|
||||
directionalLight.setDirection(direction);
|
||||
directionalLight.setDirection(glm::vec3(-0.2f, -1.0f, -0.3f));
|
||||
directionalLight.setColor(color);
|
||||
DirectionalLight *directionalLight = getDirectionalLight();
|
||||
directionalLight->setActive(active);
|
||||
directionalLight->setDirection(direction);
|
||||
directionalLight->setColor(color);
|
||||
}
|
||||
|
||||
void World::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition)
|
||||
@@ -140,6 +137,7 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg
|
||||
shaderProgram->unbind();
|
||||
|
||||
// --- Point shadows ---
|
||||
std::vector<PointLight*> pointLights = getPointLights();
|
||||
|
||||
// 4 depthMaps for 4 point lights
|
||||
for (int i = 0; i < 1; i++) {
|
||||
@@ -149,11 +147,11 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg
|
||||
|
||||
// Create 6 view matrices for every face of the cubeMap
|
||||
std::vector<glm::mat4> viewProjMatrices;
|
||||
glm::vec3 lightPos = pointLights[i].getPosition();
|
||||
glm::vec3 lightPos = pointLights[i]->getPosition();
|
||||
viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
|
||||
viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
|
||||
viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)));
|
||||
viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)));
|
||||
viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f)));
|
||||
viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
|
||||
viewProjMatrices.push_back(pointLightProjection * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
|
||||
|
||||
@@ -224,12 +222,34 @@ Entity* World::getEntityById(uint32_t id)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
PointLight* World::getPointLights()
|
||||
std::vector<PointLight*> World::getPointLights()
|
||||
{
|
||||
return pointLights.data();
|
||||
std::vector<PointLight*> temp_pointLights;
|
||||
|
||||
for (auto it = lights.begin(); it != lights.end(); it++) {
|
||||
PointLight *temp_pointLight = dynamic_cast<PointLight*>(*it);
|
||||
if (temp_pointLight) {
|
||||
temp_pointLights.push_back(temp_pointLight);
|
||||
}
|
||||
}
|
||||
|
||||
return temp_pointLights;
|
||||
}
|
||||
|
||||
std::vector< Entity* > World::getEntities()
|
||||
DirectionalLight * World::getDirectionalLight()
|
||||
{
|
||||
DirectionalLight *temp_directionalLight = nullptr;
|
||||
|
||||
for (auto it = lights.begin(); it != lights.end(); it++) {
|
||||
temp_directionalLight = dynamic_cast<DirectionalLight*>(*it);
|
||||
if(temp_directionalLight)
|
||||
break;
|
||||
}
|
||||
|
||||
return temp_directionalLight;
|
||||
}
|
||||
|
||||
std::vector<Entity*> World::getEntities()
|
||||
{
|
||||
return entities;
|
||||
}
|
||||
|
||||
10
src/World.h
10
src/World.h
@@ -18,11 +18,12 @@ public:
|
||||
void removeEntityByName(std::string name);
|
||||
void clearEntities();
|
||||
|
||||
void updatePointLight(unsigned int lightId, bool active, glm::vec3 position, glm::vec3 color);
|
||||
void updatePointLight(unsigned int lightId, bool active, glm::vec3 position, glm::vec3 color, float intensity);
|
||||
void updateDirectionalLight(bool active, glm::vec3 direction, glm::vec3 color);
|
||||
|
||||
std::vector<Entity*> getEntities();
|
||||
PointLight *getPointLights();
|
||||
std::vector<PointLight*> getPointLights();
|
||||
DirectionalLight * getDirectionalLight();
|
||||
Skybox *getSkybox();
|
||||
Entity* getEntityByName(std::string name);
|
||||
Entity* getEntityById(uint32_t id);
|
||||
@@ -39,8 +40,9 @@ private:
|
||||
Skybox *skybox;
|
||||
|
||||
// Lights
|
||||
DirectionalLight directionalLight;
|
||||
std::vector<PointLight> pointLights;
|
||||
std::vector<Light*> lights;
|
||||
//DirectionalLight directionalLight;
|
||||
//std::vector<PointLight> pointLights;
|
||||
//SpotLight spotLight;
|
||||
|
||||
// Shadows
|
||||
|
||||
Reference in New Issue
Block a user