# 3.2.2 Build and Draw the Scene Graph

in main.cpp

## Include Node.h in main.cpp

<figure><img src="https://3464970502-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JUKGJZ67JX02QZdPhsy%2Fuploads%2FiVgcXrDMCa6IqAG7Wfay%2Feb8f6272-fbdd-40b4-8ec7-c56f51467c68.png?alt=media&#x26;token=1c70103a-8d79-4d4a-94f1-cdf48766ac54" alt=""><figcaption></figcaption></figure>

## Build a Scene Graph

We build a scene graph which contains a box and a teapot, we try to put the teapot on the box.

<pre class="language-cpp"><code class="lang-cpp"><strong>    //----------------------------------------------------
</strong>    // Meshes
    std::shared_ptr&#x3C;Mesh> cube = std::make_shared&#x3C;Mesh>();
    cube->init("models/cube.obj", shader.program);

    std::shared_ptr&#x3C;Mesh> teapot = std::make_shared&#x3C;Mesh>();
    teapot->init("models/teapot.obj", shader.program);
    
    //----------------------------------------------------
    // Nodes
    std::shared_ptr&#x3C;Node> scene = std::make_shared&#x3C;Node>();
    std::shared_ptr&#x3C;Node> teapotNode = std::make_shared&#x3C;Node>();
    std::shared_ptr&#x3C;Node> cubeNode = std::make_shared&#x3C;Node>();
    
    //----------------------------------------------------
    // Build the tree
    teapotNode->addMesh(teapot);
    cubeNode->addMesh(cube);
    cubeNode->addChild(teapotNode, glm::translate(glm::vec3(0.0f, 1.0f, 0.0f)));
 
    //----------------------------------------------------
    // Add the tree to the world space
    scene->addChild(cubeNode);
</code></pre>

## Call scene->draw() in GLFW rendering loop&#x20;

```cpp
   while (!glfwWindowShouldClose(window))
    {
        glfwPollEvents();

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        scene->draw();
        
        glfwSwapBuffers(window);
    }

```

Finally, we have

<figure><img src="https://3464970502-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3JUKGJZ67JX02QZdPhsy%2Fuploads%2FRyBGgLlNLoDWBFBnP08w%2Fimage.png?alt=media&#x26;token=a6c26c9e-a96e-4323-afc7-031f3f10d751" alt="" width="483"><figcaption></figcaption></figure>

## Full Source Code

```cpp
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>

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

#include "shader.h"
#include "Mesh.h"
#include "Node.h"

static Shader shader;

// Initialize shader
void initShader(std::string pathVert, std::string pathFrag) 
{
    shader.read_source( pathVert.c_str(), pathFrag.c_str());

    shader.compile();
    glUseProgram(shader.program);
}

int main()
{
    GLFWwindow *window;

    // GLFW init
    if (!glfwInit())
    {
        std::cout << "glfw failed" << std::endl;
        return -1;
    }

    // create a GLFW window
    window = glfwCreateWindow(640, 640, "Hello OpenGL 3", NULL, NULL);
    glfwMakeContextCurrent(window);


    // loading glad
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Couldn't load opengl" << std::endl;
        glfwTerminate();
        return -1;
    }

    initShader( "shaders/colour.vert", "shaders/colour.frag");


    //----------------------------------------------------
    // Meshes
    std::shared_ptr<Mesh> cube = std::make_shared<Mesh>();
    cube->init("models/cube.obj", shader.program);

    std::shared_ptr<Mesh> teapot = std::make_shared<Mesh>();
    teapot->init("models/teapot.obj", shader.program);
    
    //----------------------------------------------------
    // Nodes
    std::shared_ptr<Node> scene = std::make_shared<Node>();
    std::shared_ptr<Node> teapotNode = std::make_shared<Node>();
    std::shared_ptr<Node> cubeNode = std::make_shared<Node>();
    
    //----------------------------------------------------
    // Build the tree
    teapotNode->addMesh(teapot);
    cubeNode->addMesh(cube);
    cubeNode->addChild(teapotNode, glm::translate(glm::vec3(0.0f, 1.0f, 0.0f)));
    // cubeNode->addChild(teapotNode, glm::translate(glm::vec3(0.0f, 1.0f, 0.0f)), glm::rotate(glm::radians(45.0f), glm::vec3(0.0f, 1.0f, 0.0f)));
    
    //----------------------------------------------------
    // Add the tree to the world space
    scene->addChild(cubeNode);
    // scene->addChild(cubeNode, glm::translate(glm::vec3(1.0f, 0.0f, 0.0f)), glm::rotate(glm::radians(45.0f), glm::vec3(1.0f, 0.0f, 0.0f)));

    // setting the background colour, you can change the value
    glClearColor(0.25f, 0.5f, 0.75f, 1.0f);
    
    glEnable(GL_DEPTH_TEST);

    // setting the event loop
    while (!glfwWindowShouldClose(window))
    {
        glfwPollEvents();

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        scene->draw();
        
        glfwSwapBuffers(window);
    }

    glfwTerminate();

    return 0;
}
```
