在OpenGL中近似一个球体

3
我正在尝试使用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

看起来有些四边形并不简单,即边缘交叉,但是改变顶点的顺序似乎没有任何影响。


1
顺便说一下,制作一个漂亮的球体(不会在极点附近出现丑陋的变形)的好方法是:从一个立方体开始,运行Catmull-Clark细分N次。该算法在Wikipedia上有介绍。 - Kos
1个回答

4

我这里没有一台可以同时运行Python和OpenGL的系统,但我还是能看出一些问题:

你在range语句中对dphidtheta进行了四舍五入。这意味着面片始终会从整数度数开始,但当你加上未经舍入的增量值时,远边不能保证也是整数度数。这可能是你重叠问题的原因。

最好将你的范围值从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的默认三角函数采用弧度而不是角度。

1
谢谢,我没有转换为弧度,我知道这是一些愚蠢的错误。 - eggbert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接