Created
June 29, 2019 02:54
-
-
Save tthtlc/c67b54382ce34bd11ec2d402d6ddea1d to your computer and use it in GitHub Desktop.
Pentagonal Torus in 3D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#### sudo pip install pyglet | |
from math import pi, sin, cos | |
import pyglet | |
from pyglet.gl import * | |
mywidth=0 | |
myheight=0 | |
try: | |
# Try and create a window with multisampling (antialiasing) | |
config = Config(sample_buffers=1, samples=4, | |
depth_size=16, double_buffer=True,) | |
window = pyglet.window.Window(resizable=True, config=config) | |
except pyglet.window.NoSuchConfigException: | |
# Fall back to no multisampling for old hardware | |
window = pyglet.window.Window(resizable=True) | |
@window.event | |
def on_resize(width, height): | |
# Override the default on_resize handler to create a 3D projection | |
global mywidth, myheight | |
mywidth = width | |
myheight = height | |
glViewport(0, 0, width, height) | |
glMatrixMode(GL_PROJECTION) | |
glLoadIdentity() | |
gluPerspective(60., width / float(height), .1, 1000.) | |
glMatrixMode(GL_MODELVIEW) | |
return pyglet.event.EVENT_HANDLED | |
def update(dt): | |
global rx, ry, rz | |
rx += dt * 30 | |
ry += dt * 30 | |
rz += dt * 30 | |
rx %= 360 | |
ry %= 360 | |
rz %= 360 | |
pyglet.clock.schedule(update) | |
@window.event | |
def on_mouse_press(x, y, button, modifiers): | |
global mywidth, myheight | |
num=(mywidth+1)*(myheight+1)*3 | |
print num | |
#a = (GLbyte * mywidth * myheight)(0) | |
a = (GLubyte * (3 * num))(0) | |
glReadPixels(0, 0, mywidth, myheight, GL_RGB, GL_UNSIGNED_BYTE,a) | |
print a | |
print a[1] | |
print a[2] | |
print a[3] | |
@window.event | |
def on_draw(): | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) | |
glLoadIdentity() | |
glTranslatef(0, 0, -4) | |
glRotatef(rz, 0, 0, 1) | |
glRotatef(ry, 0, 1, 0) | |
glRotatef(rx, 1, 0, 0) | |
batch.draw() | |
def setup(): | |
# One-time GL setup | |
glClearColor(1, 1, 1, 1) | |
glColor3f(1, 0, 0) | |
glEnable(GL_DEPTH_TEST) | |
glEnable(GL_CULL_FACE) | |
# Uncomment this line for a wireframe view | |
#glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) | |
# Simple light setup. On Windows GL_LIGHT0 is enabled by default, | |
# but this is not the case on Linux or Mac, so remember to always | |
# include it. | |
glEnable(GL_LIGHTING) | |
glEnable(GL_LIGHT0) | |
glEnable(GL_LIGHT1) | |
# Define a simple function to create ctypes arrays of floats: | |
def vec(*args): | |
return (GLfloat * len(args))(*args) | |
glLightfv(GL_LIGHT0, GL_POSITION, vec(.5, .5, 1, 0)) | |
glLightfv(GL_LIGHT0, GL_SPECULAR, vec(.5, .5, 1, 1)) | |
glLightfv(GL_LIGHT0, GL_DIFFUSE, vec(1, 1, 1, 1)) | |
glLightfv(GL_LIGHT1, GL_POSITION, vec(1, 0, .5, 0)) | |
glLightfv(GL_LIGHT1, GL_DIFFUSE, vec(.5, .5, .5, 1)) | |
glLightfv(GL_LIGHT1, GL_SPECULAR, vec(1, 1, 1, 1)) | |
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0.5, 0, 0.3, 1)) | |
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, vec(1, 1, 1, 1)) | |
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50) | |
class Torus(object): | |
list = None | |
def __init__(self, radius, inner_radius, slices, inner_slices, | |
batch, group=None): | |
# Create the vertex and normal arrays. | |
vertices = [] | |
normals = [] | |
u_step = 2 * pi / (slices) | |
v_step = 2 * pi / (inner_slices) | |
u = 0. | |
for i in range(slices+1): | |
cos_u = cos(1*u) | |
sin_u = sin(1*u) | |
v = 0. | |
for j in range(inner_slices): | |
cos_v = cos(1*v) | |
sin_v = sin(1*v) | |
d = (radius + inner_radius * cos_v) | |
x = d * (cos_u +2)* cos_u | |
y = d * (cos_u +2)* sin_u | |
z = inner_radius * sin_v | |
nx = cos_u * cos_v | |
ny = sin_u * cos_v | |
nz = sin_v | |
vertices.extend([x, y, z]) | |
normals.extend([nx, ny, nz]) | |
v += v_step | |
u += u_step | |
# Create a list of triangle indices. | |
indices = [] | |
for i in range(slices): | |
for j in range(inner_slices): | |
p = i * inner_slices + j | |
indices.extend([p, p + inner_slices, p + inner_slices + 1]) | |
indices.extend([p, p + inner_slices + 1, p + 1]) | |
self.vertex_list = batch.add_indexed(len(vertices)//3, | |
GL_TRIANGLE_STRIP, | |
group, | |
indices, | |
('v3f/static', vertices), | |
('n3f/static', normals)) | |
def delete(self): | |
self.vertex_list.delete() | |
setup() | |
batch = pyglet.graphics.Batch() | |
torus = Torus(1, 1, 5, 30, batch=batch) | |
rx = ry = rz = 0 | |
pyglet.app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment