//Sphere Approximation Example //Derived from E. Angel //Alan Jamieson //COSC 338 - Spring 2008 #include #include #define M_PI 3.14159265 void myinit(void){ glClearColor(0.0, 0.0, 0.0, 0.0); glColor3f(0.0, 1.0, 0.0); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // glPolygonMode(GL_FRONT, GL_LINE); // glPolygonMode(GL_BACK, GL_FILL); // glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); } void display(void){ float c = M_PI/180.0; float phi, theta, phir, phir20, thetar, x, y, z, c80; glClear(GL_COLOR_BUFFER_BIT); for (phi = -80.0; phi <= 80.0; phi += 5.0){ phir = c*phi; phir20 = c*(phi+5); glBegin(GL_QUAD_STRIP); for (theta = -180.0; theta <= 180.0; theta += 5.0){ thetar = c*theta; x = sin(thetar)*cos(phir); y = cos(thetar)*cos(phir); z = sin(phir); glVertex3d(x,y,z); x = sin(thetar)*cos(phir20); y = cos(thetar)*cos(phir20); z = sin(phir20); glVertex3d(x,y,z); } glEnd(); } glBegin(GL_TRIANGLE_FAN); glVertex3d(0.0, 0.0, 1.0); c80 = c*80.0; z = sin(c80); for (theta = -180.0; theta <= 180.0; theta += 5.0){ thetar = c *theta; x = sin(thetar)*cos(c80); y = cos(thetar)*cos(c80); glVertex3d(x, y, z); } glEnd(); glBegin(GL_TRIANGLE_FAN); glVertex3d(0.0, 0.0, -1.0); z = -sin(c80); for (theta = -180.0; theta <= 180.0; theta += 20.0){ thetar = c*theta; x=sin(thetar)*cos(c80); y=cos(thetar)*cos(c80); glVertex3d(x,y,z); } glEnd(); glFlush(); } void main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("Sphere Approximation"); glutDisplayFunc(display); myinit(); glutMainLoop(); }