Skip to content

Commit

Permalink
Better scene encapsulation & link between node->mesh/material
Browse files Browse the repository at this point in the history
  • Loading branch information
douze committed Jan 6, 2021
1 parent 593ed1a commit 6537465
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 42 deletions.
4 changes: 0 additions & 4 deletions renderer/include/gui-provider.h

This file was deleted.

5 changes: 3 additions & 2 deletions renderer/include/material.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef MATERIAL_H
#define MATERIAL_H

#include "gui-provider.hpp"
#include <glad/glad.h>
#include <string>

Expand All @@ -20,7 +21,7 @@ namespace material {
* @brief Define the visual aspect of the mesh.
* @note Must be derivated.
*/
class Material {
class Material : public odo::GuiProvider {
public:
/**
* @brief Create a material from vertex & fragment shaders.
Expand Down Expand Up @@ -53,7 +54,7 @@ class Material {
/**
* @brief Render the material UI to the GUI frame.
*/
virtual void render_ui() = 0;
virtual void render_ui() override {}

/**
* @brief Set uniforms.
Expand Down
16 changes: 6 additions & 10 deletions renderer/include/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,11 @@ class Node : public odo::GuiProvider {
*/
virtual void render_offscreen();

/**
* @return the node's mesh.
*/
mesh::Mesh& get_mesh() const { return *mesh.get(); }

mesh::Mesh* get_mesh_ptr() const { return mesh.get(); }

/**
* @return the node's material.
*/
material::Material& get_material() const { return *material.get(); }

material::Material* get_material_ptr() const { return material.get(); }

/**
* @return the node's transformation.
*/
Expand Down Expand Up @@ -113,8 +104,13 @@ class Node : public odo::GuiProvider {
*/
const std::string get_name() const { return name.value(); }

protected:
mesh::Mesh* get_mesh_ptr() const { return mesh.get(); }

material::Material* get_material_ptr() const { return material.get(); }

private:
/** Optional name of the node. Mainly used for GUI */
/** Optional name of the node. Mainly used for GUI. */
std::optional<std::string> name;

/** Mesh attached to the node. Can be null for transform only node. */
Expand Down
6 changes: 3 additions & 3 deletions renderer/include/terrain-material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ class TerrainMaterial : public Material {
void set_camera_matrices(scene::Camera camera) const override;
void set_transformation_matrix(mesh::Transformation transformation) const override;

int grid_size = 5;
float scale_xy = 1.0f;
int grid_size = 1;
float patch_scale = 1.0f;
float patch_size = 1.0f;

private:
bool use_wireframe = false;
float height_factor = 5.0f;
float height_factor = 1.5f;
};

} // namespace odo::material
Expand Down
46 changes: 41 additions & 5 deletions renderer/include/terrain-node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,60 @@
#include "gui-provider.hpp"
#include "scene.hpp"

namespace odo::scene {
namespace odo {

namespace mesh {
class Terrain;
}

namespace material {
class TerrainMaterial;
}

namespace scene {

/**
* @brief Specification of Node used for the terrain.
*/
class TerrainNode : public Node, public odo::GuiProvider {

public:
TerrainNode(const std::string& name, std::unique_ptr<mesh::Mesh> mesh, std::unique_ptr<material::Material> material,
mesh::Transformation transformation);
explicit TerrainNode(const std::string& name, std::unique_ptr<mesh::Mesh> mesh,
std::unique_ptr<material::Material> material, mesh::Transformation transformation) noexcept;

void render_ui() override;

private:
/**
* @brief Build the list of possible grid sizes.
* @note Max value is hardcoded in this function.
*/
void build_grid_size_list();

/**
* @brief Update the node properties, by senting them to the mesh & material.
*/
void update_node_properties();

/**
* @return the terrain mesh.
*/
mesh::Terrain* get_terrain_mesh() const;

/**
* @return the terrain material.
*/
material::TerrainMaterial* get_terrain_material() const;

private:
int grid_size_selection = 5;
/** The default index of the selection of grid size. Indexed from 0. */
int grid_size_selection_index = 2;

/** List of possible grid sizes */
std::vector<std::string> grid_size_list;
};

} // namespace odo::scene
} // namespace scene
} // namespace odo

#endif // TERRAINNODE_HPP
4 changes: 1 addition & 3 deletions renderer/include/terrain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ class Terrain : public Mesh {
void render() const override;

int instance_count = 1;
float patch_size = 1.0f;

float get_patch_size() const { return patch_size; }

private:
/** Noise texture */
GLuint texture;
float patch_size = 1.0f;
};

} // namespace odo::mesh
Expand Down
4 changes: 2 additions & 2 deletions renderer/src/terrain-material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ TerrainMaterial::TerrainMaterial() noexcept
void TerrainMaterial::render_ui() {
ImGui::Checkbox("Wireframe", &use_wireframe);
ImGui::SliderFloat("Height factor", &height_factor, 0.0f, 100.0f);
ImGui::SliderFloat("Scale XY", &scale_xy, 0.0f, 3.0f);
ImGui::SliderFloat("Scale XY", &patch_scale, 0.0f, 3.0f);
}

void TerrainMaterial::set_uniforms() const {
glProgramUniform1ui(fs, 0, use_wireframe);
glProgramUniform1f(tes, 0, height_factor);
glProgramUniform1i(vs, 3, grid_size);
glProgramUniform1f(vs, 4, scale_xy);
glProgramUniform1f(vs, 4, patch_scale);
glProgramUniform1f(vs, 5, patch_size);
}

Expand Down
32 changes: 19 additions & 13 deletions renderer/src/terrain-node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
using namespace odo::scene;

TerrainNode::TerrainNode(const std::string& name, std::unique_ptr<mesh::Mesh> mesh,
std::unique_ptr<material::Material> material, mesh::Transformation transformation)
std::unique_ptr<material::Material> material, mesh::Transformation transformation) noexcept
: Node{name, std::move(mesh), std::move(material), transformation} {
build_grid_size_list();
update_node_properties();
}

void TerrainNode::build_grid_size_list() {
Expand All @@ -21,26 +22,31 @@ void TerrainNode::build_grid_size_list() {
}
}

void TerrainNode::update_node_properties() {
const int grid_size = grid_size_selection_index + 1; // 0 indexed
get_terrain_mesh()->instance_count = grid_size * grid_size;
get_terrain_material()->grid_size = grid_size;
get_terrain_material()->patch_size = get_terrain_mesh()->get_patch_size();
}

odo::mesh::Terrain* TerrainNode::get_terrain_mesh() const { return dynamic_cast<odo::mesh::Terrain*>(get_mesh_ptr()); }

odo::material::TerrainMaterial* TerrainNode::get_terrain_material() const {
return dynamic_cast<odo::material::TerrainMaterial*>(get_material_ptr());
}

void TerrainNode::render_ui() {
if (ImGui::CollapsingHeader("Node: Terrain")) {
ImGui::Combo(
"Size", &grid_size_selection,
"Size", &grid_size_selection_index,
[](void* data, int idx, const char** out_text) {
std::vector<std::string> from_data = *static_cast<std::vector<std::string>*>(data);
*out_text = from_data[idx].c_str();
return true;
},
static_cast<void*>(&grid_size_list), grid_size_list.size());
get_material().render_ui();
// get_mesh().render_ui();

const int grid_size = grid_size_selection + 1; // 0 indexed

odo::mesh::Terrain* t = dynamic_cast<odo::mesh::Terrain*>(get_mesh_ptr());
t->instance_count = grid_size * grid_size;

odo::material::TerrainMaterial* tm = dynamic_cast<odo::material::TerrainMaterial*>(get_material_ptr());
tm->grid_size = grid_size;
tm->patch_size = t->get_patch_size();
get_terrain_material()->render_ui();
get_terrain_mesh()->render_ui();
update_node_properties();
}
}

0 comments on commit 6537465

Please sign in to comment.