Skip to content

Commit

Permalink
Avoid inverting the view matrix (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
SmilyOrg authored Apr 23, 2023
2 parents e7ba516 + 98c9ba8 commit ff5168c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
18 changes: 2 additions & 16 deletions internal/render/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Render struct {
DebugThumbnails bool

Zoom int
CanvasRect Rect
CanvasImage draw.Image
}

Expand Down Expand Up @@ -99,25 +100,13 @@ func (scene *Scene) Draw(config *Render, c *canvas.Context, scales Scales, sourc
solid.Draw(c, scales)
}

// for i := range scene.Photos {
// photo := &scene.Photos[i]
// photo.Draw(config, scene, c, scales, source)
// }

concurrent := 10
photoCount := len(scene.Photos)
if photoCount < concurrent {
concurrent = photoCount
}

// startTime := time.Now()

tileRect := Rect{X: 0, Y: 0, W: (float64)(config.TileSize), H: (float64)(config.TileSize)}
tileToCanvas := c.View().Inv()
tileCanvasRect := tileRect.Transform(tileToCanvas)
tileCanvasRect.Y = -tileCanvasRect.Y - tileCanvasRect.H

visiblePhotos := scene.GetVisiblePhotos(tileCanvasRect, math.MaxInt32)
visiblePhotos := scene.GetVisiblePhotos(config.CanvasRect, math.MaxInt32)
visiblePhotoCount := 0

wg := &sync.WaitGroup{}
Expand All @@ -131,9 +120,6 @@ func (scene *Scene) Draw(config *Render, c *canvas.Context, scales Scales, sourc
visiblePhotoCount += <-counts
}

// micros := time.Since(startTime).Microseconds()
// log.Printf("scene draw %5d / %5d photos, %6d μs all, %.2f μs / photo\n", visiblePhotoCount, photoCount, micros, float64(micros)/float64(visiblePhotoCount))

for i := range scene.Texts {
text := &scene.Texts[i]
text.Draw(config, c, scales)
Expand Down
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,25 @@ func drawTile(c *canvas.Context, r *render.Render, scene *render.Scene, zoom int
if 1 < scene.Bounds.W/scene.Bounds.H {
scale = tileSize / scene.Bounds.W
tx += (scale*scene.Bounds.W - tileSize) * 0.5

// Explicit to avoid floating point precision issues
r.CanvasRect = render.Rect{
X: float64(x) * scene.Bounds.W / float64(zoomPower),
Y: float64(y) * scene.Bounds.W / float64(zoomPower),
W: scene.Bounds.W / float64(zoomPower),
H: scene.Bounds.W / float64(zoomPower),
}
} else {
scale = tileSize / scene.Bounds.H
ty += (scale*scene.Bounds.H - tileSize) * 0.5

// Explicit to avoid floating point precision issues
r.CanvasRect = render.Rect{
X: float64(x) * scene.Bounds.H / float64(zoomPower),
Y: float64(y) * scene.Bounds.H / float64(zoomPower),
W: scene.Bounds.H / float64(zoomPower),
H: scene.Bounds.H / float64(zoomPower),
}
}

scale *= float64(zoomPower)
Expand Down

0 comments on commit ff5168c

Please sign in to comment.