-
Notifications
You must be signed in to change notification settings - Fork 0
/
procedural.py
151 lines (133 loc) · 6.16 KB
/
procedural.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import pyglet
import random
from random import uniform
import math
from vectors import v
class Background(object):
def __init__(self, num_triangles=5000, x=(-8000,8000), y=(-5000,5000), z=(-100,100), width=200):
self.vlist = pyglet.graphics.vertex_list(3*num_triangles, 'v3f', 'c3f')
verts_per_triangle = 3
for triangle_index in range(num_triangles):
color = random.random(), random.random(), random.random()
cx, cy, cz = uniform(x[0],x[1]), uniform(y[0],y[1]), uniform(z[0],z[1])
for sub_index in range(3):
vert = cx+uniform(-width/2.0, width/2.0), cy + uniform(-width/2.0, width/2.0), cz + uniform(-width/2.0, width/2.0)
vertex_index = (triangle_index*verts_per_triangle + sub_index)*3
self.vlist.vertices[vertex_index:vertex_index+3] = vert
self.vlist.colors[vertex_index:vertex_index+3] = color
x_count, y_count = 200, 200
x_width, y_width = 5000, 4000
start_x, start_y = -2500, -2000
depth, offset_z = 600, -300
cylinder_height, cylinder_radius = 1000, 200
z_scale = 1
coefficients = ((0,0.05),(1,0.15),(2,0.2),(3,0.3),(4,0.5))
vertexen = list()
colors = list()
for j in range(y_count):
a = list()
b = list()
for i in range(x_count):
x_n, y_n = 1.0*i/x_count, 1.0*j/y_count
p = sum(map(lambda c: perlin_2d(x_n,y_n, source=rands[c[0]])*c[1],coefficients) )
xp, yp, zp = x_width*x_n+start_x, y_width*y_n+start_y, offset_z+depth*p
r, theta, h = cylinder_radius + depth*p, 2*math.pi*x_n, cylinder_height*y_n
#a.append((r*math.cos(theta), h, offset_z + r*math.sin(theta)/z_scale))
a.append((xp, yp, zp))
b.append((0.3+0.7*p,)*3)
vertexen.append(a)
colors.append(b)
# diff = lambda a, b: (a[0]-b[0],a[1]-b[1],a[2]-b[2])
# cross_product = lambda a, b: (a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0])
# normalize = lambda a: (a[0]/math.sqrt(a[0]**2 + a[1]**2 + a[2]**2), a[1]/math.sqrt(a[0]**2 + a[1]**2 + a[2]**2), a[2]/math.sqrt(a[0]**2 + a[1]**2 + a[2]**2))
# normals = list()
# for j in range(y_count):
# a = list()
# for i in range(x_count):
# if i+1 < x_count and j+1 < y_count:
# a.append(normalize(cross_product(diff(vertexen[i+1][j],vertexen[i][j]),diff(vertexen[i][j],vertexen[i][j+1]))))
# else:
# a.append(normalize(cross_product(diff(vertexen[i-1][j],vertexen[i][j]),diff(vertexen[i][j-1],vertexen[i][j]))))
# normals.append(a)
vlist, clist = [], []
for y in range(y_count-1):
for x in range(x_count-1):
for i in (vertexen[y][x], vertexen[y][x+1], vertexen[y+1][x], vertexen[y][x+1], vertexen[y+1][x+1], vertexen[y+1][x]):
vlist.extend(x for x in i)
for i in (colors[y][x], colors[y][x+1], colors[y+1][x], colors[y][x+1], colors[y+1][x+1], colors[y+1][x]):
clist.extend(x for x in i)
# for i in (normals[y][x], normals[y][x+1], normals[y+1][x], normals[y][x+1], normals[y+1][x+1], normals[y+1][x]):
# nlist.extend(x for x in i)
self.vlist2 = pyglet.graphics.vertex_list(len(vlist)/3, 'v3f', 'c3f') #, 'n3f')
self.vlist2.vertices = vlist
self.vlist2.colors = clist
# self.vlist2.normals = nlist
def update(self, timestep): pass
def draw(self):
pyglet.gl.glPushMatrix()
self.vlist.draw(pyglet.gl.GL_TRIANGLES)
#pyglet.gl.glRotatef(self.pscreen.total_time*20, 0.0, 1.0, 0.0)
#pyglet.gl.glMaterialfv(pyglet.gl.GL_FRONT_AND_BACK, pyglet.gl.GL_DIFFUSE, (pyglet.gl.GLfloat * 4)(*(1, 1, 1, 1.0)))
#pyglet.gl.glMaterialfv(pyglet.gl.GL_FRONT_AND_BACK, pyglet.gl.GL_SPECULAR, (pyglet.gl.GLfloat * 4)(*(0.9, 0.9, 0.8, 1.0)))
#pyglet.gl.glMaterialf(pyglet.gl.GL_FRONT_AND_BACK, pyglet.gl.GL_SHININESS, 89)
self.vlist2.draw(pyglet.gl.GL_TRIANGLES)
pyglet.gl.glPopMatrix()
def perlin_2d(x, y, source=None, x_w=None, y_w=None, interpolate=None):
""" Takes x and y coordinates and produces perlin noise, seeded by the 2d array `source` and using the 2d interpolation function interpolate. """
if source is None: source=r_2d
if x_w is None: x_w = len(source)-1
if y_w is None: y_w = len(source[0])-1
if interpolate is None: interpolate=interpolate_2d
p = v(x*x_w,y*y_w)
i,j = int(p.x),int(p.y) # Don't need to use floor because these are assumed to be positive.
c=[(i,j),(i,j+1),(i+1,j+1),(i+1,j)]
corners = [source[u][w]*(p-v(u,w)) for (u,w) in c]
output = interpolate(p-v(i,j), corners)
return output
def rescale_2d(source, m):
x_old_w, y_old_w = len(source), len(source[0])
x_new_w, y_new_w = int(x_old_w*m), int(y_old_w*m)
output = list()
for inew in range(x_new_w):
a = list()
for jnew in range(y_new_w):
x, y = 1.0*inew/x_new_w, 1.0*jnew/y_new_w
iold, jold = int(x*x_old_w), int(y*y_old_w)
c=[(iold,jold),(iold,jold+1),(iold+1,jold+1),(iold+1,jold)]
a.append(interpolate_2d(v(x-1.0*iold/x_old_w, y-1.0*jold/y_old_w), [source[u][w] for (u,w) in c]))
output.append(a)
return output
def interpolate_2d(p, c, interpolate=None):
""" Takes a point normalized to 0,1 and four corner values (0,0, 0,1, 1,1, 1,0) and interpolates between them. """
if interpolate is None: interpolate = hermite
bottom_int = hermite(p.x, (c[0],c[3]))
top_int = hermite(p.x, (c[1],c[2]))
return hermite(p.y, (bottom_int, top_int))
def hermite(p, c):
""" Hermite interpolation between two points, with starting and ending tangents zero. """
return c[0]*(2*(p**3) - 3*(p**2) + 1) + c[1]*(-2*(p**3) + 3*(p**2))
rands = list()
rands.append( [[v(uniform(-1,1),uniform(-1,1)) for i in range(100)] for j in range(100)] )
for i in range(10):
if len(rands[i]) < 10: break
rands.append(rescale_2d(rands[i], 0.5))
def apply(function):
return lambda source, index: function(source[index])
def identity(source, index):
""" Returns the requested value. """
return source[index]
class CompositeSignal(object):
""" A signal which is a user-defined compositing of other signals and operators. """
def __init__(self, source, operation=lambda source, index: source[index]):
self.source = source
self.operation = operation
def __len__(self):
return len(self.source)
def __getitem__(self, index):
return self.v(self.t(self.source, index))
def __add__(self, other):
return CompositeSignal((self, other), operation=lambda source, index: source[0][index]+source[1][index])
class Signal1D(object):
def __init__(self): pass
def __len__(self, index): pass
def __getitem__(self, index): pass