Drawing a sphere in opengl C++

I want to draw a 3D cube using openGl and transform it into a sphere using c++. I have written the code. But the problem is the cube is ok but when I want to transform it into a sphere, the whole thing disappears.I have used two keys. when I press the up key it will draw a cube and when I will press the down key, it will become a sphere.

My code is:

#include<math.h>
#include <GL/glut.h>
float ver2[8][3]; 
int z = 0;

float ver[8][3] =
{
{ -1.0,-1.0,1.0 },
{ -1.0,1.0,1.0 },
{ 1.0,1.0,1.0 },
{ 1.0,-1.0,1.0 },
{ -1.0,-1.0,-1.0 },
{ -1.0,1.0,-1.0 },
{ 1.0,1.0,-1.0 },
{ 1.0,-1.0,-1.0 },
}; 

void initialize()
{
for (int i = 0; i < 8; i++)
{
	for (int j = 0; j < 3; j++)
	{
		if (j == 0)
		{
			ver2[i][j]= ver[i][j]* sqrtf(1.0 - (ver[i][j+1] * ver[i][j+1] / 2.0) - (ver[i][j+2] * ver[i][j+2] / 2.0) + (ver[i][j+1] * ver[i][j+1] * ver[i][j+2] * ver[i][j+2] / 3.0));
		}
		if (j == 1)
		{
			ver2[i][j] = ver[i][j] * sqrtf(1.0 - (ver[i][j+1] * ver[i][j+1] / 2.0) - (ver[i][j-1] * ver[i][j-1] / 2.0) + (ver[i][j+1] * ver[i][j+1] * ver[i][j-1] * ver[i][j-1] / 3.0));
		}
		if (j == 2)
		{
			ver2[i][j]= ver[i][j] * sqrtf(1.0 - (ver[i][j-2] * ver[i][j-2] / 2.0) - (ver[i][j-1] * ver[i][j-1] / 2.0) + (ver[i][j-2] * ver[i][j-2] * ver[i][j-1] * ver[i][j-1] / 3.0));
		}
		
	}
 }

}


GLfloat color[8][3] =
{
{ 0.0,0.0,0.0 },
{ 1.0,0.0,0.0 },
{ 1.0,1.0,0.0 },
{ 0.0,1.0,0.0 },
{ 0.0,0.0,1.0 },
{ 1.0,0.0,1.0 },
{ 1.0,1.0,1.0 },
{ 0.0,1.0,1.0 },
};

void quad(int a, int b, int c, int d)
{
glBegin(GL_QUADS);
glColor3fv(color[a]);
glVertex3fv(ver[a]);

glColor3fv(color[b]);
glVertex3fv(ver[b]);

glColor3fv(color[c]);
glVertex3fv(ver[c]);

glColor3fv(color[d]);
glVertex3fv(ver[d]);
glEnd();
} 

void quad2(int a, int b, int c, int d)
{
glBegin(GL_QUADS);
glColor3fv(color[a]);
glVertex3fv(ver2[a]);

glColor3fv(color[b]);
glVertex3fv(ver2[b]);

glColor3fv(color[c]);
glVertex3fv(ver2[c]);

glColor3fv(color[d]);
glVertex3fv(ver2[d]);
glEnd();
}

void colorcube()
{
quad(0, 3, 2, 1);
quad(2, 3, 7, 6);
quad(0, 4, 7, 3);
quad(1, 2, 6, 5);
quad(4, 5, 6, 7);
quad(0, 1, 5, 4);
} 

void colorcube2()
{
quad2(0, 3, 2, 1);
quad2(2, 3, 7, 6);
quad2(0, 4, 7, 3);
quad2(1, 2, 6, 5);
quad2(4, 5, 6, 7);
quad2(0, 1, 5, 4);
}

double rotate_y = 0;
double rotate_x = 0;
void specialKeys(int key, int x, int y)
 {

 if (key == GLUT_KEY_UP)
	z = 0;
else if (key == GLUT_KEY_DOWN)
	z = 1;
glutPostRedisplay();
}

void display()
{
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
int w = glutGet(GLUT_WINDOW_WIDTH);
int h = glutGet(GLUT_WINDOW_HEIGHT);
gluPerspective(60, w / h, 0.1, 100);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt
(
	3, 3, 3,
	0, 0, 0,
	0, 0, 1
);

glRotatef(rotate_x, 1.0, 0.0, 0.0);
glRotatef(rotate_y, 0.0, 1.0, 0.0);
if (z == 0)
{
	colorcube();
}
if (z == 1)
{
	colorcube2();
}

glutSwapBuffers();
}

int main(int argc, char **argv)
{
initialize();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutCreateWindow("GLUT");
glutDisplayFunc(display);
glutSpecialFunc(specialKeys);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}

Is there anything wrong in the logic? How will I correct it?

Don’t mind me… I don’t have an answer for you… just parsing your code so people can read it.

#include<math.h>
#include <GL/glut.h>

float ver2[8][3];
int z = 0;

float ver[8][3] =
{
{ -1.0,-1.0,1.0 },
{ -1.0,1.0,1.0 },
{ 1.0,1.0,1.0 },
{ 1.0,-1.0,1.0 },
{ -1.0,-1.0,-1.0 },
{ -1.0,1.0,-1.0 },
{ 1.0,1.0,-1.0 },
{ 1.0,-1.0,-1.0 },
};

void initialize()
{
for (int i = 0; i < 8; i++)
{
[INDENT]for (int j = 0; j < 3; j++)
{
[INDENT]if (j == 0)
{
[INDENT]ver2[i][j]= ver[i][j]* sqrtf(1.0 - (ver[i][j+1] * ver[i][j+1] / 2.0) - (ver[i][j+2] * ver[i][j+2] / 2.0) + (ver[i][j+1] * ver[i][j+1] * ver[i][j+2] * ver[i][j+2] / 3.0));
}
if (j == 1)
{
ver2[i][j] = ver[i][j] * sqrtf(1.0 - (ver[i][j+1] * ver[i][j+1] / 2.0) - (ver[i][j-1] * ver[i][j-1] / 2.0) + (ver[i][j+1] * ver[i][j+1] * ver[i][j-1] * ver[i][j-1] / 3.0));
}
if (j == 2)
{
ver2[i][j]= ver[i][j] * sqrtf(1.0 - (ver[i][j-2] * ver[i][j-2] / 2.0) - (ver[i][j-1] * ver[i][j-1] / 2.0) + (ver[i][j-2] * ver[i][j-2] * ver[i][j-1] * ver[i][j-1] / 3.0));
}[/INDENT]
}[/INDENT]
}[/INDENT]
}

GLfloat color[8][3] =
{
{ 0.0,0.0,0.0 },
{ 1.0,0.0,0.0 },
{ 1.0,1.0,0.0 },
{ 0.0,1.0,0.0 },
{ 0.0,0.0,1.0 },
{ 1.0,0.0,1.0 },
{ 1.0,1.0,1.0 },
{ 0.0,1.0,1.0 },
};

void quad(int a, int b, int c, int d)
{
glBegin(GL_QUADS);
[INDENT]glColor3fv(color[a]);
glVertex3fv(ver[a]);

glColor3fv(color[b]);
glVertex3fv(ver[b]);

glColor3fv(color[c]);
glVertex3fv(ver[c]);

glColor3fv(color[d]);
glVertex3fv(ver[d]);
glEnd();[/INDENT]
}

void quad2(int a, int b, int c, int d)
{
glBegin(GL_QUADS);
[INDENT]glColor3fv(color[a]);
glVertex3fv(ver2[a]);

glColor3fv(color[b]);
glVertex3fv(ver2[b]);

glColor3fv(color[c]);
glVertex3fv(ver2[c]);

glColor3fv(color[d]);
glVertex3fv(ver2[d]);
glEnd();[/INDENT]
}

void colorcube()
{
quad(0, 3, 2, 1);
quad(2, 3, 7, 6);
quad(0, 4, 7, 3);
quad(1, 2, 6, 5);
quad(4, 5, 6, 7);
quad(0, 1, 5, 4);
}

void colorcube2()
{
quad2(0, 3, 2, 1);
quad2(2, 3, 7, 6);
quad2(0, 4, 7, 3);
quad2(1, 2, 6, 5);
quad2(4, 5, 6, 7);
quad2(0, 1, 5, 4);
}

double rotate_y = 0;
double rotate_x = 0;

void specialKeys(int key, int x, int y)
{

if (key == GLUT_KEY_UP)
[INDENT]z = 0;
else if (key == GLUT_KEY_DOWN)
z = 1;
glutPostRedisplay();[/INDENT]
}

void display()
{
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
int w = glutGet(GLUT_WINDOW_WIDTH);
int h = glutGet(GLUT_WINDOW_HEIGHT);
gluPerspective(60, w / h, 0.1, 100);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt
(
3, 3, 3,
0, 0, 0,
0, 0, 1
);

glRotatef(rotate_x, 1.0, 0.0, 0.0);
glRotatef(rotate_y, 0.0, 1.0, 0.0);
if (z == 0)
{
[INDENT]colorcube();
}
if (z == 1)
{
colorcube2();
}

glutSwapBuffers();[/INDENT]
}

int main(int argc, char **argv)
{
initialize();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutCreateWindow(“GLUT”);
glutDisplayFunc(display);
glutSpecialFunc(specialKeys);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}

I’ve got good news and bad news for you.

I compiled and ran your code (actually the version by MensInvictaManet).
It ran fine. The up and down arrows toggle between two versions of a cube.
Version 2 is about half the size of the original version.
Print out the coordinates for version 2. They are all ±0.58, whereas
they are all ±1.0 for the original cube.
Why would you expect Version 2 to be a sphere? Not even close.

Hey - at least you are able to toggle between two objects correctly.
That’s a start. Is this an assignment?
There are different ways to compute vertices on the surface of a sphere.
You could subdivide the faces of a cube or you could go that lat-lon route.
I would choose an approach and try to compute and display vertices
first, then move on to connecting those vertices to get quads or triangles.

Google “sphere tessellation” or “lat-lon sphere” to get some ideas for the logic.
You can even get equations and code.

Good luck - Carmine

// -------------------------------------------------------------------------------------------------
//       1         2         3         4         5         6         7         8         9         0
//34567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
// -------------------------------------------------------------------------------------------------

// September 15.2016.  Carmine's adaptation of smartBoy33 OpenGL Beg Forum post.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <GLUT/glut.h>

int z = 0;
float ver2[8][3];

double rotate_y = 0, rotate_x = 0;

float ver[8][3] = {{-1.0,-1.0, 1.0 }, {-1.0, 1.0, 1.0 }, {1.0, 1.0, 1.0 }, {1.0,-1.0, 1.0},
                   {-1.0,-1.0,-1.0 }, {-1.0, 1.0,-1.0 }, {1.0, 1.0,-1.0 }, {1.0,-1.0,-1.0}};

GLfloat color[8][3] = {{ 0.0,0.0,0.0 }, { 1.0,0.0,0.0 }, { 1.0,1.0,0.0 }, { 0.0,1.0,0.0 },
                       { 0.0,0.0,1.0 }, { 1.0,0.0,1.0 }, { 1.0,1.0,1.0 }, { 0.0,1.0,1.0 }};


//--+----4----+----3----+----2----+----1----+----||----+----1----+----2----+----3----+----4----+----
//----------------------------------------   initialize   ------------------------------------------

void initialize ()
{
    int   i, j;
    float a, b, c, d, v;

    for (int i = 0; i < 8; i++)  {
    for (int j = 0; j < 3; j++)  {

    v = ver[i][j  ];
    a = ver[i][j+1];
    b = ver[i][j+2];
    c = ver[i][j-1];
    d = ver[i][j-2];

    switch (j)  {
       case 0: ver2[i][j] = v * sqrtf(1.0 - (a*a/2.0) - (b*b/2.0) + (a*a*b*b/3.0));  break;
       case 1: ver2[i][j] = v * sqrtf(1.0 - (a*a/2.0) - (c*c/2.0) + (a*a*c*c/3.0));  break;
       case 2: ver2[i][j] = v * sqrtf(1.0 - (d*d/2.0) - (c*c/2.0) + (d*d*c*c/3.0));  break;
    }
    }
    }

    printf ("
          Vertex 1              Vertex 2

");
    for (i = 0; i < 8; i++)  {
        printf (" %d)  %5.2f %5.2f %5.2f     %5.2f %5.2f %5.2f
",  i,
                      ver[i][0], ver[i][1], ver[i][2], ver2[i][0], ver2[i][1], ver2[i][2]);
    }
}

//--+----4----+----3----+----2----+----1----+----||----+----1----+----2----+----3----+----4----+----
//-------------------------------------------   quad   ---------------------------------------------

void quad (int a, int b, int c, int d)
{
    glBegin (GL_QUADS);

       glColor3fv (color[a]);  glVertex3fv (ver[a]);
       glColor3fv (color[b]);  glVertex3fv (ver[b]);
       glColor3fv (color[c]);  glVertex3fv (ver[c]);
       glColor3fv (color[d]);  glVertex3fv (ver[d]);

    glEnd ();
}

//--+----4----+----3----+----2----+----1----+----||----+----1----+----2----+----3----+----4----+----
//-------------------------------------------   quad2   --------------------------------------------

void quad2 (int a, int b, int c, int d)
{
    glBegin (GL_QUADS);

       glColor3fv (color[a]);  glVertex3fv (ver2[a]);
       glColor3fv (color[b]);  glVertex3fv (ver2[b]);
       glColor3fv (color[c]);  glVertex3fv (ver2[c]);
       glColor3fv (color[d]);  glVertex3fv (ver2[d]);

    glEnd ();
}

//--+----4----+----3----+----2----+----1----+----||----+----1----+----2----+----3----+----4----+----
//----------------------------------------   colorcube   -------------------------------------------

void colorcube()
{
    quad (0, 3, 2, 1);
    quad (2, 3, 7, 6);
    quad (0, 4, 7, 3);
    quad (1, 2, 6, 5);
    quad (4, 5, 6, 7);
    quad (0, 1, 5, 4);
}

//--+----4----+----3----+----2----+----1----+----||----+----1----+----2----+----3----+----4----+----
//----------------------------------------   colorcube2   ------------------------------------------

void colorcube2()
{
    quad2 (0, 3, 2, 1);
    quad2 (2, 3, 7, 6);
    quad2 (0, 4, 7, 3);
    quad2 (1, 2, 6, 5);
    quad2 (4, 5, 6, 7);
    quad2 (0, 1, 5, 4);
}

//--+----4----+----3----+----2----+----1----+----||----+----1----+----2----+----3----+----4----+----
//----------------------------------------   specialKeys   --------------------------------------------

void specialKeys (int key, int x, int y)
{
    if      (key == GLUT_KEY_UP  )  z = 0;
    else if (key == GLUT_KEY_DOWN)  z = 1;

    glutPostRedisplay();
}

//--+----4----+----3----+----2----+----1----+----||----+----1----+----2----+----3----+----4----+----
//-----------------------------------------   display   --------------------------------------------

void display ()
{
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode    (GL_PROJECTION);
    glLoadIdentity  ();
    int w = glutGet (GLUT_WINDOW_WIDTH);
    int h = glutGet (GLUT_WINDOW_HEIGHT);
    gluPerspective  (60, w / h, 0.1, 100);

    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();
    gluLookAt (3,3,3, 0,0,0, 0,0,1);

    glRotatef (rotate_x, 1.0, 0.0, 0.0);
    glRotatef (rotate_y, 0.0, 1.0, 0.0);

    if (z == 0) colorcube ();
    if (z == 1) colorcube2();

    glutSwapBuffers ();
}

//--+----4----+----3----+----2----+----1----+----|----+----1----+----2----+----3----+----4----+----5
//------------------------------------------   main   ----------------------------------------------

int main (int argc, char **argv)
{
    initialize ();

    glutInit            (&argc, argv);
    glutInitDisplayMode (GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize  (640, 480);
    glutCreateWindow    ("GLUT");
    glutDisplayFunc     (display);
    glutSpecialFunc     (specialKeys);

    glEnable(GL_DEPTH_TEST);

    glutMainLoop();
    return 0;
}

// -------------------------------------------------------------------------------------------------