[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创代码] [PyOpenGL]仿Vortex效果

本帖最后由 523066680 于 2016-9-24 15:41 编辑


这是一张网络图片,琢磨出了对应公式

直接贴代码了,备注:需要OpenGL模块
http://pypi.python.org/pypi/PyOpenGL
  1. '''
  2.     Torus_Vortex.py
  3.     Code by 523066680, 2013-02-05
  4.     paktcmail@gmail.com
  5.     Imitate whirlpool effect
  6. '''
  7. from OpenGL.GL import *
  8. from OpenGL.GLUT import *
  9. from OpenGL.GLU import gluLookAt,gluPerspective
  10. from math import cos,sin,pi
  11. from time import sleep
  12. import sys
  13. PI2=pi*2.0
  14. WIN_X=300
  15. WIN_Y=300
  16. ANG=0.0
  17. ANGX=0.0
  18. theVortex=0
  19. winid=0
  20. def vortex(R=20.0,r=12.0):
  21.     ''' Torus_Vortex '''
  22.     nparts=50
  23.     mparts=28
  24.     detail=float(mparts)/float(nparts)
  25.     tm=0.0
  26.    
  27.     for m in range(mparts):
  28.         m=float(m)
  29.         c=float(m%2)
  30.         glColor3f(c*0.5,c*0.8,c*1.0)
  31.         glBegin(GL_QUAD_STRIP)
  32.         move=0.0
  33.         for n in range(nparts+1):
  34.             n=float(n)
  35.             move+=detail
  36.             x=r*cos(n/nparts*PI2)
  37.             y=r*sin(n/nparts*PI2)
  38.             for o in (0.0,1.0):
  39.                 tm=o+m+move;
  40.                 mx=(x+R)*cos(tm/mparts*PI2)
  41.                 mz=(x+R)*sin(tm/mparts*PI2)
  42.                 glVertex3f(mx,y,mz)
  43.         glEnd()
  44. def init():
  45.     global theVortex
  46.     glClearColor(0.0,0.0,0.0,0.0)
  47.     glEnable(GL_DEPTH_TEST)
  48.     theVortex=glGenLists(1)
  49.     glNewList(theVortex,GL_COMPILE)
  50.     vortex(18.0,12.0)
  51.     glEndList()
  52.    
  53. def display():
  54.     global theVortex
  55.     glClearColor(0.0,0.0,0.0,0.0)  
  56.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
  57.     glPushMatrix()
  58.     glRotatef(ANGX,1.0,0.0,0.0)
  59.     glRotatef(ANG,0.0,-1.0,0.0)
  60.     glCallList(theVortex)
  61.     glPopMatrix()
  62.     glutSwapBuffers()
  63. def idle():
  64.     global ANG
  65.     ANG+=1.0
  66.     sleep(0.01)
  67.     glutPostRedisplay()
  68. def reshape(Width,Height):
  69.     far=30.0
  70.     if (Width==Height):
  71.         glViewport(0,0,Width,Height)
  72.     elif (Width>Height):
  73.         glViewport(0,0,Height,Height)
  74.     else:
  75.         glViewport(0,0,Width,Width)
  76.    
  77.     glMatrixMode(GL_PROJECTION)
  78.     glLoadIdentity()
  79.     #glFrustum(-10.0,10.0,-10.0,10.0,3.0,60.0)
  80.     gluPerspective(80.0,1.0,1.0,80.0)
  81.     glMatrixMode(GL_MODELVIEW)
  82.     glLoadIdentity()
  83.     gluLookAt(0.0,0.0,far, 0.0,0.0,0.0, 0.0,1.0,far)
  84.    
  85. def hitkey(key,mousex,mousey):
  86.     global winid,ANGX
  87.     if (key=='q'):            
  88.         glutDestroyWindow(winid)
  89.         sys.exit()
  90.     elif (key=='a'):
  91.         ANGX+=1.0
  92. def main():
  93.     global WIN_X,WIN_Y,winid
  94.     glutInit(sys.argv)
  95.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH)
  96.     glutInitWindowSize(WIN_X,WIN_Y)
  97.     glutInitWindowPosition(100,100)
  98.     winid=glutCreateWindow("Vortex")
  99.     init()
  100.     glutDisplayFunc(display)
  101.     glutIdleFunc(idle)
  102.     glutReshapeFunc(reshape)
  103.     glutKeyboardFunc(hitkey)
  104.     glutMainLoop()
  105. if __name__=="__main__":
  106.     main()
复制代码
1

评分人数

按q退出,按a旋转X轴观察

TOP



可惜报错了
python3
  1.     self.__name__, self.__name__,
  2. OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit, check for bool(glutInit) before calling
  3. >>>
复制代码
这个图让我想发廊门前的一个装饰。
去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

本帖最后由 523066680 于 2016-9-24 16:26 编辑

回复 3# codegay


    噗。发廊……  不知道是不是版本区别,提示undefined function的话,可能还是缺少了什么模块

TOP

回复 4# 523066680

楼主,3.5.2 无法运行。是不是我姿势不对?
  1. C:\Users\Yu2n>python -V
  2. Python 3.5.2
  3. C:\Users\Yu2n>R:
  4. R:\>python -m pip install --upgrade pip
  5. R:\>cd PyOpenGL-3.1.1a1\
  6. R:\PyOpenGL-3.1.1a1>setup.py install
  7. R:\PyOpenGL-3.1.1a1>cd \
  8. R:\>Torus_Vortex.py
  9. Traceback (most recent call last):
  10.   File "R:\Torus_Vortex.py", line 116, in <module>
  11.     main()
  12.   File "R:\Torus_Vortex.py", line 102, in main
  13.     glutInit(sys.argv)
  14.   File "C:\Python35-32\lib\site-packages\pyopengl-3.1.1a1-py3.5.egg\OpenGL\GLUT\special.py", line 333, in glutInit
  15.     _base_glutInit( ctypes.byref(count), holder )
  16.   File "C:\Python35-32\lib\site-packages\pyopengl-3.1.1a1-py3.5.egg\OpenGL\platform\baseplatform.py", line 407, in __call__
  17.     self.__name__, self.__name__,
  18. OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit, check for bool(glutInit) before calling
复制代码
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

本帖最后由 523066680 于 2016-9-25 12:45 编辑

回复 5# yu2n

原本测试是python2.7(重新试了一次2.7版本运行没问题),
   看到两位执行都有错误提示就去装了python3.5,确实出错。找了两个帖子,都是说安装没问题,缺少相应的dll库,复制粘贴到运行目录即可,尝试了都不见效,遂放弃……

[CSDN]Python3.5.1与pyopengl3.1.0环境配置

[StackOverflow]Attempt to call an undefined function glutInit

TOP

本帖最后由 yu2n 于 2016-9-25 21:17 编辑

回复 6# 523066680


    Python的版本之殇~

果然一定要 Python27 才行。OpenGL 使用 PyOpenGL-3.1.0.win32.exe 即可。




另:图文有差异。代码出来是蓝色的。想要显示为黑白间色的话,替换 34 行为:
  1. glColor3f(c*1.0,c*1.0,c*1.0)
复制代码
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

回复 7# yu2n


不是, 有办法的

TOP

本帖最后由 aa77dd@163.com 于 2016-9-25 23:05 编辑

回复 6# 523066680




以下过程于 中文 64位 Win 7 成功:

在 python-3.5.2 环境安装 非官方的 PyOpenGL 包,

由于此学校网站外链带宽有限, 我把 已经下载好的文件整体打包, 欢迎试用
http://pan.baidu.com/s/1jIFbWu6


来源: 加州大学欧文分校 LFD 实验室 Christoph Gohlke
http://www.lfd.uci.edu/~gohlke/pythonlibs/


下载并安装
Visual C++ 2015 (x64 and x86 for CPython 3.5) redistributable packages
https://download.microsoft.com/d ... 8/vc_redist.x86.exe
https://download.microsoft.com/d ... 8/vc_redist.x64.exe


python-3.5.2 官方安装文件:
https://www.python.org/ftp/python/3.5.2/python-3.5.2.exe


非官方包:
NumPy, a fundamental package needed for scientific computing with Python.
Numpy+MKL is linked to the Intel? Math Kernel Library and includes required DLLs in the numpy.core directory.
科学计算基础包, Intel 数学核心库
http://www.lfd.uci.edu/~gohlke/p ... p35-cp35m-win32.whl

PyOpenGL:
http://www.lfd.uci.edu/~gohlke/p ... p35-cp35m-win32.whl

PyOpenGL 加速[可选, 推荐安装]
http://www.lfd.uci.edu/~gohlke/p ... p35-cp35m-win32.whl

将 python 3.5 安装到 D:\python353_32 目录,
将 3 个 .whl 文件都拷贝到 D:\python353_32 目录[只是为了安装文件, 不是必须的]


非官方包 CMD 命令行 安装记录:
  1. D:\python353_32\Scripts\pip.exe install D:\python353_32\numpy-1.11.2rc1+mkl-cp35-cp35m-win32.whl
  2. Processing d:\python353_32\numpy-1.11.2rc1+mkl-cp35-cp35m-win32.whl
  3. Installing collected packages: numpy
  4. Successfully installed numpy-1.11.2rc1+mkl
  5. D:\python353_32\Scripts\pip.exe install D:\python353_32\PyOpenGL-3.1.1-cp35-cp35m-win32.whl
  6. Processing d:\python353_32\pyopengl-3.1.1-cp35-cp35m-win32.whl
  7. Installing collected packages: PyOpenGL
  8. Successfully installed PyOpenGL-3.1.1
  9. D:\python353_32\Scripts\pip.exe install D:\python353_32\PyOpenGL_accelerate-3.1.1-cp35-cp35m-win32.whl
  10. Processing d:\python353_32\pyopengl_accelerate-3.1.1-cp35-cp35m-win32.whl
  11. Installing collected packages: PyOpenGL-accelerate
  12. Successfully installed PyOpenGL-accelerate-3.1.1
复制代码
以上完成后, 仍会遇到一个问题
  1. Traceback (most recent call last):
  2.   File "D:\ttt.py", line 116, in <module>
  3.     main()
  4.   File "D:\ttt.py", line 106, in main
  5.     winid=glutCreateWindow("Vortex")
  6.   File "D:\python353_32\lib\site-packages\OpenGL\GLUT\special.py", line 73, in glutCreateWindow
  7.     return __glutCreateWindowWithExit(title, _exitfunc)
  8. ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type
复制代码
解决方案 参考: https://codeyarns.com/2012/04/27/pyopengl-glut-ctypes-error/

将第 106 行:
  1. winid=glutCreateWindow("Vortex")
复制代码
改为
  1. winid=glutCreateWindow( b"Vortex" )
复制代码
运行成功!
2

评分人数

TOP

本帖最后由 523066680 于 2016-9-26 00:38 编辑

回复 9# aa77dd@163.com

    相当佩服。似乎在搜索过程中也看到了一个非官方的安装包链接,但是没有去尝试。
在可以运行后,推荐安装 pyopengl-demo(主要是示例代码),找到目录并运行里面的Demo看看效果。

用Python折腾过OpenGL, 用Perl折腾过OpenGL,最后还是回到了C/C++,目前在学。
稍后对里面的函数做一些补充说明

该程序的C语言版以及环境配置
1

评分人数

TOP

本帖最后由 happy886rr 于 2016-9-26 08:53 编辑

回复 10# 523066680
C文件32位下编译出来了,但是不转,只显示一张静态的。可能是提供的freeglut.dll版本问题,后来发现是usleep的问题,直接用sleep(10)替换了。这个图很有规律,用一个闭环偏移出其他闭环,然后还有固定的重复周期。不知贝兹曲线是否有C版本?
外链图

TOP

本帖最后由 523066680 于 2016-9-26 21:52 编辑

回复 11# happy886rr


显示原理看两张外部观察图(该程序按 a 键可以旋转x轴,切换观察角度)
先一圈圈地组成一个圆环


然后再把每个圈绘制的时候的公式作修改 - 位置偏移,使其最终回到原点合并


最后把观察点放到圆环的内部,就是1楼的效果了。因为没开启光照和阴影计算,所以还少一个阴影效果。
要使图像变得更加圆润平滑,将
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );
改为
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE );
开启多重采样(一种抗锯齿方法),开启以后可以说是效果感人……

贝塞尔曲线可以改C版本,找个时间

TOP

本帖最后由 523066680 于 2016-9-27 13:24 编辑

回复 11# happy886rr

Bezier曲线C+OpenGL (固定管线) 版本
    http://www.code-by.org/viewtopic.php?f=43&t=109

2楼写了设计思路
1

评分人数

TOP

回复 13# 523066680
相当给力啊,兄这次的C版更加平滑,灵动。我把颜色换成霓虹灯了O(∩_∩)O~。

TOP

本帖最后由 523066680 于 2016-9-27 16:12 编辑

回复 14# happy886rr


    话说为啥不来我论坛聊这个~ (其实主要是做个补集,所以也没有开批处理版块(定位不同))

TOP

返回列表