Refactoring...

This commit is contained in:
2021-07-25 21:21:54 +02:00
parent 87c60187bf
commit 4927720c29
27 changed files with 320 additions and 336 deletions

View File

@@ -7,7 +7,6 @@ AllowShortLoopsOnASingleLine: 'false'
BreakBeforeBraces: Mozilla
ColumnLimit: '120'
IndentWidth: '4'
Language: Cpp
PointerAlignment: Right
...

View File

@@ -13,5 +13,4 @@ uniform Material u_material;
void main()
{
f_color = texture(u_material.texture_diffuse0, v_texCoord);
// f_color = vec4(1.0, 0.0, 0.0, 0.5);
}

View File

@@ -7,5 +7,4 @@ uniform mat4 u_modelMatrix;
void main() {
gl_Position = u_modelMatrix * vec4(a_position, 1.0f);
}

View File

@@ -1,15 +1,10 @@
[Window][Debug##Default]
Pos=-18,59
Pos=60,60
Size=400,400
Collapsed=0
[Window][Object Modifier]
Pos=46,33
Size=894,195
Collapsed=0
[Window][Debug Utils]
Pos=26,12
Size=688,343
Pos=9,24
Size=689,372
Collapsed=0

View File

@@ -11,14 +11,13 @@ add_executable(Fall-Fever
Model.cpp
Entity.cpp
Light.cpp
World.cpp
Scene.cpp
FrameBuffer.cpp
Widget.cpp
Screen.cpp
Menu.cpp
JsonParser.cpp
Helper.cpp
GLBucket.cpp
imgui/EntityWindow.cpp
imgui/GeneralInfoWindow.cpp
imgui/Handler.cpp

View File

@@ -22,13 +22,13 @@
#include "Light.h"
#include "Menu.h"
#include "Model.h"
#include "Scene.h"
#include "Screen.h"
#include "ShaderProgram.h"
#include "Texture.h"
#include "VertexArray.h"
#include "Widget.h"
#include "Window.h"
#include "World.h"
Controller::Controller() : m_gameWindow(std::unique_ptr<Window>(new Window))
{
@@ -57,7 +57,7 @@ Controller::Controller() : m_gameWindow(std::unique_ptr<Window>(new Window))
// Show main menu when loading is finished...
m_menu->showScreenByName("mainMenuScreen");
m_world = new World(m_shaderPrograms);
m_world = new Scene(m_shaderPrograms);
#ifdef _DEBUG
m_imguiHandler = std::unique_ptr<Imgui::Handler>(new Imgui::Handler(m_gameWindow->getGLFWwindow()));
@@ -87,11 +87,10 @@ void Controller::run()
{
updateExposure(getShaderProgramByName("postProcessingProgram"));
Entity *lightSource = m_world->getEntityByName("light");
ModelEntity *lightSource = m_world->getEntityByName("light");
lightSource->setScale(0.1f);
lightSource->setRotation(glm::vec3(0.f));
lightSource->setPosition(glm::vec3(-2.f, 1.5f, 2.f));
lightSource->setIsLightSource(true);
m_camera->translate(glm::vec3(0.0f, 1.5f, 5.0f));
@@ -158,7 +157,7 @@ void Controller::run()
m_world->draw(m_camera->getViewProj(), m_camera->getPosition());
m_postProcessFrameBuffer->unbind();
m_postProcessFrameBuffer->render();
m_postProcessFrameBuffer->drawOnEntireScreen();
#ifdef _DEBUG
m_imguiHandler->renderWindows();

View File

@@ -7,7 +7,7 @@
class ShaderProgram;
class Window;
class EventHandler;
class World;
class Scene;
class Camera;
class Menu;
class FrameBuffer;
@@ -39,13 +39,13 @@ private:
void updateWindowDimensions();
void renderImGui(World *world, glm::vec3 *lightColor, bool *rotateEntity, bool *rotateLightSource,
void renderImGui(Scene *world, glm::vec3 *lightColor, bool *rotateEntity, bool *rotateLightSource,
ShaderProgram *postProcessingProgram, float *intensity, bool *drawShadows);
std::unique_ptr<Window> m_gameWindow;
EventHandler *m_gameEventHandler;
World *m_world;
Scene *m_world;
Camera *m_camera;
Menu *m_menu;

View File

@@ -9,15 +9,95 @@
uint32_t Entity::s_idCounter = 0;
Entity::Entity(Prototype prototype, Model *model, ShaderProgram *shaderProgram)
: m_id(s_idCounter++), m_uniqueName(prototype.name), m_model(model), m_shaderProgram(shaderProgram)
Entity::Entity(const std::string &name) : m_id(s_idCounter++), m_uniqueName(name)
{}
Entity::~Entity()
{}
uint32_t Entity::getId() const
{
return m_id;
}
const std::string &Entity::getUniqueName() const
{
return m_uniqueName;
}
void Entity::translate(glm::vec3 vector)
{
m_position += vector;
updateModelMatrix();
}
void Entity::rotate(glm::vec3 axis, float radians)
{
glm::quat rotation = glm::angleAxis(radians, axis);
m_quaternion = rotation * m_quaternion;
updateModelMatrix();
}
void Entity::setPosition(glm::vec3 position)
{
m_position = position;
updateModelMatrix();
}
void Entity::setRotation(glm::vec3 eulerAngles)
{
m_quaternion = glm::quat(eulerAngles);
updateModelMatrix();
}
void Entity::setRotation(glm::vec3 axis, float radians)
{
m_quaternion = glm::angleAxis(radians, axis);
updateModelMatrix();
}
void Entity::setScale(float scale)
{
m_scale = scale;
updateModelMatrix();
}
void Entity::updateModelMatrix()
{
// Translate * Rotate * Scale * vertex_vec;
// First scaling, then rotation, then translation
// Translate
glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), m_position);
// Rotate
glm::mat4 rotationMatrix = glm::toMat4(m_quaternion);
// Scale
glm::mat4 scaleMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(m_scale, m_scale, m_scale));
m_modelMatrix = translationMatrix * rotationMatrix * scaleMatrix;
}
glm::vec3 Entity::getPosition() const
{
return m_position;
}
glm::mat4 Entity::getModelMatrix() const
{
return m_modelMatrix;
}
ModelEntity::ModelEntity(Prototype prototype, Model *model, ShaderProgram *shaderProgram)
: Entity(prototype.name), m_model(model), m_shaderProgram(shaderProgram)
{
setPosition(prototype.position);
setRotation(prototype.rotation);
setScale(prototype.scale);
}
void Entity::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition)
void ModelEntity::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition)
{
m_shaderProgram->bind();
@@ -37,113 +117,29 @@ void Entity::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition)
m_shaderProgram->unbind();
}
void Entity::drawDirectionalShadows(glm::mat4 viewProjMatrix, ShaderProgram *p_shaderProgram)
void ModelEntity::drawDirectionalShadows(glm::mat4 viewProjMatrix, ShaderProgram *shaderProgram)
{
p_shaderProgram->bind();
shaderProgram->bind();
glm::mat4 modelViewProj = viewProjMatrix * m_modelMatrix;
p_shaderProgram->setUniform("u_modelViewProjMatrix", modelViewProj);
shaderProgram->setUniform("u_modelViewProjMatrix", modelViewProj);
// Draw the model
m_model->drawWithoutTextures();
p_shaderProgram->unbind();
shaderProgram->unbind();
}
void Entity::drawPointShadows(ShaderProgram *p_shaderProgram)
void ModelEntity::drawPointShadows(ShaderProgram *shaderProgram)
{
p_shaderProgram->bind();
shaderProgram->bind();
p_shaderProgram->setUniform("u_modelMatrix", m_modelMatrix);
shaderProgram->setUniform("u_modelMatrix", m_modelMatrix);
// Draw the model
m_model->drawWithoutTextures();
p_shaderProgram->unbind();
}
void Entity::translate(glm::vec3 vector)
{
m_position += vector;
updateModelMatrix();
}
void Entity::rotate(glm::vec3 axis, float radians)
{
glm::quat rotation = glm::angleAxis(radians, axis);
m_quaternion = rotation * m_quaternion;
updateModelMatrix();
}
void Entity::setPosition(glm::vec3 position)
{
this->m_position = position;
updateModelMatrix();
}
void Entity::setRotation(glm::vec3 eulerAngles)
{
m_quaternion = glm::quat(eulerAngles);
updateModelMatrix();
}
void Entity::setRotation(glm::vec3 axis, float radians)
{
m_quaternion = glm::angleAxis(radians, axis);
updateModelMatrix();
}
void Entity::setScale(float scaleFactor)
{
m_modelScale = scaleFactor;
updateModelMatrix();
}
void Entity::updateModelMatrix()
{
// Translate * Rotate * Scale * vertex_vec;
// First scaling, then rotation, then translation
// Translate
glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), m_position);
// Rotate
glm::mat4 rotationMatrix = glm::toMat4(m_quaternion);
// Scale
glm::mat4 scaleMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(m_modelScale, m_modelScale, m_modelScale));
m_modelMatrix = translationMatrix * rotationMatrix * scaleMatrix;
}
void Entity::setIsLightSource(bool temp)
{
m_isLightSource = temp;
}
uint32_t Entity::getId()
{
return m_id;
}
std::string Entity::getUniqueName()
{
return m_uniqueName;
}
glm::vec3 Entity::getPosition()
{
return m_position;
}
glm::mat4 Entity::getModelMatrix()
{
return m_modelMatrix;
}
bool Entity::getIsLightSource()
{
return m_isLightSource;
shaderProgram->unbind();
}
Skybox::Skybox(Prototype prototype, Model *cubeModel, ShaderProgram *shaderProgram)

View File

@@ -4,6 +4,7 @@
#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
#include <memory>
class VertexArray;
class ShaderProgram;
@@ -14,20 +15,22 @@ class Entity
public:
struct Prototype
{
Prototype(const std::string &name, glm::vec3 position, glm::vec3 rotation, float scale)
: name(name), position(position), rotation(rotation), scale(scale)
{}
virtual ~Prototype() = default;
std::string name;
std::string modelName;
std::string shaderProgramName;
glm::vec3 position;
glm::vec3 rotation;
float scale;
};
Entity(Prototype prototype, Model *model, ShaderProgram *shaderProgram);
~Entity() = default;
Entity(const std::string &name);
virtual ~Entity() = 0;
void draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition);
void drawDirectionalShadows(glm::mat4 viewProjMatrix, ShaderProgram *p_shaderProgram);
void drawPointShadows(ShaderProgram *p_shaderProgram);
uint32_t getId() const;
const std::string &getUniqueName() const;
void translate(glm::vec3 vector);
void rotate(glm::vec3 axis, float radians);
@@ -35,34 +38,54 @@ public:
void setPosition(glm::vec3 position);
void setRotation(glm::vec3 eulerAngles);
void setRotation(glm::vec3 axis, float radians);
void setScale(float scaleFactor);
void setIsLightSource(bool temp);
void setScale(float scale);
uint32_t getId();
std::string getUniqueName();
glm::vec3 getPosition();
glm::mat4 getModelMatrix();
bool getIsLightSource();
glm::vec3 getPosition() const;
glm::mat4 getModelMatrix() const;
private:
protected:
void updateModelMatrix();
const uint32_t m_id;
static uint32_t s_idCounter;
// TODO
std::weak_ptr<Entity> m_parent;
std::vector<std::shared_ptr<Entity>> m_children;
std::string m_uniqueName;
Model *m_model;
ShaderProgram *m_shaderProgram;
bool m_isLightSource = false;
glm::mat4 m_modelMatrix = glm::mat4(1.0f);
glm::vec3 m_position = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 m_velocity = glm::vec3(0.0f, 0.0f, 0.0f);
glm::quat m_quaternion;
float m_scale = 1.0f;
};
float m_modelScale = 1.0f;
class ModelEntity : public Entity
{
public:
struct Prototype : public Entity::Prototype
{
Prototype(const std::string &name, glm::vec3 position, glm::vec3 rotation, float scale,
const std::string &modelName, const std::string &shaderProgramName)
: Entity::Prototype(name, position, rotation, scale), modelName(modelName),
shaderProgramName(shaderProgramName)
{}
std::string modelName;
std::string shaderProgramName;
};
ModelEntity(Prototype prototype, Model *model, ShaderProgram *shaderProgram);
~ModelEntity() = default;
void draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition);
void drawDirectionalShadows(glm::mat4 viewProjMatrix, ShaderProgram *p_shaderProgram);
void drawPointShadows(ShaderProgram *p_shaderProgram);
private:
Model *m_model;
ShaderProgram *m_shaderProgram;
};
class Skybox

View File

@@ -6,6 +6,24 @@
#include <cstddef>
#include <iostream>
AbstractFrameBuffer::~AbstractFrameBuffer()
{}
void AbstractFrameBuffer::bind() const
{
glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);
}
void AbstractFrameBuffer::unbind() const
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
GLuint AbstractFrameBuffer::getFBO() const
{
return m_FBO;
}
FrameBuffer::FrameBuffer(uint32_t width, uint32_t height, ShaderProgram *shaderProgram) : m_shaderProgram(shaderProgram)
{
glGenFramebuffers(1, &m_FBO);
@@ -21,17 +39,12 @@ FrameBuffer::~FrameBuffer()
glDeleteRenderbuffers(1, &m_depthStencilBuffer);
}
void FrameBuffer::bind()
GLuint FrameBuffer::getTextureId() const
{
glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);
return m_colorBuffer;
}
void FrameBuffer::unbind()
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void FrameBuffer::render()
void FrameBuffer::drawOnEntireScreen() const
{
// Disable wireframe mode
GLint wireframe;
@@ -92,29 +105,24 @@ void FrameBuffer::generateTextures(uint32_t width, uint32_t height)
unbind();
}
void FrameBuffer::setExposureCorrection(bool exposureCorrection)
void FrameBuffer::setExposureCorrection(bool exposureCorrection) const
{
m_shaderProgram->bind();
m_shaderProgram->setUniform("u_exposureCorrection", exposureCorrection);
m_shaderProgram->unbind();
}
GLuint FrameBuffer::getTextureId()
{
return m_colorBuffer;
}
AbstractDepthMap::~AbstractDepthMap()
{}
DepthMap::DepthMap(DepthMapType type, int RESOLUTION)
DepthMap::DepthMap(int RESOLUTION)
{
glGenFramebuffers(1, &m_depthMapFBO);
glGenFramebuffers(1, &m_FBO);
bind();
if (type == DepthMapType::Normal) {
glGenTextures(1, &m_depthMap);
glBindTexture(GL_TEXTURE_2D, m_depthMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, RESOLUTION, RESOLUTION, 0, GL_DEPTH_COMPONENT, GL_FLOAT,
NULL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, RESOLUTION, RESOLUTION, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
@@ -126,14 +134,6 @@ DepthMap::DepthMap(DepthMapType type, int RESOLUTION)
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
} else if (type == DepthMapType::CubeMap) {
m_cubeMap = new CubeMap(RESOLUTION);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_cubeMap->getTextureId(), 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
}
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "[Error] FrameBuffer is not complete!" << std::endl;
@@ -146,27 +146,32 @@ DepthMap::~DepthMap()
// delete m_cubeMap;
}
void DepthMap::bind()
{
glBindFramebuffer(GL_FRAMEBUFFER, m_depthMapFBO);
}
void DepthMap::unbind()
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
GLuint DepthMap::getFBO()
{
return m_depthMapFBO;
}
GLuint DepthMap::getDepthMap()
GLuint DepthMap::getDepthMap() const
{
return m_depthMap;
}
GLuint DepthMap::getCubeMapId()
DepthMapCube::DepthMapCube(int RESOLUTION)
{
glGenFramebuffers(1, &m_FBO);
bind();
m_cubeMap = new CubeMap(RESOLUTION);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_cubeMap->getTextureId(), 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "[Error] FrameBuffer is not complete!" << std::endl;
unbind();
}
DepthMapCube::~DepthMapCube()
{}
GLuint DepthMapCube::getCubeMapTextureId()
{
return m_cubeMap->getTextureId();
}

View File

@@ -5,58 +5,69 @@
class ShaderProgram;
class CubeMap;
class FrameBuffer
class AbstractFrameBuffer
{
public:
virtual ~AbstractFrameBuffer() = 0;
void bind() const;
void unbind() const;
GLuint getFBO() const;
protected:
GLuint m_FBO;
};
class FrameBuffer : public AbstractFrameBuffer
{
public:
FrameBuffer(uint32_t width, uint32_t height, ShaderProgram *shaderProgram);
~FrameBuffer();
void bind();
void unbind();
void render();
void drawOnEntireScreen() const;
void changeDimensions(uint32_t width, uint32_t height);
GLuint getTextureId();
void setExposureCorrection(bool exposureCorrection);
void setExposureCorrection(bool exposureCorrection) const;
GLuint getTextureId() const;
private:
void generateTextures(uint32_t width, uint32_t height);
GLuint m_FBO;
GLuint m_colorBuffer;
GLuint m_depthStencilBuffer;
ShaderProgram *m_shaderProgram;
};
enum class DepthMapType
class AbstractDepthMap : public AbstractFrameBuffer
{
Normal,
CubeMap
public:
virtual ~AbstractDepthMap() = 0;
};
// FrameBuffer without color buffer. (Shadows)
class DepthMap
class DepthMap : public AbstractDepthMap
{
public:
// Normal depthMap with texture and point depthMap with cubeMap
DepthMap(DepthMapType type, int RESOLUTION);
DepthMap(int RESOLUTION);
~DepthMap();
void bind();
void unbind();
GLuint getFBO();
GLuint getDepthMap();
GLuint getCubeMapId();
GLuint getDepthMap() const;
private:
GLuint m_depthMapFBO;
// Either a normal depthMap is used (Directional shadows)
// or a cubeMap is used (Point shadows)
GLuint m_depthMap;
};
class DepthMapCube : public AbstractDepthMap
{
public:
DepthMapCube(int RESOLUTION);
~DepthMapCube();
GLuint getCubeMapTextureId();
private:
CubeMap *m_cubeMap;
};

View File

@@ -1,14 +0,0 @@
#include "GLBucket.h"
std::unique_ptr<GLBucket> GLBucket::s_instance = std::unique_ptr<GLBucket>(new GLBucket);
GLBucket &GLBucket::instance()
{
return *s_instance.get();
}
void GLBucket::runGlCall(const std::function<void(void)> &f)
{
std::lock_guard<std::mutex> lock(m_mutex);
f();
}

View File

@@ -1,19 +0,0 @@
#pragma once
#include <functional>
#include <memory>
#include <mutex>
class GLBucket
{
public:
static GLBucket &instance();
void runGlCall(const std::function<void(void)> &f);
private:
GLBucket() = default;
std::mutex m_mutex;
static std::unique_ptr<GLBucket> s_instance;
};

View File

@@ -41,9 +41,9 @@ std::vector<Model::Prototype> JsonParser::getModelPrototypes() const
return modelPrototypes;
}
std::vector<Entity::Prototype> JsonParser::getEntityPrototypes() const
std::vector<ModelEntity::Prototype> JsonParser::getEntityPrototypes() const
{
std::vector<Entity::Prototype> entityPrototypes;
std::vector<ModelEntity::Prototype> entityPrototypes;
const Json::Value entitiesJson = m_root["entities"];
@@ -71,8 +71,8 @@ std::vector<Entity::Prototype> JsonParser::getEntityPrototypes() const
entityScale = scaleJson.asFloat();
}
Entity::Prototype prototype{entityName, entityModel, entityShaderProgram,
entitiyPosition, entityRotation, entityScale};
ModelEntity::Prototype prototype(entityName, entitiyPosition, entityRotation, entityScale, entityModel,
entityShaderProgram);
entityPrototypes.push_back(prototype);
}
@@ -131,9 +131,6 @@ std::vector<std::unique_ptr<Light::Prototype>> JsonParser::getLightPrototypes()
auto prototype = std::unique_ptr<Light::Prototype>(new DirectionalLight::Prototype{direction, color, intensity});
// DirectionalLight *current_directionalLight = new DirectionalLight(*prototype, shaderProgram);
// current_directionalLight->setActive(true);
prototypes.push_back(std::move(prototype));
// Pointlights
@@ -161,9 +158,6 @@ std::vector<std::unique_ptr<Light::Prototype>> JsonParser::getLightPrototypes()
auto prototype = std::unique_ptr<Light::Prototype>(new PointLight::Prototype{position, color, intensity});
// current_pointLight = new PointLight(*prototype, shaderProgram);
// current_pointLight->setActive(true);
prototypes.push_back(std::move(prototype));
}
@@ -175,9 +169,6 @@ std::vector<std::unique_ptr<Light::Prototype>> JsonParser::getLightPrototypes()
auto prototype = std::unique_ptr<PointLight::Prototype>(
new PointLight::Prototype{default_position, default_color, default_intensity});
// PointLight *current_pointLight = new PointLight(*prototype, shaderProgram);
// current_pointLight->setActive(false);
prototypes.push_back(std::move(prototype));
}

View File

@@ -23,7 +23,7 @@ public:
~JsonParser();
std::vector<Model::Prototype> getModelPrototypes() const;
std::vector<Entity::Prototype> getEntityPrototypes() const;
std::vector<ModelEntity::Prototype> getEntityPrototypes() const;
std::vector<std::unique_ptr<Light::Prototype>> getLightPrototypes() const;
std::vector<Screen::Prototype> getScreenPrototypes() const;
Skybox::Prototype getSkyboxPrototype() const;

View File

@@ -6,7 +6,7 @@
uint32_t Light::s_idCounter = 0;
Light::Light(glm::vec3 color, float intensity, ShaderProgram *shaderProgram)
: m_shaderProgram(shaderProgram), m_intensity(intensity)
: Entity("Light"), m_shaderProgram(shaderProgram), m_intensity(intensity)
{
m_id = s_idCounter++;
m_lightColor = color * intensity;

View File

@@ -1,5 +1,7 @@
#pragma once
#include "Entity.h"
#include <glm/glm.hpp>
#include <string>
@@ -7,7 +9,7 @@
class ShaderProgram;
class Light
class Light : public Entity
{
public:
struct Prototype

View File

@@ -25,14 +25,15 @@ void Mesh::draw(ShaderProgram *shaderProgram)
uint8_t typeNumberCount[static_cast<int>(TextureType::TEXTURE_TYPE_NUM_ITEMS)]{0};
glBindTexture(GL_TEXTURE_2D, 0);
// Bind all textures in order to its texture unit
for (auto it = m_textures.begin(); it != m_textures.end(); it++) {
const int i = it - m_textures.begin();
int i = 0;
for (auto it : m_textures) {
TextureType currentTextureType = it->getTextureType();
TextureType currentTextureType = (*it)->getTextureType();
(*it)->bind(i, shaderProgram, typeNumberCount[static_cast<int>(currentTextureType)]);
it->bind(i, shaderProgram, typeNumberCount[static_cast<int>(currentTextureType)]);
typeNumberCount[static_cast<int>(currentTextureType)] += 1;
i++;
}
// Draw elements

View File

@@ -1,5 +1,4 @@
#include "Model.h"
#include "GLBucket.h"
#include "Mesh.h"
#include "ShaderProgram.h"
#include "Texture.h"
@@ -97,7 +96,7 @@ void Model::loadModel(const std::string &pathToModel)
std::string texturePath = m_workingPath + '/' + textureSources[i].c_str();
Texture::Prototype texturePrototype{texturePath, textureTypes[i]};
auto loadModel = [=, &mutex]() {
Texture *currentTex = new Texture(texturePrototype);
Texture *currentTex = new Texture(texturePrototype.texturePath, texturePrototype.textureType);
std::lock_guard<std::mutex> lock(mutex);
m_textures.push_back(currentTex);
@@ -115,8 +114,7 @@ void Model::loadModel(const std::string &pathToModel)
}
if (!hasNormalMap) {
Texture::Prototype texturePrototype{"data/res/models/tex/fallback_normal.png", TextureType::Normal};
Texture *currentTex = new Texture(texturePrototype);
Texture *currentTex = new Texture("data/res/models/tex/fallback_normal.png", TextureType::Normal);
m_textures.push_back(currentTex);
}

View File

@@ -1,7 +1,8 @@
#include "World.h"
#include "Scene.h"
#include "Camera.h"
#include "Controller.h"
#include "Entity.h"
#include "FrameBuffer.h"
#include "JsonParser.h"
#include "Light.h"
#include "Model.h"
@@ -12,13 +13,13 @@
#include <iostream>
#include <thread>
World::World(std::vector<ShaderProgram *> shaderPrograms)
Scene::Scene(std::vector<ShaderProgram *> shaderPrograms)
: m_shaderProgram(Controller::getShaderProgramByName("defaultProgram", shaderPrograms)),
m_depthMapDirectionalFBO(DepthMapType::Normal, SHADOW_RES)
m_depthMapDirectionalFBO(SHADOW_RES)
{
// Create 4 depthMaps
for (int i = 0; i < 4; i++) {
DepthMap *temp_depthMap = new DepthMap(DepthMapType::CubeMap, SHADOW_RES);
DepthMapCube *temp_depthMap = new DepthMapCube(SHADOW_RES);
m_depthMapPointFBO.push_back(temp_depthMap);
}
@@ -63,9 +64,9 @@ World::World(std::vector<ShaderProgram *> shaderPrograms)
std::cout << "Loaded Skybox \"" << skyboxPrototype.texturePath << "\"" << std::endl;
});
std::vector<Entity::Prototype> entityPrototypes = modelParser.getEntityPrototypes();
std::vector<ModelEntity::Prototype> entityPrototypes = modelParser.getEntityPrototypes();
std::vector<Entity *> entities;
std::vector<ModelEntity *> entities;
{
for (auto &prototype : entityPrototypes) {
// Get model
@@ -86,7 +87,7 @@ World::World(std::vector<ShaderProgram *> shaderPrograms)
currentProgram = Controller::getShaderProgramByName("basic", shaderPrograms);
}
Entity *currentEntity = new Entity(prototype, currentModel, currentProgram);
ModelEntity *currentEntity = new ModelEntity(prototype, currentModel, currentProgram);
std::cout << "Loaded Entity \"" << prototype.name << "\" with model \"" << prototype.modelName << "\""
<< std::endl;
@@ -120,7 +121,7 @@ World::World(std::vector<ShaderProgram *> shaderPrograms)
m_skybox->initializeOnGPU();
}
World::~World()
Scene::~Scene()
{
// Iterate over depthMapPointFBO vector and delete all items
for (auto it = m_depthMapPointFBO.begin(); it != m_depthMapPointFBO.end(); it++) {
@@ -137,12 +138,12 @@ World::~World()
delete m_skybox;
}
void World::addEntity(Entity *entity)
void Scene::addEntity(ModelEntity *entity)
{
m_entities.push_back(entity);
}
void World::removeEntityByName(std::string name)
void Scene::removeEntityByName(std::string name)
{
for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
if ((*it)->getUniqueName() == name) {
@@ -154,14 +155,14 @@ void World::removeEntityByName(std::string name)
std::cout << "[Warning] Entity with name " << name << " could not be removed." << std::endl;
}
void World::clearEntities()
void Scene::clearEntities()
{
for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
m_entities.erase(it);
}
}
void World::updatePointLight(unsigned int lightId, bool active, glm::vec3 position, glm::vec3 color, float intensity)
void Scene::updatePointLight(unsigned int lightId, bool active, glm::vec3 position, glm::vec3 color, float intensity)
{
std::vector<PointLight *> pointLights = getPointLights();
pointLights[lightId]->setActive(active);
@@ -170,7 +171,7 @@ void World::updatePointLight(unsigned int lightId, bool active, glm::vec3 positi
pointLights[lightId]->setColor(color);
}
void World::updateDirectionalLight(bool active, glm::vec3 direction, glm::vec3 color)
void Scene::updateDirectionalLight(bool active, glm::vec3 direction, glm::vec3 color)
{
DirectionalLight *directionalLight = getDirectionalLight();
directionalLight->setActive(active);
@@ -178,7 +179,7 @@ void World::updateDirectionalLight(bool active, glm::vec3 direction, glm::vec3 c
directionalLight->setColor(color);
}
void World::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition)
void Scene::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition)
{
// Draw all entities
for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
@@ -189,7 +190,7 @@ void World::draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition)
// calculateShadows();
}
void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProgram *pointShaderProgram)
void Scene::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProgram *pointShaderProgram)
{
// Get old viewport dimensions to reset them later...
GLint VIEWPORT[4];
@@ -285,7 +286,7 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg
textureUnit = static_cast<int>(TextureType::TEXTURE_TYPE_NUM_ITEMS) * 2 + i + 1;
m_shaderProgram->setUniform("u_texture_pointShadowMap0", (int)textureUnit);
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_depthMapPointFBO[i]->getCubeMapId());
glBindTexture(GL_TEXTURE_CUBE_MAP, m_depthMapPointFBO[i]->getCubeMapTextureId());
m_shaderProgram->unbind();
}
@@ -295,7 +296,7 @@ void World::calculateShadows(ShaderProgram *directionalShaderProgram, ShaderProg
glCullFace(GL_FRONT);
}
Model *World::getModelByName(std::string name)
Model *Scene::getModelByName(std::string name)
{
for (auto it = m_models.begin(); it != m_models.end(); it++) {
if ((*it)->getUniqueName() == name) {
@@ -306,7 +307,7 @@ Model *World::getModelByName(std::string name)
return nullptr;
}
Entity *World::getEntityByName(std::string name)
ModelEntity *Scene::getEntityByName(std::string name)
{
for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
if ((*it)->getUniqueName() == name) {
@@ -317,7 +318,7 @@ Entity *World::getEntityByName(std::string name)
return nullptr;
}
Entity *World::getEntityById(uint32_t id)
ModelEntity *Scene::getEntityById(uint32_t id)
{
for (auto it = m_entities.begin(); it != m_entities.end(); it++) {
if ((*it)->getId() == id) {
@@ -328,7 +329,7 @@ Entity *World::getEntityById(uint32_t id)
return nullptr;
}
std::vector<PointLight *> World::getPointLights()
std::vector<PointLight *> Scene::getPointLights()
{
std::vector<PointLight *> temp_pointLights;
@@ -342,7 +343,7 @@ std::vector<PointLight *> World::getPointLights()
return temp_pointLights;
}
DirectionalLight *World::getDirectionalLight()
DirectionalLight *Scene::getDirectionalLight()
{
DirectionalLight *temp_directionalLight = nullptr;
@@ -355,12 +356,12 @@ DirectionalLight *World::getDirectionalLight()
return temp_directionalLight;
}
std::vector<Entity *> World::getEntities()
std::vector<ModelEntity *> Scene::getEntities()
{
return m_entities;
}
Skybox *World::getSkybox()
Skybox *Scene::getSkybox()
{
return m_skybox;
}

View File

@@ -9,7 +9,7 @@
#include <vector>
class Camera;
class Entity;
class ModelEntity;
class Light;
class PointLight;
class DirectionalLight;
@@ -17,25 +17,25 @@ class ShaderProgram;
class Skybox;
class Model;
class World
class Scene
{
public:
World(std::vector<ShaderProgram *> shaderPrograms);
~World();
Scene(std::vector<ShaderProgram *> shaderPrograms);
~Scene();
void addEntity(Entity *entity);
void addEntity(ModelEntity *entity);
void removeEntityByName(std::string name);
void clearEntities();
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();
std::vector<ModelEntity *> getEntities();
std::vector<PointLight *> getPointLights();
DirectionalLight *getDirectionalLight();
Skybox *getSkybox();
Entity *getEntityByName(std::string name);
Entity *getEntityById(uint32_t id);
ModelEntity *getEntityByName(std::string name);
ModelEntity *getEntityById(uint32_t id);
Model *getModelByName(std::string name);
void draw(glm::mat4 viewProjMatrix, glm::vec3 viewPosition);
@@ -45,7 +45,7 @@ private:
ShaderProgram *m_shaderProgram;
std::vector<Model *> m_models;
std::vector<Entity *> m_entities;
std::vector<ModelEntity *> m_entities;
Skybox *m_skybox;
// Lights
@@ -54,7 +54,7 @@ private:
// Shadows
const int SHADOW_RES = 4096 / 4;
DepthMap m_depthMapDirectionalFBO;
std::vector<DepthMap *> m_depthMapPointFBO;
std::vector<DepthMapCube *> m_depthMapPointFBO;
// Shadow projection matrices
const float m_nearPlaneDirectional = 1.0f;
const float m_farPlaneDirectional = 15.0f;

View File

@@ -12,7 +12,8 @@ Screen::Screen(Prototype prototype, FrameBuffer *framebuffer, ShaderProgram *sha
: m_id(s_idCounter++), m_uniqueName(prototype.name), m_frameBuffer(framebuffer), m_shaderProgram(shaderProgram)
{
for (auto &prototype : prototype.widgetPrototypes) {
Texture *currentTexture = new Texture(prototype.texturePrototype);
auto texturePrototype = prototype.texturePrototype;
Texture *currentTexture = new Texture(texturePrototype.texturePath, texturePrototype.textureType);
currentTexture->initializeOnGPU();
Widget *currentWidget = new Widget(prototype, currentTexture);
@@ -70,6 +71,6 @@ void Screen::draw() const
}
m_frameBuffer->unbind();
m_frameBuffer->render();
m_frameBuffer->drawOnEntireScreen();
m_frameBuffer->setExposureCorrection(true);
}

View File

@@ -3,8 +3,7 @@
#include <iostream>
Texture::Texture(const Prototype &prototype)
: m_texturePath(prototype.texturePath), m_textureType(prototype.textureType)
Texture::Texture(const std::string &path, TextureType type) : m_texturePath(path), m_textureType(type)
{
stbi_set_flip_vertically_on_load(1);
m_textureBuffer = stbi_load(m_texturePath.c_str(), &m_textureWidth, &m_textureHeight, &m_numComponents, 0);
@@ -117,12 +116,12 @@ GLuint Texture::getTextureId()
return m_textureId;
}
CubeMap::CubeMap(const char *texturePseudoPath)
CubeMap::CubeMap(const std::string &texturePseudoPath)
{
// Reserve space in vector so that elements can be accessed explicitly.
m_texturePaths.resize(CUBEMAP_FACES_NUM_ITEMS);
m_textureBuffers.resize(CUBEMAP_FACES_NUM_ITEMS);
m_numComponents.resize(CUBEMAP_FACES_NUM_ITEMS);
m_texturePaths.resize(static_cast<int>(cubeMapFaces::CUBEMAP_FACES_NUM_ITEMS));
m_textureBuffers.resize(static_cast<int>(cubeMapFaces::CUBEMAP_FACES_NUM_ITEMS));
m_numComponents.resize(static_cast<int>(cubeMapFaces::CUBEMAP_FACES_NUM_ITEMS));
fillTexturePathVector(texturePseudoPath);
@@ -242,19 +241,19 @@ void CubeMap::unbind()
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
void CubeMap::fillTexturePathVector(const char *texturePseudoPath)
void CubeMap::fillTexturePathVector(const std::string &texturePseudoPath)
{
for (unsigned int i = 0; i < CUBEMAP_FACES_NUM_ITEMS; i++) {
m_texturePaths[cm_front] = std::string(texturePseudoPath) + "front.png";
m_texturePaths[cm_back] = std::string(texturePseudoPath) + "back.png";
m_texturePaths[cm_top] = std::string(texturePseudoPath) + "top.png";
m_texturePaths[cm_bottom] = std::string(texturePseudoPath) + "bottom.png";
m_texturePaths[cm_left] = std::string(texturePseudoPath) + "left.png";
m_texturePaths[cm_right] = std::string(texturePseudoPath) + "right.png";
for (unsigned int i = 0; i < static_cast<int>(cubeMapFaces::CUBEMAP_FACES_NUM_ITEMS); i++) {
m_texturePaths[static_cast<int>(cubeMapFaces::cm_front)] = texturePseudoPath + "front.png";
m_texturePaths[static_cast<int>(cubeMapFaces::cm_back)] = texturePseudoPath + "back.png";
m_texturePaths[static_cast<int>(cubeMapFaces::cm_top)] = texturePseudoPath + "top.png";
m_texturePaths[static_cast<int>(cubeMapFaces::cm_bottom)] = texturePseudoPath + "bottom.png";
m_texturePaths[static_cast<int>(cubeMapFaces::cm_left)] = texturePseudoPath + "left.png";
m_texturePaths[static_cast<int>(cubeMapFaces::cm_right)] = texturePseudoPath + "right.png";
}
}
GLuint CubeMap::getTextureId()
GLuint CubeMap::getTextureId() const
{
return m_textureId;
}

View File

@@ -11,7 +11,7 @@
class ShaderProgram;
// Order is important!
enum cubeMapFaces
enum class cubeMapFaces
{
cm_right,
cm_left,
@@ -31,7 +31,7 @@ public:
TextureType textureType;
};
Texture(const Prototype &prototype);
Texture(const std::string &path, TextureType type);
~Texture();
void initializeOnGPU();
@@ -62,7 +62,7 @@ private:
class CubeMap
{
public:
CubeMap(const char *texturePseudoPath);
CubeMap(const std::string &texturePseudoPath);
CubeMap(int RESOLUTION);
void initializeOnGPU();
@@ -72,10 +72,10 @@ public:
void bind(ShaderProgram *shaderProgram);
void unbind();
GLuint getTextureId();
GLuint getTextureId() const;
private:
void fillTexturePathVector(const char *texturePseudoPath);
void fillTexturePathVector(const std::string &texturePseudoPath);
bool m_isInitialized = false;

View File

@@ -1,5 +1,4 @@
#include "VertexArray.h"
#include "GLBucket.h"
#include "definitions/models.h"
#include <cstddef>

View File

@@ -1,13 +1,13 @@
#include "GeneralInfoWindow.h"
#include "../Controller.h"
#include "../Entity.h"
#include "../Scene.h"
#include "../ShaderProgram.h"
#include "../World.h"
#include <imgui.h>
#include <math.h>
Imgui::GeneralInfoWindow::GeneralInfoWindow(Controller *controller, World *world, ShaderProgram *postProcessingProgram,
Imgui::GeneralInfoWindow::GeneralInfoWindow(Controller *controller, Scene *world, ShaderProgram *postProcessingProgram,
bool *rotateEntity, bool *drawShadows, bool *rotateLightSource,
glm::vec3 *lightColor, float *exposure, float *intensity)
: m_controller(controller), m_world(world), m_postProcessingProgram(postProcessingProgram),
@@ -26,7 +26,7 @@ void Imgui::GeneralInfoWindow::addWidgets()
ImGui::Checkbox("Rotate Object", m_rotateEntity);
Entity *mainObject = m_world->getEntityById(0);
ModelEntity *mainObject = m_world->getEntityById(0);
mainObject->setPosition(glm::vec3(m_translation[0], m_translation[1], m_translation[2]));
if (!*m_rotateEntity) {
mainObject->setRotation(glm::vec3(0.f, 1.0f, 0.f), m_rotation);

View File

@@ -4,7 +4,7 @@
#include <glm/glm.hpp>
class World;
class Scene;
class ShaderProgram;
class Controller;
@@ -13,7 +13,7 @@ namespace Imgui {
class GeneralInfoWindow : public Window
{
public:
GeneralInfoWindow(Controller *controller, World *world, ShaderProgram *postProcessingProgram, bool *rotateEntity,
GeneralInfoWindow(Controller *controller, Scene *world, ShaderProgram *postProcessingProgram, bool *rotateEntity,
bool *drawShadows, bool *rotateLightSource, glm::vec3 *lightColor, float *exposure,
float *intensity);
@@ -21,7 +21,7 @@ private:
void addWidgets() override;
Controller *m_controller;
World *m_world;
Scene *m_world;
ShaderProgram *m_postProcessingProgram;
bool *m_rotateEntity;