Skip to content

Commit

Permalink
add UI
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Oct 8, 2024
1 parent 92ef791 commit 30c5645
Show file tree
Hide file tree
Showing 8 changed files with 414 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ All images and shapes in readme were generated using this library.

## Features

- UI for visualizing parts, rendered directly from shaders. See [UI example](./examples/ui) by running `go run ./examples/ui`

- GPU and CPU implementations for all shapes and operations. CPU implementations are actually faster for simple parts.

- Include arbitrary buffers into GPU calculation. See [`Shader` interface](./glbuild/glbuild.go).
Expand Down
49 changes: 49 additions & 0 deletions examples/ui/ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"log"
"math"
"runtime"

"github.com/soypat/glgl/math/ms3"
"github.com/soypat/gsdf"
"github.com/soypat/gsdf/forge/threads"
"github.com/soypat/gsdf/glbuild"
"github.com/soypat/gsdf/gsdfaux"
)

func init() {
runtime.LockOSThread()
}

// scene generates the 3D object for rendering.
func scene() (glbuild.Shader3D, error) {
const L, shank = 8, 3
threader := threads.ISO{D: 3, P: 0.5, Ext: true}
M3, err := threads.Bolt(threads.BoltParams{
Thread: threader,
Style: threads.NutHex,
TotalLength: L + shank,
ShankLength: shank,
})
if err != nil {
return nil, err
}
M3, _ = gsdf.Rotate(M3, 2.5*math.Pi/2, ms3.Vec{X: 1, Z: 0.1})
return M3, nil
}

func main() {
shape, err := scene()
shape = gsdf.Scale(shape, 0.3)
if err != nil {
log.Fatal("creating scene:", err)
}
err = gsdfaux.UI(shape, gsdfaux.UIConfig{
Width: 800,
Height: 600,
})
if err != nil {
log.Fatal("UI:", err)
}
}
6 changes: 5 additions & 1 deletion gleval/gpu_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ func (lines *Lines2DGPU) evaluate(pos []ms2.Vec, dist []float32, userData any) (
}
defer prog.Delete()
prog.Bind()
prog.SetUniform1f("WidthOffset\x00", lines.Width/2)
loc, err := prog.UniformLocation("WidthOffset\x00")
if err != nil {
return err
}
prog.SetUniformf(loc, lines.Width/2)
err = glgl.Err()
if err != nil {
return fmt.Errorf("binding LinesGPU program: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.1
require (
github.com/chewxy/math32 v1.11.1
github.com/go-gl/gl v0.0.0-20231021071112-07e5d0ea2e71
github.com/soypat/glgl v0.0.0-20241007020858-78b69f528d70
github.com/soypat/glgl v0.0.0-20241008221808-d7a058908ab3
)

require (
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ github.com/soypat/glgl v0.0.0-20241005144144-20e590a649e7 h1:mBevCWAgR+poTuSPqmy
github.com/soypat/glgl v0.0.0-20241005144144-20e590a649e7/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/soypat/glgl v0.0.0-20241007020858-78b69f528d70 h1:oAG7pXMAOS2v9nBaTp8qhmofwt/LlPKXPV7lxPJh60Y=
github.com/soypat/glgl v0.0.0-20241007020858-78b69f528d70/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/soypat/glgl v0.0.0-20241007044135-f3d594c19467 h1:I3OXt51Iddvx24TmQrh8mRl91KgTNKZwnKote6YHBac=
github.com/soypat/glgl v0.0.0-20241007044135-f3d594c19467/go.mod h1:1LcEp6XHSMCI91WlJHzl/aW4Bp5v6yQOiYFyjrlk350=
github.com/soypat/glgl v0.0.0-20241008221808-d7a058908ab3 h1:XQ/ITfV8+nbgFXL+tyjihgwppmxF2hyjDtRNCClwalY=
github.com/soypat/glgl v0.0.0-20241008221808-d7a058908ab3/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=
11 changes: 11 additions & 0 deletions gsdfaux/gsdfaux.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ type RenderConfig struct {
EnableCaching bool
}

type UIConfig struct {
Width, Height int
}

func UI(s glbuild.Shader3D, cfg UIConfig) error {
if s == nil {
return errors.New("nil shader")
}
return ui(s, cfg)
}

// RenderShader3D is an auxiliary function to aid users in getting setup in using gsdf quickly.
// Ideally users should implement their own rendering functions since applications may vary widely.
func RenderShader3D(s glbuild.Shader3D, cfg RenderConfig) (err error) {
Expand Down
9 changes: 9 additions & 0 deletions gsdfaux/gsdfaux_nocgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build tinygo || !cgo

package gsdfaux

import "github.com/soypat/gsdf/glbuild"

func ui(s glbuild.Shader3D, cfg UIConfig) error {
return errors.new("require cgo for UI rendering")
}
Loading

0 comments on commit 30c5645

Please sign in to comment.