如何在不使用 blender bpy 的情况下创建一个用 Python 连接两点的圆柱体?

How can I create a cylinder linking two points with Python withour using blender's bpy?

我想使用纯 Python API.

我想使用 Python OpenGL 或任何其他 GL 库而不是 blender 库来实现此目的

我的问题
我在 Whosebug 中搜索并找到了这个问题

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

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

所以 bpy 模块无法解决ide blender

我想要这样格式的函数

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

函数必须显示圆柱体

请顾ide我

2) also suggest how to clear the screen fully after drawing the cylinder

如果您想使用 modern OpenGL,那么您必须生成 Vertex Array Object and to write a Shader.

代码行数较少的解决方案是使用Legacy OpenGL and either gluCylinder or glutSolidCylinder

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

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


Is there any way to implement manually do pan , rotate , tilt ,move the viewport like we do in 3D modeling software using mouse (that alt+mmb )

参见: