Skip to content

Commit

Permalink
Add Context.DepthImage
Browse files Browse the repository at this point in the history
  • Loading branch information
fogleman committed Aug 18, 2020
1 parent 34562c3 commit 27cddc1
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fauxgl

import (
"image"
"image/color"
"math"
"runtime"
"sync"
Expand Down Expand Up @@ -83,6 +84,38 @@ func (dc *Context) Image() image.Image {
return dc.ColorBuffer
}

func (dc *Context) DepthImage() image.Image {
lo := math.MaxFloat64
hi := -math.MaxFloat64
for _, d := range dc.DepthBuffer {
if d == math.MaxFloat64 {
continue
}
if d < lo {
lo = d
}
if d > hi {
hi = d
}
}

im := image.NewGray16(image.Rect(0, 0, dc.Width, dc.Height))
var i int
for y := 0; y < dc.Height; y++ {
for x := 0; x < dc.Width; x++ {
d := dc.DepthBuffer[i]
t := (d - lo) / (hi - lo)
if d == math.MaxFloat64 {
t = 1
}
c := color.Gray16{uint16(t * 0xffff)}
im.SetGray16(x, y, c)
i++
}
}
return im
}

func (dc *Context) ClearColorBufferWith(color Color) {
c := color.NRGBA()
for y := 0; y < dc.Height; y++ {
Expand Down

0 comments on commit 27cddc1

Please sign in to comment.