3.2.1 Define a Scene Graph Node

Here I am going to demo you a simple composition based scene graph. It is only for demonstration purpose and may have many drawbacks. You may need to consider your own design.

Node

A Node is a basic unit of a scene which has children nodes.

In this work, a node has Transform and Mesh as attributes

Create Node.h and Node.cpp

#ifndef __NODE_H__
#define __NODE_H__

#include <vector>

#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>

#include "Mesh.h"

class Node {
private:
    // Note: separating child nodes and transforms is not a good design
    // the code is only for concept demonstration
    std::vector< std::shared_ptr <Node> > childNodes;
    std::vector< glm::mat4 > childMats;

    std::vector< std::shared_ptr <Mesh> > meshes;
    std::vector< glm::mat4 > meshMats;

public:
    void addChild(std::shared_ptr<Node> child, glm::mat4 trans = glm::mat4(1.0), glm::mat4 rot = glm::mat4(1.0));
    void addMesh(std::shared_ptr<Mesh> mesh, glm::mat4 trans = glm::mat4(1.0), glm::mat4 rot = glm::mat4(1.0), glm::mat4 scale = glm::mat4(1.0));

    void draw(glm::mat4 mat = glm::mat4(1.0));

};

#endif

Node.cpp

Add Node.cpp to CMakeLists.txt

change the add_executable line in CMakeLists.txt to add Node.cpp

Last updated