Sphere formula

Hey,
does anyone know or maybe have a formula for drawing sphere?Like, I need quads vertices. Other stuff I bet I can work out.
I tried getting vertices data from sphere using blender, but I cant figure it out how.
Last edited on
isn't it something like
x^2 + y^2 + z^2 = r^2 ??
Sorry, I'm kinda weak at mathematics
I'm not great with the OpenGL stuff, but I do know math. If your vertex is defined something like this:
1
2
3
4
5
6
struct VERTEX
{
    double x; 
    double y;
    double z;
};


Then this function will give you a vector of VERTEXs on a sphere:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Input arguments
// in  - VERTEX center                 : defines the center of the sphere, all points will be offset from this
// in  - double r                      : defines the radius of the sphere
// out - vector<VERTEX> & spherePoints : vector containing the points of the sphere
void make_Sphere(VERTEX center, double r, std::vector<VERTEX> &spherePoints)
{
    const double PI = 3.141592653589793238462643383279502884197;
    spherePoints.clear();

    // Iterate through phi, theta then convert r,theta,phi to  XYZ
    for (double phi = 0.; phi < 2*PI; phi += PI/10.) // Azimuth [0, 2PI]
    {
        for (double theta = 0.; theta < PI; theta += PI/10.) // Elevation [0, PI]
        {
            VERTEX point;
            point.x = r * cos(phi) * sin(theta) + center.x;
            point.y = r * sin(phi) * sin(theta) + center.y;
            point.z = r            * cos(theta) + center.z;
            spherePoints.push_back(point);        
        }
    }
    return;
}


Here I linearly just go through all of angles for phi and theta. You'll find that points at the top and bottom will be super concentrated so there is certainly room to make the iterators more efficient to avoid this.
Last edited on
I just read about quad vertices.

Just replace VERTEX with Vector3 everywhere in that function and I think you'll have what you are looking for.
Thats just awesome. Much appreciated. One last question: are the quads being described clockwise or anticlockwise?
From increasing theta then increasing phi. When viewed from the top it goes counterclockwise I believe.
Topic archived. No new replies allowed.