progress...

This commit is contained in:
2021-08-16 21:37:09 +02:00
parent 4927720c29
commit a53db1e7b4
17 changed files with 210 additions and 89 deletions

View File

@@ -9,6 +9,7 @@
#include <GLFW/glfw3.h>
#ifdef _DEBUG
#include "imgui/EntityWindow.h"
#include "imgui/GeneralInfoWindow.h"
#include "imgui/Handler.h"
#endif
@@ -57,7 +58,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 Scene(m_shaderPrograms);
m_scene = new Scene(m_shaderPrograms);
#ifdef _DEBUG
m_imguiHandler = std::unique_ptr<Imgui::Handler>(new Imgui::Handler(m_gameWindow->getGLFWwindow()));
@@ -70,7 +71,7 @@ Controller::~Controller()
delete program;
}
delete m_world;
delete m_scene;
delete m_camera;
delete m_menu;
delete m_postProcessFrameBuffer;
@@ -87,7 +88,7 @@ void Controller::run()
{
updateExposure(getShaderProgramByName("postProcessingProgram"));
ModelEntity *lightSource = m_world->getEntityByName("light");
ModelEntity *lightSource = m_scene->getEntityByName("light");
lightSource->setScale(0.1f);
lightSource->setRotation(glm::vec3(0.f));
lightSource->setPosition(glm::vec3(-2.f, 1.5f, 2.f));
@@ -99,10 +100,13 @@ void Controller::run()
glm::vec3 lightColor = glm::vec3(1.f);
float intensity = 7.5f;
#ifdef _DEBUG
std::shared_ptr<Imgui::Window> imguiWindow = std::make_shared<Imgui::GeneralInfoWindow>(
this, m_world, getShaderProgramByName("postProcessingProgram"), &rotateEntity, &drawShadows, &rotateLightSource,
std::shared_ptr<Imgui::Window> generalWindow = std::make_shared<Imgui::GeneralInfoWindow>(
this, m_scene, getShaderProgramByName("postProcessingProgram"), &rotateEntity, &drawShadows, &rotateLightSource,
&lightColor, &m_exposure, &intensity);
m_imguiHandler->addImguiWindow(imguiWindow);
m_imguiHandler->addImguiWindow(generalWindow);
std::shared_ptr<Imgui::Window> entityWindow = std::make_shared<Imgui::EntityWindow>(m_scene->getEntities());
m_imguiHandler->addImguiWindow(entityWindow);
#endif
// This is the game loop
@@ -116,14 +120,14 @@ void Controller::run()
if (rotateLightSource) {
float radius = 4.0;
glm::vec3 newPos = glm::vec3(-cos(glfwGetTime() * 0.5), 0.5f, sin(glfwGetTime() * 0.5)) * radius;
m_world->getEntityByName("light")->setPosition(newPos);
m_scene->getEntityByName("light")->setPosition(newPos);
}
if (rotateEntity) {
m_world->getEntityById(0)->rotate(glm::vec3(0.0f, 1.0f, 0.0f), -0.2f * m_deltaTime);
m_scene->getEntityById(0)->rotate(glm::vec3(0.0f, 1.0f, 0.0f), -0.2f * m_deltaTime);
}
m_world->updatePointLight(0, true, m_world->getEntityByName("light")->getPosition(), lightColor, intensity);
m_world->updateDirectionalLight(true, m_world->getDirectionalLight()->getDirection(), lightColor);
m_scene->updatePointLight(0, true, m_scene->getEntityByName("light")->getPosition(), lightColor, intensity);
m_scene->updateDirectionalLight(true, m_scene->getDirectionalLight()->getDirection(), lightColor);
getShaderProgramByName("lightProgram")->bind();
getShaderProgramByName("lightProgram")->setUniform("v_lightColor", lightColor * 100.0f);
getShaderProgramByName("lightProgram")->unbind();
@@ -137,7 +141,7 @@ void Controller::run()
getShaderProgramByName("defaultProgram")->unbind();
if (drawShadows || firstRun) {
firstRun = false;
m_world->calculateShadows(getShaderProgramByName("directionalShadowDepthProgram"),
m_scene->calculateShadows(getShaderProgramByName("directionalShadowDepthProgram"),
getShaderProgramByName("pointShadowDepthProgram"));
}
@@ -153,8 +157,8 @@ void Controller::run()
m_camera->lookForward();
m_camera->updateVPM();
m_world->getSkybox()->draw(m_camera->getView(), m_camera->getProj());
m_world->draw(m_camera->getViewProj(), m_camera->getPosition());
m_scene->getSkybox()->draw(m_camera->getView(), m_camera->getProj());
m_scene->draw(m_camera->getViewProj(), m_camera->getPosition());
m_postProcessFrameBuffer->unbind();
m_postProcessFrameBuffer->drawOnEntireScreen();

View File

@@ -45,7 +45,7 @@ private:
std::unique_ptr<Window> m_gameWindow;
EventHandler *m_gameEventHandler;
Scene *m_world;
Scene *m_scene;
Camera *m_camera;
Menu *m_menu;

View File

@@ -25,39 +25,75 @@ const std::string &Entity::getUniqueName() const
return m_uniqueName;
}
void Entity::setParent(Entity *parent)
{
m_parent = parent;
}
void Entity::addChild(Entity *child)
{
m_children.push_back(child);
}
void Entity::translate(glm::vec3 vector)
{
for (auto &child : m_children) {
child->translate(vector);
}
m_position += vector;
updateModelMatrix();
}
void Entity::rotate(glm::vec3 axis, float radians)
{
for (auto &child : m_children) {
child->rotate(axis, radians);
}
glm::quat rotation = glm::angleAxis(radians, axis);
m_quaternion = rotation * m_quaternion;
updateModelMatrix();
}
void Entity::setPosition(glm::vec3 position)
{
for (auto &child : m_children) {
child->setPosition(child->getPosition() - m_position + position);
}
m_position = position;
updateModelMatrix();
}
void Entity::setRotation(glm::vec3 eulerAngles)
{
for (auto &child : m_children) {
child->setRotation(eulerAngles);
}
m_quaternion = glm::quat(eulerAngles);
updateModelMatrix();
}
void Entity::setRotation(glm::vec3 axis, float radians)
{
for (auto &child : m_children) {
child->setRotation(axis, radians);
}
m_quaternion = glm::angleAxis(radians, axis);
updateModelMatrix();
}
void Entity::setScale(float scale)
{
for (auto &child : m_children) {
child->setScale(scale);
}
m_scale = scale;
updateModelMatrix();
}

View File

@@ -15,12 +15,14 @@ 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)
Prototype(const std::string &name, const std::string &parent, glm::vec3 position, glm::vec3 rotation,
float scale)
: name(name), parent(parent), position(position), rotation(rotation), scale(scale)
{}
virtual ~Prototype() = default;
std::string name;
std::string parent;
glm::vec3 position;
glm::vec3 rotation;
float scale;
@@ -32,6 +34,13 @@ public:
uint32_t getId() const;
const std::string &getUniqueName() const;
void setParent(Entity *parent);
void addChild(Entity *child);
const std::vector<Entity *> &getChildren() const
{
return m_children;
}
void translate(glm::vec3 vector);
void rotate(glm::vec3 axis, float radians);
@@ -50,8 +59,10 @@ protected:
static uint32_t s_idCounter;
// TODO
std::weak_ptr<Entity> m_parent;
std::vector<std::shared_ptr<Entity>> m_children;
// std::weak_ptr<Entity> m_parent;
Entity *m_parent = nullptr;
// std::vector<std::shared_ptr<Entity>> m_children;
std::vector<Entity *> m_children;
std::string m_uniqueName;
@@ -67,9 +78,9 @@ 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),
Prototype(const std::string &name, const std::string &parent, glm::vec3 position, glm::vec3 rotation,
float scale, const std::string &modelName, const std::string &shaderProgramName)
: Entity::Prototype(name, parent, position, rotation, scale), modelName(modelName),
shaderProgramName(shaderProgramName)
{}
std::string modelName;

View File

@@ -52,11 +52,13 @@ std::vector<ModelEntity::Prototype> JsonParser::getEntityPrototypes() const
std::string entityModel = entitiesJson[index]["model"].asString();
std::string entityShaderProgram = entitiesJson[index]["shaderProgram"].asString();
glm::vec3 entitiyPosition = {}, entityRotation = {};
std::string entityParent = "";
float entityScale = 1.0f;
const Json::Value positionJson = entitiesJson[index]["position"];
const Json::Value rotationJson = entitiesJson[index]["rotation"];
const Json::Value scaleJson = entitiesJson[index]["scale"];
const Json::Value parentJson = entitiesJson[index]["parent"];
if (!positionJson.empty()) {
entitiyPosition.x = positionJson[0].asFloat();
entitiyPosition.y = positionJson[1].asFloat();
@@ -70,9 +72,11 @@ std::vector<ModelEntity::Prototype> JsonParser::getEntityPrototypes() const
if (!scaleJson.empty()) {
entityScale = scaleJson.asFloat();
}
if (!parentJson.empty())
entityParent = parentJson.asString();
ModelEntity::Prototype prototype(entityName, entitiyPosition, entityRotation, entityScale, entityModel,
entityShaderProgram);
ModelEntity::Prototype prototype(entityName, entityParent, entitiyPosition, entityRotation, entityScale,
entityModel, entityShaderProgram);
entityPrototypes.push_back(prototype);
}
@@ -107,6 +111,7 @@ std::vector<std::unique_ptr<Light::Prototype>> JsonParser::getLightPrototypes()
glm::vec3 direction = {1.0f, 0.0f, 0.0f};
glm::vec3 position = {};
glm::vec3 color = {1.0f, 1.0f, 1.0f};
std::string unique_name = "";
float intensity = 10.0f;
const Json::Value directionalLightsJson = m_root["directionalLight"];
@@ -114,6 +119,7 @@ std::vector<std::unique_ptr<Light::Prototype>> JsonParser::getLightPrototypes()
const Json::Value directionJson = directionalLightsJson["direction"];
Json::Value colorJson = directionalLightsJson["color"];
Json::Value intensityJson = directionalLightsJson["intensity"];
Json::Value nameJson = directionalLightsJson["unique_name"];
if (!intensityJson.empty()) {
intensity = intensityJson.asFloat();
@@ -128,8 +134,11 @@ std::vector<std::unique_ptr<Light::Prototype>> JsonParser::getLightPrototypes()
color.y = colorJson[1].asFloat();
color.z = colorJson[2].asFloat();
}
if (!nameJson.empty())
unique_name = nameJson.asString();
auto prototype = std::unique_ptr<Light::Prototype>(new DirectionalLight::Prototype{direction, color, intensity});
auto prototype =
std::unique_ptr<Light::Prototype>(new DirectionalLight::Prototype{unique_name, direction, color, intensity});
prototypes.push_back(std::move(prototype));
@@ -141,6 +150,9 @@ std::vector<std::unique_ptr<Light::Prototype>> JsonParser::getLightPrototypes()
const Json::Value positionJson = pointLightsJson[index]["position"];
colorJson = pointLightsJson[index]["color"];
intensityJson = pointLightsJson[index]["intensity"];
nameJson = pointLightsJson[index]["unique_name"];
const Json::Value parentJson = pointLightsJson[index]["parent"];
std::string parent = "";
if (!intensityJson.empty()) {
intensity = intensityJson.asFloat();
@@ -156,7 +168,15 @@ std::vector<std::unique_ptr<Light::Prototype>> JsonParser::getLightPrototypes()
color.z = colorJson[2].asFloat();
}
auto prototype = std::unique_ptr<Light::Prototype>(new PointLight::Prototype{position, color, intensity});
unique_name = "";
if (!nameJson.empty())
unique_name = nameJson.asString();
if (!parentJson.empty())
parent = parentJson.asString();
auto prototype = std::unique_ptr<Light::Prototype>(
new PointLight::Prototype{unique_name, parent, position, color, intensity});
prototypes.push_back(std::move(prototype));
}
@@ -167,7 +187,7 @@ std::vector<std::unique_ptr<Light::Prototype>> JsonParser::getLightPrototypes()
const glm::vec3 default_color(1.0f);
const float default_intensity = 10.0f;
auto prototype = std::unique_ptr<PointLight::Prototype>(
new PointLight::Prototype{default_position, default_color, default_intensity});
new PointLight::Prototype{"_fallbackLight", "", default_position, default_color, default_intensity});
prototypes.push_back(std::move(prototype));
}

View File

@@ -5,8 +5,8 @@
uint32_t Light::s_idCounter = 0;
Light::Light(glm::vec3 color, float intensity, ShaderProgram *shaderProgram)
: Entity("Light"), m_shaderProgram(shaderProgram), m_intensity(intensity)
Light::Light(const std::string &name, glm::vec3 color, float intensity, ShaderProgram *shaderProgram)
: Entity(name), m_shaderProgram(shaderProgram), m_intensity(intensity)
{
m_id = s_idCounter++;
m_lightColor = color * intensity;
@@ -44,7 +44,7 @@ void Light::setActive(bool active)
}
PointLight::PointLight(Prototype prototype, ShaderProgram *shaderProgram)
: Light(prototype.color, prototype.intensity, shaderProgram), m_position(prototype.position)
: Light(prototype.name, prototype.color, prototype.intensity, shaderProgram), m_position(prototype.position)
{}
void PointLight::update()
@@ -77,7 +77,7 @@ void PointLight::setPosition(glm::vec3 position)
}
DirectionalLight::DirectionalLight(Prototype prototype, ShaderProgram *shaderProgram)
: Light(prototype.color, prototype.intensity, shaderProgram), m_direction(prototype.direction)
: Light(prototype.name, prototype.color, prototype.intensity, shaderProgram), m_direction(prototype.direction)
{}
void DirectionalLight::update()

View File

@@ -14,10 +14,18 @@ class Light : public Entity
public:
struct Prototype
{
Prototype(const std::string &name, const std::string &parent, glm::vec3 color, float intensity)
: name(name), parent(parent), color(color), intensity(intensity)
{}
virtual ~Prototype() = default;
std::string name;
std::string parent;
glm::vec3 color;
float intensity;
};
Light(glm::vec3 color, float intensity, ShaderProgram *shaderProgram);
Light(const std::string &name, glm::vec3 color, float intensity, ShaderProgram *shaderProgram);
virtual ~Light() = 0;
virtual void update() = 0;
@@ -49,12 +57,11 @@ class PointLight : public Light
public:
struct Prototype : public Light::Prototype
{
Prototype(glm::vec3 position, glm::vec3 color, float intensity)
: position(position), color(color), intensity(intensity)
Prototype(const std::string &name, const std::string &parent, glm::vec3 position, glm::vec3 color,
float intensity)
: Light::Prototype(name, parent, color, intensity), position(position)
{}
glm::vec3 position;
glm::vec3 color;
float intensity;
};
PointLight(Prototype prototype, ShaderProgram *shaderProgram);
@@ -76,12 +83,10 @@ class DirectionalLight : public Light
public:
struct Prototype : public Light::Prototype
{
Prototype(glm::vec3 direction, glm::vec3 color, float intensity)
: direction(direction), color(color), intensity(intensity)
Prototype(const std::string &name, glm::vec3 direction, glm::vec3 color, float intensity)
: Light::Prototype(name, "", color, intensity), direction(direction)
{}
glm::vec3 direction;
glm::vec3 color;
float intensity;
};
DirectionalLight(Prototype prototype, ShaderProgram *shaderProgram);

View File

@@ -28,7 +28,7 @@ Scene::Scene(std::vector<ShaderProgram *> shaderPrograms)
m_shaderProgram->setUniform("u_material.shininess", 100.0f);
m_shaderProgram->unbind();
JsonParser modelParser("data/models.json");
JsonParser modelParser("data/scene.json");
std::vector<Model::Prototype> modelPrototypes = modelParser.getModelPrototypes();
@@ -66,7 +66,6 @@ Scene::Scene(std::vector<ShaderProgram *> shaderPrograms)
std::vector<ModelEntity::Prototype> entityPrototypes = modelParser.getEntityPrototypes();
std::vector<ModelEntity *> entities;
{
for (auto &prototype : entityPrototypes) {
// Get model
@@ -92,12 +91,19 @@ Scene::Scene(std::vector<ShaderProgram *> shaderPrograms)
std::cout << "Loaded Entity \"" << prototype.name << "\" with model \"" << prototype.modelName << "\""
<< std::endl;
entities.push_back(currentEntity);
if (!prototype.parent.empty()) {
Entity *parent = getEntityByName(prototype.parent);
if (parent) {
parent->addChild(currentEntity);
currentEntity->setParent(parent);
}
}
m_entities.push_back(currentEntity);
}
}
m_entities = entities;
JsonParser lightParser("data/lights.json");
JsonParser lightParser("data/scene.json");
auto lightPrototypes = lightParser.getLightPrototypes();
std::vector<Light *> lights;
@@ -111,6 +117,13 @@ Scene::Scene(std::vector<ShaderProgram *> shaderPrograms)
auto pointPrototype = dynamic_cast<PointLight::Prototype *>(prototype.get());
if (pointPrototype) {
currentLight = new PointLight(*pointPrototype, m_shaderProgram);
if (!pointPrototype->parent.empty()) {
Entity *parent = getEntityByName(pointPrototype->parent);
if (parent) {
parent->addChild(currentLight);
currentLight->setParent(parent);
}
}
}
lights.push_back(currentLight);
}

View File

@@ -0,0 +1,31 @@
#include "EntityWindow.h"
#include "../Entity.h"
#include <imgui.h>
Imgui::EntityWindow::EntityWindow(const std::vector<ModelEntity *> &entities) : Window("Entities"), m_entites(entities)
{}
void Imgui::EntityWindow::addWidgets()
{
ImGui::Text("Treelist");
for (const auto &entity : m_entites) {
// addChildWidget(*entity);
}
}
void Imgui::EntityWindow::addChildWidget(const ModelEntity &entity)
{
if (entity.getChildren().empty()) {
ImGui::Text(entity.getUniqueName().c_str());
} else {
if (ImGui::TreeNode(entity.getUniqueName().c_str())) {
for (const auto &child : entity.getChildren()) {
addChildWidget(*(const ModelEntity *)child);
}
ImGui::TreePop();
ImGui::Separator();
}
}
}

View File

@@ -0,0 +1,24 @@
#pragma once
#include "Window.h"
#include <vector>
class ModelEntity;
namespace Imgui {
class EntityWindow : public Window
{
public:
EntityWindow(const std::vector<ModelEntity *> &entities);
private:
void addWidgets() override;
static void addChildWidget(const ModelEntity &entity);
const std::vector<ModelEntity *> &m_entites;
};
} // namespace Imgui

View File

@@ -10,14 +10,13 @@
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),
: Window("Debug Utils"), m_controller(controller), m_scene(world), m_postProcessingProgram(postProcessingProgram),
m_rotateEntity(rotateEntity), m_drawShadows(drawShadows), m_rotateLightSource(rotateLightSource),
m_lightColor(lightColor), m_exposure(exposure), m_intensity(intensity)
{}
void Imgui::GeneralInfoWindow::addWidgets()
{
ImGui::Begin("Debug Utils");
ImGui::Text("Object");
ImGui::SliderFloat("Rotation", &m_rotation, 0, 2 * M_PI);
@@ -26,7 +25,7 @@ void Imgui::GeneralInfoWindow::addWidgets()
ImGui::Checkbox("Rotate Object", m_rotateEntity);
ModelEntity *mainObject = m_world->getEntityById(0);
ModelEntity *mainObject = m_scene->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);
@@ -53,6 +52,4 @@ void Imgui::GeneralInfoWindow::addWidgets()
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0 / ImGui::GetIO().Framerate,
ImGui::GetIO().Framerate);
ImGui::End();
}

View File

@@ -21,7 +21,7 @@ private:
void addWidgets() override;
Controller *m_controller;
Scene *m_world;
Scene *m_scene;
ShaderProgram *m_postProcessingProgram;
bool *m_rotateEntity;

View File

@@ -38,6 +38,13 @@ void Imgui::Handler::addImguiWindow(std::shared_ptr<Window> window)
void Imgui::Handler::renderWindows()
{
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
for (auto window : m_windows)
window->render();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}

View File

@@ -4,17 +4,12 @@
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
Imgui::Window::Window()
Imgui::Window::Window(const std::string &title) : m_title(title)
{}
void Imgui::Window::render()
{
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::Begin(m_title.c_str());
addWidgets();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
ImGui::End();
}

View File

@@ -1,15 +1,19 @@
#pragma once
#include <string>
namespace Imgui {
class Window
{
public:
Window();
Window(const std::string &title);
void render();
protected:
virtual void addWidgets() = 0;
std::string m_title;
};
} // namespace Imgui