Skip to content

Commit

Permalink
bettwe geb example, api cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Nov 29, 2024
1 parent bed2388 commit da0bdf9
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 75 deletions.
97 changes: 97 additions & 0 deletions examples/image-text/text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package main

import (
"flag"
"fmt"
"image/color"
"log"
"runtime"
"time"

"github.com/chewxy/math32"
"github.com/soypat/glgl/math/ms1"
"github.com/soypat/gsdf"
"github.com/soypat/gsdf/forge/textsdf"
"github.com/soypat/gsdf/glbuild"
"github.com/soypat/gsdf/gleval"
"github.com/soypat/gsdf/gsdfaux"
)

const filename = "text.png"

func init() {
runtime.LockOSThread()
}

func scene(bld *gsdf.Builder) (glbuild.Shader2D, error) {
var f textsdf.Font
f.Configure(textsdf.FontConfig{
RelativeGlyphTolerance: 0.001,
})
err := f.LoadTTFBytes(textsdf.ISO3098TTF())
if err != nil {
return nil, err
}
return f.TextLine("Abc123~")
}

func main() {
useGPU := false
flag.BoolVar(&useGPU, "gpu", useGPU, "Enable GPU usage")
flag.Parse()
if useGPU {
term, err := gleval.Init1x1GLFW()
if err != nil {
log.Fatal(err)
}
defer term()
}
var bld gsdf.Builder
s, err := scene(&bld)
if err != nil {
log.Fatal(err)
}
var sdf2 gleval.SDF2
if useGPU {
sdf2, err = gsdfaux.MakeGPUSDF2(s)
} else {
sdf2, err = gleval.NewCPUSDF2(s)
}
if err != nil {
log.Fatal(err)
}
if !useGPU {
fmt.Println("GPU usage not enabled (-gpu flag). Enable for faster rendering")
}

charHeight := sdf2.Bounds().Size().Y
edgeAliasing := charHeight / 1000
start := time.Now()
err = gsdfaux.RenderPNGFile(filename, sdf2, 300, blackAndWhite(edgeAliasing))
if err != nil {
log.Fatal(err)
}
fmt.Println("PNG file rendered to", filename, "in", time.Since(start))
}

func blackAndWhite(edgeSmooth float32) func(d float32) color.Color {
if edgeSmooth <= 0 {
return blackAndWhiteNoSmoothing
}
return func(d float32) color.Color {
// Smoothstep anti-aliasing near the edge
blend := 0.5 + 0.5*math32.Tanh(d/edgeSmooth)
// Clamp blend to [0, 1] for valid grayscale values
blend = ms1.Clamp(blend, 0, 1)
// Convert blend to grayscale
grayValue := uint8(blend * 255)
return color.Gray{Y: grayValue}
}
}

func blackAndWhiteNoSmoothing(d float32) color.Color {
if d < 0 {
return color.Black
}
return color.White
}
67 changes: 0 additions & 67 deletions examples/text/text.go

This file was deleted.

7 changes: 4 additions & 3 deletions examples/ui-geb/uigeb.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ func scene(bld *gsdf.Builder) (glbuild.Shader3D, error) {
B3 := bld.Extrude(B, L)

// Non-uniform scaling to fill letter intersections.
G3 = bld.Transform(G3, ms3.ScaleMat4(ms3.Vec{X: sclG.X, Y: sclG.Y, Z: 1}))
E3 = bld.Transform(E3, ms3.ScaleMat4(ms3.Vec{X: sclE.X, Y: sclE.Y, Z: 1}))
B3 = bld.Transform(B3, ms3.ScaleMat4(ms3.Vec{X: sclB.X, Y: sclB.Y, Z: 1}))
G3 = bld.Transform(G3, ms3.ScalingMat4(ms3.Vec{X: sclG.X, Y: sclG.Y, Z: 1}))
E3 = bld.Transform(E3, ms3.ScalingMat4(ms3.Vec{X: sclE.X, Y: sclE.Y, Z: 1}))
B3 = bld.Transform(B3, ms3.ScalingMat4(ms3.Vec{X: sclB.X, Y: sclB.Y, Z: 1}))

const round2 = 0.025
G3 = bld.Offset(G3, -round2)
E3 = bld.Offset(E3, -round2)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/go-gl/glfw v0.0.0-20221017161538-93cebf72946b
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/soypat/glgl v0.0.0-20241121001014-cc8498d2a83d
github.com/soypat/glgl v0.0.0-20241124175250-a2463fe190a5
golang.org/x/image v0.22.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/soypat/glgl v0.0.0-20241121001014-cc8498d2a83d h1:kDdWM661L/RAxg0j4gV+18hky7/3Tvbhd8O6p8CLB7w=
github.com/soypat/glgl v0.0.0-20241121001014-cc8498d2a83d/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/soypat/glgl v0.0.0-20241124175250-a2463fe190a5 h1:PyD0ceAopD2FDv3ddx99Q+h7QxIzDPPuOQiaZrRA7yU=
github.com/soypat/glgl v0.0.0-20241124175250-a2463fe190a5/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30 h1:m9O6OTJ627iFnN2JIWfdqlZCzneRO6EEBsHXI25P8ws=
golang.org/x/exp v0.0.0-20221230185412-738e83a70c30/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/image v0.22.0 h1:UtK5yLUzilVrkjMAZAZ34DXGpASN8i8pj8g+O+yd10g=
Expand Down
6 changes: 3 additions & 3 deletions gsdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func TestTransformDuplicateBug(t *testing.T) {
B3 := bld.Extrude(B, L)

// Non-uniform scaling to fill letter intersections.
G3 = bld.Transform(G3, ms3.ScaleMat4(ms3.Vec{X: 1.2, Y: 1.3, Z: 1}))
E3 = bld.Transform(E3, ms3.ScaleMat4(ms3.Vec{X: 1.2, Y: 1.3, Z: 1}))
B3 = bld.Transform(B3, ms3.ScaleMat4(ms3.Vec{X: 1.2, Y: 1.3, Z: 1}))
G3 = bld.Transform(G3, ms3.ScalingMat4(ms3.Vec{X: 1.2, Y: 1.3, Z: 1}))
E3 = bld.Transform(E3, ms3.ScalingMat4(ms3.Vec{X: 1.2, Y: 1.3, Z: 1}))
B3 = bld.Transform(B3, ms3.ScalingMat4(ms3.Vec{X: 1.2, Y: 1.3, Z: 1}))
const round2 = 0.025
G3 = bld.Offset(G3, -round2)
E3 = bld.Offset(E3, -round2)
Expand Down
2 changes: 1 addition & 1 deletion operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func (bld *Builder) Rotate(s glbuild.Shader3D, radians float32, axis ms3.Vec) gl
if axis == (ms3.Vec{}) {
bld.shapeErrorf("null vector")
}
T := ms3.RotationMat4(radians, axis)
T := ms3.RotatingMat4(radians, axis)
return bld.Transform(s, T)
}

Expand Down

0 comments on commit da0bdf9

Please sign in to comment.