我试图用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的系统,但是我可以看到一些问题:
您对}取整。这意味着面总是从一个完整的角度开始,但是当你添加未取整的delta值时,远边不能保证这样做。这可能是你的重叠原因。在
range
语句中的dphi
和{最好将范围值从
0 .. facets-1
开始,然后将这些指数乘以360/facets(或者纬度线为180)。这样可以避免舍入误差,例如:还有,你在其他地方转换成弧度吗?Python的默认trig函数采用弧度而不是度数。在
相关问题 更多 >
编程相关推荐