Commit 80b06f47 authored by Georg Schaefer's avatar Georg Schaefer
Browse files

add staticmesh component

parent bc8209e4
#ifndef __GDW_RENDERING_SYSTEM_HPP__
#define __GDW_RENDERING_SYSTEM_HPP__
#include <vector>
#include <glm/glm.hpp>
#include <graphics/program.hpp>
......@@ -9,6 +11,7 @@
namespace gdw {
class engine;
class staticmesh_component;
}
namespace gdw {
......@@ -21,6 +24,10 @@ namespace gdw {
gdw::mesh_manager mesh_manager_;
program staticmesh_program_;
std::vector<staticmesh_component*> staticmesh_components_;
friend class staticmesh_component;
public:
rendering_system(engine& engine);
~rendering_system() = default;
......@@ -32,6 +39,7 @@ namespace gdw {
rendering_system& operator=(rendering_system&&) = default;
void update(float delta_time);
void render_gbuffer(float delta_time);
vertex_layout& staticmesh_layout() noexcept {
return staticmesh_layout_;
......@@ -40,6 +48,10 @@ namespace gdw {
gdw::mesh_manager& mesh_manager() noexcept {
return mesh_manager_;
}
private:
void register_component(staticmesh_component* component);
void unregister_component(staticmesh_component* component);
};
}
......
#ifndef __GDW_STATICMESH_COMPONENT_HPP__
#define __GDW_STATICMESH_COMPONENT_HPP__
#include <memory>
#include <ecs/component.hpp>
#include <util/id.hpp>
namespace gdw {
class engine;
class entity;
class mesh;
}
namespace gdw {
class staticmesh_component : public component {
private:
std::shared_ptr<const gdw::mesh> mesh_;
public:
staticmesh_component(engine& engine, entity& owner, const std::string& filename);
~staticmesh_component() noexcept;
staticmesh_component(const staticmesh_component&) = delete;
staticmesh_component& operator=(const staticmesh_component&) = delete;
staticmesh_component(staticmesh_component&&) = default;
staticmesh_component& operator=(staticmesh_component&&) = default;
const gdw::mesh& mesh() const {
return *mesh_;
}
};
}
#endif
#ifndef __GDW_COMPONENT_HELPER_HPP__
#define __GDW_COMPONENT_HELPER_HPP__
#include <vector>
namespace gdw {
template <typename t>
void remove_component(std::vector<t*>& v, t* component) {
auto it = std::find(v.begin(), v.end(), component);
if (it != v.end()) {
v.erase(it);
}
}
}
#endif
......@@ -3,12 +3,15 @@
#include <glm/gtc/matrix_transform.hpp>
#include <core/engine.hpp>
#include <ecs/entity.hpp>
#include <graphics/graphics_system.hpp>
#include <graphics/shader.hpp>
#include <graphics/shader_manager.hpp>
#include <rendering/mesh.hpp>
#include <rendering/mesh_vertex.hpp>
#include <rendering/rendering_system.hpp>
#include <rendering/staticmesh_component.hpp>
#include <util/component_helper.hpp>
#include <util/logger.hpp>
namespace gdw {
......@@ -44,22 +47,28 @@ namespace gdw {
}
void rendering_system::update(float delta_time) {
glEnable(GL_DEPTH_TEST);
render_gbuffer(delta_time);
}
static auto toggle = true;
static std::shared_ptr<const mesh> cube;
if (toggle) {
cube = mesh_manager_.load("mesh/cube.msh");
toggle = false;
}
void rendering_system::render_gbuffer(float delta_time) {
glEnable(GL_DEPTH_TEST);
if (cube) {
staticmesh_program_.use();
staticmesh_program_.uniform("projection", false, projection_);
staticmesh_program_.uniform("view", false, view_);
cube->draw(staticmesh_program_);
staticmesh_program_.use();
staticmesh_program_.uniform("projection", false, projection_);
staticmesh_program_.uniform("view", false, view_);
for (auto sm : staticmesh_components_) {
staticmesh_program_.uniform("model", false, sm->owner().transform());
sm->mesh().draw(staticmesh_program_);
}
glDisable(GL_DEPTH_TEST);
}
void rendering_system::register_component(staticmesh_component* component) {
staticmesh_components_.emplace_back(component);
}
void rendering_system::unregister_component(staticmesh_component* component) {
remove_component(staticmesh_components_, component);
}
}
#include <asset/asset.hpp>
#include <asset/asset_manager.hpp>
#include <ecs/entity.hpp>
#include <core/engine.hpp>
#include <rendering/mesh.hpp>
#include <rendering/mesh_manager.hpp>
#include <rendering/rendering_system.hpp>
#include <rendering/staticmesh_component.hpp>
namespace gdw {
staticmesh_component::staticmesh_component(engine& engine, entity& owner, const std::string& filename)
: component(engine, owner) {
engine_.rendering_system().register_component(this);
mesh_ = engine_.rendering_system().mesh_manager().load(filename);
if (!mesh_) {
throw std::runtime_error("could not load mesh from file " + filename);
}
}
staticmesh_component::~staticmesh_component() noexcept {
engine_.rendering_system().unregister_component(this);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment