Skip to content

Commit

Permalink
Tutorials update, based on the HARFANG 2.0 release.
Browse files Browse the repository at this point in the history
  • Loading branch information
astrofra committed Jun 18, 2021
1 parent 005b6db commit c7d5ecd
Show file tree
Hide file tree
Showing 65 changed files with 1,706 additions and 1,244 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# HARFANG® 2.0 Tutorials

These **tutorials** demonstrate the usage of the HARFANG 2.0 API in **Python** and **Lua**.
If you want to know more about HARFANG, please visit the [official website](https://www.harfang3d.com).

## Screenshots
* AAA Rendering Pipeline
![AAA rendering pipeline](screenshots/aaa.png)

* Mouse flight controller
![Mouse flight controller](screenshots/game_mouse_flight.png)

* PBR materials
![PBR materials](screenshots/scene_pbr.png)

* Draw to multiple viewports
![Physics pool](screenshots/scene_draw_to_multiple_viewports.png)

* Physics pool of objects
![Physics pool](screenshots/physics_pool_of_objects.png)

* Physics Kapla
![Physics Kapla](screenshots/physics_kapla.png)

* Scene Instances
![Scene Instances](screenshots/scene_instances.png)

* Scene with many nodes
![Many object nodes](screenshots/scene_many_nodes.png)

* Forward rendering pipeline, using the lights priority
![Lights priority](screenshots/scene_light_priority.png)

* Dear ImGui edition
![Dear ImGui edition](screenshots/imgui_edit.png)
64 changes: 32 additions & 32 deletions audio_play_sound_spatialized.lua
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
-- Spatialized sound

hg = require("harfang")

hg.InputInit()
hg.WindowSystemInit()

hg.AddAssetsFolder("resources_compiled")

snd_ref = hg.LoadWAVSoundAsset("sounds/metro_announce.wav")
src_ref = hg.PlaySpatialized(snd_ref, hg.SpatializedSourceState(hg.Mat4.Identity, 1, hg.SR_Loop))

angle = 0

while not hg.ReadKeyboard('raw'):Key(hg.K_Escape) do
dt = hg.TickClock()
dt_sec_f = hg.time_to_sec_f(dt) -- delta frame in seconds

-- compute the source old and new position in world
src_old_pos = hg.Vec3(math.sin(angle), 0, math.cos(angle)) * 5
angle = angle + hg.time_to_sec_f(hg.TickClock()) * 45
src_new_pos = hg.Vec3(math.sin(angle), 0, math.cos(angle)) * 5

-- source velocity in m.s-1
src_vel = (src_new_pos - src_old_pos) / dt_sec_f

-- update source properties
hg.SetSourceTransform(src_ref, hg.TranslationMat4(src_new_pos), src_vel)
end

hg.AudioShutdown()
hg.InputShutdown()
-- Spatialized sound

hg = require("harfang")

hg.InputInit()
hg.WindowSystemInit()

hg.AddAssetsFolder("resources_compiled")

snd_ref = hg.LoadWAVSoundAsset("sounds/metro_announce.wav")
src_ref = hg.PlaySpatialized(snd_ref, hg.SpatializedSourceState(hg.Mat4.Identity, 1, hg.SR_Loop))

angle = 0

while not hg.ReadKeyboard('raw'):Key(hg.K_Escape) do
dt = hg.TickClock()
dt_sec_f = hg.time_to_sec_f(dt) -- delta frame in seconds

-- compute the source old and new position in world
src_old_pos = hg.Vec3(math.sin(angle), 0, math.cos(angle)) * 5
angle = angle + hg.time_to_sec_f(hg.TickClock()) * 45
src_new_pos = hg.Vec3(math.sin(angle), 0, math.cos(angle)) * 5

-- source velocity in m.s-1
src_vel = (src_new_pos - src_old_pos) / dt_sec_f

-- update source properties
hg.SetSourceTransform(src_ref, hg.TranslationMat4(src_new_pos), src_vel)
end

hg.AudioShutdown()
hg.InputShutdown()
38 changes: 19 additions & 19 deletions audio_play_sound_stereo.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
-- Play a mono sound with stereo panning

hg = require("harfang")

hg.InputInit()
hg.AudioInit()

snd_ref = hg.LoadWAVSoundFile('resources_compiled/sounds/metro_announce.wav') -- WAV 44.1kHz 16bit mono
src_ref = hg.PlayStereo(snd_ref, hg.StereoSourceState(1, hg.SR_Loop))

angle = 0

while not hg.ReadKeyboard('raw'):Key(hg.K_Escape) do
angle = angle + hg.time_to_sec_f(hg.TickClock()) * 0.5
hg.SetSourcePanning(src_ref, math.sin(angle)) -- panning left = -1, panning right = 1
end

hg.AudioShutdown()
hg.InputShutdown()
-- Play a mono sound with stereo panning

hg = require("harfang")

hg.InputInit()
hg.AudioInit()

snd_ref = hg.LoadWAVSoundFile('resources_compiled/sounds/metro_announce.wav') -- WAV 44.1kHz 16bit mono
src_ref = hg.PlayStereo(snd_ref, hg.StereoSourceState(1, hg.SR_Loop))

angle = 0

while not hg.ReadKeyboard('raw'):Key(hg.K_Escape) do
angle = angle + hg.time_to_sec_f(hg.TickClock()) * 0.5
hg.SetSourcePanning(src_ref, math.sin(angle)) -- panning left = -1, panning right = 1
end

hg.AudioShutdown()
hg.InputShutdown()
44 changes: 22 additions & 22 deletions basic_loop.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
-- Basic loop

hg = require("harfang")

hg.InputInit()
hg.WindowSystemInit()

width, height = 1280, 720
window = hg.RenderInit('Harfang - Basic Loop', width, height, hg.RF_VSync)

while not hg.ReadKeyboard():Key(hg.K_Escape) do
hg.SetViewClear(0, hg.CF_Color | hg.CF_Depth, hg.Color.Green, 1, 0)
hg.SetViewRect(0, 0, 0, width, height)

hg.Touch(0) -- force the view to be processed as it would be ignored since nothing is drawn to it (a clear does not count)

hg.Frame()
hg.UpdateWindow(window)
end

hg.RenderShutdown()
hg.DestroyWindow(window)
-- Basic loop

hg = require("harfang")

hg.InputInit()
hg.WindowSystemInit()

width, height = 1280, 720
window = hg.RenderInit('Harfang - Basic Loop', width, height, hg.RF_VSync)

while not hg.ReadKeyboard():Key(hg.K_Escape) do
hg.SetViewClear(0, hg.CF_Color | hg.CF_Depth, hg.Color.Green, 1, 0)
hg.SetViewRect(0, 0, 0, width, height)

hg.Touch(0) -- force the view to be processed as it would be ignored since nothing is drawn to it (a clear does not count)

hg.Frame()
hg.UpdateWindow(window)
end

hg.RenderShutdown()
hg.DestroyWindow(window)
90 changes: 45 additions & 45 deletions draw_lines.lua
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
-- Draw Lines

hg = require("harfang")

hg.InputInit()
hg.WindowSystemInit()

res_x, res_y = 1280, 720
win = hg.RenderInit('Harfang - Draw Lines', res_x, res_y, hg.RF_VSync | hg.RF_MSAA4X)

line_count = 1000

shader = hg.LoadProgramFromFile('resources_compiled/shaders/white')

-- vertices
vtx_layout = hg.VertexLayout()
vtx_layout:Begin()
vtx_layout:Add(hg.A_Position, 3, hg.AT_Float)
vtx_layout:End()

vtx = hg.Vertices(vtx_layout, line_count * 2)

-- main loop
angle = 0

while not hg.ReadKeyboard():Key(hg.K_Escape) do
hg.SetViewClear(0, hg.CF_Color | hg.CF_Depth, hg.ColorI(64, 64, 64), 1, 0)
hg.SetViewRect(0, 0, 0, res_x, res_y)

vtx:Clear()
for i = 0, line_count-1 do
vtx:Begin(2 * i):SetPos(hg.Vec3(math.sin(angle + i * 0.005), math.cos(angle + i * 0.01), 0)):End()
vtx:Begin(2 * i + 1):SetPos(hg.Vec3(math.sin(angle + i * -0.005), math.cos(angle + i * 0.005), 0)):End()
end

hg.DrawLines(0, vtx, shader) -- submit all lines in a single call

angle = angle + hg.time_to_sec_f(hg.TickClock())

hg.Frame()
hg.UpdateWindow(win)
end

hg.RenderShutdown()
hg.DestroyWindow(win)
-- Draw Lines

hg = require("harfang")

hg.InputInit()
hg.WindowSystemInit()

res_x, res_y = 1280, 720
win = hg.RenderInit('Harfang - Draw Lines', res_x, res_y, hg.RF_VSync | hg.RF_MSAA4X)

line_count = 1000

shader = hg.LoadProgramFromFile('resources_compiled/shaders/white')

-- vertices
vtx_layout = hg.VertexLayout()
vtx_layout:Begin()
vtx_layout:Add(hg.A_Position, 3, hg.AT_Float)
vtx_layout:End()

vtx = hg.Vertices(vtx_layout, line_count * 2)

-- main loop
angle = 0

while not hg.ReadKeyboard():Key(hg.K_Escape) do
hg.SetViewClear(0, hg.CF_Color | hg.CF_Depth, hg.ColorI(64, 64, 64), 1, 0)
hg.SetViewRect(0, 0, 0, res_x, res_y)

vtx:Clear()
for i = 0, line_count-1 do
vtx:Begin(2 * i):SetPos(hg.Vec3(math.sin(angle + i * 0.005), math.cos(angle + i * 0.01), 0)):End()
vtx:Begin(2 * i + 1):SetPos(hg.Vec3(math.sin(angle + i * -0.005), math.cos(angle + i * 0.005), 0)):End()
end

hg.DrawLines(0, vtx, shader) -- submit all lines in a single call

angle = angle + hg.time_to_sec_f(hg.TickClock())

hg.Frame()
hg.UpdateWindow(win)
end

hg.RenderShutdown()
hg.DestroyWindow(win)
121 changes: 62 additions & 59 deletions draw_lines_starfield.lua
Original file line number Diff line number Diff line change
@@ -1,59 +1,62 @@
-- Starfield 3D

hg = require("harfang")

hg.InputInit()
hg.WindowSystemInit()

width, height = 1280, 720
window = hg.RenderInit('Harfang - Starfield', width, height, hg.RF_VSync | hg.RF_MSAA4X)

-- vertex layout
vtx_layout = hg.VertexLayout()
vtx_layout:Begin()
vtx_layout:Add(hg.A_Position, 3, hg.AT_Float)
vtx_layout:Add(hg.A_Color0, 3, hg.AT_Float)
vtx_layout:End()

-- simple shader program
shader = hg.LoadProgramFromFile('resources_compiled/shaders/pos_rgb')

-- initialize stars
starfield_size = 10

max_stars = 1000
vtx = hg.Vertices(vtx_layout, max_stars * 2)

stars = {}
for i = 1, max_stars do
table.insert(stars, hg.RandomVec3(-starfield_size, starfield_size))
end

-- main loop
while not hg.ReadKeyboard():Key(hg.K_Escape) do
hg.SetViewClear(0, hg.CF_Color | hg.CF_Depth, hg.Color.Black, 1, 0)
hg.SetViewRect(0, 0, 0, width, height)

dt = hg.TickClock()
dt_f = hg.time_to_sec_f(dt)

-- update stars
vtx:Clear()
for i, star in ipairs(stars) do
star.z = star.z - 2 * dt_f
if star.z < starfield_size then
star.z = star.z + starfield_size
end
vtx:Begin(2 * (i-1)):SetPos(star * hg.Vec3(1 / star.z, 1 / star.z, 0)):SetColor0(hg.Color.Black):End()
vtx:Begin(2 * (i - 1) + 1):SetPos(star * hg.Vec3(1.04 / star.z, 1.04 / star.z, 0)):SetColor0(hg.Color.White):End()
end

-- draw stars as lines
hg.DrawLines(0, vtx, shader)

hg.Frame()
hg.UpdateWindow(window)
end

hg.RenderShutdown()
hg.DestroyWindow(window)
-- Starfield 3D

hg = require("harfang")

hg.InputInit()
hg.WindowSystemInit()

width, height = 1280, 720
window = hg.RenderInit('Harfang - Starfield', width, height, hg.RF_VSync | hg.RF_MSAA4X)

-- vertex layout
vtx_layout = hg.VertexLayout()
vtx_layout:Begin()
vtx_layout:Add(hg.A_Position, 3, hg.AT_Float)
vtx_layout:Add(hg.A_Color0, 3, hg.AT_Float)
vtx_layout:End()

-- simple shader program
shader = hg.LoadProgramFromFile('resources_compiled/shaders/pos_rgb')

-- initialize stars
starfield_size = 10

max_stars = 1000
vtx = hg.Vertices(vtx_layout, max_stars * 2)

stars = {}
for i = 1, max_stars do
table.insert(stars, hg.RandomVec3(-starfield_size, starfield_size))
end

-- main loop
while not hg.ReadKeyboard():Key(hg.K_Escape) do
hg.SetViewClear(0, hg.CF_Color | hg.CF_Depth, hg.Color.Black, 1, 0)
hg.SetViewRect(0, 0, 0, width, height)

dt = hg.TickClock()
dt_f = hg.time_to_sec_f(dt)

-- update stars
vtx:Clear()
for i, star in ipairs(stars) do
star.z = star.z - 2 * dt_f
if star.z < starfield_size then
star.z = star.z + starfield_size
end
vtx:Begin(2 * (i-1)):SetPos(star * hg.Vec3(1 / star.z, 1 / star.z, 0)):SetColor0(hg.Color.Black):End()
vtx:Begin(2 * (i - 1) + 1):SetPos(star * hg.Vec3(1.04 / star.z, 1.04 / star.z, 0)):SetColor0(hg.Color.White):End()
end

-- draw stars as lines
hg.DrawLines(0, vtx, shader)

hg.Frame()
hg.UpdateWindow(window)

-- prevent GC bottleneck
collectgarbage()
end

hg.RenderShutdown()
hg.DestroyWindow(window)
Loading

0 comments on commit c7d5ecd

Please sign in to comment.