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

cd ${PROJECT_DIR}
git clone https://github.com/vsg-dev/vsgTutorial.git

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)

project(hello_world)

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

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 .
make 

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;
#endif

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

    // create the scene graph using OpenStreetMap convenience function
    auto scene = vsg::TileDatabase::create();
    scene->settings = vsg::createOpenStreetMapSettings(options);
    scene->readDatabase(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);
    viewer->addWindow(window);

    // 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
    viewer->addEventHandler(vsg::CloseHandler::create(viewer));

    // add a trackball event handler to control the camera view using the mouse
    viewer->addEventHandler(vsg::Trackball::create(camera));

    // create a command graph to render the scene on specified window
    auto commandGraph = vsg::createCommandGraphForView(window, camera, scene);
    viewer->assignRecordAndSubmitTaskAndPresentation({commandGraph});

    // compile all the Vulkan objects and transfer data required to render the scene
    viewer->compile();

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

        // update the scene graph, such as adding/removing database pager tiles
        viewer->update();

        // record the commands in the scene graph and submit the completed command buffers to the vulkan queue
        viewer->recordAndSubmit();

        // wait for completion of the rendering and present the resulting color buffer to the Window's swap chain.
        viewer->present();
    }

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

To run the application:

./helloworld

Prev: Building VulkanSceneGraph Software Next: Next Chapter : Foundations