Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import itertools
- import pyglet
- import random
- from pyglet.gl import *
- import noise
- import numpy as np
- import colorsys as cs
- import math
- window = pyglet.window.Window(resizable=True)
- def randcol():
- return [int(random.random()*256) for x in range(3)]
- def narray(x, y, s=200, p = 0.5, oct = 3):
- return noise.snoise2(x/s, y/s, octaves = oct, persistence = p)
- def trimesh(vcmax, kmax, scale, offsetx, offsety, func, theight):
- num = vcmax*kmax
- const = (3**0.5)/2
- eo = [[[k*const*scale+offsetx,
- offsety+scale*(k/2.0+vc),
- theight*func(k*const*scale+offsetx, offsety+scale*(k/2.0+vc))
- ] for vc in xrange(0,vcmax)] for k in xrange(0,kmax)] #k, vc, 3list
- eo = list(itertools.chain(*eo))
- counter = 0
- order = []
- flag = True
- while 1:
- order.append(eo[counter])
- if counter == num-1:
- break
- if flag:
- counter += vcmax
- flag = False
- else:
- counter -= (vcmax-1)
- flag = True
- sorder = [tuple(order[x:x+2*vcmax]) for x in xrange(0,len(order),2*vcmax)]
- for s in xrange(0,kmax-1):
- sorder[s] = list(itertools.chain(*sorder[s]))
- vlb = pyglet.graphics.Batch()
- color = []
- for s in xrange(0,len(sorder)):
- color.append([])
- for v in xrange(2,len(sorder[s]),3): #start on 3 vertice, the altitude
- val = (sorder[s][v]/theight+1)/2.0 #map in range 0,1
- for colr in cs.hsv_to_rgb(0.5,1,val): #get rgb
- color[s].append(int(colr*256))
- for s in xrange(0,len(sorder)):
- sorder[s] = tuple(sorder[s][0:3] + sorder[s] + sorder[s][-3:])
- color[s] = tuple(color[s][0:3] + color[s] + color[s][-3:])
- vlb.add(2*vcmax+2, pyglet.gl.GL_TRIANGLE_STRIP, None, ('v3f', sorder[s]), ('c3B', color[s]))
- return vlb
- world = trimesh(90,90,10, 0,-400, narray, 50)
- def display():
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
- glShadeModel(GL_SMOOTH)
- glLoadIdentity()
- def reshape():
- glShadeModel( GL_FLAT )
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- gluPerspective(120., window.width / float(window.height), .1, 1000.)
- glMatrixMode(GL_MODELVIEW)
- glTranslatef(0.,0.,-100.)
- glRotatef(0,1,0,0)
- a, b, = window.width/2, window.height/2 #this stores mouse position
- k = 0.01 #sensitivity
- gx = 0 #this stores the angle
- gy = 0 #this also stores the angle
- @window.event
- def on_mouse_press(x, y, button, mod): #No effect?
- global a, b
- a = x
- b = y
- return pyglet.event.EVENT_HANDLED
- @window.event
- def on_draw():
- global a, b, k, gx, gy
- display()
- reshape()
- window.clear()
- #
- glMatrixMode(GL_MODELVIEW)
- gx += -k*(a-window.width/2) #if mouse on right side of screen, gx will be positive. If left, gx is negative
- gy += -k*(b-window.height/2) #if mouse on top of sceen, gy will ne positive. If bottom, gy is negative
- a, b, = window.width/2, window.height/2 #reset positions so scene does not move
- #
- glRotatef(-gx, 0,0,1)#side to side
- glRotatef(-gy,math.cos(-gx/180*math.pi),math.sin(-gx/180*math.pi),0) #up down
- #
- world.draw()
- pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement