如何在3D空间中绘制彼此面对的两个圆

时间:2013-03-22 01:45:46

标签: python opengl pyglet

我正在尝试使用Python的Pyglet库在3D中绘制网络。

我有表示蛋白质的球体,它们被绘制到位(给定x,y,z坐标),而不是将它们绘制在0,0,0并将它们平移到位置。我现在正在寻找用气缸连接它们。我的目标是通过计算以每个球体坐标为中心的顶点圆,然后将它们与四边形连接起来。

然而,我遇到了一些试图计算顶点的障碍,以便两个圆圈面对

到目前为止,这是我的代码:

class Cylinder(object):
    def __init__(self, start, end, radius, slices, batch, group=None):
        """
        :param start: The (x,y,z) coordinates of the cylinder's start point
        :type start: (float, float, float)
        :param end: The (x,y,z) coordinates of the cylinder's end point
        :type end: (float, float, float)
        :param radius: The radius of the cylinder
        :type radius: float
        :param slices: Number of sections to divide the cylinder into
        :type slices: int
        :param batch: The pyglet batch to add the cylinder to
        :type batch: pyglet.graphics.Batch
        :param group: The pyglet group to add the cylinder to
        :type group: int
        """
        self.start_x, self.start_y, self.start_z = start
        self.end_x, self.end_y, self.end_z = end

        self.num_vertices = 6 * slices
        self.num_faces = slices
        self.num_indexes = 4 * self.num_faces

        self.vertices = numpy.zeros(self.num_vertices, dtype=numpy.float)
        self.normals = numpy.zeros(self.num_vertices, dtype=numpy.float)
        self.indexes = numpy.zeros(self.num_indexes, dtype=numpy.uint32)

        step = 2 * pi / slices
        lat = 0
        lon = 0
        for i in xrange(slices):
            sin_lat = sin(lat)
            cos_lat = cos(lat)
            sin_lon = sin(lon)
            cos_lon = cos(lon)

            # Calculate vertex positions
            sx = self.start_x + radius * cos_lon * sin_lat
            sy = self.start_y + radius * sin_lon * sin_lat
            sz = self.start_z + radius * cos_lat
            ex = self.end_x + radius * cos_lon * sin_lat
            ey = self.end_y + radius * sin_lon * sin_lat
            ez = self.end_z + radius * cos_lat

            # Calculate vertex normals
            snx = cos_lon * sin_lat
            sny = sin_lon * sin_lat
            snz = cos_lat
            enx = cos_lon * sin_lat
            eny = sin_lon * sin_lat
            enz = cos_lat

            # Add vertex positions and normals to
            index = 6 * i
            self.vertices[index:index + 6] = (sx, sy, sz, ex, ey, ez)
            self.normals[index:index + 6] = (snx, sny, snz, enx, eny, enz)

            # Go to next vertex pair
            lat += step

        for i in xrange(slices):
            self.indexes[4 * i:4 * i + 4] = ((2 * i),
                                             (2 * i + 2) % (self.num_vertices // 3),
                                             (2 * i + 3) % (self.num_vertices // 3),
                                             (2 * i + 1))

        if batch:
            self.vertex_list = batch.add_indexed(len(self.vertices) // 3,
                                                 gl.GL_QUADS,
                                                 group,
                                                 self.indexes,
                                                 ('v3f/static', self.vertices),
                                                 ('n3f/static', self.normals))

    def delete(self):
        self.vertex_list.delete()

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

为什么你需要面对面的圈子?

要构建管,您需要以下算法:

  1. 计算从A到B的矢量AB并且它是垂直的(Norm);
  2. 按管半径缩放范数
  3. 开始添加顶点以构建管,其中段数为Seg:

    I := 0;
    for K := 0 to Seg-1 do
    begin
      TubeCos := Cos(K/Seg*2*pi);
      TubeSin := Sin(K/Seg*2*pi);
      Vert[I].Normal := (-Norm.X*TubeSin, TubeCos, -Norm.Y*TubeSin);
      Vert[I].Vert := (A.X-Norm.X*TubeRad*TubeSin, TubeRad*TubeCos, A.Y-Norm.Y*TubeRad*TubeSin);
      I++;
      Vert[I].Normal := (-Norm.X*TubeSin, TubeCos, -Norm.Y*TubeSin);
      Vert[I].VertB := (B.X-Norm.X*TubeRad*TubeSin, TubeRad*TubeCos, B.Y-Norm.Y*TubeRad*TubeSin);
      I++;
    end;
    
  4. 将顶点连接成三角形(顺序很重要) //在这个例子中 0-1-2,2-3-1,2-3-4,4-5-3,......