To download the tutorial exercises, clone the vsgTutorial repository. It contains both the book contents and the exercises:

git clone

To build the hello world exercise change into the exercise directory, and use the following CMake script to build a standalone application:

cmake_minimum_required(VERSION 3.7)


# set the use of C++17 globally as all examples require it

find_package(vsg REQUIRED)
find_package(vsgXchange REQUIRED)

add_executable(hello_world hello_world.cpp)

target_link_libraries(hello_world vsg::vsg vsgXchange::vsgXchange)

To build the application change into source directory, run CMake then make:

cd vsgTutorial/1_SettingTheScene/01_hello_world
cmake .

The hello world exercise is just a single main() function that has three sections to it:

  1. create the scene graph
  2. create and setup the viewer to render the scene graph
  3. execute the frame loop which does the handling of GUI events and rendering.
#include <vsg/all.h>
#include <vsgXchange/all.h>
#include <iostream>

int main(int, char**)
#ifndef vsgXchange_curl
    std::cerr<<"vsgXchange::curl not available, so can not load OpenStreetMap data over http."<<std::endl;
    std::cerr<<"Please install libcurl-dev and rebuild vsgXchange, then rebuild hello_world."<<std::endl;
    return 1;

// Section 1: Create the scene graph
    // create options object that is used to guide IO operations
    auto options = vsg::Options::create();

    // create the scene graph using OpenStreetMap convenience function
    auto scene = vsg::TileDatabase::create();
    scene->settings = vsg::createOpenStreetMapSettings(options);

// Section 2 : Create and setup the Viewer, Window and compile Vulkan objects
    // create the viewer and assign window(s) to it
    auto viewer = vsg::Viewer::create();

    // create window with default traits
    auto windowTraits = vsg::WindowTraits::create();
    auto window = vsg::Window::create(windowTraits);

    // set up the camera
    double radius = vsg::WGS_84_RADIUS_EQUATOR;
    double nearFarRatio = 0.0001;

    auto lookAt = vsg::LookAt::create(vsg::dvec3(0.0, -radius*4.5, 0.0), vsg::dvec3(0.0, 0.0, 0.0), vsg::dvec3(0.0, 0.0, 1.0));
    auto perspective = vsg::Perspective::create(30.0, static_cast<double>(window->extent2D().width) / static_cast<double>(window->extent2D().height), nearFarRatio*radius, radius * 4.5);
    auto camera = vsg::Camera::create(perspective, lookAt, vsg::ViewportState::create(window->extent2D()));

    // add close handler to respond to pressing the window close window button and pressing escape

    // add a trackball event handler to control the camera view using the mouse

    // create a command graph to render the scene on specified window
    auto commandGraph = vsg::createCommandGraphForView(window, camera, scene);

    // compile all the Vulkan objects and transfer data required to render the scene

// Section 3 : execute the frame loop
    while (viewer->advanceToNextFrame())
        // pass any events into EventHandlers assigned to the Viewer

        // update the scene graph, such as adding/removing database pager tiles

        // record the commands in the scene graph and submit the completed command buffers to the vulkan queue

        // wait for completion of the rendering and present the resulting color buffer to the Window's swap chain.

    // clean up done automatically thanks to ref_ptr<>
    return 0;

To run the application:


Prev: Building VulkanSceneGraph Software Next: Next Chapter : Foundations