Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Graphics in Go

Graphics in Go

Presentation containing a (small) selection of my graphics experiments.

https://github.com/peterhellberg/gfx

Peter Hellberg

March 05, 2019
Tweet

More Decks by Peter Hellberg

Other Decks in Programming

Transcript

  1. Graphics in Go

    View Slide

  2. @peterhellberg

    View Slide

  3. Interfaces!

    View Slide

  4. image.Image
    // Image is a finite rectangular
    // grid of color.Color values taken from a color model.
    type Image interface {
    // ColorModel returns the Image's color model.
    ColorModel() color.Model
    // Bounds returns the domain for which At can return non-zero color.
    // The bounds do not necessarily contain the point (0, 0).
    Bounds() Rectangle
    // At returns the color of the pixel at (x, y).
    At(x, y int) color.Color
    }

    View Slide

  5. // Model can convert any Color to one from its own color model.
    // The conversion may be lossy.
    type Model interface {
    Convert(c Color) Color
    }
    color.Model

    View Slide

  6. color.Color
    // Color can convert itself to alpha-premultiplied 16-bits per channel RGBA.
    // The conversion may be lossy.
    type Color interface {
    // RGBA returns the alpha-premultiplied red, green, blue and alpha values
    // for the color. Each value ranges within [0, 0xffff], but is represented
    // by a uint32 so that multiplying by a blend factor up to 0xffff will not
    // overflow.
    //
    // An alpha-premultiplied color component c has been scaled by alpha (a),
    // so has valid values 0 <= c <= a.
    RGBA() (r, g, b, a uint32)
    }

    View Slide

  7. draw.Image
    // Image is an image.Image with a Set method to
    change a single pixel.
    type Image interface {
    image.Image
    Set(x, y int, c color.Color)
    }

    View Slide

  8. WHAT ABOUT
    THE ELEPHANT IN 

    THE ROOM?(Shaders)

    View Slide

  9. Packages?

    View Slide

  10. go get github.com/peterhellberg/gfx
    gfx

    View Slide

  11. go get github.com/hajimehoshi/ebiten
    ebiten

    View Slide

  12. go get github.com/faiface/pixel
    pixel

    View Slide

  13. #gamedev
    #gamedev
    slack.gopher.se

    View Slide

  14. View Slide

  15. Experiments

    View Slide

  16. The XOR texture

    lodev.org/cgtutor/xortexture.html

    View Slide

  17. The XOR texture
    package main
    import "github.com/peterhellberg/gfx"
    func main() {
    m := gfx.NewImage(640, 360)
    gfx.EachPixel(m.Bounds(), func(x, y int) {
    c := uint8(x ^ y)
    m.Set(x, y, gfx.ColorNRGBA(c, c%192, c, 255))
    })
    gfx.SavePNG("xor.png", gfx.NewScaledImage(m, 4))
    }

    View Slide

  18. View Slide

  19. Plasma
    lodev.org/cgtutor/plasma.html

    View Slide

  20. The Plasma effect
    package main
    import (
    "github.com/peterhellberg/gfx"
    "github.com/peterhellberg/plasma"
    "github.com/peterhellberg/plasma/palette"
    )
    func main() {
    w, h, scale, seed := 2560, 1440, 64.0, 1234
    gfx.SavePNG("plasma.png", plasma.New(w, h, scale).
    Image(w, h, seed, palette.MaterialDesign700),
    )
    }

    View Slide

  21. View Slide

  22. Tunnel
    lodev.org/cgtutor/tunnel.html

    View Slide

  23. View Slide

  24. View Slide

  25. Starfield
    vendian.org/mncharity/dir3/starcolor

    View Slide

  26. View Slide

  27. View Slide

  28. View Slide

  29. View Slide

  30. DOOM fire
    fabiensanglard.net/doom_fire_psx

    View Slide

  31. View Slide

  32. Particles

    View Slide

  33. View Slide

  34. Fractals

    View Slide

  35. The Julia set

    View Slide

  36. View Slide

  37. Popcorn fractals
    paulbourke.net/fractals/popcorn

    View Slide

  38. View Slide

  39. Procedural Tree

    View Slide

  40. View Slide

  41. View Slide

  42. View Slide

  43. View Slide

  44. View Slide

  45. Domain warping
    iquilezles.org/www/articles/warp/warp.htm

    View Slide

  46. View Slide

  47. View Slide

  48. View Slide

  49. Tiles

    View Slide

  50. package main
    import "github.com/peterhellberg/gfx"
    func main() {
    gfx.SavePNG("fireflower.png", gfx.NewScaledImage(
    gfx.NewTile(palette, 8, []uint8{
    0, 1, 1, 1, 1, 1, 1, 0,
    1, 1, 2, 2, 2, 2, 1, 1,
    1, 2, 3, 3, 3, 3, 2, 1,
    1, 1, 2, 2, 2, 2, 1, 1,
    0, 1, 1, 1, 1, 1, 1, 0,
    0, 0, 0, 4, 4, 0, 0, 0,
    0, 0, 0, 4, 4, 0, 0, 0,
    4, 4, 0, 4, 4, 0, 4, 4,
    0, 4, 0, 4, 4, 0, 4, 0,
    0, 4, 4, 4, 4, 4, 4, 0,
    0, 0, 4, 4, 4, 4, 0, 0,
    }), 128))
    }
    var palette = gfx.Palette{{},
    {0xA4, 0xE8, 0xFE, 0xFF},
    {0xFD, 0xB7, 0x2C, 0xFF},
    {0xFC, 0x59, 0x29, 0xFF},
    {0x31, 0xAB, 0x4B, 0xFF},
    }

    View Slide

  51. package main
    import "github.com/peterhellberg/gfx"
    func main() {
    gfx.SavePNG("layer.png", gfx.NewScaledImage(
    gfx.NewLayer(tileset, 5, gfx.LayerData{
    4, 7, 7, 7, 4,
    5, 2, 0, 2, 6,
    5, 0, 3, 0, 6,
    5, 2, 0, 2, 6,
    4, 8, 8, 8, 4,
    }), 128),
    )
    }
    var tileset = gfx.NewTileset(
    gfx.PaletteEN4, gfx.Pt(2, 2),
    gfx.TilesetData{
    {0, 0, 0, 0}, {1, 1, 1, 1}, {2, 2, 2, 2},
    {3, 3, 3, 3}, {9, 9, 9, 9}, {1, 2, 1, 2},
    {2, 1, 2, 1}, {1, 1, 2, 2}, {2, 2, 1, 1},
    })

    View Slide

  52. Triangles

    View Slide

  53. package main
    import "github.com/peterhellberg/gfx"
    var p = gfx.PaletteFamicube
    func vx(x, y float64, n int) gfx.Vertex {
    return gfx.Vertex{Position: gfx.V(x, y), Color: p.Color(n)}
    }
    func main() {
    m := gfx.NewPaletted(640, 1080, p, p.Color(57))
    gfx.NewDrawTarget(m).MakeTriangles(&gfx.TrianglesData{
    vx(128, 12, 51), vx(12, 244, 52), vx(604, 244, 53),
    vx(12, 300, 54), vx(300, 300, 55), vx(240, 972, 56),
    }).Draw()
    gfx.SavePNG("triangles.png", m)
    }
    Triangles

    View Slide

  54. Isometric blocks

    View Slide

  55. package main
    import "github.com/peterhellberg/gfx"
    func main() {
    m := gfx.NewImage(1080, 1080)
    v := gfx.V(1.2, -1.94)
    o := gfx.BoundsCenterOrigin(m.Bounds(), v, 320)
    b := gfx.Blocks{
    {gfx.V3(0, 0, 0), gfx.V3(1, 1, 1), gfx.BlockColorRed},
    {gfx.V3(0, 1, 0), gfx.V3(1, 1, 1), gfx.BlockColorGreen},
    {gfx.V3(1, 1, 0), gfx.V3(1, 1, 1), gfx.BlockColorBlue},
    }
    b.Sort()
    b.DrawPolygons(m, o)
    gfx.SavePNG("three-blocks.png", m)
    }
    Three blocks

    View Slide

  56. View Slide

  57. View Slide

  58. View Slide

  59. View Slide

  60. View Slide

  61. View Slide

  62. View Slide

  63. View Slide

  64. View Slide

  65. SDF
    iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm

    View Slide

  66. package main
    import "github.com/peterhellberg/gfx"
    func main() {
    c := gfx.PaletteEDG36.Color
    m := gfx.NewImage(2560, 1440, c(5))
    gfx.EachImageVec(m, gfx.ZV, func(u gfx.Vec) {
    sd := gfx.SignedDistance{u}
    if d := sd.OpRepeat(gfx.V(256, 256), func(sd gfx.SignedDistance) float64 {
    return sd.OpSubtraction(sd.Circle(50), sd.Line(gfx.V(0, 0), gfx.V(64, 64)))
    }); d < 40 {
    gfx.SetVec(m, u, c(int(gfx.MathAbs(d/5))))
    }
    })
    gfx.SavePNG("sdf-repeat.png", m)
    }
    Repeat: Subtract Circle from Line

    View Slide

  67. View Slide

  68. SmoothUnion of Rectangle
    and IsoscelesTriangle
    EquilateralTriangle

    View Slide

  69. Domain coloring

    View Slide

  70. package main
    import "github.com/peterhellberg/gfx"
    const (
    w, h, fovY = 2560, 1440, 1.9
    aspect = float64(w) / float64(h)
    ahc = aspect * fovY / 2.0
    hfc = fovY / 2.0
    )
    func pixelCoordinates(px, py int) gfx.Vec {
    return gfx.V(
    ((float64(px)/(w-1))*2-1)*ahc,
    ((float64(h-py-1)/(h-1))*2-1)*hfc,
    )
    }
    Domain coloring
    func main() {
    var (
    p0 = pixelCoordinates(0, 0)
    p1 = pixelCoordinates(w-1, h-1)
    y = p0.Y
    d = gfx.V((p1.X-p0.X)/(w-1), (p1.Y-p0.Y)/(h-1))
    m = gfx.NewImage(w, h)
    )
    for py := 0; py < h; py++ {
    x := p0.X
    for px := 0; px < w; px++ {
    z := gfx.CmplxSin(1 / complex(x, y))
    c := gfx.PaletteEN4.CmplxPhaseAt(z)
    m.Set(px, py, c)
    x += d.X
    }
    y += d.Y
    }
    gfx.SavePNG("domain-coloring.png", m)
    }

    View Slide

  71. View Slide

  72. View Slide

  73. View Slide

  74. View Slide

  75. View Slide

  76. View Slide

  77. View Slide

  78. View Slide

  79. View Slide

  80. View Slide

  81. View Slide

  82. View Slide

  83. Ray Casting

    View Slide

  84. View Slide

  85. View Slide

  86. View Slide

  87. Ray Marching

    View Slide

  88. KABOOM!
    github.com/ssloy/tinykaboom

    View Slide

  89. Draw a sphere

    View Slide

  90. Diffuse shading

    View Slide

  91. Draw a pattern

    View Slide

  92. Displacement mapping

    View Slide

  93. Another implicit surface

    View Slide

  94. Pseudorandom Noise

    View Slide

  95. Fire colors

    View Slide

  96. View Slide

  97. gist.github.com/peterhellberg

    View Slide