diff --git a/src/Camera.cpp b/src/Camera.cpp index ca55886..d874ef8 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -4,10 +4,10 @@ #include #include -Camera::Camera(float fov, int width, int height) { +Camera::Camera(float fov, float aspectRatio) { this->fov = fov; viewMatrix = glm::mat4(1.0f); - updateAspectRatio(width, height); + updateAspectRatio(aspectRatio); updateVPM(); } @@ -15,9 +15,9 @@ void Camera::updateVPM() { viewProjectionMatrix = projectionMatrix * viewMatrix; } -void Camera::updateAspectRatio(int width, int height) { +void Camera::updateAspectRatio(float aspectRatio) { //projectionMatrix = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -10.f, 100.0f); - projectionMatrix = glm::perspective(fov/2.0f, (float)width / (float)height, 0.1f, 1000.0f); + projectionMatrix = glm::perspective(fov/2.0f, aspectRatio, 0.1f, 1000.0f); updateVPM(); } diff --git a/src/Camera.h b/src/Camera.h index 737c214..063c668 100644 --- a/src/Camera.h +++ b/src/Camera.h @@ -6,11 +6,11 @@ class Camera { public: - Camera(float fov, int width, int height); + Camera(float fov, float aspectRatio); ~Camera() = default; void updateVPM(); - void updateAspectRatio(int width, int height); + void updateAspectRatio(float aspectRatio); void updatePositionFromKeyboardInput(bool *actionCameraRegister, float deltaTime); void updateDirectionFromMouseInput(double *cameraMouseActionRegister); diff --git a/src/Controller.cpp b/src/Controller.cpp index 2cf8356..24ced32 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -38,7 +38,8 @@ Controller::Controller() { gameWindow = new Window(); gameEventHandler = new EventHandler(gameWindow->getGLFWwindow()); - camera = new Camera(90.0f, gameWindow->getWindowWidth(), gameWindow->getWindowHeight()); + + camera = new Camera(90.0f, gameWindow->getWindowAspectRatio()); #ifdef _DEBUG glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); @@ -77,8 +78,6 @@ void Controller::run() { ShaderProgram shaderProgram("res/shaders/basic.vert", "res/shaders/basic.frag"); ShaderProgram lightProgram("res/shaders/light.vert", "res/shaders/light.frag"); - std::vector scene; - //Model model_backpack("res/models/backpack.ffo"); //Model model_plant("res/models/plant.ffo"); Model model_container("res/models/container.ffo"); @@ -113,9 +112,8 @@ void Controller::run() { shaderProgram.unbind(); Scene scene(&shaderProgram); - - scene.push_back(dragon); - scene.push_back(lightSource); + scene.addEntity(dragon); + scene.addEntity(lightSource); camera->translate(glm::vec3(0.0f, 0.0f, 7.5f)); @@ -133,12 +131,10 @@ void Controller::run() { camera->lookForward(); camera->updateVPM(); - for(auto it = scene.begin(); it != scene.end(); it++) { - it->draw(camera->getViewProj(), camera->getPosition()); - } + scene.drawScene(camera->getViewProj(), camera->getPosition()); #ifdef _DEBUG - renderImGui(&scene[0]); + renderImGui(scene.getEntities().data()); #endif glfwSwapBuffers(gameWindow->getGLFWwindow()); @@ -187,7 +183,7 @@ void Controller::error_callback(int error, const char* description) { } void Controller::updateWindowSize() { - camera->updateAspectRatio(gameWindow->getWindowWidth(), gameWindow->getWindowHeight()); + camera->updateAspectRatio(gameWindow->getWindowAspectRatio()); gameEventHandler->setFirstMouseInput(1); } diff --git a/src/Entity.h b/src/Entity.h index bea5387..fe20cad 100644 --- a/src/Entity.h +++ b/src/Entity.h @@ -20,6 +20,9 @@ public: void setOrientiation(glm::vec3 orientation); void setScale(float scaleFactor); + void setId(uint32_t id) { this->id = id; } + uint32_t getId() { return id; } + glm::vec3 getPosition() { return position; } glm::mat4 getModelMatrix() { return modelMatrix; } @@ -27,7 +30,6 @@ private: void updateModelMatrix(); - // May be used later... uint32_t id; glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f); diff --git a/src/Light.cpp b/src/Light.cpp index 7f2fa8b..cbb9ac1 100644 --- a/src/Light.cpp +++ b/src/Light.cpp @@ -5,6 +5,7 @@ PointLight::PointLight(ShaderProgram *shaderProgram) : Light(shaderProgram) { + isActive = false; } diff --git a/src/Light.h b/src/Light.h index 792e581..ab2c4d7 100644 --- a/src/Light.h +++ b/src/Light.h @@ -13,9 +13,14 @@ public: virtual void update() = 0; void setActive(bool active) { isActive = active; } + void setShaderProgram(ShaderProgram *shaderProgram) { + this->shaderProgram = shaderProgram; + update(); + } protected: + Light() = default; Light(ShaderProgram *shaderProgram) : shaderProgram(shaderProgram) {} ShaderProgram *shaderProgram; @@ -32,15 +37,23 @@ protected: class PointLight : public Light { +public: + + PointLight() = default; PointLight(ShaderProgram *shaderProgram); + void setPosition(glm::vec3 position) { + this->position = position; + update(); + } + void update(); private: unsigned int lightId; - glm::vec3 position; + glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f); float K_c = 1.0f; float K_l = 0.09f; float K_q = 0.032f; diff --git a/src/Scene.cpp b/src/Scene.cpp index 2fc5824..d5a40f6 100644 --- a/src/Scene.cpp +++ b/src/Scene.cpp @@ -1,6 +1,43 @@ #include "Scene.h" +#include + +// At this moment, I don't have any idea how to implement the initializer list +// with an array of objects that don't have an default contructor. So the default +// constructors for Light and PointLight have only this reason to exist. + Scene::Scene(ShaderProgram *shaderProgram) - : shaderProgram(shaderProgram), pointLights(shaderProgram) { + : shaderProgram(shaderProgram) { + + for(unsigned int i = 0; i < NUM_POINT_LIGHTS; i++) { + pointLights[i].setShaderProgram(shaderProgram); + } + + pointLights[0].setActive(true); + +} + +void Scene::addEntity(Entity entity) { + uint32_t new_id = entities.size(); + entity.setId(new_id); + entities.push_back(entity); +} + +void Scene::removeEntity(uint32_t id) { + + for(auto it = entities.begin(); it != entities.end(); it++) { + if(it->getId() == id) { + entities.erase(it); + } + } + + std::cout << "[Warning] No Entity found with ID " << id << std::endl; +} + +void Scene::drawScene(glm::mat4 viewProjMatrix, glm::vec3 viewPosition) { + + for(auto it = entities.begin(); it != entities.end(); it++) { + it->draw(viewProjMatrix, viewPosition); + } } diff --git a/src/Scene.h b/src/Scene.h index 6990f15..20ca2c8 100644 --- a/src/Scene.h +++ b/src/Scene.h @@ -1,17 +1,31 @@ #pragma once +#include + #include "Light.h" +#include "Camera.h" +#include "Entity.h" class Scene { public: Scene(ShaderProgram *shaderProgram); + ~Scene() = default; + + void addEntity(Entity entity); + void removeEntity(uint32_t id); + + std::vector getEntities() { return entities; } + + void drawScene(glm::mat4 viewProjMatrix, glm::vec3 viewPosition); private: ShaderProgram *shaderProgram; + std::vector entities; + //DirectionalLight directionalLight; PointLight pointLights[NUM_POINT_LIGHTS]; //SpotLight spotLight; diff --git a/src/Window.h b/src/Window.h index 4b96fbc..539e480 100644 --- a/src/Window.h +++ b/src/Window.h @@ -13,6 +13,7 @@ public: int getWindowWidth() { return width; } int getWindowHeight() { return height; } + float getWindowAspectRatio() { return (float) width / (float) height; } bool getMouseIsCatched() { return mouseCatched; }