0

I wanted to create a function that would draw me a sphere. When I wrote the code, the system failed to show anything.

This code has an error somewhere:

#define TAU 6.2832    

//function to create a sphere

void spherel(double r, double x=0, double y=0, double z=0)
{
    short i=30,j=15;
    double theta = 0, tau = TAU/30, phi=0;
    double a[30][30][3]; 
    //this loop creates an array to save all the points
    while(i)
    {
        j=15;
        theta = 0;
        while(j)
        {

            a[i][j][0]=r*cos(theta)*cos(phi);
            a[i][j][1]=r*sin(theta);
            a[i][j][2]=r*cos(theta)*sin(phi);
            a[29-i][29-j][0]=a[i][j][0];
            a[29-i][29-j][1]=-a[i][j][1];
            a[29-i][29-j][2]=a[i][j][2];
            theta += tau;

            j--;
        }
        phi += tau;
        i--;
    }
    //drawing
    glBegin(GL_TRIANGLES);

    i=30;
    while(i)
    {
        j=30;
        while(j)
        {
            //all the vertex to make a rectangle
            glVertex3d(x+a[i][j][0],y+a[i][j][1],z+a[i][j][2]);
            glVertex3d(x+a[i][j+1][0],y+a[i][j+1][1],z+a[i][j+1][2]);
            glVertex3d(x+a[i+1][j][0],y+a[i+1][j][1],z+a[i+1][j][2]);
            glVertex3d(x+a[i+1][j+1][0],y+a[i+1][j+1][1],z+a[i+1][j+1][2]);
            glVertex3d(x+a[i][j+1][0],y+a[i][j+1][1],z+a[i][j+1][2]);
            glVertex3d(x+a[i+1][j][0],y+a[i+1][j][1],z+a[i+1][j][2]);

            j--;
        }
        i--;
    }
  
    glEnd();

}

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 4
    For complex tasks with many steps, it's good to start simpler and work your way up. 1) Are you able to draw a cube with OpenGL, by just manually specifying all 12 triangles? 2) Are you able to generate those 12 triangles using a loop? 3) Are you able to generate a cube with lots of triangles on each face intsead of just 2? 4) Are you able to do #3 but with all vertices normalized to be on the surface of a sphere? – David Grayson Jul 04 '22 at 20:03
  • Which value for r do you use? Which camera matrix? Is the sphere even in view? – BDL Jul 04 '22 at 20:51

0 Answers0