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?