|
| 1 | +#!/usr/bin/env jruby |
| 2 | +require 'propane' |
| 3 | +require 'arcball' |
| 4 | + |
| 5 | +class CSplantSketch < Propane::App |
| 6 | + ######################################################## |
| 7 | + # csplant.rb |
| 8 | + # A 3D Plant implemented using a Context Sensitive |
| 9 | + # Lindenmayer System in JRubyArt |
| 10 | + # by Martin Prout (30 January 2013) |
| 11 | + # Hold down 'y' key and drag mouse to rotate about y axis |
| 12 | + ######################################################## |
| 13 | + |
| 14 | + load_libraries :cs_grammar |
| 15 | + attr_reader :csplant |
| 16 | + |
| 17 | + def setup |
| 18 | + sketch_title 'Context Sensitive Plant' |
| 19 | + Processing::ArcBall.constrain(self) # Arcball constrained to y-axis rotation |
| 20 | + @csplant = CSPlant.new(height) |
| 21 | + csplant.create_grammar 5 |
| 22 | + no_stroke |
| 23 | + end |
| 24 | + |
| 25 | + def draw |
| 26 | + background 0 |
| 27 | + lights |
| 28 | + translate(0, height * 0.3) |
| 29 | + csplant.render |
| 30 | + end |
| 31 | + |
| 32 | + def settings |
| 33 | + size 800, 800, P3D |
| 34 | + end |
| 35 | +end |
| 36 | + |
| 37 | +############ |
| 38 | +# CSPlant |
| 39 | +############ |
| 40 | +class CSPlant |
| 41 | + include Propane::Proxy |
| 42 | + |
| 43 | + IGNORE = '[]+-^&3' |
| 44 | + attr_reader :grammar, :axiom, :production, :premis, :rule, |
| 45 | + :theta, :scale_factor, :len, :phi, :len |
| 46 | + |
| 47 | + def initialize(len) |
| 48 | + @axiom = 'F' |
| 49 | + @grammar = Grammar.new( |
| 50 | + axiom, |
| 51 | + { 'F' => 'F[-EF[3&A]]E[+F[3^A]]', 'F<E' => 'F[&F[3+A]][^F[3-A]]' }, |
| 52 | + IGNORE |
| 53 | + ) |
| 54 | + @production = axiom |
| 55 | + @len = len |
| 56 | + @theta = 25.radians |
| 57 | + @phi = 25.radians |
| 58 | + no_stroke |
| 59 | + end |
| 60 | + |
| 61 | + def render |
| 62 | + fill(0, 75, 152) |
| 63 | + light_specular(204, 204, 204) |
| 64 | + specular(255, 255, 255) |
| 65 | + shininess(1.0) |
| 66 | + repeat = 1 |
| 67 | + production.each_char do |ch| |
| 68 | + case ch |
| 69 | + when 'F' |
| 70 | + translate(0, len / -2, 0) |
| 71 | + box(len / 9, len, len / 9) |
| 72 | + translate(0, len / -2, 0) |
| 73 | + when '+' |
| 74 | + rotateX(-theta * repeat) |
| 75 | + repeat = 1 |
| 76 | + when '-' |
| 77 | + rotateX(theta * repeat) |
| 78 | + repeat = 1 |
| 79 | + when '&' |
| 80 | + rotateZ(-phi * repeat) |
| 81 | + repeat = 1 |
| 82 | + when '^' |
| 83 | + rotateZ(phi * repeat) |
| 84 | + repeat = 1 |
| 85 | + when '3' |
| 86 | + repeat = 3 |
| 87 | + when '[' |
| 88 | + push_matrix |
| 89 | + when ']' |
| 90 | + pop_matrix |
| 91 | + when 'E', 'A' |
| 92 | + else |
| 93 | + puts("character '#{ch}' not in grammar") |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | + ############################## |
| 98 | + # create grammar from axiom and |
| 99 | + # rules (adjust scale) |
| 100 | + ############################## |
| 101 | + |
| 102 | + def create_grammar(gen) |
| 103 | + @len *= 0.6**gen |
| 104 | + @production = grammar.generate gen |
| 105 | + end |
| 106 | +end |
| 107 | + |
| 108 | +CSplantSketch.new |
0 commit comments