python:pygame, pyOpenGL 示例:旋转的八面体
参阅:【程序员数学:用Python学透线性代数和微积分】
下载:随书下载=> 源代码文件.zip , 程序员数学 附录.pdf
解压 源代码文件.zip 后访问目录 Math-for-Programmers-master\
点击 Anaconda3:Jupyter Notebook 打开 *.ipynb 文件,可见源代码。
我比较喜欢附录C 示例:旋转的八面体
D:\数学\Math-for-Programmers-master\Appendix C\rotate_octahedron.py
pip install pygame
pip install pyOpenGL
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import matplotlib.cm
from vectors import *
from math import *
def normal(face):
return(cross(subtract(face[1], face[0]), subtract(face[2], face[0])))
#blues = matplotlib.cm.get_cmap('Blues')
blues = matplotlib.colormaps['Blues']
def shade(face,color_map=blues,light=(1,2,3)):
return color_map(1 - dot(unit(normal(face)), unit(light)))
light = (1,2,3)
faces = [
[(1,0,0), (0,1,0), (0,0,1)],
[(1,0,0), (0,0,-1), (0,1,0)],
[(1,0,0), (0,0,1), (0,-1,0)],
[(1,0,0), (0,-1,0), (0,0,-1)],
[(-1,0,0), (0,0,1), (0,1,0)],
[(-1,0,0), (0,1,0), (0,0,-1)],
[(-1,0,0), (0,-1,0), (0,0,1)],
[(-1,0,0), (0,0,-1), (0,-1,0)],
]
def Axes():
axes = [
[(-1000,0,0),(1000,0,0)],
[(0,-1000,0),(0,1000,0)],
[(0,0,-1000),(0,0,1000)]
]
glBegin(GL_LINES)
for axis in axes:
for vertex in axis:
glColor3fv((1,1,1))
glVertex3fv(vertex)
glEnd()
pygame.init()
display = (400,400)
window = pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
gluPerspective(45, 1, 0.1, 50.0)
glTranslatef(0.0,0.0, -5)
glEnable(GL_CULL_FACE)
glEnable(GL_DEPTH_TEST)
glCullFace(GL_BACK)
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
degrees_per_second = 360./5.
degrees_per_millisecond = degrees_per_second / 1000.
milliseconds = clock.tick()
degrees = degrees_per_millisecond * milliseconds
glRotatef(degrees, 1,1,1)
glBegin(GL_TRIANGLES)
for face in faces:
color = shade(face,blues,light)
for vertex in face:
glColor3fv((color[0], color[1], color[2]))
glVertex3fv(vertex)
glEnd()
pygame.display.flip()
运行 python rotate_octahedron.py