我正在尝试使用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
似乎有些四边形不简单,即边缘交叉,但改变顶点的顺序似乎没有任何区别。
答案 0 :(得分:4)
我这里没有可以同时运行Python和OpenGL的系统,但我还是可以看到一些问题:
您在dphi
语句中舍入dtheta
和range
。这意味着facet将始终以整个度数开始,但是当您添加未接地的delta值时,远边缘不能保证这样做。这可能是您重叠的原因。
最好让你的范围值从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的默认触发函数采用弧度而不是度数。