Skip to content

Commit

Permalink
Tutorials update for HARFANG v3.2.4.
Browse files Browse the repository at this point in the history
Core update for for HARFANG v3.2.4.
Added the Go tutorials.
  • Loading branch information
astrofra committed Sep 27, 2022
1 parent 73b0418 commit c8f2621
Show file tree
Hide file tree
Showing 52 changed files with 2,517 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/resources_compiled
/_bin
/assetc-ubuntu-x64-2.3.0
11 changes: 9 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
"configurations": [
{
"name": "Python: Current File",
"type": "python",
Expand All @@ -19,7 +19,14 @@
"stopOnEntry": false,
"program": "${file}",
"cwd": "${workspaceFolder}/",
"luaexe": "${workspaceFolder}/_bin/lua.exe",
"luaexe": "d:/harfang/hg_lua/lua.exe",
},
{
"name": "Go: Current file",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${file}"
}
]
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ You can now execute the tutorials from the folder you unzipped them to.
```bash
D:\tutorials-hg2>python draw_lines.py
```
or
```bash
D:\tutorials-hg2>go run draw_lines.go
```

Alternatively, you can open the tutorial folder using [Visual Studio Code](https://code.visualstudio.com/) and use the provided debug targets to run the tutorials.

Expand Down
37 changes: 37 additions & 0 deletions audio_play_sound_spatialized.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"math"

hg "github.com/harfang3d/harfang-go/v3"
)

func main() {
hg.InputInit()
hg.AudioInit()

hg.AddAssetsFolder("resources_compiled")

snd_ref := hg.LoadWAVSoundAsset("sounds/metro_announce.wav")
src_ref := hg.PlaySpatialized(hg.SoundRef(snd_ref), hg.NewSpatializedSourceStateWithMtxVolumeRepeat(hg.Mat4GetIdentity(), 1, hg.SRLoop))

angle := 0.0

for !hg.ReadKeyboard().Key(hg.KEscape) {
dt := hg.TickClock()
dt_sec_f := hg.TimeToSecF(dt) // delta frame in seconds

// compute the source old and new position in world
src_old_pos := hg.NewVec3WithXYZ(float32(math.Sin(angle)), 0, float32(math.Cos(angle))).MulWithK(5.0)
angle += float64(hg.TimeToSecF(hg.TickClock())) * 45.0
src_new_pos := hg.NewVec3WithXYZ(float32(math.Sin(angle)), 0, float32(math.Cos(angle))).MulWithK(5.0)

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

// update source properties
hg.SetSourceTransform(hg.SourceRef(src_ref), hg.TranslationMat4(src_new_pos), src_vel)
}
hg.AudioShutdown()
hg.InputShutdown()
}
24 changes: 24 additions & 0 deletions audio_play_sound_stereo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"math"

hg "github.com/harfang3d/harfang-go/v3"
)

func main() {
hg.InputInit()
hg.AudioInit()

snd_ref := hg.LoadWAVSoundFile("resources_compiled/sounds/metro_announce.wav") // WAV 44.1kHz 16bit mono
src_ref := hg.PlayStereo(hg.SoundRef(snd_ref), hg.NewStereoSourceStateWithVolumeRepeat(1, hg.SRLoop))

angle := 0.0

for !hg.ReadKeyboardWithName("raw").Key(hg.KEscape) {
angle += float64(hg.TimeToSecF(hg.TickClock())) * 0.5
hg.SetSourcePanning(hg.SourceRef(src_ref), float32(math.Sin(angle))) // panning left = -1, panning right = 1
}
hg.AudioShutdown()
hg.InputShutdown()
}
23 changes: 23 additions & 0 deletions audio_stream_ogg_stereo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"math"

hg "github.com/harfang3d/harfang-go/v3"
)

func main() {
hg.InputInit()
hg.AudioInit()

src_ref := hg.StreamOGGFileStereo("resources_compiled/sounds/metro_announce.ogg", hg.NewStereoSourceStateWithVolumeRepeat(1, hg.SRLoop)) // OGG 44.1kHz 16bit mono

angle := 0.0

for !hg.ReadKeyboardWithName("raw").Key(hg.KEscape) {
angle += float64(hg.TimeToSecF(hg.TickClock())) * 0.5
hg.SetSourcePanning(hg.SourceRef(src_ref), float32(math.Sin(angle))) // panning left = -1, panning right = 1
}
hg.AudioShutdown()
hg.InputShutdown()
}
31 changes: 31 additions & 0 deletions basic_loop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"runtime"

hg "github.com/harfang3d/harfang-go/v3"
)

func main() {
hg.InputInit()
hg.WindowSystemInit()

width, height := int32(1280), int32(720)

window := hg.RenderInitWithWindowTitleWidthHeightResetFlags("Harfang - Basic Loop", width, height, hg.RFVSync)

for !hg.ReadKeyboard().Key(hg.KEscape) && hg.IsWindowOpen(window) {
hg.SetViewClearWithColDepthStencil(0, hg.CFColor|hg.CFDepth, hg.ColorGetGreen(), 1, 0)
hg.SetViewRect(0, 0, 0, uint16(width), uint16(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)
runtime.GC()
}

hg.RenderShutdown()
hg.DestroyWindow(window)

}
55 changes: 55 additions & 0 deletions draw_lines.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"math"
"runtime"

hg "github.com/harfang3d/harfang-go/v3"
)

func main() {
hg.InputInit()
hg.WindowSystemInit()

resX, resY := int32(1280), int32(720)

window := hg.RenderInitWithWindowTitleWidthHeightResetFlags("Harfang - Draw Lines", resX, resY, hg.RFVSync|hg.RFMSAA4X)

lineCount := int32(1000)

shader := hg.LoadProgramFromFile("resources_compiled/shaders/white")

// vertices
vtxLayout := hg.NewVertexLayout()
vtxLayout.Begin()
vtxLayout.Add(hg.APosition, 3, hg.ATFloat)
vtxLayout.End()

vtx := hg.NewVertices(vtxLayout, lineCount*2)

// main loop
angle := 0.0

for !hg.ReadKeyboard().Key(hg.KEscape) && hg.IsWindowOpen(window) {
hg.SetViewClearWithColDepthStencil(0, hg.CFColor|hg.CFDepth, hg.ColorI(64, 64, 64), 1.0, 0)
hg.SetViewRect(0, 0, 0, uint16(resX), uint16(resY))

vtx.Clear()
for i := 0.0; i < float64(lineCount); i++ {
vtx.Begin(int32(2 * i)).SetPos(hg.NewVec3WithXYZ(float32(math.Sin(angle+i*0.005)), float32(math.Cos(angle+i*0.01)), 0.0)).End()
vtx.Begin(int32(2*i + 1)).SetPos(hg.NewVec3WithXYZ(float32(math.Sin(angle+i*-0.005)), float32(math.Cos(angle+i*0.005)), 0.0)).End()
}

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

angle = angle + float64(hg.TimeToSecF(hg.TickClock()))

hg.Frame()
hg.UpdateWindow(window)
runtime.GC()
}

hg.RenderShutdown()
hg.DestroyWindow(window)

}
67 changes: 67 additions & 0 deletions draw_lines_starfield.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"runtime"

hg "github.com/harfang3d/harfang-go/v3"
)

func main() {
hg.InputInit()
hg.WindowSystemInit()

width, height := int32(1280), int32(720)
window := hg.RenderInitWithWindowTitleWidthHeightResetFlags("Harfang - Starfield", width, height, hg.RFVSync|hg.RFMSAA4X)

// vertex layout
vtxLayout := hg.NewVertexLayout()
vtxLayout.Begin()
vtxLayout.Add(hg.APosition, 3, hg.ATFloat)
vtxLayout.Add(hg.AColor0, 3, hg.ATFloat)
vtxLayout.End()

// simple shader program
shader := hg.LoadProgramFromFile("resources_compiled/shaders/pos_rgb")

// initialize stars
starfieldSize := float32(10.0)

maxStars := 1000
vtx := hg.NewVertices(vtxLayout, int32(maxStars*2))

var stars []*hg.Vec3
for i := 0; i < maxStars; i++ {
stars = append(stars, hg.RandomVec3(-starfieldSize, starfieldSize))
}

for !hg.ReadKeyboard().Key(hg.KEscape) && hg.IsWindowOpen(window) {
hg.SetViewClearWithColDepthStencil(0, hg.CFColor|hg.CFDepth, hg.ColorGetBlack(), 1.0, 0)
hg.SetViewRect(0, 0, 0, uint16(width), uint16(height))

dt := hg.TickClock()
dt_f := hg.TimeToSecF(dt)

// update stars
vtx.Clear()
for i, star := range stars {
star.SetZ(star.GetZ() - float32(2.0*dt_f))
if star.GetZ() < starfieldSize {
star.SetZ(star.GetZ() + starfieldSize)
}

// draw stars
vtx.Begin(int32(2 * i)).SetPos(star.Mul(hg.NewVec3WithXYZ(1.0/star.GetZ(), 1.0/star.GetZ(), 0.0))).SetColor0(hg.ColorGetBlack()).End()
vtx.Begin(int32(2*i + 1)).SetPos(star.Mul(hg.NewVec3WithXYZ(1.04/star.GetZ(), 1.04/star.GetZ(), 0.0))).SetColor0(hg.ColorGetWhite()).End()
}

hg.DrawLines(0, vtx, shader)

hg.Frame()
hg.UpdateWindow(window)
runtime.GC()
}

hg.RenderShutdown()
hg.DestroyWindow(window)

}
42 changes: 42 additions & 0 deletions draw_model_no_pipeline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
hg "github.com/harfang3d/harfang-go/v3"
)

func main() {
hg.InputInit()
hg.WindowSystemInit()

resX, resY := int32(1280), int32(720)

window := hg.RenderInitWithWindowTitleWidthHeightResetFlags("Harfang - Draw Models no Pipeline", resX, resY, hg.RFVSync|hg.RFMSAA4X)

// vertex layout and models
vtxLayout := hg.VertexLayoutPosFloatNormUInt8()

cubeMdl := hg.CreateCubeModel(vtxLayout, 1, 1, 1)
groundMdl := hg.CreatePlaneModel(vtxLayout, 5, 5, 1, 1)

shader := hg.LoadProgramFromFile("resources_compiled/shaders/mdl")

// main loop
angle := float32(0.0)

for !hg.ReadKeyboard().Key(hg.KEscape) && hg.IsWindowOpen(window) {
dt := hg.TickClock()
angle = angle + hg.TimeToSecF(dt)

viewpoint := hg.TranslationMat4(hg.NewVec3WithXYZ(0, 1, -3))
hg.SetViewPerspective(0, 0, 0, resX, resY, viewpoint)

hg.DrawModelWithSliceOfValuesSliceOfTextures(0, cubeMdl, shader, hg.GoSliceOfUniformSetValue{}, hg.GoSliceOfUniformSetTexture{}, hg.TransformationMat4(hg.NewVec3WithXYZ(0.0, 1, 0.0), hg.NewVec3WithXYZ(angle, angle, angle)))
hg.DrawModelWithSliceOfValuesSliceOfTextures(0, groundMdl, shader, hg.GoSliceOfUniformSetValue{}, hg.GoSliceOfUniformSetTexture{}, hg.TranslationMat4(hg.NewVec3WithXYZ(0.0, 0.0, 0.0)))

hg.Frame()
hg.UpdateWindow(window)
}

hg.RenderShutdown()
hg.DestroyWindow(window)
}
34 changes: 34 additions & 0 deletions draw_text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
hg "github.com/harfang3d/harfang-go/v3"
)

func main() {
hg.InputInit()
hg.WindowSystemInit()

resX, resY := int32(1280), int32(720)
win := hg.RenderInitWithWindowTitleWidthHeightResetFlags("Harfang - Draw Text", resX, resY, hg.RFVSync)

// load font and shader program
font := hg.LoadFontFromFileWithSize("resources_compiled/font/default.ttf", 96)
font_prg := hg.LoadProgramFromFile("resources_compiled/core/shader/font")

// text uniforms and render state
text_uniform_values := hg.GoSliceOfUniformSetValue{hg.MakeUniformSetValueWithVec4V("u_color", hg.NewVec4WithXYZ(1, 1, 0))}
text_render_state := hg.ComputeRenderStateWithDepthTestCulling(hg.BMAlpha, hg.DTAlways, hg.FCDisabled)

// main loop
for !hg.ReadKeyboard().Key(hg.KEscape) && hg.IsWindowOpen(win) {
hg.SetView2DWithZnearZfarFlagsColorDepthStencil(0, 0, 0, resX, resY, -1, 1, hg.CFColor|hg.CFDepth, hg.ColorI(32, 32, 32), 0, 1)

hg.DrawTextWithPosHalignValignSliceOfValuesSliceOfTexturesState(0, font, "Hello world!", font_prg, "u_tex", 0, hg.Mat4GetIdentity(), hg.NewVec3WithXYZ(float32(resX/2), float32(resY/2), 0), hg.DTHACenter, hg.DTVACenter, text_uniform_values, hg.GoSliceOfUniformSetTexture{}, text_render_state)

hg.Frame()
hg.UpdateWindow(win)
}

hg.RenderShutdown()
hg.DestroyWindow(win)
}
Loading

0 comments on commit c8f2621

Please sign in to comment.