https://github.com/tsoding/wang-tiles
Wang Tiles Grid generator
https://github.com/tsoding/wang-tiles
computer-graphics gradients procedural-generation shaders wang-tiles
Last synced: about 1 month ago
JSON representation
Wang Tiles Grid generator
- Host: GitHub
- URL: https://github.com/tsoding/wang-tiles
- Owner: tsoding
- License: mit
- Created: 2021-07-23T14:13:59.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-08-28T11:44:44.000Z (almost 4 years ago)
- Last Synced: 2025-04-29T12:26:00.430Z (about 1 month ago)
- Topics: computer-graphics, gradients, procedural-generation, shaders, wang-tiles
- Language: C
- Homepage:
- Size: 2.14 MB
- Stars: 52
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Wang Tiles Grid generator

## Quick Start
The project is using [nobuild](https://github.com/tsoding/nobuild#nobuild) build system.
```console
$ cc -o nobuild nobuild.c
$ ./nobuild
$ ./wang -live
```## Goal
Trying to generate a random grid of 2-colored [Wang Tiles](http://www.cr31.co.uk/stagecast/wang/intro.html) based on a single function definition:
```c
typedef struct { float c[2]; } Vec2f;
typedef struct { float c[3]; } Vec3f;
typedef Vec2f UV;
typedef Vec3f RGB;
typedef uint32_t BLTR;RGB wang(BLTR bltr, UV uv);
```where
- `bltr` is a 4 bit mask encoding the specific Wang Tile. Each bit represents a side of the tile in the following order: Bottom, Left, Top, Right. `0` is the first color, `1` is the second one.
- `uv` is a 2D vector that represents a point on the tile. Top-Left corner is `[0.0, 0.0]`, Bottom-Right corner is `[1.0, 1.0]`. Coordinates are normalized and resolution-independant.
- The result of the function is a 3D vector that represents an RGB color of a pixel on the tile `bltr` at `uv`.Think of it as a [Fragment Shader](https://www.khronos.org/opengl/wiki/Fragment_Shader) that generates a Texture for a Wang Tile based on its bitmask description.
## Phases
The whole rendering is split into roughly 3 phases and done entirely on CPU.
### Phase 1. Atlas Rendering
Given the "Shader" function described in the [Goal](#goal) section we generate an atlas (spritesheet) of all 16 Wang Tiles. (4 sides, 2 colors, 2⁴ = 16)
 
### Phase 2. Grid Generation
Given the size of the grid in tiles we generate 2D array of `BLTR`-s (defined in the [Goal](#goal) section) satisfying the Wang Tile constraints between the tiles (the adjacent side of the tiles must have the same color).
### Phase 3. Grid Rendering
After the [Atlas](#phase-1-atlas-rendering) and the [Grid](#phase-2-grid-generation) are generated we iterate over each `BLTR` in the Grid and copy the corresponding tiles from the Atlas into the final Grid image.

## Screencasts
The development was streamed on [Twitch](https://twitch.tv/tsoding). Here is an archive of screencasts: https://www.youtube.com/playlist?list=PLpM-Dvs8t0VYgJXZyQzWjfYUm3MxcvqR0