# 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

```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

```cpp
#include "Node.h"

void Node::addChild(std::shared_ptr<Node> child, glm::mat4 trans, glm::mat4 rot )
{
    childNodes.push_back(child);
    childMats.push_back(trans * rot);
}

void Node::addMesh(std::shared_ptr<Mesh> mesh, glm::mat4 trans, glm::mat4 rot, glm::mat4 scale)
{
    meshes.push_back(mesh);
    meshMats.push_back(trans * rot * scale);
}

// all drawings go to Mesh::draw()
void Node::draw(glm::mat4 mat)
{
    //std::cout << "Node::draw()" << std::endl;

    for (int i = 0; i < meshes.size(); i++) {
        meshes[i]->draw(mat * meshMats[i]);
    }

    // for (const auto& child : childNodes) {
    for (int i = 0; i < childNodes.size(); i++) {
        childNodes[i]->draw(mat * childMats[i] );
    }
}
```

## Add Node.cpp to CMakeLists.txt

change the add\_executable line in CMakeLists.txt to add Node.cpp

```
add_executable(run01 src/main.cpp src/glad.c src/shader.cpp src/Mesh.cpp src/Node.cpp)
```
