Skip to content

Commit

Permalink
Some refactoring for rendering architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
douze committed Jan 5, 2021
1 parent 1b817f9 commit 593ed1a
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 41 deletions.
2 changes: 1 addition & 1 deletion demo/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ int main() {
scene.attach_main_camera(std::make_shared<scene::Camera>(camera));

// Run loop
renderer.prerun();
renderer.prepare();
return renderer.run();
}
5 changes: 2 additions & 3 deletions renderer/include/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Renderer {
/**
* @brief Method used before the run loop.
*/
void prerun();
void prepare();

/**
* @brief Run the main loop.
Expand Down Expand Up @@ -110,9 +110,8 @@ class Renderer {
/**
* @brief Set state for offscreen rendering.
* @param fbo to render to
* @param texture to bind
*/
void set_offscreen_render_state(GLuint fbo, GLuint texture) const;
void set_offscreen_render_state(GLuint fbo) const;

private:
/** Width of the display */
Expand Down
19 changes: 18 additions & 1 deletion renderer/include/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class Node : public odo::GuiProvider {
*/
void add_child(Node& child);

/**
* @brief Prepare the node for rendering.
*/
void prepare();

/**
* @brief Prepare the node for offscreen rendering.
* @param width of the display
Expand All @@ -51,6 +56,18 @@ class Node : public odo::GuiProvider {

void render_ui() override;

/**
* @brief Render the node.
* @param camera to render for
* @param parent of the node
*/
virtual void render(const Camera& camera, std::optional<std::reference_wrapper<scene::Node>> parent);

/**
* @brief Render the node, offscreen.
*/
virtual void render_offscreen();

/**
* @return the node's mesh.
*/
Expand Down Expand Up @@ -83,7 +100,7 @@ class Node : public odo::GuiProvider {
/**
* @return true if the node has to be renderer offscreen.
*/
bool is_offscreen() const { return offscreen; }
bool is_offscreen_renderable() const { return offscreen; }

/**
* @return true if the node has a name.
Expand Down
51 changes: 19 additions & 32 deletions renderer/src/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,19 @@ void Renderer::process_keyboard(float delta_time) {
}
}

void Renderer::prerun() { prepare_node(scene.get_root()); }
void Renderer::prepare() { prepare_node(scene.get_root()); }

void Renderer::prepare_node(scene::Node& node) const {
if (node.is_renderable()) {
node.prepare();
} else if (node.is_offscreen_renderable()) {
node.prepare_offscreen(width, height);
}

for (scene::Node& child : node.get_children()) {
prepare_node(child);
}
}

int Renderer::run() {
set_render_state();
Expand All @@ -177,40 +189,17 @@ void Renderer::update_timer() {
timer.last = current;
}

void Renderer::prepare_node(scene::Node& node) const {
if (node.is_renderable()) {
node.get_mesh().prepare();
} else if (node.is_offscreen()) {
node.prepare_offscreen(width, height);
}

for (scene::Node& child : node.get_children()) {
prepare_node(child);
}
}

void Renderer::render_node(scene::Node& node, std::optional<std::reference_wrapper<scene::Node>> parent) const {
if (node.is_renderable()) {
material::Material& material = node.get_material();
material.use();
material.set_transformation_matrix(node.get_transformation());
material.set_camera_matrices(scene.get_main_camera());
material.set_uniforms();
if (parent.has_value()) {
glBindTextureUnit(0, parent.value().get().get_material().get_offscreen_texture());
}
node.get_mesh().render();
} else if (node.is_offscreen()) {
material::Material& material = node.get_material();
set_offscreen_render_state(material.get_offscreen_fbo(), material.get_offscreen_texture());
material.use();
material.set_uniforms();
node.get_mesh().render();
node.render(scene.get_main_camera(), parent);
} else if (node.is_offscreen_renderable()) {
set_offscreen_render_state(node.get_material().get_offscreen_fbo());
node.render_offscreen();
set_render_state();
}

for (scene::Node& child : node.get_children()) {
render_node(child, /*std::nullopt */ std::make_optional<std::reference_wrapper<scene::Node>>(node));
render_node(child, std::make_optional<std::reference_wrapper<scene::Node>>(node));
}
}

Expand All @@ -223,13 +212,11 @@ void Renderer::set_render_state() const {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void Renderer::set_offscreen_render_state(GLuint fbo, GLuint texture) const {
void Renderer::set_offscreen_render_state(GLuint fbo) const {
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glViewport(0, 0, width, height);
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, texture);
glActiveTexture(GL_TEXTURE0);
}
33 changes: 29 additions & 4 deletions renderer/src/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,47 @@ Node::Node(const std::string& name, std::unique_ptr<mesh::Mesh> mesh, std::uniqu

void Node::add_child(Node& child) { children.push_back(child); }

void Node::prepare() {
if (is_renderable()) {
mesh->prepare();
}
}

void Node::prepare_offscreen(const int width, const int height) {
if (is_offscreen()) {
if (is_offscreen_renderable()) {
material->prepare_offscreen(width, height);
}
}

void Node::render_ui() {
if (is_renderable() || is_offscreen()) {
if (is_renderable() || is_offscreen_renderable()) {
if (has_name() && ImGui::CollapsingHeader(get_name().c_str())) {
get_mesh().render_ui();
get_material().render_ui();
mesh->render_ui();
material->render_ui();
}
// Do i need a case without collapsing header ?
}
}

void Node::render(const Camera& camera, std::optional<std::reference_wrapper<scene::Node>> parent) {
material->use();
material->set_transformation_matrix(get_transformation());
material->set_camera_matrices(camera);
material->set_uniforms();
if (parent.has_value()) {
material->set_uniforms_from_parent(parent.value().get().get_material());
}
mesh->render();
}

void Node::render_offscreen() {
if (is_offscreen_renderable()) {
material->use();
material->set_uniforms();
mesh->render();
}
}

Scene::Scene() noexcept : root{Node{Transformation{glm::vec3{0.f, 0.f, 0.f}}}} {}

void Scene::render_ui(Node& node) {
Expand Down

0 comments on commit 593ed1a

Please sign in to comment.