0

I'm attempting to draw a network in 3D using Python's Pyglet library.

I have spheres representing proteins and they are drawn in place (given x,y,z coordinates) rather than drawing them at 0,0,0 and translating them to position. I am now looking to connect them with cylinders. I aim to do this by calculating a circle of vertexes centred around each of the spheres coordinates, then connecting them with quads.

However, I've run into a bit of a roadblock trying to calculate the vertexes so that the two circles are facing each other.

Here is my code so far:

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

Any help would be greatly appreciated!

Josha Inglis
  • 1,018
  • 11
  • 23

1 Answers1

0

Why would you need circles facing each other?

To construct a tube you need the following algorithm:

  1. Calculate vector AB from A to B and it's perpendicular (Norm);
  2. Scale Norm by tube radius
  3. Start adding vertices to construct a tube, with segments count 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. Connect vertices into triangles (order is important) //In this example 0-1-2, 2-3-1, 2-3-4, 4-5-3, ...

Kromster
  • 7,181
  • 7
  • 63
  • 111