Skip to content

Commit da0bdf9

Browse files
committed
bettwe geb example, api cleanup
1 parent bed2388 commit da0bdf9

File tree

7 files changed

+108
-75
lines changed

7 files changed

+108
-75
lines changed

examples/image-text/text.go

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"image/color"
7+
"log"
8+
"runtime"
9+
"time"
10+
11+
"github.com/chewxy/math32"
12+
"github.com/soypat/glgl/math/ms1"
13+
"github.com/soypat/gsdf"
14+
"github.com/soypat/gsdf/forge/textsdf"
15+
"github.com/soypat/gsdf/glbuild"
16+
"github.com/soypat/gsdf/gleval"
17+
"github.com/soypat/gsdf/gsdfaux"
18+
)
19+
20+
const filename = "text.png"
21+
22+
func init() {
23+
runtime.LockOSThread()
24+
}
25+
26+
func scene(bld *gsdf.Builder) (glbuild.Shader2D, error) {
27+
var f textsdf.Font
28+
f.Configure(textsdf.FontConfig{
29+
RelativeGlyphTolerance: 0.001,
30+
})
31+
err := f.LoadTTFBytes(textsdf.ISO3098TTF())
32+
if err != nil {
33+
return nil, err
34+
}
35+
return f.TextLine("Abc123~")
36+
}
37+
38+
func main() {
39+
useGPU := false
40+
flag.BoolVar(&useGPU, "gpu", useGPU, "Enable GPU usage")
41+
flag.Parse()
42+
if useGPU {
43+
term, err := gleval.Init1x1GLFW()
44+
if err != nil {
45+
log.Fatal(err)
46+
}
47+
defer term()
48+
}
49+
var bld gsdf.Builder
50+
s, err := scene(&bld)
51+
if err != nil {
52+
log.Fatal(err)
53+
}
54+
var sdf2 gleval.SDF2
55+
if useGPU {
56+
sdf2, err = gsdfaux.MakeGPUSDF2(s)
57+
} else {
58+
sdf2, err = gleval.NewCPUSDF2(s)
59+
}
60+
if err != nil {
61+
log.Fatal(err)
62+
}
63+
if !useGPU {
64+
fmt.Println("GPU usage not enabled (-gpu flag). Enable for faster rendering")
65+
}
66+
67+
charHeight := sdf2.Bounds().Size().Y
68+
edgeAliasing := charHeight / 1000
69+
start := time.Now()
70+
err = gsdfaux.RenderPNGFile(filename, sdf2, 300, blackAndWhite(edgeAliasing))
71+
if err != nil {
72+
log.Fatal(err)
73+
}
74+
fmt.Println("PNG file rendered to", filename, "in", time.Since(start))
75+
}
76+
77+
func blackAndWhite(edgeSmooth float32) func(d float32) color.Color {
78+
if edgeSmooth <= 0 {
79+
return blackAndWhiteNoSmoothing
80+
}
81+
return func(d float32) color.Color {
82+
// Smoothstep anti-aliasing near the edge
83+
blend := 0.5 + 0.5*math32.Tanh(d/edgeSmooth)
84+
// Clamp blend to [0, 1] for valid grayscale values
85+
blend = ms1.Clamp(blend, 0, 1)
86+
// Convert blend to grayscale
87+
grayValue := uint8(blend * 255)
88+
return color.Gray{Y: grayValue}
89+
}
90+
}
91+
92+
func blackAndWhiteNoSmoothing(d float32) color.Color {
93+
if d < 0 {
94+
return color.Black
95+
}
96+
return color.White
97+
}

examples/text/text.go

-67
This file was deleted.

examples/ui-geb/uigeb.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ func scene(bld *gsdf.Builder) (glbuild.Shader3D, error) {
6565
B3 := bld.Extrude(B, L)
6666

6767
// Non-uniform scaling to fill letter intersections.
68-
G3 = bld.Transform(G3, ms3.ScaleMat4(ms3.Vec{X: sclG.X, Y: sclG.Y, Z: 1}))
69-
E3 = bld.Transform(E3, ms3.ScaleMat4(ms3.Vec{X: sclE.X, Y: sclE.Y, Z: 1}))
70-
B3 = bld.Transform(B3, ms3.ScaleMat4(ms3.Vec{X: sclB.X, Y: sclB.Y, Z: 1}))
68+
G3 = bld.Transform(G3, ms3.ScalingMat4(ms3.Vec{X: sclG.X, Y: sclG.Y, Z: 1}))
69+
E3 = bld.Transform(E3, ms3.ScalingMat4(ms3.Vec{X: sclE.X, Y: sclE.Y, Z: 1}))
70+
B3 = bld.Transform(B3, ms3.ScalingMat4(ms3.Vec{X: sclB.X, Y: sclB.Y, Z: 1}))
71+
7172
const round2 = 0.025
7273
G3 = bld.Offset(G3, -round2)
7374
E3 = bld.Offset(E3, -round2)

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/go-gl/glfw v0.0.0-20221017161538-93cebf72946b
99
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b
1010
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
11-
github.com/soypat/glgl v0.0.0-20241121001014-cc8498d2a83d
11+
github.com/soypat/glgl v0.0.0-20241124175250-a2463fe190a5
1212
golang.org/x/image v0.22.0
1313
)
1414

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF0
1010
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
1111
github.com/soypat/glgl v0.0.0-20241121001014-cc8498d2a83d h1:kDdWM661L/RAxg0j4gV+18hky7/3Tvbhd8O6p8CLB7w=
1212
github.com/soypat/glgl v0.0.0-20241121001014-cc8498d2a83d/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
13+
github.com/soypat/glgl v0.0.0-20241124175250-a2463fe190a5 h1:PyD0ceAopD2FDv3ddx99Q+h7QxIzDPPuOQiaZrRA7yU=
14+
github.com/soypat/glgl v0.0.0-20241124175250-a2463fe190a5/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
1315
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30 h1:m9O6OTJ627iFnN2JIWfdqlZCzneRO6EEBsHXI25P8ws=
1416
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
1517
golang.org/x/image v0.22.0 h1:UtK5yLUzilVrkjMAZAZ34DXGpASN8i8pj8g+O+yd10g=

gsdf_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ func TestTransformDuplicateBug(t *testing.T) {
2222
B3 := bld.Extrude(B, L)
2323

2424
// Non-uniform scaling to fill letter intersections.
25-
G3 = bld.Transform(G3, ms3.ScaleMat4(ms3.Vec{X: 1.2, Y: 1.3, Z: 1}))
26-
E3 = bld.Transform(E3, ms3.ScaleMat4(ms3.Vec{X: 1.2, Y: 1.3, Z: 1}))
27-
B3 = bld.Transform(B3, ms3.ScaleMat4(ms3.Vec{X: 1.2, Y: 1.3, Z: 1}))
25+
G3 = bld.Transform(G3, ms3.ScalingMat4(ms3.Vec{X: 1.2, Y: 1.3, Z: 1}))
26+
E3 = bld.Transform(E3, ms3.ScalingMat4(ms3.Vec{X: 1.2, Y: 1.3, Z: 1}))
27+
B3 = bld.Transform(B3, ms3.ScalingMat4(ms3.Vec{X: 1.2, Y: 1.3, Z: 1}))
2828
const round2 = 0.025
2929
G3 = bld.Offset(G3, -round2)
3030
E3 = bld.Offset(E3, -round2)

operations.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func (bld *Builder) Rotate(s glbuild.Shader3D, radians float32, axis ms3.Vec) gl
394394
if axis == (ms3.Vec{}) {
395395
bld.shapeErrorf("null vector")
396396
}
397-
T := ms3.RotationMat4(radians, axis)
397+
T := ms3.RotatingMat4(radians, axis)
398398
return bld.Transform(s, T)
399399
}
400400

0 commit comments

Comments
 (0)