Write a glut program to draw sphere with horizontal and vertical lines



CODE:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <MATH.h>

#if defined(__APPLE__)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#define PI 3.1415926535898
#define PI 3.1415926535898


void MySphere(GLfloat radius)
{
  GLdouble inc = PI/12;
  GLdouble theta, phi;
  bool even = true;
  for (theta=-PI/2; theta<(PI/2-inc);theta+=inc){
    for (phi = 0; phi < 2*PI;phi+=inc) {
      glBegin(GL_POLYGON);
      glVertex3f(radius*cos(theta)*cos(phi),      radius*sin(theta),radius*cos(theta)*sin(phi));
      glVertex3f(radius*cos(theta+inc)*cos(phi), radius*sin(theta+inc),radius*cos(theta+inc)*sin(phi));
      glVertex3f(radius*cos(theta+inc)*cos(phi+inc), radius*sin(theta+inc),radius*cos(theta+inc)*sin(phi+inc));
      glVertex3f(radius*cos(theta)*cos(phi+inc), radius*sin(theta),radius*cos(theta)*sin(phi+inc));
    glEnd();
    }
  } 
  glFlush();
}
void Display(void)
{
       glClear (GL_COLOR_BUFFER_BIT);
       GLfloat mat_diffuse[] = { 2.0, 1.0, 7.0,1.0};
       GLfloat mat_ambient[] = { 0.0, 0.0, 2.0,1.0};
       GLfloat mat_specular[] = { 1.0, 1.0,1.0,1.0};                
       GLfloat mat_shininess[] = {14.0 };
       glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
       glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
       glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
       glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
       glEnable(GL_LIGHTING);
       glEnable(GL_LIGHT0);
       glColor3f (0.0, 0.0, 0.0);
       glTranslatef(0.,0.,-5.);
           glTranslatef(-2,0.,0.);
       MySphere(1.5);
glTranslatef(4,0.,0);
       glRotatef(90,1.,0.,0.);
       glutSolidSphere(1.5,24,12);
       glFlush();
}

void reshape (int w, int h)
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity();
       glFrustum (-0.5*(GLfloat)w/h,0.5*(GLfloat)w/h, -0.5,0.5, 1., 20.0);
    glMatrixMode (GL_MODELVIEW);
       glLoadIdentity ();
       gluLookAt(0.2,0.2,3, 0,0,-100,0.,1.,0.);
}

void init(void)
{
  glClearColor(1.0,1.0,1.0,1.0);
  glPolygonMode(GL_FRONT, GL_LINE);
  glPolygonMode(GL_BACK, GL_LINE);
}

void main(int argc, char* argv[])
{
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
  glutInitWindowSize (320, 200);  
  glutCreateWindow("A Sphere");
  init();
  glutDisplayFunc(Display);
  glutReshapeFunc(reshape);
  glutMainLoop();
}

OUTPUT:

0 comments:

Post a Comment