Skip to content

Commit

Permalink
Simpler (& correct i guess) scene graph with unique_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
douze committed Nov 7, 2020
1 parent 885526f commit 2cd828a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 63 deletions.
70 changes: 30 additions & 40 deletions demo/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,44 @@
#include "triangle.hpp"
#include "vertexcolormaterial.hpp"

#include <iostream>
#include <string>
struct Configuration {
int width;
int height;
};

using namespace renderer;
#include <iostream>
int main() {
spdlog::set_level(spdlog::level::debug);

const int width = 1280;
const int height = 800;

Renderer renderer{width, height};
using namespace renderer;

// Do I really need shared_ptr ? For the update loop ?
std::shared_ptr<mesh::Triangle> triangle{std::make_shared<mesh::Triangle>()};
std::shared_ptr<material::VertexColorMaterial> vertexcolormaterial{
std::make_shared<material::VertexColorMaterial>()};

std::shared_ptr<mesh::Terrain> terrain{std::make_shared<mesh::Terrain>()};
std::shared_ptr<material::TerrainMaterial> terrainMaterial{
std::make_shared<material::TerrainMaterial>()};
// Configuration
spdlog::set_level(spdlog::level::debug);
Configuration configuration{1280, 800};

std::shared_ptr<mesh::FullScreenQuad> fsq{
std::make_shared<mesh::FullScreenQuad>()};
std::shared_ptr<material::NoiseTerrainMaterial> noiseTerrainMaterial{
std::make_shared<material::NoiseTerrainMaterial>()};
// Create renderer
Renderer renderer{configuration.width, configuration.height};
scene::Scene& scene{renderer.getScene()};
scene::Node& root{scene.getRoot()};

scene::Node triangleNode{triangle,
// Add test triangle
scene::Node triangleNode{std::make_unique<mesh::Triangle>(mesh::Triangle{}),
mesh::Transformation{glm::vec3{0.0f, 0.0f, -2.0f}},
vertexcolormaterial};
renderer.getScene().getRoot().addChild(triangleNode);

scene::Node terrainNode{terrain,
mesh::Transformation{glm::vec3{0.0f, 0.0f, -4.0f}},
terrainMaterial};
renderer.getScene().getRoot().addChild(terrainNode);

// scene::Node fsqNode{fsq,
// mesh::Transformation{glm::vec3{0.0f, 0.0f, -2.0f}},
// noiseTerrainMaterial};
// renderer.getScene().getRoot().addChild(fsqNode);

std::make_unique<material::VertexColorMaterial>(
material::VertexColorMaterial{})};
root.addChild(triangleNode);

// Add terrain
scene::Node terrainNode{
std::make_unique<mesh::Terrain>(mesh::Terrain{}),
mesh::Transformation{glm::vec3{0.0f, 0.0f, -4.0f}},
std::make_unique<material::TerrainMaterial>(material::TerrainMaterial{})};
root.addChild(terrainNode);

// Assign main camera
scene::Camera camera{glm::vec3{0.0f, 0.0f, 0.0f},
width / static_cast<float>(height)};

renderer.getScene().setCamera(std::make_shared<scene::Camera>(camera));
configuration.width /
static_cast<float>(configuration.height)};
scene.setCamera(std::make_shared<scene::Camera>(camera));

// Run loop
renderer.prerun();

return renderer.run();
}
19 changes: 10 additions & 9 deletions renderer/include/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class Node {
* @brief Create a node with a mesh.
* @param mesh to attach to the node
*/
explicit Node(const std::shared_ptr<mesh::Mesh>& mesh,
explicit Node(std::unique_ptr<mesh::Mesh> mesh,
mesh::Transformation transformation,
const std::shared_ptr<material::Material>& material) noexcept;
std::unique_ptr<material::Material> material) noexcept;

/**
* @brief Add a node child to the current node.
Expand All @@ -41,7 +41,8 @@ class Node {
/**
* @brief Return the node's mesh by const reference.
*/
const std::shared_ptr<mesh::Mesh>& getMesh() const { return mesh; }
// const std::shared_ptr<mesh::Mesh>& getMesh() const { return mesh; }
mesh::Mesh& getMesh() const { return *mesh.get(); }

/**
* @brief Return the node's children by const reference.
Expand All @@ -58,16 +59,16 @@ class Node {
/**
* @brief Return the node's material.
*/
const std::shared_ptr<material::Material>& getMaterial() const {
return material;
}
const material::Material& getMaterial() const { return *material.get(); }

bool isRenderable() const { return mesh != nullptr && material != nullptr; }

private:
/** Mesh attached to the node. Can be null for transform only node. */
std::shared_ptr<mesh::Mesh> mesh;
std::unique_ptr<mesh::Mesh> mesh;

/** Material attached to the node. Can be null for transform only node. */
std::shared_ptr<material::Material> material;
std::unique_ptr<material::Material> material;

/** Transformation attached to the node */
mesh::Transformation transformation;
Expand All @@ -90,7 +91,7 @@ class Scene {
* @brief Return the scene's root by reference.
*/
Node& getRoot() { return root; }

void setCamera(std::shared_ptr<Camera> camera) { this->camera = camera; }
Camera& getCamera() const { return *camera.get(); }

Expand Down
18 changes: 8 additions & 10 deletions renderer/src/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,7 @@ void Renderer::processMouse(float deltaTime) {

#include <iostream>

void Renderer::prerun() {
prepareNode(scene.getRoot());
}
void Renderer::prerun() { prepareNode(scene.getRoot()); }

int Renderer::run() {
glEnable(GL_DEPTH_TEST);
Expand All @@ -189,11 +187,11 @@ void Renderer::updateTimer() {
}

void Renderer::renderNode(scene::Node& node) const {
if (node.getMesh() != nullptr) {
node.getMaterial()->use();
node.getMaterial()->setTransformationMatrix(node.getTransformation());
node.getMaterial()->setCameraMatrices(scene.getCamera());
node.getMesh()->render();
if (node.isRenderable()) {
node.getMaterial().use();
node.getMaterial().setTransformationMatrix(node.getTransformation());
node.getMaterial().setCameraMatrices(scene.getCamera());
node.getMesh().render();
}

for (scene::Node& child : node.getChildren()) {
Expand All @@ -202,8 +200,8 @@ void Renderer::renderNode(scene::Node& node) const {
}

void Renderer::prepareNode(scene::Node& node) const {
if (node.getMesh() != nullptr) {
node.getMesh()->prepare();
if (node.isRenderable()) {
node.getMesh().prepare();
}

for (scene::Node& child : node.getChildren()) {
Expand Down
9 changes: 5 additions & 4 deletions renderer/src/scene.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#include "scene.hpp"
#include <memory>

using namespace renderer::scene;
using namespace renderer::mesh;

Node::Node(mesh::Transformation transformation) noexcept
: Node{nullptr, transformation, nullptr} {}
: mesh{nullptr}, material{nullptr}, transformation{transformation} {}

Node::Node(const std::shared_ptr<mesh::Mesh>& mesh,
Node::Node(std::unique_ptr<mesh::Mesh> mesh,
mesh::Transformation transformation,
const std::shared_ptr<material::Material>& material) noexcept
: mesh{mesh}, material{material}, transformation{transformation} {}
std::unique_ptr<material::Material> material) noexcept
: mesh{std::move(mesh)}, material{std::move(material)}, transformation{transformation} {}

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

Expand Down

0 comments on commit 2cd828a

Please sign in to comment.