0

The below given code is an algorithm to draw sphere in opengl without using glutsphere.The arguments are same as glutsphere(). But I am unable to understand this code. can anyone please explain me this code. How is it working and drawing sphere?

def draw(radius,lats,longs) :
    for i in range(0,lats):
        lat0 = math.pi*(-0.5 + float((i-1))/lats)
        z0   = math.sin(lat0)
        zr0 = math.cos(lat0)

        lat1 = math.pi*(-0.5 + float(i)/lats)
        z1 = math.sin(lat1)
        zr1 = math.cos(lat1)

        glColor3f(0.0,0.0,1.0)
        glBegin(GL_QUAD_STRIP)
        for j in range(0,longs):
            lng = 2*math.pi*float((j-1))/longs
            x=  math.cos(lng)
            y = math.sin(lng)

            glNormal3f(x * zr0, y * zr0, z0)
            glVertex3f(x * zr0, y * zr0, z0)
            glNormal3f(x * zr1, y * zr1, z1)
            glVertex3f(x * zr1, y * zr1, z1)
        glEnd()
user1118321
  • 25,567
  • 4
  • 55
  • 86
  • if your making a new piece of code the you may want to avoid using deprecated methods. Anyway its just a simple revolve. To visualize this draw each quad separately with some delay. – joojaa Apr 25 '14 at 11:02
  • This code computes the points on the sphere based on spherical coordinate system. See: http://en.wikipedia.org/wiki/Spherical_coordinate_system#Coordinate_system_conversions The rest uses these points to draw quad strips in "belt"-like-shapes to form a sphere (That's why the second loop iterates for the given longitudinal subdivision factor). – meandbug Apr 25 '14 at 11:39

0 Answers0