from __future__ import division from visual import * from random import * import pickle # Bruce Sherwood, June 2006; use this any way you like. # This program generates a wood-like surface by "line integral convolution" # in which you start from an array of random values, then average them # along lines (or curves) for a certain distance, so that locations along # a curve have similar values, giving a streaked appearance. This simple program # only handles straight stream lines. # For more information, google "line integral convolution". # There also exists a much more efficient "fast line integral convolution". M = 256 # texture must be 2**M by 2**N N = M filename = 'wood%ilum.vpt' % M print 'Texture file name: '+filename slope = 0.5 # use negative slope in calculations below; texture y gets inverted write = 1 # 1 to create a texture file, 0 to read existing texture file if write: t = zeros([M,N], float) # numpy array; elements are random floats 0 to 1 u = zeros([M,N], ubyte) # numpy array; elements are integers 0 to 255 for y in range(M): for x in range(N): t[y][x] = random() # t array contains random numbers halfrange = int(0.1*M) # average 2*halfrange+1 points along stream line for y in range(M): for x in range(N): avg = 0 for s in range(-halfrange,halfrange+1): # modulus arithmetic gives wrap-around of data: avg += t[int((y-s*slope) % N)][((x+s) % M)] # u array contains stream averages of data in the t array: u[y][x] = int(255*avg/(2*halfrange+1)) if write: pickle.dump(u, open(filename, 'w')) # save the texture in a file else: # 'rU' eliminates cross-platform end-of-line problems when reading file: u = pickle.load(open(filename, 'rU')) # retrieve the texture from a file lum = texture(data=u, type="luminance") # Display 3 by 3 abutted boxes and note smooth connections at edges # thanks to the modulus wrap-around algorithm used above: for x in range(-1,2): for y in range(-1,2): box(pos=(x,y,0), color=color.orange, texture=lum) ##sphere(pos=(3,0,0), radius=1.5, color=color.cyan, texture=lum) ##sphere(pos=(-3,0,0), radius=1.5, color=color.green, texture=lum)