Understanding function for generating sphere ...

Started by
0 comments, last by CulDeVu 9 years, 2 months ago
Hello,

So I have been looking around for a good function that generates a sphere for OpenGL and I found the function below.

I am not trying to simply cut and paste code in my project so I and trying to understand how it works.

It seems to me that the first three arguments are the x, y, and z coordinates of the center of the sphere and the fourth is the radius.

What I don't understand is the fifth argument; is that the number of sections or "poles" for the sphere being generated?

Thank you for your time:

Source: https://gist.github.com/stuartjmoore/1076642


void renderSphere(float cx, float cy, float cz, float r, int p)

{

    float theta1 = 0.0, theta2 = 0.0, theta3 = 0.0;

    float ex = 0.0f, ey = 0.0f, ez = 0.0f;

    float px = 0.0f, py = 0.0f, pz = 0.0f;

    GLfloat vertices[p*6+6], normals[p*6+6], texCoords[p*4+4];

    

    if( r < 0 )

        r = -r;

    

    if( p < 0 )

        p = -p;

    

    for(int i = 0; i < p/2; ++i)

    {

        theta1 = i * (M_PI*2) / p - M_PI_2;

        theta2 = (i + 1) * (M_PI*2) / p - M_PI_2;

        

        for(int j = 0; j <= p; ++j)

        {

            theta3 = j * (M_PI*2) / p;

            

            ex = cosf(theta2) * cosf(theta3);

            ey = sinf(theta2);

            ez = cosf(theta2) * sinf(theta3);

            px = cx + r * ex;

            py = cy + r * ey;

            pz = cz + r * ez;

            

            vertices[(6*j)+(0%6)] = px;

            vertices[(6*j)+(1%6)] = py;

            vertices[(6*j)+(2%6)] = pz;

            

            normals[(6*j)+(0%6)] = ex;

            normals[(6*j)+(1%6)] = ey;

            normals[(6*j)+(2%6)] = ez;

            

            texCoords[(4*j)+(0%4)] = -(j/(float)p);

            texCoords[(4*j)+(1%4)] = 2*(i+1)/(float)p;

            

            

            ex = cosf(theta1) * cosf(theta3);

            ey = sinf(theta1);

            ez = cosf(theta1) * sinf(theta3);

            px = cx + r * ex;

            py = cy + r * ey;

            pz = cz + r * ez;

            

            vertices[(6*j)+(3%6)] = px;

            vertices[(6*j)+(4%6)] = py;

            vertices[(6*j)+(5%6)] = pz;

            

            normals[(6*j)+(3%6)] = ex;

            normals[(6*j)+(4%6)] = ey;

            normals[(6*j)+(5%6)] = ez;

            

            texCoords[(4*j)+(2%4)] = -(j/(float)p);

            texCoords[(4*j)+(3%4)] = 2*i/(float)p;

        }

        glVertexPointer(3, GL_FLOAT, 0, vertices);

        glNormalPointer(GL_FLOAT, 0, normals);

        glTexCoordPointer(2, GL_FLOAT, 0, texCoords);

        glDrawArrays(GL_TRIANGLE_STRIP, 0, (p+1)*2);

    }

}

Advertisement

Read up on Spherical Coordinates: http://mathworld.wolfram.com/SphericalCoordinates.html

Specifically pay attention to the part where it talks about decomposing spherical angles to euclidean coordinates:


Inline48.gifInline49.gifInline50.gif

Inline51.gifInline52.gifInline53.gif

Inline54.gifInline55.gifInline56.gif

, and keep in mind that axises my be flipped, or might be viewed in a different frames of reference.

As for the p variable, notice that theta2 - theta1 = (M_PI*2)/p, or, in degrees, 360/p. So the variable p has to do with the number of slices around the sphere you make to generate a soup of flat polygons

Also, for the normals, notice that the surface normal of a sphere is exactly the position of the sphere, without taking into account any radius scaling or translation

Hope that sorta helped!

EDIT: text editor decided half my post didn't exist tongue.png

I'm sorry about any spelling or grammar mistakes or any undue brevity, as I'm most likely typing on my phone

"Hell, there's more evidence that we are just living in a frequency wave that flows in harmonic balance creating the universe and all its existence." ~ GDchat

This topic is closed to new replies.

Advertisement