/wang-tiles

Wang Tiles Experiements in Golang

Primary LanguageGo

Wang Tiles

This Project is heavily (almost too much) inspired by Tsoding Daily's Project On Wang Tiles. It might be considered a port of the project to Golang.

Introduction

Wang tiles is a mathematical concept for procedural generation that was proposed by Hao Wang in 1961. Each tile has 4 sides. Each side can have one of 2 colors. Thus, each tile can be represented as a 4 bit number.

These tiles are stacked together with some specific rules.

  • all 4 edges of a tile must match with it's neighbour
  • All cells of the grid are filled, there is no overloap or empty cell.

This leads to very interesting patterns and visualisations.

Coming Up!

Grid Grid Grid

Usage

go run main.go {output-filename}

output-filename = must be a jpeg, default="./temp/atlas.jpeg"

Goals

  • To learn about procedural generation using wang-tiles.
  • To learn about GoLang's idiomatic way of doing things and exploring multiple solutions to the same problem.
  • To learn more about go-routines and generate graphics using nothing but the CPU.

Phases

This project is done in phases (again, very similar to Tsoding Daily's project) and each phase might add or remove stuff from the codebase. Please go through every phase to understand the journey.

Phase 1 - Atlas Rendering

A single struct named Tile is responsible for representing a single tile in the project. It embeds the image.Image type from golang. It's colors are dicided with a single uint8 called bltr. The method wangFragmentShader is analogus to fragment shaders in graphics libraries. It takes in normalized co-ordintaes and outputs a color for that particular coordinte. BLTR stands for Bottom, Left, Top, Right. It represents the order in which the bits represent the colors of a tile.

Doing This in a loop for each possible bltr results in an atlas of all posible wang tiles (kind of like a sprite sheet.).

Phase 2 - Grid Rendering

  • A grid is generated in the form of 2D slice of uint8 or [][]uint8 called bltrs. These are semi-randomly generated by following the basic rules.
  • Next, these are translated into a grid image using an atlas or tileSet which is a mapping of bltr to image.Image
  • Calculating Offsets from Rows and Cols, the following demo images were created (note: I have implemented 3 fragment shaders, there can be many more!!!)

Maze Like Demo

Atlas

Atlas

Generated Grid

Grid

Bokeh or Bubble like Demo

Atlas

Atlas

Generated Grid

Grid

Triangles Demo

Atlas

Atlas

Generated Grid

Grid

Things I learnt

  • I learnt about image.Image type and how to use it to manipulate images in golang. (It can be thought of as a wrapper around a 2D array which makes it easy to deal with images).
  • I learnt about vardiac functions which let you take a variable number of arguments e.g. the definition of fmt.Printf is
    func Printf(format string, a ...interface{}) (n int, err error)
  • I also learnt about go-routines and how they can help in parrellely processing individual tiles in of the wang-tiles set.
  • I implemented a basic linear algebra library to help in calculations
  • I learnt about the image.Image and draw.Draw interfaces and how the facilitate interacting and manipulating images