Sphere Zoom in OpenGL

I am trying to approximate a sphere using the instructions http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/sphere_cylinder/ , but it does not look like that at all. This is my code:

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

It seems that some of the quads are not simple, i.e. the edges intersect, but changing the order of the vertices does not seem to make any difference.

+3
source share
1 answer

I do not have a system that can run Python and OpenGL together, but I still see several problems:

range dphi dtheta. , , , , . .

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 , .

+4

Source: https://habr.com/ru/post/1786195/


All Articles