#include <GL/glut.h>
#include <math.h>
void earth()
{
float x, y, z, thet, phi;
float c = 3.14159/180.0;
for (phi = -90.0; phi <= 90.0; phi += 20.0)
{
glBegin(GL_QUAD_STRIP);
for (thet = -180.0; thet <= 180.0; thet += 20.0)
{
x = sin(c*thet)*cos(c*phi);
y = cos(c*thet)*cos(c*phi);
z = sin(c*phi);
glNormal3f(x, y, z); // just need 1 normal for flat shading
glVertex3f(x, y, z);
x = sin(c*thet) * cos(c*(phi+20.0));
y = cos(c*thet) * cos(c*(phi+20.0));
z = sin(c*(phi+20.0));
glVertex3f(x, y, z);
}
glEnd();
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(105, 1.0, 0.0, 0.0);
earth();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.0, 2.0, -2.0*(GLfloat)h/w, 2.0*(GLfloat)h/w, -10.0, 10.0);
else
glOrtho(-2.0*(GLfloat)w/h, 2.0*(GLfloat)w/h, -2.0, 2.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}
void init()
{
glEnable(GL_DEPTH_TEST);
// enable lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// set the shading model to flat
glShadeModel(GL_FLAT);
// position the light before any transformations take place
GLfloat pos[] = {0.0, 0.0, 2.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glClearColor(1.0, 1.0, 1.0, 1.0);
// glColor3f(0.0, 0.0, 0.0); // optional
// glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // optional
glPolygonMode(GL_FRONT, GL_FILL);
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("sphere"
;
glutReshapeFunc(reshape);
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}