Skip to content

Commit 4756dd9

Browse files
committed
Terrain with Vec3D
1 parent 5e21deb commit 4756dd9

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
WIDTH = 1_400
3+
HEIGHT = 1_100
4+
SCL = 30
5+
attr_reader :terrain, :rows, :columns, :mover
6+
7+
def settings
8+
size 800, 800, P3D
9+
end
10+
11+
def setup
12+
sketch_title 'Terrain'
13+
@columns = WIDTH / SCL
14+
@rows = HEIGHT / SCL
15+
@terrain = {}
16+
@mover = 0
17+
end
18+
19+
def draw
20+
background 0
21+
@mover -= 0.1
22+
yoff = mover
23+
(0..rows).each do |y|
24+
xoff = 0
25+
(0..columns).each do |x|
26+
terrain[hash_key(x, y)] = Vec3D.new(x * SCL, y * SCL, map1d(noise(xoff, yoff), 0..1.0, -65..65))
27+
xoff += 0.2
28+
end
29+
yoff += 0.2
30+
end
31+
no_fill
32+
stroke 235, 69, 129
33+
translate width / 2, height / 2
34+
rotate_x PI / 3
35+
translate(-WIDTH / 2, -HEIGHT / 2)
36+
(0...rows).each do |y|
37+
begin_shape(TRIANGLE_STRIP)
38+
(0..columns).each do |x|
39+
terrain[hash_key(x, y)].to_vertex(renderer)
40+
terrain[hash_key(x, y.succ)].to_vertex(renderer)
41+
end
42+
end_shape
43+
end
44+
end
45+
46+
private
47+
48+
# HACK should be safe here
49+
def hash_key(x, y)
50+
WIDTH * y + x
51+
end
52+
53+
def renderer
54+
@renderer ||= AppRender.new(self)
55+
end

0 commit comments

Comments
 (0)