It took a while, but doesn’t it look beautiful... in a mathematical way.

Comments Widget

A sphere x2 + y2 + z2 = a2 can be represented as


r (u,v) = a cos v cos u i + a cos v sin u j + a sin v k


for 0 ≤ u ≤ 2π and -π/2 ≤ u ≤ π/2

x =a cos v cos u    y = a cos v sin u    z = a sin v

int createSphere (GLfloat spherePoints[], GLfloat fRadius, GLfloat step)

{

    int points = 0;


    GLfloat uStep = DEGREES_TO_RADIANS (step);

    GLfloat vStep = uStep;


    for (GLfloat u = 0.0f; u <= (2 * M_PI); u += uStep) {

        for (GLfloat v = -M_PI_2; v <= M_PI_2; v += vStep) {


            // Point as per equation

            points++;

            spherePoints[(points - 1) * 3] = fRadius * cosf(v) * cosf(u);                        // x

            spherePoints[((points - 1) * 3) + 1] = fRadius * cosf(v) * sinf(u);                  // y

            spherePoints[((points - 1) * 3) + 2] = fRadius * sinf(v);                            // z


            // Next point around

            points++;

            spherePoints[(points - 1) * 3] = fRadius * cosf(v) * cosf(u + uStep);                // x

            spherePoints[((points - 1) * 3) + 1] = fRadius * cosf(v) * sinf(u + uStep);          // y

            spherePoints[((points - 1) * 3) + 2] = fRadius * sinf(v);                            // z


            // Next point up

            points++;

            spherePoints[(points - 1) * 3] = fRadius * cosf(v + vStep) * cosf(u);                // x

            spherePoints[((points - 1) * 3) + 1] = fRadius * cosf(v + vStep) * sinf(u);          // y

            spherePoints[((points - 1) * 3) + 2] = fRadius * sinf(v + vStep);                    // z


            // Next point up and around

            points++;

            spherePoints[(points - 1) * 3] = fRadius * cosf(v + vStep) * cosf(u + uStep);        // x

            spherePoints[((points - 1) * 3) + 1] = fRadius * cosf(v + vStep) * sinf(u + uStep);  // y

            spherePoints[((points - 1) * 3) + 2] = fRadius * sinf(v + vStep);                    // z


        }

    }


    return points;

}