package main import ( "fmt" "log" "math" "math/rand" "os" "time" "github.com/gen2brain/raylib-go/raylib" "github.com/ojrac/opensimplex-go" ) const ( screenWidth = 800 screenHeight = 600 sourceWidth = 10000 sourceHeight = 10000 displayScale = 2 snapshotsDir = "snapshots" ) func main3() { rl.InitWindow(800, 600, "rt test") defer rl.CloseWindow() rt := rl.LoadRenderTexture(512, 512) defer rl.UnloadRenderTexture(rt) for !rl.WindowShouldClose() { // draw into offscreen buffer rl.BeginTextureMode(rt) rl.ClearBackground(rl.Blank) rl.DrawCircle(256, 256, 100, rl.Red) rl.EndTextureMode() // draw to screen rl.BeginDrawing() rl.ClearBackground(rl.RayWhite) src := rl.Rectangle{ X: 0, Y: 0, Width: float32(rt.Texture.Width), Height: -float32(rt.Texture.Height), } dst := rl.Rectangle{ X: 0, Y: 0, Width: 800, Height: 600, } rl.DrawTexturePro(rt.Texture, src, dst, rl.Vector2{}, 0, rl.White) rl.EndDrawing() } } func main() { 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.SetConfigFlags(rl.FlagWindowHighdpi) rl.InitWindow(screenWidth, screenHeight, "sumi sierpinski arrow") log.Printf("screen=%dx%d render=%dx%d", rl.GetScreenWidth(), rl.GetScreenHeight(), rl.GetRenderWidth(), rl.GetRenderHeight(), ) w := rl.GetRenderWidth() h := rl.GetRenderHeight() var camera = rl.Camera2D{ Target: rl.Vector2{X: 0, Y: 0}, Offset: rl.Vector2{X: float32(w) / 2, Y: float32(h) / 2}, Rotation: 0, Zoom: 1.0, } /* field := ScaleField { Scale: 50.0, Field: &SimplexNoiseField { Noise: opensimplex.NewNormalized32(0), }, } imgf := NewImageField("/home/d/Dropbox/art/passage/data/david.png") imageField := ScaleField{ field: &imgf, scale: 0.5, } */ //rng := rand.New(rand.NewSource(0)) //contourSketch := NewContourLayer(rng, &imageField) sketch := NewSketch() sketch.CreateLayer("testPattern", &TestPattern{}, sourceWidth, sourceHeight) rl.SetTargetFPS(60) t0 := time.Now() ports := MakePorts() ports["sierpinskiArrowAngle"] = Sine{ Amp: 120, Bias: 100, Freq: 0.1, } for !rl.WindowShouldClose() { updateCamera(&camera) // begin drawing t := time.Since(t0).Seconds() // set up RenderCtx renderCtx := &RenderCtx{ TargetWidth: int32(w), TargetHeight: int32(h), SourceWidth: int32(sourceWidth), SourceHeight: int32(sourceHeight), Time: t, Ports: ports.Eval(t), Cam: camera, } /** MAIN DRAWING **/ rl.BeginDrawing() rl.ClearBackground(rl.Blank) //rl.PushMatrix() sketch.Draw(renderCtx) rl.DrawText("Mouse right button drag to move, mouse wheel to zoom", 10, 10, 20, rl.White) //rl.PopMatrix() rl.EndDrawing() //rl.DrawCircle(0, 0, 10, rl.Green) if rl.IsKeyDown(rl.KeySpace) { if _, err := storage.Save(); err != nil { log.Printf("Error saving snapshot: %v\n", err) } } //rl.EndMode2D() // HUD } rl.CloseWindow() } type FieldSketch struct { Field Field } func (s *FieldSketch) Draw(ctx *RenderCtx) { fmt.Printf("drawing field") for x := range ctx.TargetWidth { for y := range ctx.TargetHeight { screen := rl.Vector2{X: float32(x), Y: float32(y)} world := rl.GetScreenToWorld2D(screen, ctx.Cam) v := s.Field.Get(world.X, world.Y) clr := GrayCurve(v, 1.0) rl.DrawPixelV(world, clr) } } } type ContourLayer struct { field Field actors []*Actor } func NewContourLayer(rng *rand.Rand, field Field) ContourLayer { actors := make([]*Actor, 20000) for i := range len(actors) { actors[i] = &Actor{ position: RandRadialVec(rng, 0, 500, 0, 360), field: field, stepSize: 1, color: rl.NewColor(11, 35, 176, 100), } } return ContourLayer{ actors: actors, } } func (s *ContourLayer) Draw(ctx *RenderCtx) { for _, actor := range s.actors { actor.Draw() } } type Actor struct { position rl.Vector2 field Field stepSize float32 color rl.Color } func (a *Actor) Draw() { v := a.field.Get(a.position.X, a.position.Y) rad := rl.Remap(v, 0, 1, 0, 3*math.Pi) nextPosition := rl.Vector2{X: a.position.X + a.stepSize*float32(math.Cos(float64(rad))), Y: a.position.Y + a.stepSize*float32(math.Sin(float64(rad)))} rl.DrawLineV(a.position, nextPosition, a.color) //fmt.Printf("position %v -> nextPosition %v \n", a.position, nextPosition) a.position = nextPosition } func RandRadialVec(rng *rand.Rand, minRadius float32, maxRadius float32, loAngle float32, hiAngle float32) rl.Vector2 { r := float64(rl.Remap(rng.Float32(), 0, 1, minRadius, maxRadius)) deg := float64(rl.Remap(rng.Float32(), 0, 1, loAngle, hiAngle)) rad := rl.Deg2rad * deg return rl.Vector2{X: float32(r * math.Cos(rad)), Y: float32(r * math.Sin(rad))} } func updateCamera(camera *rl.Camera2D) { // Get the world point that is under the mouse mouseVec2 := rl.GetMousePosition() if rl.IsMouseButtonDown(rl.MouseRightButton) { // get mouse delta from last frame delta := rl.GetMouseDelta() // compute the amount to move scaled by the camera zoom 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 { mouseWorldPos := rl.GetScreenToWorld2D(mouseVec2, *camera) // Set the offset to where the mouse is camera.Offset = mouseVec2 // 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 Worm struct { position rl.Vector2 angles []float32 angleIndex int stepSize int renderPct float32 } func (w *Worm) Draw(ctx *RenderCtx) { rl.PushMatrix() rl.Translatef(w.position.X, w.position.Y, 0) lastAngle := float32(0.0) stepCount := 0 nudged := false for i := range w.angles { ii := (i + w.angleIndex) % len(w.angles) angle := w.angles[ii] deltaAngle := angle - lastAngle if !nudged { rad := float64(deltaAngle * math.Pi / 180.0) nudge := rl.Vector2{X: float32(w.stepSize) * float32(math.Cos(rad)), Y: float32(w.stepSize) * float32(math.Sin(rad))} w.position = rl.Vector2Add(w.position, nudge) nudged = true } rl.Rotatef(deltaAngle, 0, 0, 1) rl.DrawLine(0, 0, int32(w.stepSize), 0, rl.NewColor(184, 187, 38, 50)) rl.Translatef(float32(w.stepSize), 0, 0) lastAngle = angle stepCount++ if stepCount > int(float32(len(w.angles))*w.renderPct) { break } } rl.PopMatrix() w.angleIndex = (w.angleIndex + 1) % len(w.angles) } type SierpinskiArrow struct{} func (s *SierpinskiArrow) Draw(ctx *RenderCtx) { sierpinskiArrow(ctx, int(ctx.Ports["sierpinskiArrowDepth"]), ctx.Ports["sierpinskiArrowLength"]) } func sierpinskiArrow(ctx *RenderCtx, order int, length float64) { if order == 0 { curve(ctx, order, length, ctx.Ports["sierpinskiArrowAngle"]) } else { rl.Rotatef(float32(ctx.Ports["sierpinskiArrowAngle"]), 0, 0, 1) curve(ctx, order, length, -ctx.Ports["sierpinskiArrowAngle"]) } } 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(length), 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() } }