From 56190d3cb690b320fe6c65ae320b37a7971a373d Mon Sep 17 00:00:00 2001 From: 4VRDriver <44267643+4VRDriver@users.noreply.github.com> Date: Tue, 15 Sep 2020 13:46:17 +0200 Subject: [PATCH] Add Directional Light to world class --- res/shaders/basic.frag | 4 +++ src/Controller.cpp | 29 ++++++--------------- src/EventHandler.cpp | 4 +-- src/Light.cpp | 58 ++++++++++++++++++++++++++---------------- src/Light.h | 32 +++++++++++++++++++++-- src/Texture.cpp | 4 +-- src/World.cpp | 13 ++++++++-- src/World.h | 4 +-- 8 files changed, 95 insertions(+), 53 deletions(-) diff --git a/res/shaders/basic.frag b/res/shaders/basic.frag index 75cd51c..e2c686a 100644 --- a/res/shaders/basic.frag +++ b/res/shaders/basic.frag @@ -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; diff --git a/src/Controller.cpp b/src/Controller.cpp index 8073bda..8405f65 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -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)); diff --git a/src/EventHandler.cpp b/src/EventHandler.cpp index 35a49dc..37eb504 100644 --- a/src/EventHandler.cpp +++ b/src/EventHandler.cpp @@ -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; diff --git a/src/Light.cpp b/src/Light.cpp index 629a30f..8c745ef 100644 --- a/src/Light.cpp +++ b/src/Light.cpp @@ -2,6 +2,8 @@ #include +// 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(); } diff --git a/src/Light.h b/src/Light.h index af17fcd..f633c8c 100644 --- a/src/Light.h +++ b/src/Light.h @@ -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); + +}; diff --git a/src/Texture.cpp b/src/Texture.cpp index faad32f..11daf51 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -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) { diff --git a/src/World.cpp b/src/World.cpp index d25d3ff..8c3053e 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -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); } diff --git a/src/World.h b/src/World.h index af1c4ae..4bd6e0f 100644 --- a/src/World.h +++ b/src/World.h @@ -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 * getEntities() { return &entities; } @@ -28,7 +28,7 @@ private: std::vector entities; - //DirectionalLight directionalLight; + DirectionalLight directionalLight; PointLight pointLights[NUM_POINT_LIGHTS]; //SpotLight spotLight;