与顶点建立联系

Make connections with vertices

我有一个生成球体顶点的函数,但我不知道如何生成球体的 edges/connections。有没有人知道我如何才能做到这一点?

这就是我生成球体顶点的方式:

def uv_sphere_vertices(position, radius, stackcount : int, sectorcount : int):
    verts = []
    sectorstep = 2 * math.pi / sectorcount
    stackstep = math.pi / stackcount
    for num in range(stackcount):
        stackangle = math.pi / 2 - num * stackstep
        for num2 in range(sectorcount):
            sectorangle = num2 * sectorstep
            x = radius * math.cos(stackangle) * math.cos(sectorangle)
            y = radius * math.cos(stackangle) * math.sin(sectorangle)
            z = radius * math.sin(stackangle)
            verts.append([x,y,z])

    return verts

这是我目前拥有的:

我需要连接作为顶点列表中顶点的索引。这是连接需要的示例:

我试过他们在 http://www.songho.ca/opengl/gl_sphere.html 中做的方法,但那并没有奏效。我将其中的内容转换为:

    for num in range(stackcount):
        k1 = num * (sectorcount + 1)
        k2 = k1 + sectorcount + 1
        for num2 in range(sectorcount):
            if num != 0:
                edges.append([k1,k2,k1+1])
            if num != stackcount-1:
                edges.append([k1+1,k2,k2+1])

            k1 += 1
            k2 += 1

得到的结果非常缓慢,根本不是预期的结果:

我想出了一个解决办法。事实证明,我需要将范围值加一。这是我的最终代码:

def uv_sphere_vertices(position, radius, stackcount : int, sectorcount : int):
    verts = []
    edges = []
    sectorstep = 2 * math.pi / sectorcount
    stackstep = math.pi / stackcount
    for num in range(stackcount+1):
        stackangle = math.pi / 2 - num * stackstep
        for num2 in range(sectorcount+1):
            sectorangle = num2 * sectorstep
            x = radius * math.cos(stackangle) * math.cos(sectorangle)
            y = radius * math.cos(stackangle) * math.sin(sectorangle)
            z = radius * math.sin(stackangle)
            verts.append([x,y,z])

    for num in range(stackcount):
        cstack = num * (sectorcount + 1)
        nstack = cstack + sectorcount + 1
        for num2 in range(sectorcount):
            if num != 0:
                edges.append([cstack, nstack, cstack + 1])
            if num != stackcount - 1:
                edges.append([cstack + 1, nstack, nstack + 1])

            cstack += 1
            nstack += 1

    return verts,edges