diff --git a/main.go b/main.go index 77a84e0..698d88d 100644 --- a/main.go +++ b/main.go @@ -13,50 +13,17 @@ import ( ) const ( - screenWidth = 800 - screenHeight = 600 - sourceWidth = 10000 - sourceHeight = 10000 - displayScale = 2 + targetWidth = 800 + targetHeight = 600 + sourceScale = 1 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() { + sourceWidth := sourceScale * targetWidth + sourceHeight := sourceScale * targetHeight + os.MkdirAll(snapshotsDir, 0755) log := log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile) storage, err := NewStorage(snapshotsDir) @@ -64,22 +31,22 @@ func main() { log.Printf("Error loading storage: %v\n", err) os.Exit(1) } - rl.SetConfigFlags(rl.FlagWindowHighdpi) - rl.InitWindow(screenWidth, screenHeight, "sumi sierpinski arrow") + + rl.SetConfigFlags(rl.FlagWindowHighdpi & rl.FlagMsaa4xHint) + rl.InitWindow(targetWidth, targetHeight, "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, + // point at source center + // put source center at center of screen + var camera = rl.Camera2D { + Target: rl.Vector2{X: float32(targetWidth) / 2, Y: float32(targetHeight) / 2}, + Offset: rl.Vector2{}, Zoom: 1.0, + Rotation: 0, } /* @@ -90,9 +57,7 @@ func main() { Noise: opensimplex.NewNormalized32(0), }, } - imgf := NewImageField("/home/d/Dropbox/art/passage/data/david.png") - imageField := ScaleField{ field: &imgf, @@ -105,7 +70,7 @@ func main() { //contourSketch := NewContourLayer(rng, &imageField) sketch := NewSketch() - sketch.CreateLayer("testPattern", &TestPattern{}, sourceWidth, sourceHeight) + sketch.CreateLayer("testPattern", &TestPattern{}, int32(sourceWidth), int32(sourceHeight)) rl.SetTargetFPS(60) @@ -127,8 +92,8 @@ func main() { // set up RenderCtx renderCtx := &RenderCtx{ - TargetWidth: int32(w), - TargetHeight: int32(h), + TargetWidth: int32(targetWidth), + TargetHeight: int32(targetHeight), SourceWidth: int32(sourceWidth), SourceHeight: int32(sourceHeight), Time: t, @@ -142,14 +107,11 @@ func main() { 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 { @@ -243,6 +205,7 @@ func updateCamera(camera *rl.Camera2D) { delta := rl.GetMouseDelta() // compute the amount to move scaled by the camera zoom delta = rl.Vector2Scale(delta, -1.0/camera.Zoom) + delta.Y = -delta.Y camera.Target = rl.Vector2Add(camera.Target, delta) } // Zoom based on mouse wheel diff --git a/sketch.go b/sketch.go index 4423b67..906361f 100644 --- a/sketch.go +++ b/sketch.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "github.com/gen2brain/raylib-go/raylib" ) @@ -23,27 +24,55 @@ func (s *Sketch) CreateLayer(name string, layer Layer, sourceWidth int32, source } } +func LayerViewRect(ctx *RenderCtx) rl.Rectangle { + + z := ctx.Cam.Zoom + + // get the viewport width in texture space + viewportWidth := float32(ctx.TargetWidth) / z + viewportHeight := float32(ctx.TargetHeight) / z + + x := rl.Clamp(ctx.Cam.Target.X - viewportWidth/2, 0, float32(ctx.SourceWidth) - viewportWidth/2.0) + y := rl.Clamp(ctx.Cam.Target.Y - viewportHeight/2, 0, float32(ctx.SourceHeight) - viewportHeight/2.0) + + return rl.Rectangle{ + X: x, + Y: y, + Width: viewportWidth, + Height: viewportHeight, + } +} + func (s *Sketch) Draw(ctx *RenderCtx) { // render onto all layer textures for _, instance := range s.layerTools { layer := instance.layer rl.BeginTextureMode(*instance.texture) + rl.ClearBackground(rl.Blank) layer.Draw(ctx) rl.EndTextureMode() } // composite all layers to screen + //ClampCameraToLayer(ctx) + + //view := LayerViewRect(ctx) src := rl.Rectangle { - X: 0, Y: 0, + X: 0, + Y: 0, Width: float32(ctx.SourceWidth), Height: -float32(ctx.SourceHeight), } + dst := rl.Rectangle { - X: 0, Y: 0, + X: 0, + Y: 0, Width: float32(ctx.TargetWidth), Height: float32(ctx.TargetHeight), } + fmt.Printf("src = %v, dst %v\n", src, dst) + for _, instance := range s.layerTools { rl.DrawTexturePro(instance.texture.Texture, src, dst, rl.Vector2{}, 0, rl.White) } @@ -56,7 +85,6 @@ type LayerTools struct { texture *rl.RenderTexture2D } - /** Layer **/ type Layer interface { @@ -65,11 +93,36 @@ type Layer interface { type TestPattern struct { } +func DrawGrid(spacing int32, halfExtent int32) { + col := rl.Color{R: 220, G: 220, B: 220, A: 255} + + for x := -halfExtent; x <= halfExtent; x += spacing { + rl.DrawLine(x, -halfExtent, x, halfExtent, col) + } + for y := -halfExtent; y <= halfExtent; y += spacing { + rl.DrawLine(-halfExtent, y, halfExtent, y, col) + } +} + func (tp *TestPattern) Draw(ctx *RenderCtx) { - rl.DrawRectangle(0, 0, int32(ctx.SourceWidth), int32(ctx.SourceHeight), rl.Magenta) + + rl.ClearBackground(rl.Black) + centerX := float32(ctx.SourceWidth)/2 + centerY := float32(ctx.SourceHeight)/2 + + rl.DrawRectangleRec(rl.Rectangle { X: 0, Y: 0, Width: centerX, Height: centerY }, rl.Red) + rl.DrawRectangleRec(rl.Rectangle { X: centerX, Y: 0, Width: centerX, Height: centerY }, rl.Green) + rl.DrawRectangleRec(rl.Rectangle { X: 0, Y: centerY, Width: centerX, Height: centerY }, rl.Blue) + rl.DrawRectangleRec(rl.Rectangle { X: centerX, Y: centerY, Width: centerX, Height: centerY }, rl.White) + + rl.DrawLine(0, 0, ctx.SourceWidth, ctx.SourceHeight, rl.Black) + rl.PushMatrix() - rl.Translatef(float32(ctx.SourceWidth)/2.0, float32(ctx.SourceHeight)/2.0, 0.0) - rl.DrawRectangle(-100, -100, 200, 200, rl.Green) + rl.Translatef(centerX, centerY, 0) + rl.SetLineWidth(4.0) + rl.DrawLine(-10000, 0, 10000, 0, rl.Red) + rl.DrawLine(0, -10000, 0, 10000, rl.Green) + rl.DrawRectangleLines(-50, -50, 100, 100, rl.Magenta) rl.PopMatrix() }