[C++] SDK QuickStart


#1

HARFANG3D® C++ SDK quick starter

HARFANG3D® can be deployed via different languages (C++, Python, Lua).
We will describe here how to setup the C ++ SDK toolchain using Visual Studio C++ 2017.

You can download SDK here.
To work with C++ SDK, you will need the glfw library.

Visual Studio configuration

Configuration for Windows 64bits:

Disable precompiled headers

When you start a new project, don’t forget to disable precompiled headers:

Specify includes paths

You have to specify SDK and glfw includes path

Specify .lib paths

SDK and glfw .lib path.


Don’t forget to copy HARFANG3D® dlls dependencies in your project directory

Sample Code in C++

Here is a code sample C++

-- Basic scene - C++

#include <iostream>
#include <engine/init.h>
#include <engine/plugin_system.h>
#include <engine/plus.h>
#include <foundation/matrix4.h>
#include <foundation/vector3.h>
#include <engine/light.h>
#include <engine/transform.h>
#include <engine/light_system.h>
#include <foundation/color.h>
#include <engine/environment.h>
#include <engine/scene.h>
#include <tuple>

using namespace std;
using namespace hg;

constexpr int resolution_x = 1920;
constexpr int resolution_y = 1080;
constexpr int antialiasing = 8;
constexpr auto screenMode = Window::Windowed;

#define PI 3.141592535
#define RAD PI/180.0

void init_lights(Plus &plus, shared_ptr<Scene> &scene)
{
    shared_ptr<Node> light_sun = plus.AddLight(*scene, Matrix4::RotationMatrix(Vector3(RAD * 22., RAD * -45., 0.)), Light::ModelLinear);
    light_sun->SetName("Sun");
    shared_ptr<Light> lt = light_sun->GetComponent<Light>();
    lt->SetDiffuseColor(Color(255. / 255., 255. / 255., 255. / 255., 1.));
    lt->SetShadow(Light::ShadowMap);
    lt->SetShadowRange(50.);
    lt->SetDiffuseIntensity(1.);
    lt->SetSpecularIntensity(1.);

    //Ambient:
    Color ambient_color = Color(103. / 255., 157. / 255., 141. / 255., 1.);
    Color ambient_color_min = Color(ambient_color.r * 0.3, ambient_color.g*0.3, ambient_color.b*0.3, ambient_color.a);
    shared_ptr<Environment> environment = make_shared<Environment>();
    environment->SetAmbientColor(ambient_color);
    environment->SetAmbientIntensity(0.3);
    environment->SetFogColor(ambient_color_min);
    environment->SetFogNear(1.);
    environment->SetFogFar(50.);
    environment->SetBackgroundColor(ambient_color_min);
    scene->AddComponent(environment);
}

tuple<shared_ptr<Scene>,shared_ptr<Node>,shared_ptr<FPSController>> init_scene(Plus &plus)
{
    shared_ptr<Scene> scene = plus.NewScene();
    shared_ptr<Node> camera = plus.AddCamera(*scene, Matrix4::TranslationMatrix(Vector3(0, 2, -15)));
    camera->SetName("Camera");
    init_lights(plus, scene);

    // Ground:
    shared_ptr<Node> ground = plus.AddPlane(*scene, Matrix4::TransformationMatrix(Vector3(0, 0, 0), Vector3(0, 0, 0)), 100., 100.);
    shared_ptr<Node> cube = plus.AddCube(*scene, Matrix4::TransformationMatrix(Vector3(0, 3, 0), Vector3(0, 0, 0)), 3, 3, 3);
    shared_ptr<FPSController> fps = make_shared<FPSController>(0, 2, -15);

    return make_tuple(scene,cube,fps);
}

int main()
{
    Init();
    LoadPlugins();
    Plus &plus = g_plus.get();
    plus.Mount("./");
    plus.RenderInit(resolution_x, resolution_y, antialiasing, screenMode);
    plus.SetBlend2D(BlendAlpha);
    
    shared_ptr<Scene> scene;
    shared_ptr<Node> cube;
    shared_ptr<FPSController> fps;
    tie(scene,cube,fps) = init_scene(plus);

    scene->UpdateAndCommitWaitAll();

    shared_ptr<Node> camera = scene->GetNode("Camera");    
    Vector3 rot = Vector3(0, 0, 0);

    while (!plus.KeyDown(KeyEscape) && !plus.IsAppEnded())
    {
        time_ns delta_t = plus.UpdateClock();
        int64_t time_to_sec(delta_t);
        fps->UpdateAndApplyToNode(*camera, delta_t);
        rot.x = rot.x + 0.01;
        rot.y = rot.y + 0.045;
        rot.z = rot.z + 0.03146;
        cube->GetComponent<Transform>()->SetRotation(rot);
        plus.UpdateScene(*scene, delta_t);
        plus.Flip();
        plus.EndFrame();
    }
    Uninit();
}

For the sake of the demonstration of the advantages of Lua VS C++, the equivalent source code shows the very same application in Lua :

-- Basic scene - Lua

hg = require('harfang')


function init_scene(plus)
    scene = plus:NewScene()
    camera = plus:AddCamera(scene, hg.Matrix4.TranslationMatrix(hg.Vector3(20, 10, 10)))
    camera:SetName("Camera")
    init_lights(plus, scene)

    -- Ground:
    ground = plus:AddPlane(scene, hg.Matrix4.TransformationMatrix(hg.Vector3(0, 0, 0), hg.Vector3(0, 0, 0)), 100, 100)
    cube = plus:AddCube(scene, hg.Matrix4.TransformationMatrix(hg.Vector3(0, 3, 0), hg.Vector3(0, 0, 0)), 3, 3, 3)
    fps = hg.FPSController(0, 2, -15)

    return scene, cube, fps
end

function init_lights(plus, scene)
    -- Main light:
    ligth_sun = plus:AddLight(scene, hg.Matrix4.RotationMatrix(hg.Vector3(math.rad(22), math.rad(-45), 0)), hg.LightModelLinear)
    ligth_sun:SetName("Sun")
    ligth_sun:GetLight():SetDiffuseColor(hg.Color(255. / 255., 255. / 255., 255. / 255., 1.))
    ligth_sun:GetLight():SetShadow(hg.LightShadowMap)
    ligth_sun:GetLight():SetShadowRange(50)
    ligth_sun:GetLight():SetDiffuseIntensity(1.)
    ligth_sun:GetLight():SetSpecularIntensity(1.)

    -- Ambient:
    ambient_color = hg.Color(103. / 255., 157. / 255., 141. / 255., 1.)
    environment = hg.Environment()
    environment:SetAmbientColor(ambient_color)
    environment:SetAmbientIntensity(0.3)
    environment:SetFogColor(ambient_color * 0.3)
    environment:SetFogNear(1)
    environment:SetFogFar(50)
    environment:SetBackgroundColor(ambient_color * 0.3)
    scene:AddComponent(environment)
end

---- Program starts here ----

-- Display settings
resolution = hg.Vector2(1600, 900)
antialiasing = 8
screenMode = hg.Windowed

-- System setup
plus = hg.GetPlus()
hg.LoadPlugins()
plus:Mount("./")

-- Run display
plus:RenderInit(resolution.x, resolution.y, antialiasing, screenMode)
plus:SetBlend2D(hg.BlendAlpha)

-- Setup scene:
scene, cube, fps= init_scene(plus)

scene:UpdateAndCommitWaitAll();
camera = scene:GetNode("Camera")
rot=hg.Vector3(0,0,0)

-- Main loop:

while not plus:KeyDown(hg.KeyEscape) and not plus:IsAppEnded() do
    delta_t = plus:UpdateClock()
    dts = hg.time_to_sec_f(delta_t)

    fps:UpdateAndApplyToNode(camera, delta_t)
    rot.x=rot.x+0.01
    rot.y=rot.y+0.045
    rot.z=rot.z+0.03146
    cube:GetTransform():SetRotation(rot)

    plus:UpdateScene(scene, delta_t)
    plus:Flip()
    plus:EndFrame()
end
plus:RenderUninit()

pinned #2