返回列表 发帖

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

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


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

直接贴代码了,备注:需要OpenGL模块
http://pypi.python.org/pypi/PyOpenGL
'''
    Torus_Vortex.py
    Code by 523066680, 2013-02-05
    paktcmail@gmail.com
    Imitate whirlpool effect
'''
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import gluLookAt,gluPerspective
from math import cos,sin,pi
from time import sleep
import sys
PI2=pi*2.0
WIN_X=300
WIN_Y=300
ANG=0.0
ANGX=0.0
theVortex=0
winid=0
def vortex(R=20.0,r=12.0):
    ''' Torus_Vortex '''
    nparts=50
    mparts=28
    detail=float(mparts)/float(nparts)
    tm=0.0
   
    for m in range(mparts):
        m=float(m)
        c=float(m%2)
        glColor3f(c*0.5,c*0.8,c*1.0)
        glBegin(GL_QUAD_STRIP)
        move=0.0
        for n in range(nparts+1):
            n=float(n)
            move+=detail
            x=r*cos(n/nparts*PI2)
            y=r*sin(n/nparts*PI2)
            for o in (0.0,1.0):
                tm=o+m+move;
                mx=(x+R)*cos(tm/mparts*PI2)
                mz=(x+R)*sin(tm/mparts*PI2)
                glVertex3f(mx,y,mz)
        glEnd()
def init():
    global theVortex
    glClearColor(0.0,0.0,0.0,0.0)
    glEnable(GL_DEPTH_TEST)
    theVortex=glGenLists(1)
    glNewList(theVortex,GL_COMPILE)
    vortex(18.0,12.0)
    glEndList()
   
def display():
    global theVortex
    glClearColor(0.0,0.0,0.0,0.0)  
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glPushMatrix()
    glRotatef(ANGX,1.0,0.0,0.0)
    glRotatef(ANG,0.0,-1.0,0.0)
    glCallList(theVortex)
    glPopMatrix()
    glutSwapBuffers()
def idle():
    global ANG
    ANG+=1.0
    sleep(0.01)
    glutPostRedisplay()
def reshape(Width,Height):
    far=30.0
    if (Width==Height):
        glViewport(0,0,Width,Height)
    elif (Width>Height):
        glViewport(0,0,Height,Height)
    else:
        glViewport(0,0,Width,Width)
   
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    #glFrustum(-10.0,10.0,-10.0,10.0,3.0,60.0)
    gluPerspective(80.0,1.0,1.0,80.0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    gluLookAt(0.0,0.0,far, 0.0,0.0,0.0, 0.0,1.0,far)
   
def hitkey(key,mousex,mousey):
    global winid,ANGX
    if (key=='q'):            
        glutDestroyWindow(winid)
        sys.exit()
    elif (key=='a'):
        ANGX+=1.0
def main():
    global WIN_X,WIN_Y,winid
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH)
    glutInitWindowSize(WIN_X,WIN_Y)
    glutInitWindowPosition(100,100)
    winid=glutCreateWindow("Vortex")
    init()
    glutDisplayFunc(display)
    glutIdleFunc(idle)
    glutReshapeFunc(reshape)
    glutKeyboardFunc(hitkey)
    glutMainLoop()
if __name__=="__main__":
    main()COPY
1

评分人数

[url=][/url]

按q退出,按a旋转X轴观察
[url=][/url]

TOP



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

TOP

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

回复 3# codegay


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

TOP

回复 4# 523066680

楼主,3.5.2 无法运行。是不是我姿势不对?
C:\Users\Yu2n>python -V
Python 3.5.2
C:\Users\Yu2n>R:
R:\>python -m pip install --upgrade pip
R:\>cd PyOpenGL-3.1.1a1\
R:\PyOpenGL-3.1.1a1>setup.py install
R:\PyOpenGL-3.1.1a1>cd \
R:\>Torus_Vortex.py
Traceback (most recent call last):
  File "R:\Torus_Vortex.py", line 116, in <module>
    main()
  File "R:\Torus_Vortex.py", line 102, in main
    glutInit(sys.argv)
  File "C:\Python35-32\lib\site-packages\pyopengl-3.1.1a1-py3.5.egg\OpenGL\GLUT\special.py", line 333, in glutInit
    _base_glutInit( ctypes.byref(count), holder )
  File "C:\Python35-32\lib\site-packages\pyopengl-3.1.1a1-py3.5.egg\OpenGL\platform\baseplatform.py", line 407, in __call__
    self.__name__, self.__name__,
OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit, check for bool(glutInit) before callingCOPY
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    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
[url=][/url]

TOP

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

回复 6# 523066680


    Python的版本之殇~

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




另:图文有差异。代码出来是蓝色的。想要显示为黑白间色的话,替换 34 行为:
glColor3f(c*1.0,c*1.0,c*1.0)COPY
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    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 命令行 安装记录:
D:\python353_32\Scripts\pip.exe install D:\python353_32\numpy-1.11.2rc1+mkl-cp35-cp35m-win32.whl
Processing d:\python353_32\numpy-1.11.2rc1+mkl-cp35-cp35m-win32.whl
Installing collected packages: numpy
Successfully installed numpy-1.11.2rc1+mkl
D:\python353_32\Scripts\pip.exe install D:\python353_32\PyOpenGL-3.1.1-cp35-cp35m-win32.whl
Processing d:\python353_32\pyopengl-3.1.1-cp35-cp35m-win32.whl
Installing collected packages: PyOpenGL
Successfully installed PyOpenGL-3.1.1
D:\python353_32\Scripts\pip.exe install D:\python353_32\PyOpenGL_accelerate-3.1.1-cp35-cp35m-win32.whl
Processing d:\python353_32\pyopengl_accelerate-3.1.1-cp35-cp35m-win32.whl
Installing collected packages: PyOpenGL-accelerate
Successfully installed PyOpenGL-accelerate-3.1.1COPY
以上完成后, 仍会遇到一个问题
Traceback (most recent call last):
  File "D:\ttt.py", line 116, in <module>
    main()
  File "D:\ttt.py", line 106, in main
    winid=glutCreateWindow("Vortex")
  File "D:\python353_32\lib\site-packages\OpenGL\GLUT\special.py", line 73, in glutCreateWindow
    return __glutCreateWindowWithExit(title, _exitfunc)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong typeCOPY
解决方案 参考: https://codeyarns.com/2012/04/27/pyopengl-glut-ctypes-error/

将第 106 行:
winid=glutCreateWindow("Vortex")COPY
改为
winid=glutCreateWindow( b"Vortex" )COPY
运行成功!
2

评分人数

TOP

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

回复 9# aa77dd@163.com

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

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

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

评分人数

[url=][/url]

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版本,找个时间
[url=][/url]

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

评分人数

[url=][/url]

TOP

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

TOP

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

回复 14# happy886rr


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

TOP

返回列表