Skip to content

Instantly share code, notes, and snippets.

@davepape
Created December 4, 2013 15:59
Show Gist options
  • Select an option

  • Save davepape/7790064 to your computer and use it in GitHub Desktop.

Select an option

Save davepape/7790064 to your computer and use it in GitHub Desktop.
Draws a flat world map - 360x180 unit mesh of triangle strips, with biosphere map textured on it. Includes simple driving using the keyboard arrows.
from pyglet.gl import *
from math import *
tex = pyglet.image.load('biosphere.png').get_texture()
vlists = []
for lat in range(-90,90,30):
verts = []
texc = []
for lon in range(-180,181,30):
x = lon
y = lat
s = (lon+180) / 360.0
t = (lat+90) / 180.0
verts += [x,y]
texc += [s,t]
x = lon
y = lat+30
s = (lon+180) / 360.0
t = ((lat+30)+90) / 180.0
verts += [x,y]
texc += [s,t]
vlist = pyglet.graphics.vertex_list(len(verts)/2, ('v2f', verts), ('t2f', texc))
vlists.append(vlist)
window = pyglet.window.Window(1024,512)
angle = 0
fovy = 45
heading = 0
campos = [ 0, 0, 200]
@window.event
def on_draw():
glClear(GL_COLOR_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
# glOrtho(-180,180,-90,90,-1000,1000)
gluPerspective(fovy, 2, 0.1, 1000)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
# Move camera back 200 units
glRotatef(-heading, 0, 1, 0)
glTranslatef(-campos[0], -campos[1], -campos[2])
glPushMatrix()
# Rotate object 'angle' degrees
glRotatef(angle, 1, 0, 0)
glColor3f(1,1,1)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, tex.id)
for v in vlists:
v.draw(GL_TRIANGLE_STRIP)
glPopMatrix()
glPushMatrix()
# draw something else with other object transformation
glPopMatrix()
@window.event
def on_key_press(key,mod):
global heading, campos
if key == pyglet.window.key.A:
heading = heading + 5
if key == pyglet.window.key.S:
heading = heading - 5
if key == pyglet.window.key.UP:
campos[0] -= 5 * sin(radians(heading))
campos[2] -= 5 * cos(radians(heading))
if key == pyglet.window.key.DOWN:
campos[0] += 5 * sin(radians(heading))
campos[2] += 5 * cos(radians(heading))
if key == pyglet.window.key.LEFT:
campos[0] -= 5 * cos(radians(heading))
campos[2] += 5 * sin(radians(heading))
if key == pyglet.window.key.RIGHT:
campos[0] += 5 * cos(radians(heading))
campos[2] -= 5 * sin(radians(heading))
print fovy
def update(dt):
global angle
angle = -45
pass
pyglet.clock.schedule_interval(update,1/60.0)
pyglet.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment