Skip to content

Commit 173b536

Browse files
committed
terrain sketch
1 parent 771ad33 commit 173b536

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

library/vecmath/vec3d/terrain.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env jruby
2+
require 'picrate'
3+
4+
# Alternative to using array or string as key
5+
Key = Struct.new(:x, :y) do
6+
def eql?(key)
7+
return false unless key.y == y
8+
key.x == x
9+
end
10+
end
11+
12+
class Terrain < Processing::App
13+
WIDTH = 1400
14+
HEIGHT = 1100
15+
SCL = 30
16+
attr_reader :terrain, :rows, :columns, :mover
17+
18+
def settings
19+
size 800, 800, P3D
20+
end
21+
22+
def setup
23+
sketch_title 'Terreno' # sketch_title doesn't work with P3D on PI
24+
@columns = WIDTH / SCL
25+
@rows = HEIGHT / SCL
26+
@terrain = {}
27+
@mover = 0
28+
end
29+
30+
def draw
31+
background 0
32+
@mover -= 0.1
33+
yoff = mover
34+
(0..rows).each do |y|
35+
xoff = 0
36+
(0..columns).each do |x|
37+
terrain[Key.new(x, y)] = Vec3D.new(x * SCL, y * SCL, map1d(noise(xoff, yoff), 0..1.0, -65..65))
38+
xoff += 0.2
39+
end
40+
yoff += 0.2
41+
end
42+
no_fill
43+
stroke 235, 69, 129
44+
translate width / 2, height / 2
45+
rotate_x PI / 3
46+
translate(-WIDTH / 2, -HEIGHT / 2)
47+
(0...rows).each do |y|
48+
begin_shape(TRIANGLE_STRIP)
49+
(0..columns).each do |x|
50+
terrain[Key.new(x, y)].to_vertex(renderer)
51+
terrain[Key.new(x, y.succ)].to_vertex(renderer)
52+
end
53+
end_shape(CLOSE)
54+
end
55+
end
56+
57+
private
58+
59+
def renderer
60+
@renderer ||= AppRender.new(self)
61+
end
62+
end
63+
64+
Terrain.new

0 commit comments

Comments
 (0)