Jump to content
  • Advertisement
Sign in to follow this  
pongping

formula to draw a sphere

This topic is 4424 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I'm trying to make my own functions to create mathematical structures such as spheres, circles, etc... But I'm stuck now. What is the math behind drawing a sphere? I managed to draw a circle. Here is the code:
void Circle(float radius, float center_x, float center_y, float center_z)
{
    float angle;
    float current_x;
    float current_y;
    float next_x;
    float next_y;

    current_x = center_x + radius;
    current_y = center_y;

    glBegin(GL_LINES);
    for (angle = 0;angle < 2*M_PI;angle+=0.001)
    {
        next_x = radius*cos(angle) + center_x;
        next_y = radius*sin(angle) + center_y;

        glVertex3f(current_x, current_y, center_z);
        glVertex3f(next_x, next_y, center_z);

        current_x = next_x;
        current_y = next_y;
    }
    glEnd();
}

The main formula is: x = radius*cos(angle) + center_x; y = radius*sin(angle) + center_y; But for the sphere I don't manage to get such a formula. We learned at school a formula for a sphere: (x - center_x)² + (y - center_y)² + (z - center_z)² = radius² Can I use it in my program? I hope you understand my problem. Thanks, Yoran

Share this post


Link to post
Share on other sites
Advertisement
Well, if you can draw a circle then you can draw a sphere. A unit circle is r(t)=(cos(t),sin(t)) a t varies from 0 to 2pi. A unit sphere is r(s,t)=(cos(t)*sin(s),sin(t)*sin(s),cos(s)) as t varies from 0 to 2pi and s varies from 0 to pi. If you hold s constant then you get a latitude line. If you hold t constant you get a longitude line. So to build a sphere you generate triangle strips between two latitude lines as you circle the globe.

Share this post


Link to post
Share on other sites
Mathworld has a pretty good description of the parametirc sphere equation. Your circle function uses the parametric equation for a circle with one param "angle". With a sphere you are generating a surface with two params. The link above gives equations to derive x, y, and z from the params.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!