Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. import itertools
  2. import pyglet
  3. import random
  4. from pyglet.gl import *
  5. import noise
  6. import numpy as np
  7. import colorsys as cs
  8. import math
  9.  
  10. window = pyglet.window.Window(resizable=True)
  11.  
  12. def randcol():
  13. return [int(random.random()*256) for x in range(3)]
  14.  
  15. def narray(x, y, s=200, p = 0.5, oct = 3):
  16. return noise.snoise2(x/s, y/s, octaves = oct, persistence = p)
  17.  
  18. def trimesh(vcmax, kmax, scale, offsetx, offsety, func, theight):
  19. num = vcmax*kmax
  20. const = (3**0.5)/2
  21. eo = [[[k*const*scale+offsetx,
  22. offsety+scale*(k/2.0+vc),
  23. theight*func(k*const*scale+offsetx, offsety+scale*(k/2.0+vc))
  24. ] for vc in xrange(0,vcmax)] for k in xrange(0,kmax)] #k, vc, 3list
  25. eo = list(itertools.chain(*eo))
  26. counter = 0
  27. order = []
  28. flag = True
  29. while 1:
  30. order.append(eo[counter])
  31. if counter == num-1:
  32. break
  33. if flag:
  34. counter += vcmax
  35. flag = False
  36. else:
  37. counter -= (vcmax-1)
  38. flag = True
  39. sorder = [tuple(order[x:x+2*vcmax]) for x in xrange(0,len(order),2*vcmax)]
  40. for s in xrange(0,kmax-1):
  41. sorder[s] = list(itertools.chain(*sorder[s]))
  42. vlb = pyglet.graphics.Batch()
  43. color = []
  44. for s in xrange(0,len(sorder)):
  45. color.append([])
  46. for v in xrange(2,len(sorder[s]),3): #start on 3 vertice, the altitude
  47. val = (sorder[s][v]/theight+1)/2.0 #map in range 0,1
  48. for colr in cs.hsv_to_rgb(0.5,1,val): #get rgb
  49. color[s].append(int(colr*256))
  50. for s in xrange(0,len(sorder)):
  51. sorder[s] = tuple(sorder[s][0:3] + sorder[s] + sorder[s][-3:])
  52. color[s] = tuple(color[s][0:3] + color[s] + color[s][-3:])
  53. vlb.add(2*vcmax+2, pyglet.gl.GL_TRIANGLE_STRIP, None, ('v3f', sorder[s]), ('c3B', color[s]))
  54. return vlb
  55.  
  56. world = trimesh(90,90,10, 0,-400, narray, 50)
  57.  
  58. def display():
  59. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
  60. glShadeModel(GL_SMOOTH)
  61. glLoadIdentity()
  62.  
  63. def reshape():
  64. glShadeModel( GL_FLAT )
  65. glMatrixMode(GL_PROJECTION)
  66. glLoadIdentity()
  67. gluPerspective(120., window.width / float(window.height), .1, 1000.)
  68. glMatrixMode(GL_MODELVIEW)
  69. glTranslatef(0.,0.,-100.)
  70. glRotatef(0,1,0,0)
  71.  
  72. a, b, = window.width/2, window.height/2 #this stores mouse position
  73. k = 0.01 #sensitivity
  74. gx = 0 #this stores the angle
  75. gy = 0 #this also stores the angle
  76.  
  77. @window.event
  78. def on_mouse_press(x, y, button, mod): #No effect?
  79. global a, b
  80. a = x
  81. b = y
  82. return pyglet.event.EVENT_HANDLED
  83.  
  84. @window.event
  85. def on_draw():
  86. global a, b, k, gx, gy
  87. display()
  88. reshape()
  89. window.clear()
  90. #
  91. glMatrixMode(GL_MODELVIEW)
  92. gx += -k*(a-window.width/2) #if mouse on right side of screen, gx will be positive. If left, gx is negative
  93. gy += -k*(b-window.height/2) #if mouse on top of sceen, gy will ne positive. If bottom, gy is negative
  94. a, b, = window.width/2, window.height/2 #reset positions so scene does not move
  95. #
  96. glRotatef(-gx, 0,0,1)#side to side
  97. glRotatef(-gy,math.cos(-gx/180*math.pi),math.sin(-gx/180*math.pi),0) #up down
  98. #
  99. world.draw()
  100.  
  101. pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement