【问题标题】:How can I create a cylinder linking two points with Python withour using blender's bpy?如何在不使用搅拌机 bpy 的情况下创建一个用 Python 连接两个点的圆柱体?
【发布时间】:2019-10-15 00:59:18
【问题描述】:

我想使用纯 Python API 定位一个圆柱体,使其一端的中心位于 (x0, y0, z0),而另一端的中心位于 (x1, y1, z1)。

我想使用 Python OpenGL 或任何其他 GL 库而不是 blender 库来实现这一点

我的问题
我在stackoverflow中搜索并发现了这个问题

https://blender.stackexchange.com/questions/5898/how-can-i-create-a-cylinder-linking-two-points-with-python

But it is using blender and I don't want it

我需要从命令行或pycharm ide运行python脚本

所以 bpy 模块不能在搅拌机之外工作

我想要这种格式的函数

cylinder_between(x1, y1, z1, x2, y2, z2, rad):

函数必须显示一个圆柱体

请指导我

2) 还建议画完后如何完全清屏 气缸

【问题讨论】:

    标签: python opengl graphics 3d pyopengl


    【解决方案1】:

    如果您想使用 现代 OpenGL,那么您必须生成一个 Vertex Array Object 并编写一个 Shader

    使用更少代码行的解决方案是使用Legacy OpenGLgluCylinderglutSolidCylinder

    使用这个(已弃用)工具集,可以绘制带有几行代码的圆柱体,例如:

    from OpenGL.GLUT import *
    from OpenGL.GLU import *
    from OpenGL.GL import *
    import math
    
    def cross(a, b):
        return [a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0]]
    
    def cylinder_between(x1, y1, z1, x2, y2, z2, rad):
        v = [x2-x1, y2-y1, z2-z1]
        height = math.sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2])
        axis = (1, 0, 0) if math.hypot(v[0], v[1]) < 0.001 else cross(v, (0, 0, 1))
        angle = -math.atan2(math.hypot(v[0], v[1]), v[2])*180/math.pi
        
        glPushMatrix()
        glTranslate(x1, y1, z1)
        glRotate(angle, *axis)
        glutSolidCylinder(rad, height, 32, 16)
        glPopMatrix()
    
    def draw():
    
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45, wnd_w/wnd_h, 0.1, 10)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        gluLookAt(0, -2, 0, 0, 0, 0, 0, 0, 1)
    
        glClearColor(0.5, 0.5, 0.5, 1.0)  
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    
        # glEnable(GL_DEPTH_TEST)
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) # causes wire frame
        glColor(1, 1, 0.5)
        cylinder_between(0.2, 0.4, -0.5, -0.2, -0.4, 0.5, 0.3)
    
        glutSwapBuffers()
        glutPostRedisplay()
    
    wnd_w, wnd_h = 300, 300
    glutInit()
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
    glutInitWindowSize(wnd_w, wnd_h)
    glutInitWindowPosition(50, 50)
    glutCreateWindow("cylinder")
    glutDisplayFunc(draw)
    glutMainLoop()
    

    另见Immediate mode and legacy OpenGL


    有没有什么方法可以手动实现平移、旋转、倾斜、移动视口,就像我们在 3D 建模软件中使用鼠标(即 alt+mmb)所做的那样

    见:

    【讨论】:

    • @LordCommander 我很惊讶,因为我已经在 Win10 x64 上使用 python 3.7 开发(并测试)了它。 PyOpenGL 我已经安装了Unofficial Windows Binaries for Python Extension Packages
    • @LordCommander 取决于视角和投影。分别见gluLookAtgluPerspective。试试gluPerspective(90, wnd_w/wnd_h, 0.1, 1000)gluLookAt(200, 200, 200, 200, 200, 0, 0, 1, 0)
    • @LordCommander 计算出现问题。我现在修好了。您只需绕轴旋转 1 圈,该轴由平面的法线向量给出,该平面由圆柱体的轴和世界的 z 轴定义。
    • “但是图像是颠倒的” 没有图像这取决于模型坐标一个观察方向。 *“有什么方法可以手动实现平移、旋转” - 这对于一个问题来说非常广泛,但是我添加到答案中的链接。
    • 我使用你提供的链接成功实现stackoverflow.com/a/54044085/9052139
    猜你喜欢
    • 2019-12-19
    • 2010-12-25
    • 2011-08-11
    • 1970-01-01
    • 2022-11-01
    • 2010-12-17
    • 1970-01-01
    • 2020-01-06
    • 2011-02-26
    相关资源
    最近更新 更多