2011-01-18 16 views
3

http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/sphere_cylinder/の手順で球を近似しようとしていますが、まったく見えません。これは私のコードです:OpenGLの球の近似

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

Btw、美しい球体(極の近くに醜い変形がない)を作る良い方法:立方体で始まり、Catmull-Clark細分割をN回実行する。アルゴリズムは[Wikipedia](http://en.wikipedia.org/wiki/Catmull-clark_subdivision)にあります。 – Kos

答えて

4

私は一緒にPythonとOpenGLを実行することができ、ここでシステムを持っていないが、私はとにかくいくつかの問題を見ることができます:

あなたはrange文でdphidthetaを丸めています。これは、ファセットが常に完全な程度で開始することを意味しますが、丸められていないデルタ値を追加すると、遠端ではそれが保証されません。これがあなたの重複の原因です。

範囲の値を0 .. facets-1から取得し、これらのインデックスに360 /ファセット(または緯度線の場合は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関数は度数ではなく、ラジアンをとります。

+1

ありがとう、私はラジアンに変換していない、私はそれがそれのような何かばかだと分かっていた。 – eggbert