如何用Python的两个点连接我们的圆柱体?

2024-05-20 02:44:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用纯pythonapi定位一个圆柱体,使一端的中心位于(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

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

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

我想要一个这样格式的函数

^{pr2}$

圆柱体具有显示功能

请指引我

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


Tags: the定位pythonapiit中心howx1blender
1条回答
网友
1楼 · 发布于 2024-05-20 02:44:41

如果您想使用modernOpenGL,那么您必须生成一个Vertex Array Object并编写一个Shader。在

使用更少代码行的解决方案是使用Legacy OpenGL^{}或{a5}。在

使用此(不推荐使用的)工具集,可以绘制几行代码的圆柱体,例如:

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()


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 )

参见:

相关问题 更多 >