Files
sumi/main.go
2026-01-05 14:41:26 -06:00

286 lines
8.9 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"math"
"math/rand"
"os"
"time"
g "github.com/d2fn/sumi/internal/graphics"
"github.com/ojrac/opensimplex-go"
gui "github.com/gen2brain/raylib-go/raygui"
rl "github.com/gen2brain/raylib-go/raylib"
//"github.com/ojrac/opensimplex-go"
)
var (
snapshotsPath string
storage *Storage
)
type Layout struct {
monitor rl.RectangleInt32
window rl.RectangleInt32
controls rl.RectangleInt32
viewport rl.RectangleInt32
graphics rl.RectangleInt32
}
func bootstrap() Layout {
rl.InitWindow(800, 600, "bootstrap")
monitor := rl.GetCurrentMonitor()
fmt.Printf("Using monitor %d\n", monitor)
// derive defaults from the monitor size
monitorWidth := rl.GetMonitorWidth(monitor)
monitorHeight := rl.GetMonitorHeight(monitor)
// use a large portion of the available space, but not all of it!
defaultWindowWidth := int(float32(monitorWidth) * 0.95)
defaultWindowHeight := int(float32(monitorHeight) * 0.9)
windowWidth := defaultWindowWidth
windowHeight := defaultWindowHeight
// set controls to use 1/6th of the width
controlsRelWidth := 1.0 / 12.0
defaultGraphicsWidth := 5*int((float64(defaultWindowWidth)*(1.0-controlsRelWidth)))
defaultGraphicsHeight := 5*defaultWindowHeight
graphicsWidth := defaultGraphicsWidth
graphicsHeight := defaultGraphicsHeight
fmt.Printf("monitor : %d x %d / window : %d x %d / buffer : %d x %d",
monitorWidth, monitorHeight,
defaultWindowWidth, defaultWindowHeight,
defaultGraphicsWidth, defaultWindowHeight)
flag.StringVar(&snapshotsPath, "path", "snapshots", "Path to snapshots and db")
flag.IntVar(&graphicsWidth, "gw", defaultGraphicsWidth, "Width of the internal graphics buffer. Can be much larger than the screen.")
flag.IntVar(&graphicsHeight, "gh", defaultGraphicsHeight, "Height of the internal graphics buffer. Can be much larger than the screen.")
flag.IntVar(&windowWidth, "w", defaultWindowWidth, "Width of the display window")
flag.IntVar(&windowHeight, "h", defaultWindowHeight, "Height of the display window")
flag.Parse()
controlsWidth := int(float64(windowWidth) * controlsRelWidth)
viewportWidth := windowWidth - controlsWidth
rl.CloseWindow()
log.Printf("Storing snapshots at '%s'\n", snapshotsPath)
os.MkdirAll(snapshotsPath, 0755)
var err error
storage, err = NewStorage(snapshotsPath)
if err != nil {
log.Printf("Error loading storage: %v\n", err)
os.Exit(1)
}
return Layout {
monitor: rl.RectangleInt32{ X: 0, Y: 0, Width: int32(monitorWidth), Height: int32(monitorHeight) },
window: rl.RectangleInt32{ X: 0, Y: 0, Width: int32(windowWidth), Height: int32(windowHeight) },
controls: rl.RectangleInt32{ X: 0, Y: 0, Width: int32(controlsWidth), Height: int32(windowHeight) },
viewport: rl.RectangleInt32{ X: int32(controlsWidth), Y: 0, Width: int32(viewportWidth), Height: int32(windowHeight) },
graphics: rl.RectangleInt32{ X: 0, Y: 0, Width: int32(graphicsWidth), Height: int32(graphicsHeight) },
}
}
func main() {
log := log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
layout := bootstrap()
//rl.SetConfigFlags(rl.FlagMsaa4xHint)
rl.InitWindow(layout.window.Width, layout.window.Height, "sumi sierpinski arrow")
// reproducable flourescent color cycle
colorCycle := g.NewFixedColorCycle(g.FlourescentColors).Shuffle(0)
rl.SetTargetFPS(60)
t0 := time.Now()
rng := rand.New(rand.NewSource(0))
//imageField := NewImageField("/home/d/Dropbox/art/data/david.png")
noiseField := &SimplexNoiseField { Noise: opensimplex.New32(0) }
sinXYField := &SinXYField { }
//imageField := NewImageField("/home/d/Dropbox/art/data/ramstatue.png")
//imageField := NewImageField("/home/d/Dropbox/art/data/bassrockastro/Photo Dec 24 2025, 5 58 23 PM.jpg")
//imageField := NewImageField("/home/d/Dropbox/art/data/bassrockastro/andromeda.jpg")
//imageField := NewImageField("/home/d/Dropbox/art/data/moses_statue.jpg")
field :=
&TranslateField {
x: -float32(layout.graphics.Width / 2.0),
y: -float32(layout.graphics.Height / 2.0),
field: &ScaleField{
scale: 100.0,
field: &AdderField {
fields: []Field {
&ScaleField { scale: 10,field: noiseField },
sinXYField,
},
},
},
}
//sierpinskiLayer := &SierpinskiArrow { dirty: true }
sketch := NewSketch(layout.graphics.Width, layout.graphics.Height)
fieldColor := colorCycle.Next()
fmt.Printf("field color = %v\n", fieldColor)
sketch.AddColorLayer("background-magenta", rl.Magenta)
sketch.AddColorLayer("background-black", rl.Black)
//sketch.AddLayer("field", &FieldLayer{field: field, loColor: rl.NewColor(0, 0, 0, 0), hiColor: fieldColor, dirty: true})
actorColor := colorCycle.Next()
fmt.Printf("actor color = %v\n", actorColor)
hsv := rl.ColorToHSV(actorColor);
hsv.Z *= 0.7
actorColor = rl.ColorFromHSV(hsv.X, hsv.Y, hsv.Z)
actorColor = g.Clamp(actorColor, 10, 255)
actorColor.A = 10
//NewColor(11, 35, 176, 50),
//r
contourLayer := NewContourLayer(&sketch, rng, field, actorColor, -25*math.Pi, 25*math.Pi)
sketch.AddLayer("contours", contourLayer)
//sketch.AddLayer("sierpinski-arrowhead", sierpinskiLayer)
// aurora := NewImageLayer("/home/d/Dropbox/photos/Events/2025/Aurora/Photo Nov 11 2025, 9 52 03 PM.jpg")
// sketch.AddLayer("aurora", aurora)
// cave := NewImageLayer("/home/d/Dropbox/photos/Events/2025/ Chelsea and James visit Lindell/Photo Nov 29 2025, 5 26 40 PM (29).jpg")
// sketch.AddLayer("cave", cave)
ports := MakePorts()
ports["sierpinskiArrowAngle"] =
Sine {
Amp: 5,
Freq: 0.1,
Bias: 60,
}
ports["sierpinskiArrowDepth"] =
Const {
V: 6,
}
ports["sierpinskiArrowLength"] =
Const {
V: 8000,
}
for !rl.WindowShouldClose() {
// begin drawing
t := time.Since(t0).Seconds()
// set up RenderCtx
renderCtx := &RenderCtx{
TargetBounds: layout.viewport,
SourceWidth: layout.graphics.Width,
SourceHeight: layout.graphics.Height,
Time: t,
Ports: ports.Eval(t),
}
sketch.Update(renderCtx)
/**
* MAIN DRAWING
*/
rl.BeginDrawing()
rl.ClearBackground(rl.GetColor(uint(gui.GetStyle(gui.DEFAULT, gui.BACKGROUND_COLOR))))
sketch.Draw(renderCtx)
gui.SetStyle(gui.DEFAULT, gui.BACKGROUND_COLOR, 0x181818FF)
gui.SetStyle(gui.DEFAULT, gui.BASE_COLOR_NORMAL, 0x2A2A2AFF)
gui.SetStyle(gui.DEFAULT, gui.BASE_COLOR_FOCUSED, 0x3A3A3AFF)
gui.SetStyle(gui.DEFAULT, gui.BASE_COLOR_PRESSED, 0x4A4A4AFF)
gui.SetStyle(gui.DEFAULT, gui.TEXT_COLOR_NORMAL, 0xE0E0E0FF)
gui.SetStyle(gui.DEFAULT, gui.TEXT_COLOR_FOCUSED, 0xFFFFFFFF)
gui.SetStyle(gui.DEFAULT, gui.BORDER_COLOR_NORMAL, 0x404040FF)
y := float32(10)
minX := float32(20)
maxX := float32(layout.controls.X + layout.controls.Width - 20)
sliderWidth := maxX - minX - 20
controlRowHeight := 20
for _, layerTools := range sketch.layerToolsOrdered {
config := layerTools.config
gui.Label(rl.Rectangle{X: minX, Y: y, Width: 120, Height: 24}, layerTools.name)
y += float32(controlRowHeight + 10)
config.visible = gui.Toggle(rl.Rectangle{X: minX, Y: y, Width: 16, Height: 16}, "A", config.visible)
config.a = uint8(gui.Slider(rl.Rectangle{X: minX + 20, Y: y, Width: sliderWidth, Height: 16}, "", "", float32(config.a), 0, 255))
y += float32(controlRowHeight)
config.rVisible = gui.Toggle(rl.Rectangle{X: minX, Y: y, Width: 16, Height: 16}, "R", config.rVisible)
config.r = uint8(gui.Slider(rl.Rectangle{X: minX + 20, Y: y, Width: sliderWidth, Height: 16}, "", "", float32(config.r), 0, 255))
y += float32(controlRowHeight)
config.gVisible = gui.Toggle(rl.Rectangle{X: minX, Y: y, Width: 16, Height: 16}, "G", config.gVisible)
config.g = uint8(gui.Slider(rl.Rectangle{X: minX + 20, Y: y, Width: sliderWidth, Height: 16}, "", "", float32(config.g), 0, 255))
y += float32(controlRowHeight)
config.bVisible = gui.Toggle(rl.Rectangle{X: minX, Y: y, Width: 16, Height: 16}, "B", config.bVisible)
config.b = uint8(gui.Slider(rl.Rectangle{X: minX + 20, Y: y, Width: sliderWidth, Height: 16}, "", "", float32(config.b), 0, 255))
y += float32(controlRowHeight)
config.desaturate = !gui.Toggle(rl.Rectangle{X: minX, Y: y, Width: 16, Height: 16}, "S", !config.desaturate)
config.saturation = gui.Slider(rl.Rectangle{X: minX + 20, Y: y, Width: sliderWidth, Height: 16}, "", "", config.saturation, 0, 100)
y += float32(controlRowHeight)
gui.Label(rl.Rectangle{X: minX, Y: y, Width: 16, Height: 16}, "K")
config.kValue = gui.Slider(rl.Rectangle{X: minX + 20, Y: y, Width: sliderWidth, Height: 16}, "", "", config.kValue, 0, 2)
y += float32(controlRowHeight + 10)
}
rl.EndDrawing()
if rl.IsKeyDown(rl.KeySpace) {
capture := sketch.Capture()
if _, err := storage.Save(capture); err != nil {
log.Printf("Error saving snapshot: %v\n", err)
}
}
for ch := rl.GetCharPressed(); ch != 0; ch = rl.GetCharPressed() {
c := rune(ch)
if c == 'c' {
sketch.ResetCamera()
} else if c >= '1' && c <= '9' {
zoom := 1 << int(ch-'0')
sketch.cam.Zoom = float32(zoom)
}
}
//rl.EndMode2D()
// HUD
}
rl.CloseWindow()
}