Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Understanding function for generating sphere ...

  1. #1
    Junior Member Regular Contributor
    Join Date
    Apr 2014
    Posts
    109

    Understanding function for generating sphere ...

    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

    Code :
    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);
     
        }
     
    }

  2. #2
    Senior Member OpenGL Lord
    Join Date
    May 2009
    Posts
    5,907
    You can't render an actual sphere in OpenGL (at least not directly); you can only render triangles. So you must render triangles that look like a sphere. Because a sphere is defined by mathematics, you can make a triangular sphere mesh as finely detailed as you like. Therefore, most sphere-generation algorithms have a parameter that lets you control how finely detailed the triangular mesh will be. The larger p is, the more triangles will be used, and therefore the more like a sphere it will look.

    Usually, this particular algorithm will have two such variables (one for the number of latitude segments, and one for the number of longitude ones). This one uses the same value for both.

  3. #3
    Junior Member Regular Contributor
    Join Date
    Apr 2014
    Posts
    109
    Thank you; I will play around with it; Really trying to understand the procedural generation side of things.

    Quote Originally Posted by Alfonse Reinheart View Post
    You can't render an actual sphere in OpenGL (at least not directly); you can only render triangles. So you must render triangles that look like a sphere. Because a sphere is defined by mathematics, you can make a triangular sphere mesh as finely detailed as you like. Therefore, most sphere-generation algorithms have a parameter that lets you control how finely detailed the triangular mesh will be. The larger p is, the more triangles will be used, and therefore the more like a sphere it will look.

    Usually, this particular algorithm will have two such variables (one for the number of latitude segments, and one for the number of longitude ones). This one uses the same value for both.

  4. #4
    Junior Member Regular Contributor
    Join Date
    Apr 2014
    Posts
    109
    One last question;

    I assume I am correct with my original summation that cx, cy, and cz coorespond with the world coordinates for the center of the sphere that would be created with this function.

    Is it best to simply use zeros (0) for that and then use a Model Matrix or translation?

    Or it really doesn't matter in the long run?

    Thank you.


    Quote Originally Posted by Alfonse Reinheart View Post
    You can't render an actual sphere in OpenGL (at least not directly); you can only render triangles. So you must render triangles that look like a sphere. Because a sphere is defined by mathematics, you can make a triangular sphere mesh as finely detailed as you like. Therefore, most sphere-generation algorithms have a parameter that lets you control how finely detailed the triangular mesh will be. The larger p is, the more triangles will be used, and therefore the more like a sphere it will look.

    Usually, this particular algorithm will have two such variables (one for the number of latitude segments, and one for the number of longitude ones). This one uses the same value for both.

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,404
    Quote Originally Posted by tmason View Post
    I assume I am correct with my original summation that cx, cy, and cz coorespond with the world coordinates for the center of the sphere that would be created with this function.
    Yes.

    Quote Originally Posted by tmason View Post
    Is it best to simply use zeros (0) for that and then use a Model Matrix or translation?
    Probably.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •