Skip to content

Commit

Permalink
add plant pot and fix revolve operation
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Sep 16, 2024
1 parent 4658186 commit af073a0
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cpu_evaluators.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ func (e *revolution) Evaluate(pos []ms3.Vec, dist []float32, userData any) error
if err != nil {
return err
}
sdf, err := gleval.AssertSDF2(e.s)
sdf, err := gleval.AssertSDF2(e.s2d)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion examples/gasket/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func init() {
runtime.LockOSThread() // In case we wish to use OpenGL.
}

// scene returns the showerhead object.
// scene returns the gasket object.
func scene() (glbuild.Shader3D, error) {
// Sistema Food Storage Container geometry definitions.
// The problem we are trying to solve is how the container is not airtight
Expand Down
98 changes: 98 additions & 0 deletions examples/plantpot/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"flag"
"fmt"
"log"

"runtime"

"os"

math "github.com/chewxy/math32"
"github.com/soypat/glgl/math/ms2"
"github.com/soypat/gsdf"
"github.com/soypat/gsdf/glbuild"
"github.com/soypat/gsdf/gsdfaux"
)

const (
exampleName = "plantpot"
stl = exampleName + ".stl"
visualization = exampleName + ".glsl"
visualization2D = exampleName + ".png"
potBaseRadius = 40.
)

func init() {
runtime.LockOSThread() // In case we wish to use OpenGL.
}

// scenePotBase returns the plant pot base object.
func scenePotBase() (glbuild.Shader3D, error) {
const (
baseHeight = 10.
baseInclinationDeg = 45.
baseInclination = baseInclinationDeg * math.Pi / 180 //Convert to radians.
baseWallThick = 5.
baseLipRadius = baseWallThick * .54
)

xOff := baseHeight * math.Sin(baseInclination)
var poly ms2.PolygonBuilder
poly.AddXY(0, 0)
poly.AddXY(potBaseRadius, 0)
poly.AddXY(potBaseRadius+xOff, baseHeight)
poly.AddRelativeXY(baseWallThick/3, -baseWallThick).Arc(-baseLipRadius, 20)
poly.AddXY(potBaseRadius+baseWallThick/2, -baseWallThick)
poly.AddXY(0, -baseWallThick)
verts, err := poly.AppendVecs(nil)
if err != nil {
return nil, err
}
poly2, err := gsdf.NewPolygon(verts)
if err != nil {
return nil, err
}
err = gsdfaux.RenderPNGFile(visualization2D, poly2, 1080, false, gsdfaux.ColorConversionInigoQuilez(poly2.Bounds().Diagonal()/3))
if err != nil {
return nil, err
}
return gsdf.Revolve(poly2, 0)
}

func run() error {
useGPU := flag.Bool("gpu", false, "Enable GPU usage")
flag.Parse()
object, err := scenePotBase()
if err != nil {
return err
}
fpstl, err := os.Create(stl)
if err != nil {
return err
}
defer fpstl.Close()
fpvis, err := os.Create(visualization)
if err != nil {
return err
}
defer fpvis.Close()

err = gsdfaux.Render(object, gsdfaux.RenderConfig{
STLOutput: fpstl,
VisualOutput: fpvis,
Resolution: object.Bounds().Diagonal() / 350,
UseGPU: *useGPU,
})

return err
}

func main() {
err := run()
if err != nil {
log.Fatal(err)
}
fmt.Println(exampleName, "example done")
}
3 changes: 2 additions & 1 deletion glrender/octree.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func (oc *Octree) Reset(s gleval.SDF3, cubeResolution float32) error {
}
// Scale the bounding box about the center to make sure the boundaries
// aren't on the object surface.
bb := s.Bounds()
scaling := ms3.Vec{X: 1.01, Y: 1.01, Z: 1.01}
bb := s.Bounds().ScaleCentered(scaling)
topCube, origin, err := makeICube(bb, cubeResolution)
if err != nil {
return err
Expand Down
17 changes: 9 additions & 8 deletions gsdf2d.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,39 +343,40 @@ func Revolve(s glbuild.Shader2D, axisOffset float32) (glbuild.Shader3D, error) {
} else if axisOffset < 0 {
return nil, errors.New("negative axis offset")
}
return &revolution{s: s, off: axisOffset}, nil
return &revolution{s2d: s, off: axisOffset}, nil
}

type revolution struct {
s glbuild.Shader2D
s2d glbuild.Shader2D
off float32
}

func (r *revolution) Bounds() ms3.Box {
b2 := r.s.Bounds()
b2 := r.s2d.Bounds()
radius := math32.Max(0, b2.Max.X-r.off)
return ms3.Box{
Min: ms3.Vec{X: b2.Min.X, Y: b2.Min.Y, Z: -r.off},
Max: ms3.Vec{X: b2.Max.X, Y: b2.Max.Y, Z: r.off}, // TODO
Min: ms3.Vec{X: -radius, Y: b2.Min.Y, Z: -radius},
Max: ms3.Vec{X: radius, Y: b2.Max.Y, Z: radius}, // TODO
}
}

func (r *revolution) ForEach2DChild(userData any, fn func(userData any, s *glbuild.Shader2D) error) error {
return fn(userData, &r.s)
return fn(userData, &r.s2d)
}
func (r *revolution) ForEachChild(userData any, fn func(userData any, s *glbuild.Shader3D) error) error {
return nil
}

func (r *revolution) AppendShaderName(b []byte) []byte {
b = append(b, "revolution_"...)
b = r.s.AppendShaderName(b)
b = r.s2d.AppendShaderName(b)
return b
}

func (r *revolution) AppendShaderBody(b []byte) []byte {
b = glbuild.AppendFloatDecl(b, "w", r.off)
b = append(b, "vec2 q = vec2( length(p.xz) - w, p.y );\n"...)
b = glbuild.AppendDistanceDecl(b, "d", "q", r.s)
b = glbuild.AppendDistanceDecl(b, "d", "q", r.s2d)
b = append(b, "return d;"...)
return b
}
Expand Down

0 comments on commit af073a0

Please sign in to comment.