python-在OpenGL中逼近球体

我正在尝试使用http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/sphere_cylinder/处的指令近似一个球体,但是看起来并不正确.这是我的代码:

def draw_sphere(facets, radius=100):
    """approximate a sphere using a certain number of facets"""

    dtheta = 180.0 / facets
    dphi = 360.0 / facets

    global sphere_list
    sphere_list = glGenLists(2)
    glNewList(sphere_list, GL_COMPILE)

    glBegin(GL_QUADS)

    for theta in range(-90, 90, int(dtheta)):
        for phi in range(0, 360, int(dphi)):
            print theta, phi
            a1 = theta, phi
            a2 = theta + dtheta, phi
            a3 = theta + dtheta, phi + dphi
            a4 = theta, phi + dphi

            angles = [a1, a2, a3, a4]

            print 'angles: %s' % (angles)


            glColor4f(theta/360.,phi/360.,1,0.5)

            for angle in angles:
                x, y, z = angle_to_coords(angle[0], angle[1], radius)
                print 'coords: %s,%s,%s' % (x, y, z)
                glVertex3f(x, y, z)



    glEnd()

    glEndList()


def angle_to_coords(theta, phi, radius):  
    """return coordinates of point on sphere given angles and radius"""

    x = cos(theta) * cos(phi)
    y = cos(theta) * sin(phi)
    z = sin(theta)

    return x * radius, y * radius, z * radius

看起来有些四边形并不简单,即边线交叉,但是更改顶点的顺序似乎没有任何区别.

解决方法:

我这里没有一个可以同时运行Python和OpenGL的系统,但是我仍然可以看到一些问题:

您要在range语句中四舍五入dphi和dtheta.这意味着这些构面将始终以一个整体开始,但是随后当您添加未取整的增量值时,就不能保证远处的边缘.这是您重叠的可能原因.

最好使范围值从0 .. facets-1开始,然后将这些索引乘以360 / facets(对于纬度线为180).这样可以避免舍入错误,例如:

dtheta = 180.0 / facets
dphi = 360.0 / facets

for y in range(facets):
    theta = y * dtheta - 90
    for x in range(facets):
        phi = x * dphi
        ...

另外,您是否要在其他地方转换成弧度? Python的默认Trig函数采用弧度而不是度.

上一篇:CodeGo.net>如何绘制石原变换(圆圈没有交集的圆圈)?


下一篇:将弧的3d点拟合为圆(Python中的回归)