CFDG: Context-Free Grammar for Procedural Image Generation
Context Free uses the CFDG (Context Free Design Grammar) language to describe procedural image generation. This domain-specific language features a context-free grammar, with basic primitives being CIRCLE and SQUARE. Rendering is powered by the Anti-Grain Geometry library. Rules define recursive transformations of shapes with parameters like rotation (r), scaling (s), translation (x, y), color (hue, sat, b), transparency (a), and more.
The language enables generating intricate fractal structures from simple descriptions. Each example below showcases real CFDG scripts that produce stunning visual compositions.
Basic Example: Fractal Pattern
This first script creates a symmetrical pattern on a black background with repeating circles and gradients.
startshape T
// FLIGIZ
background{b -1}
tile {s 2.5}
rule T {3*{r 120 hue 30}S{x .3}}
rule S 3{CIRCLE{hue 30}4*{r 20 b.007 sat .1}S[s.3.7y.9]}
rule S {CIRCLE{hue 15}9*{r 20 b.05 hue -3}S[s.3.7y.9]}
rule S {S{flip 90}}
Here, rule T kicks off three branches rotated by 120°, each transforming S. The S rules add circles with brightness (b) and saturation (sat) gradients, then recursively scale down (s 0.37) and shift upward (y 0.9).
Rainbow Effect with Shadows
This script generates a shimmering orb with shadows and highlights using nested rules.
startshape START
rule START {
CIRCLE{}
RAINBOW_SPLIT{hue 0 sat 0 b 0}
RAINBOW_SPLIT{r 180 hue 120 sat 0}
}
rule RAINBOW_SPLIT{
RAINBOW{}
}
rule RAINBOW_SPLIT{
RAINBOW_MINI{alpha -.9 y -.4 s .2 flip 1 b -1}
RAINBOW{}
}
rule RAINBOW{
SHADOW{}
CIRCLE{}
HIGHLIGHT{
RAINBOW_SPLIT{hue 1.5 sat .1 s .99 b .001 x .09 r 2.8}
}
}
rule RAINBOW_MINI{
SHADOW{}
CIRCLE{}
HIGHLIGHT{}
RAINBOW_MINI{alpha .1 hue 2 s .98 x .3 r 2.8}
}
rule RAINBOW_MINI .04{
SHADOW{}
CIRCLE{}
HIGHLIGHT{}
RAINBOW_MINI{flip 1 alpha .1 hue 5 s .99 x .3 r 2.8}
}
rule SHADOW {
CIRCLE{hue 3 b -1 sat -.6 s 1.1 y .04 alpha -.7}
}
rule HIGHLIGHT {
CIRCLE{ hue 3 b .7 sat -.3 s .98 y .05}
CIRCLE{ hue 3 b 2 sat -.7 s .9 y .01}
}
Key transformations: RAINBOW_SPLIT creates a rainbow gradient, while SHADOW and HIGHLIGHT simulate lighting. Recursion in RAINBOW_MINI with a 0.04 probability halts branching.
Simulating Ancient Texts
This script mimics handwritten text using strokes and symbols.
background { hue 40 sat 0.2 b -0.2}
startshape LINES
rule LINES {
20 * {y -90} NEWLINE {hue 90 sat 0.7 b 0.2 a -0.5}
}
rule NEWLINE {
LINE {y 0}
}
rule NEWLINE {
LINE {y 20}
}
rule NEWLINE {
LINE {y 30}
}
rule LINE {
50 * {x 36} CHAR {}
}
rule CHAR 0.3 { }
rule CHAR {
2 * {x 20 flip 180} STROKE {r 90}
2 * {y 20 flip 180} STROKE {}
}
rule CHAR {
4 * {r 60} STROKE {}
}
rule CHAR {
STROKE {r 90}
3 * {y 10 flip 180} STROKE {}
}
rule STROKE {
B {}
}
rule STROKE {
B {flip 90}
}
rule B 30 {
MARK {}
B {x .6 r 10}
}
rule B 30 {
MARK {}
B {x .6 r 3}
}
rule B 250 {
MARK {}
B {x .9}
}
rule B 10 {
MARK {}
B {flip 90}
}
rule B 10 { }
rule MARK 3 {
CIRCLE {}
}
rule MARK {
CIRCLE {s 2}
}
rule MARK {
SQUARE {s 3}
}
rule MARK {
CIRCLE {s 4}
}
rule MARK 0.01 {
CIRCLE {s 7}
}
Structure: LINES generates rows of NEWLINE, each with 50 CHAR. CHAR combines STROKE with rotations and flips. B recursively draws strokes using MARK (CIRCLE/SQUARE).
Animated Scene with Trees
This script models a dynamic scene with time-based animation.
background{b -1} startshape init
rule init{time{z 0 r -90 sat .5 |h 220}}
rule time 40 {draw{} time{r 1 s .9991 b .1| a .1|}}
rule time {slow{|b -1 |a 1 |sat 1}}
rule time {slow{|b 1 |a -1 |sat 1}}
rule slow 20 {draw{} slow{ r 1 s .9991 b .03| a .03| h -1}}
rule slow {time{|sat -1}}
rule draw{
SQUARE[z 0 x 1 s .27 .018 x -.5 a 1 h 1|]
stars{z 1 x .91}
cloud[z 2 x .83 s .17 x .5 h .7| a 1 sat .3|]
ground[z 3 x .73 s .1 x .5]
}
// ... (remaining rules for ground, stuff, block, bush, tree, stars, cloud abbreviated for example)
Rules time and slow control animation: rotation, fading (a), brightness (b). draw composes layers by Z-order.
Advanced Example: Procedural Tree
The most complex script generates a realistic tree with branches and leaves, using functions and loops.
startshape MAIN
CF::Background = [hue 0 sat 0.84 b -0.9]
CF::MinimumSize = 1
// Global constants and functions: GTreePartMove, MaxTrunkDepth() = randint(10,40), RandRange, etc.
shape MAIN {
rule 1 {
SKY [ z -2 s 100 ]
SUN [ z -1 s 400 ]
TREE [ x -0.1 y -0.75 ]
GRASS [ x -1 y -1 ]
}
}
// ... (full code with TRUNK, LEAFBUNCH, LEAFSTEM etc. defines recursive branching with randint and rand)
Functions like MaxTrunkDepth() and NewBranchRotation() add randomization. TRUNK grows recursively to EndDepth, then branches. LEAFBUNCH simulates foliage with twists.
Core CFDG Constructs
- Rules (rule): rule Shape { ... } with probabilities (e.g., rule S 3 {..}).
- Transformations: s (scale), r (rotate), x/y (translate), flip, z (layering).
- Color and Effects: hue, sat, b (brightness), a (alpha).
- Functions: randint(min,max), rand(min,max), loop N.
- Conditionals: if/else for recursion.
- Start: startshape NAME, background{}.
Key Takeaways
- CFDG is a context-free grammar, perfect for fractals and L-systems without context dependencies.
- Recursion with probabilities and randomization ensures variety.
- Supports Z-buffering for 2.5D scenes.
- GPL-licensed, cross-platform (Windows/Mac/Linux).
- Alternatives: Structure Synth for 3D.
— Editorial Team
No comments yet.