Created
October 15, 2019 11:07
-
-
Save emily33901/f695b5af5ae3a3c14676ee044c92b05c to your computer and use it in GitHub Desktop.
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
def cube3d(): | |
win = GraphWin("3d", 200, 200, False) | |
cube = [glm.vec3(1, 1, 1), | |
glm.vec3(-1, 1, 1), | |
glm.vec3(1, -1, 1), | |
glm.vec3(1, 1, -1), | |
glm.vec3(-1, -1, 1), | |
glm.vec3(-1, 1, -1), | |
glm.vec3(1, -1, -1), | |
glm.vec3(-1, -1, -1)] | |
proj = glm.perspective(90, 1, 0.1, 5) | |
view = glm.lookAt(glm.vec3(-2, -3, 0), | |
glm.vec3(0, 0, 0), glm.vec3(0, 0, 1)) | |
ispin = 0 | |
while True: | |
ispin += 0.001 | |
mvp = proj * view | |
view = glm.lookAt(glm.vec3(1, 2, 1), | |
glm.vec3(0, 0, 0), glm.vec3(0, 0, 1)) | |
transformedCube = [] | |
rot = glm.mat4() | |
rot = glm.rotate(rot, ispin, glm.vec3(1.0, 1.0, 1.0)) | |
rot = glm.rotate(rot, ispin, glm.vec3(0.0, 1.0, 1.0)) | |
for p in cube: | |
clipSpace = mvp * rot * glm.vec4(p, 1.0) | |
ndc = clipSpace.xyz / clipSpace.w | |
screenPos = ((ndc.xy + glm.vec2(1.0)) / 2.0) * glm.vec2(200, 200) | |
transformedCube.append(screenPos) | |
pairs = [(0, 1), (1, 4), (4, 2), (2, 0), (1, 5), (2, 6), | |
(0, 3), (4, 7), (5, 7), (5, 3), (3, 6), (6, 7)] | |
tc = transformedCube | |
for i1, i2 in pairs: | |
Line(Point(*tc[i1].xy), Point(*tc[i2].xy)).draw(win) | |
for i, p in enumerate(transformedCube): | |
Point(p.x, p.y).draw(win) | |
Text(Point(p.x, p.y), "%d" % i).draw(win) | |
win.flush() | |
win.update() | |
if win.closed: | |
break | |
for i in win.items: | |
i.undraw() | |
cube3d() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment