当前位置: 首页 > article >正文

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 


http://www.kler.cn/news/364527.html

相关文章:

  • 一起搭WPF架构之LiveCharts.Wpf的简单了解与安装
  • jQuery快速填充非form数据
  • 数据结构编程实践20讲(Python版)—19字典树
  • C:浅谈数组指针的声明
  • ecmp观察
  • Docker 基础入门
  • JAVA 单例模式实验(头歌)
  • 【ROS GitHub使用】
  • ​8.13TB高清卫星影像更新(WGS84坐标投影)
  • 简单三步完成 Telegram 生态的 Web3 冷启动
  • rsync算法原理
  • Vue3 + Element Plus 封装文本超出长度显示省略号,鼠标移上悬浮展示全部内容的组件
  • 关于建造者模式(Builder Pattern)
  • 写出Windows操作系统内核的程序员,70多岁,还去办公室敲代码
  • Scala trait
  • 912.排序数组(计数排序)
  • QML列表视图 ListView的使用
  • Jenkins + GitLab + Docker实现自动化部署(Java项目)
  • 深入了解 Pandas 中的数据:Series 和 DataFrame 的使用指南
  • 借助栈逆置单链表
  • 基于YOLOv8深度学习的高密度人脸智能检测与统计系统【python源码+Pyqt5界面+数据集+训练代码】深度学习实战、目标检测
  • 【golang】学习文档整理
  • OpenFace安装教程及踩坑记录 (Ubuntu20.04—2024.10.24)
  • ElasticSearch全文检索和倒排索引
  • 杂项笔记
  • 100种算法【Python版】第8篇——群体智能优化算法之人工蜂群算法