Add Directional Light to world class

This commit is contained in:
4VRDriver
2020-09-15 13:46:17 +02:00
parent 6dd36f4254
commit 56190d3cb6
8 changed files with 95 additions and 53 deletions

View File

@@ -11,6 +11,10 @@ struct Material {
sampler2D texture_diffuse1;
sampler2D texture_specular0;
sampler2D texture_specular1;
sampler2D texture_normal0;
sampler2D texture_normal1;
sampler2D texture_gloss0;
sampler2D texture_gloss1;
float shininess;
};
uniform Material u_material;

View File

@@ -82,41 +82,28 @@ void Controller::run() {
//Model model_plant("res/models/plant.ffo");
Model model_container("res/models/container.ffo");
Model model_cube("res/models/cube.ffo");
Model model_dragon("res/models/dragon.ffo");
//Model model_dragon("res/models/dragon.ffo");
Model model_hut("res/models/hut.ffo");
//Model model_sphere("res/models/sphere.ffo");
//Entity backpack(&model_backpack, &shaderProgram);
//Entity sphere(&model_sphere, &shaderProgram);
Entity container(&model_container, &shaderProgram);
Entity dragon(&model_dragon, &shaderProgram);
//Entity container(&model_container, &shaderProgram);
Entity hut(&model_hut, &shaderProgram);
//Entity dragon(&model_dragon, &shaderProgram);
//Entity plant(&model_plant, &shaderProgram);
Entity lightSource(&model_cube, &lightProgram);
lightSource.translate(glm::vec3(-5.0f, 1.0f, 0.0f));
lightSource.setScale(0.2f);
//plant.setScale(5.0f);
dragon.setScale(0.2f);
glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f);
glm::vec3 diffuseColor = lightColor * glm::vec3(1.0f);
glm::vec3 ambientColor = diffuseColor * glm::vec3(0.1f);
glm::vec3 specularColor = glm::vec3(1.0f);
shaderProgram.bind();
shaderProgram.setUniform("u_directionalLight.isActive", 1);
shaderProgram.setUniform("u_directionalLight.direction", glm::vec3(-0.2f, -1.0f, -0.3f));
shaderProgram.setUniform("u_directionalLight.ambient", ambientColor * 0.25f);
shaderProgram.setUniform("u_directionalLight.diffuse", diffuseColor * 0.25f);
shaderProgram.setUniform("u_directionalLight.specular", specularColor * 0.25f);
shaderProgram.setUniform("u_material.shininess", 32.0f);
shaderProgram.unbind();
//dragon.setScale(0.2f);
World world(&shaderProgram);
world.addEntity(dragon);
world.addEntity(hut);
world.addEntity(lightSource);
world.updateLight(0, lightSource.getPosition(), glm::vec3(1.0f));
world.updatePointLight(0, true, lightSource.getPosition(), glm::vec3(1.0f));
camera->translate(glm::vec3(0.0f, 0.0f, 7.5f));

View File

@@ -91,8 +91,8 @@ void EventHandler::mouse_callback(GLFWwindow* window, double xpos, double ypos)
// Check if this is the first VALID mouse event after window being resized
if(firstMouseInput && !(deltaCursorPosX == 0 && deltaCursorPosY == 0)) {
firstMouseInput = 0;
deltaCursorPosX = 0.0f;
deltaCursorPosY = 0.0f;
deltaCursorPosX = 0.0;
deltaCursorPosY = 0.0;
}
deltaCursorPosX *= mouseSensitivity;

View File

@@ -2,6 +2,8 @@
#include <string>
// PointLight
PointLight::PointLight(ShaderProgram *shaderProgram)
: Light(shaderProgram) {
@@ -9,32 +11,44 @@ PointLight::PointLight(ShaderProgram *shaderProgram)
void PointLight::update() {
std::string uName;
shaderProgram->bind();
shaderProgram->setUniform((_getStructMemberName() + "isActive").c_str(), isActive);
shaderProgram->setUniform((_getStructMemberName() + "position").c_str(), position);
shaderProgram->setUniform((_getStructMemberName() + "ambient").c_str(), ambientColor);
shaderProgram->setUniform((_getStructMemberName() + "diffuse").c_str(), diffuseColor);
shaderProgram->setUniform((_getStructMemberName() + "specular").c_str(), specularColor);
shaderProgram->setUniform((_getStructMemberName() + "K_c").c_str(), K_c);
shaderProgram->setUniform((_getStructMemberName() + "K_l").c_str(), K_l);
shaderProgram->setUniform((_getStructMemberName() + "K_q").c_str(), K_q);
shaderProgram->unbind();
}
std::string PointLight::_getStructMemberName() {
std::string temp = "u_pointLight[" + std::to_string(lightId) + "].";
return temp;
}
// DirectionalLight
DirectionalLight::DirectionalLight(ShaderProgram *shaderProgram)
: Light(shaderProgram) {
}
void DirectionalLight::update() {
shaderProgram->bind();
uName = "u_pointLight[" + std::to_string(lightId) + "].isActive";
shaderProgram->setUniform(uName.c_str(), isActive);
uName = "u_pointLight[" + std::to_string(lightId) + "].position";
shaderProgram->setUniform(uName.c_str(), position);
shaderProgram->setUniform("u_directionalLight.isActive", isActive);
shaderProgram->setUniform("u_directionalLight.direction", direction);
shaderProgram->setUniform("u_directionalLight.ambient", ambientColor * 0.25f);
shaderProgram->setUniform("u_directionalLight.diffuse", diffuseColor * 0.25f);
shaderProgram->setUniform("u_directionalLight.specular", specularColor * 0.25f);
uName = "u_pointLight[" + std::to_string(lightId) + "].ambient";
shaderProgram->setUniform(uName.c_str(), ambientColor);
uName = "u_pointLight[" + std::to_string(lightId) + "].diffuse";
shaderProgram->setUniform(uName.c_str(), diffuseColor);
uName = "u_pointLight[" + std::to_string(lightId) + "].specular";
shaderProgram->setUniform(uName.c_str(), specularColor);
uName = "u_pointLight[" + std::to_string(lightId) + "].K_c";
shaderProgram->setUniform(uName.c_str(), K_c);
uName = "u_pointLight[" + std::to_string(lightId) + "].K_l";
shaderProgram->setUniform(uName.c_str(), K_l);
uName = "u_pointLight[" + std::to_string(lightId) + "].K_q";
shaderProgram->setUniform(uName.c_str(), K_q);
shaderProgram->unbind();
}

View File

@@ -12,11 +12,15 @@ public:
virtual void update() = 0;
void setActive(bool active) { isActive = active; }
void setActive(bool active) {
isActive = active;
update();
}
void setColor(glm::vec3 color) {
lightColor = color;
update();
}
void setShaderProgram(ShaderProgram *shaderProgram) {
this->shaderProgram = shaderProgram;
update();
@@ -50,12 +54,14 @@ public:
this->position = position;
update();
}
void setId(unsigned int id) { lightId = id; }
void update();
private:
void update() override;
std::string _getStructMemberName();
unsigned int lightId;
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
@@ -64,3 +70,25 @@ private:
float K_q = 0.032f;
};
class DirectionalLight : public Light {
public:
DirectionalLight() = default;
DirectionalLight(ShaderProgram *shaderProgram);
void setDirection(glm::vec3 direction) {
this->direction = direction;
update();
}
private:
void update() override;
bool isActive = true;
glm::vec3 direction = glm::vec3(-0.2f, -1.0f, -0.3f);
};

View File

@@ -19,8 +19,8 @@ Texture::Texture(const char* texturePath, uint8_t textureType) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
if(textureBuffer) {

View File

@@ -9,11 +9,20 @@
World::World(ShaderProgram *shaderProgram)
: shaderProgram(shaderProgram) {
// PointLights
for(unsigned int i = 0; i < NUM_POINT_LIGHTS; i++) {
pointLights[i].setId(i);
pointLights[i].setShaderProgram(shaderProgram);
}
// DirectionalLight
directionalLight.setShaderProgram(shaderProgram);
// This will be removed in future when gloss maps are implemented
shaderProgram->bind();
shaderProgram->setUniform("u_material.shininess", 32.0f);
shaderProgram->unbind();
}
void World::addEntity(Entity entity) {
@@ -33,8 +42,8 @@ void World::removeEntity(uint32_t id) {
std::cout << "[Warning] Entity with ID " << id << " could not be removed." << std::endl;
}
void World::updateLight(unsigned int lightId, glm::vec3 position, glm::vec3 color) {
pointLights[lightId].setActive(true);
void World::updatePointLight(unsigned int lightId, bool active, glm::vec3 position, glm::vec3 color) {
pointLights[lightId].setActive(active);
pointLights[lightId].setPosition(position);
pointLights[lightId].setColor(color);
}

View File

@@ -16,7 +16,7 @@ public:
void addEntity(Entity entity);
void removeEntity(uint32_t id);
void updateLight(unsigned int lightId, glm::vec3 position, glm::vec3 color);
void updatePointLight(unsigned int lightId, bool active, glm::vec3 position, glm::vec3 color);
std::vector<Entity> * getEntities() { return &entities; }
@@ -28,7 +28,7 @@ private:
std::vector<Entity> entities;
//DirectionalLight directionalLight;
DirectionalLight directionalLight;
PointLight pointLights[NUM_POINT_LIGHTS];
//SpotLight spotLight;