208 lines
4.2 KiB
Go
208 lines
4.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"math"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/gen2brain/raylib-go/raylib"
|
|
"github.com/ojrac/opensimplex-go"
|
|
)
|
|
|
|
func clamp01(v float64) float64 {
|
|
if v < 0 {
|
|
return 0
|
|
}
|
|
if v > 1 {
|
|
return 1
|
|
}
|
|
return v
|
|
}
|
|
|
|
func GrayCurve(v, k float64) rl.Color {
|
|
v = math.Pow(clamp01(v), k) // k < 1 boosts highlights, k > 1 boosts shadows
|
|
c := uint8(v * 255)
|
|
return rl.Color{R: c, G: c, B: c, A: 255}
|
|
}
|
|
|
|
func main() {
|
|
|
|
const (
|
|
screenWidth = 1200
|
|
screenHeight = 700
|
|
snapshotsDir = "snapshots"
|
|
)
|
|
|
|
os.MkdirAll(snapshotsDir, 0755)
|
|
|
|
log := log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
|
|
|
|
storage, err := NewStorage(snapshotsDir)
|
|
if err != nil {
|
|
log.Printf("Error loading storage: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
rl.InitWindow(screenWidth, screenHeight, "sumi sierpinski arrow")
|
|
|
|
sketches := []Sketch {
|
|
SierpinskiArrow{},
|
|
}
|
|
|
|
var camera = rl.Camera2D{
|
|
Target: rl.Vector2{X: 0, Y: 0},
|
|
Offset: rl.Vector2{X: float32(screenWidth) / 2, Y: float32(screenHeight) / 2},
|
|
Rotation: 0,
|
|
Zoom: 1.0,
|
|
}
|
|
|
|
rl.SetTargetFPS(60)
|
|
|
|
t0 := time.Now()
|
|
|
|
ports := MakePorts()
|
|
ports["sierpinskiArrowLength"] = Sine {
|
|
Amp: 1000.0,
|
|
Freq: 0.001,
|
|
}
|
|
|
|
ports["sierpinskiArrowDepth"] = Saw {
|
|
Min: 1, Max: 9, Period: 10,
|
|
}
|
|
|
|
for !rl.WindowShouldClose() {
|
|
updateCamera(&camera)
|
|
|
|
// begin drawing
|
|
rl.BeginDrawing()
|
|
rl.ClearBackground(rl.RayWhite)
|
|
rl.BeginMode2D(camera)
|
|
rl.PushMatrix()
|
|
|
|
t := time.Since(t0).Seconds()
|
|
|
|
// set up RenderCtx
|
|
renderCtx := &RenderCtx {
|
|
Width: screenWidth,
|
|
Height: screenHeight,
|
|
Time: t,
|
|
Ports: ports.Eval(t),
|
|
}
|
|
|
|
/**
|
|
MAIN DRAWING
|
|
**/
|
|
for _, s := range sketches {
|
|
s.Draw(renderCtx)
|
|
}
|
|
|
|
if rl.IsKeyDown(rl.KeySpace) {
|
|
if _, err := storage.Save(); err != nil {
|
|
log.Printf("Error saving snapshot: %v\n", err)
|
|
}
|
|
}
|
|
|
|
rl.PopMatrix()
|
|
rl.EndMode2D()
|
|
|
|
// HUD
|
|
rl.DrawText("Mouse right button drag to move, mouse wheel to zoom", 10, 10, 20, rl.White)
|
|
rl.EndDrawing()
|
|
}
|
|
|
|
rl.CloseWindow()
|
|
}
|
|
|
|
func updateCamera(camera *rl.Camera2D) {
|
|
if rl.IsMouseButtonDown(rl.MouseRightButton) {
|
|
delta := rl.GetMouseDelta()
|
|
delta = rl.Vector2Scale(delta, -1.0/camera.Zoom)
|
|
camera.Target = rl.Vector2Add(camera.Target, delta)
|
|
}
|
|
// Zoom based on mouse wheel
|
|
wheel := rl.GetMouseWheelMove()
|
|
if wheel != 0 {
|
|
// Get the world point that is under the mouse
|
|
mouseWorldPos := rl.GetScreenToWorld2D(rl.GetMousePosition(), *camera)
|
|
|
|
// Set the offset to where the mouse is
|
|
camera.Offset = rl.GetMousePosition()
|
|
|
|
// Set the target to match, so that the camera maps the world space point
|
|
// under the cursor to the screen space point under the cursor at any zoom
|
|
camera.Target = mouseWorldPos
|
|
|
|
// Zoom increment
|
|
const zoomIncrement float32 = 0.125
|
|
|
|
camera.Zoom += (wheel * zoomIncrement)
|
|
if camera.Zoom < zoomIncrement {
|
|
camera.Zoom = zoomIncrement
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type SierpinskiArrow struct {}
|
|
|
|
func (s SierpinskiArrow) Draw(ctx *RenderCtx) {
|
|
rl.PushMatrix()
|
|
sierpinskiArrow(ctx, int(ctx.Ports["sierpinskiArrowDepth"]), ctx.Ports["sierpinskiArrowLength"])
|
|
rl.PopMatrix()
|
|
}
|
|
|
|
func sierpinskiArrow(ctx *RenderCtx, order int, length float64) {
|
|
if order == 0 {
|
|
curve(ctx, order, length, 60)
|
|
} else {
|
|
rl.Rotatef(60, 0, 0, 1)
|
|
curve(ctx, order, length, -60)
|
|
}
|
|
}
|
|
|
|
func curve(ctx *RenderCtx, order int, length float64, angle float64) {
|
|
if order == 0 {
|
|
len := int32(length)
|
|
rl.DrawLine(0, 0, len, 0, rl.Black)
|
|
rl.Translatef(float32(len), 0, 0)
|
|
} else {
|
|
curve(ctx, order-1, length/2, -angle)
|
|
rl.Rotatef(float32(angle), 0, 0, 1)
|
|
curve(ctx, order-1, length/2, angle)
|
|
rl.Rotatef(float32(angle), 0, 0, 1)
|
|
curve(ctx, order-1, length/2, -angle)
|
|
}
|
|
}
|
|
|
|
func main2() {
|
|
angles := make([]float32, 1000)
|
|
noise := opensimplex.NewNormalized(0)
|
|
for i := range len(angles) {
|
|
angles[i] = float32(noise.Eval2(float64(i)*0.05, 0.00))*0.1 - 0.05
|
|
}
|
|
|
|
frameNum := 0
|
|
|
|
for !rl.WindowShouldClose() {
|
|
|
|
frameNum++
|
|
|
|
// initial transform by halfway again through angle array
|
|
|
|
angleIndex := (frameNum/10) % len(angles)
|
|
angle := angles[angleIndex]
|
|
|
|
initAngle := angles[(angleIndex+len(angles)/2)%len(angles)]
|
|
rl.Rotatef(2500*initAngle, 0, 0, 1)
|
|
rl.Translatef(100*initAngle, 100*initAngle, 0)
|
|
|
|
fmt.Printf("%.3f", angle)
|
|
|
|
rl.EndMode2D()
|
|
}
|
|
|
|
}
|