Skip to content

Instantly share code, notes, and snippets.

@davepape
Last active June 24, 2022 09:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davepape/7324958 to your computer and use it in GitHub Desktop.
Save davepape/7324958 to your computer and use it in GitHub Desktop.
Draws a sphere with a texture.
from pyglet.gl import *
from math import *
tex = pyglet.image.load('biosphere.png').get_texture()
step = 10
vlists = []
for lat in range(-90,90,step):
verts = []
texc = []
for lon in range(-180,181,step):
x = -cos(radians(lat)) * cos(radians(lon))
y = sin(radians(lat))
z = cos(radians(lat)) * sin(radians(lon))
s = (lon+180) / 360.0
t = (lat+90) / 180.0
verts += [x,y,z]
texc += [s,t]
x = -cos(radians((lat+step))) * cos(radians(lon))
y = sin(radians((lat+step)))
z = cos(radians((lat+step))) * sin(radians(lon))
s = (lon+180) / 360.0
t = ((lat+step)+90) / 180.0
verts += [x,y,z]
texc += [s,t]
vlist = pyglet.graphics.vertex_list(int(len(verts)/3), ('v3f', verts), ('t2f', texc))
vlists.append(vlist)
window = pyglet.window.Window(1024,512)
angle = 0
glEnable(GL_DEPTH_TEST)
@window.event
def on_draw():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-2,2,-1,1,-1,1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glRotatef(angle, 0, 1, 0)
glColor3f(1,1,1)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, tex.id)
for v in vlists:
v.draw(GL_TRIANGLE_STRIP)
def update(dt):
global angle
angle = angle + 1
pyglet.clock.schedule_interval(update,1/60.0)
pyglet.app.run()
@pvince99
Copy link

line 27 needs change: from "len(verts)/3" to "int(len(verts)/3)"

@davepape
Copy link
Author

davepape commented Apr 22, 2021

"int()" change made. Note that this code was written for Python 2, and the int() is only needed for Python 3 - the point being, I don't know if there are any other such Python3-related bugs lurking; I haven't used this code in many years.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment