Skip to content

Instantly share code, notes, and snippets.

@migerh
Forked from anonymous/map.py
Last active December 10, 2015 16:39
Show Gist options
  • Save migerh/4462689 to your computer and use it in GitHub Desktop.
Save migerh/4462689 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import Image
import ImageDraw
import random
import math
import pyglet
import pyglet.gl as gl
import pyglet.window.key as key
w = 257
h = 257
randomness = 1.99
data = [[0 for x in range (w)] for y in range(h)]
def fill_rect(data, bx, by, w, tl, tr, bl, br):
t = (tl + tr) // 2
l = (tl + bl) // 2
b = (bl + br) // 2
r = (tr + br) // 2
c = min(255, math.trunc(((random.random() * 2 - 1) * w * randomness + (tl + tr + bl + br)) // 4))
w2 = w // 2
data[bx][by + w2] = t
data[bx + w2][by] = l
data[bx + w2][by + w2] = c
data[bx + w][by + w2] = r
data[bx + w2][by + w] = b
# recursion, only if there's space left to fill
if (w > 1):
fill_rect(data, bx, by, w2, tl, t, l, c)
fill_rect(data, bx + w2, by, w2, t, tr, c, r)
fill_rect(data, bx, by + w2, w2, l, c, bl, b)
fill_rect(data, bx + w2, by + w2, w2, c, r, b, br)
# init grid with random heights
data[0][0] = random.randint(0, 255)
data[w - 1][0] = random.randint(0, 255)
data[0][h - 1] = random.randint(0, 255)
data[w - 1][h - 1] = random.randint(0, 255)
fill_rect(data, 0, 0, w - 1, data[0][0], data[w - 1][0], data[0][h - 1], data[w - 1][h - 1])
window = pyglet.window.Window(resizable=True)
gl.glEnable(gl.GL_DEPTH_TEST)
class Camera(object):
def __init__(self, position, yaw, pitch, scale=1):
self.x, self.y, self.z = position # cam position
self.yaw = yaw # look left/right
self.pitch = pitch # look up/down
self.scale = scale # zoom / unused
def focus(self, width, height):
# Set viewport matrix
gl.glViewport(0, 0, width, height)
gl.glMatrixMode(gl.GL_PROJECTION)
gl.glLoadIdentity()
gl.gluPerspective(45.0, (width / float(height)), 0.1, 1000.0)
gl.glMatrixMode(gl.GL_MODELVIEW)
gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_LINE);
# Set modelview matrix to move, scale & rotate
gl.glMatrixMode(gl.GL_MODELVIEW)
gl.glLoadIdentity()
gl.glRotatef(-self.pitch, 1.0, 0.0, 0.0)
gl.glRotatef(-self.yaw, 0.0, 1.0, 0.0)
gl.glTranslatef(-self.x, -self.y, -self.z)
cam = Camera(position = (128, 128, 5), yaw = 0, pitch = 0, scale = 1)
cam.focus(window.width, window.height)
def draw_map(vlist):
vlist.draw(gl.GL_TRIANGLE_STRIP)
#vlists = [pyglet.graphics.vertex_list(4, ('v3f\static', (-5, -5, 0, 5, -5, 0, -5, 5, 0, 5, 5, 0)))]
vlists = [0 for i in range(h - 1)]
for y in range(h - 1):
verts = [0 for i in range(6 * (w - 1))]
for x in range(w - 1):
verts[6 * x] = x
verts[6 * x + 1] = y
verts[6 * x + 2] = -data[x][y]
verts[6 * x + 3] = x
verts[6 * x + 4] = y + 1
verts[6 * x + 5] = -data[x][y + 1]
vlists[y] = pyglet.graphics.vertex_list(len(verts)/3, ('v3f\static', verts))
@window.event
def on_draw():
global vlists
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
gl.glColor3f(0.0, 0.5, 0.0)
for x in range(len(vlists)):
draw_map(vlists[x])
return pyglet.event.EVENT_HANDLED
@window.event
def on_resize(width, height):
global cam
cam.focus(width, height)
return pyglet.event.EVENT_HANDLED
@window.event
def on_key_release(symbol, modifiers):
global cam, window
if symbol == pyglet.window.key.Q:
pyglet.app.exit()
@window.event
def on_mouse_scroll(x, y, scrollx, scrolly):
global cam
f = 5
cam.z -= f * scrolly
cam.focus(window.width, window.height)
@window.event
def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
global cam
f = 1/5.
cam.yaw -= f * dx
cam.pitch += f * dy
cam.focus(window.width, window.height)
@window.event
def on_key_press(symbol, modifiers):
global cam
degtorad = math.pi/180.
if symbol == key.A or symbol == key.LEFT:
cam.yaw += 5
if symbol == key.D or symbol == key.RIGHT:
cam.yaw -= 5
if symbol == key.W:
cam.x -= math.sin(degtorad * cam.yaw) * 30;
if symbol == key.S:
cam.x += math.sin(degtorad * cam.yaw) * 30;
cam.focus(window.width, window.height)
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment