Skip to content

Commit

Permalink
Add Translate2D; fix gsdfaux.RenderImage inverted y axis
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Aug 30, 2024
1 parent 057dfcd commit 57c8f9e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
18 changes: 18 additions & 0 deletions cpu_evaluators.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,3 +807,21 @@ func (r *offset2D) Evaluate(pos []ms2.Vec, dist []float32, userData any) error {
}
return nil
}

func (t *translate2D) Evaluate(pos []ms2.Vec, dist []float32, userData any) error {
sdf, err := gleval.AssertSDF2(t.s)
if err != nil {
return err
}
vp, err := gleval.GetVecPool(userData)
if err != nil {
return err
}
transformed := vp.V2.Acquire(len(pos))
defer vp.V2.Release(transformed)
T := t.p
for i, p := range pos {
transformed[i] = ms2.Sub(p, T)
}
return sdf.Evaluate(transformed, dist, userData)
}
3 changes: 2 additions & 1 deletion examples/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ func scene() (glbuild.Shader2D, error) {
}

func main() {
useGPU := false
s, err := scene()
if err != nil {
log.Fatal(err)
}
err = gsdfaux.RenderPNGFile(filename, s, 1080, nil)
err = gsdfaux.RenderPNGFile(filename, s, 1080, useGPU, nil)
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 3 additions & 1 deletion glrender/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ func (ir *ImageRendererSDF2) Render(sdf gleval.SDF2, img setImage, userData any)
bb.Min = ms2.Add(bb.Min, ms2.Vec{X: dx / 2, Y: dy / 2}) // Offset to center image.
// Keep track of next index to start reading.
for j := 0; j < dyi; j++ {
y := float32(j)*dy + bb.Min.Y
// y is inverted in the image interface, maximum index (maxI, maxJ) represents upper left corner.
// See [image.At] method so we must invert y here.
y := bb.Max.Y - (float32(j)*dy + bb.Min.Y)
err := ir.renderRow(sdf, j, y, bb.Min.X, dx, imgBB, img, userData)
if err != nil {
return err
Expand Down
35 changes: 35 additions & 0 deletions gsdf2d.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,38 @@ func (s *offset2D) AppendShaderBody(b []byte) []byte {
b = append(b, ')', ';')
return b
}

// Translate2D moves the SDF s in the given direction.
func Translate2D(s glbuild.Shader2D, dirX, dirY float32) glbuild.Shader2D {
return &translate2D{s: s, p: ms2.Vec{X: dirX, Y: dirY}}
}

type translate2D struct {
s glbuild.Shader2D
p ms2.Vec
}

func (u *translate2D) Bounds() ms2.Box {
return u.s.Bounds().Add(u.p)
}

func (s *translate2D) ForEach2DChild(userData any, fn func(userData any, s *glbuild.Shader2D) error) error {
return fn(userData, &s.s)
}

func (s *translate2D) AppendShaderName(b []byte) []byte {
b = append(b, "translate2D"...)
arr := s.p.Array()
b = glbuild.AppendFloats(b, 0, 'n', 'p', arr[:]...)
b = append(b, '_')
b = s.s.AppendShaderName(b)
return b
}

func (s *translate2D) AppendShaderBody(b []byte) []byte {
b = glbuild.AppendVec2Decl(b, "t", s.p)
b = append(b, "return "...)
b = s.s.AppendShaderName(b)
b = append(b, "(p-t);"...)
return b
}
6 changes: 5 additions & 1 deletion gsdfaux/gsdfaux.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ func stopwatch() func() time.Duration {
// RenderPNGFile renders a 2D SDF as an image and saves result to a PNG file with said filename.
// The image width is sized automatically from the image height argument to preserve SDF aspect ratio.
// If a nil color conversion function is passed then one is automatically chosen.
func RenderPNGFile(filename string, s glbuild.Shader2D, picHeight int, colorConversion func(float32) color.Color) error {
func RenderPNGFile(filename string, s glbuild.Shader2D, picHeight int, useGPU bool, colorConversion func(float32) color.Color) error {
if useGPU {
return errors.New("TODO: implement GPU rendering for RenderPNGFile")
}
bb := s.Bounds()
sz := bb.Size()
if colorConversion == nil {
Expand All @@ -155,6 +158,7 @@ func RenderPNGFile(filename string, s glbuild.Shader2D, picHeight int, colorConv
if err != nil {
return err
}

sdf, err := gleval.NewCPUSDF2(s)
if err != nil {
return err
Expand Down

0 comments on commit 57c8f9e

Please sign in to comment.