Saturday, September 17, 2016

4.. Write a program to create a wireframe model of globe using equation of ellipse.

#include <windows.h>
#include <GL/glut.h>
#include <iostream>
#include <math.h>
#define PI 3.142
using namespace std;

double X1, Y1, X2, Y2;

/* Handler for window-repaint event. Call back when the window first appears and
   whenever the window needs to be re-painted. */
void Init()
{
    glClearColor(1.0,1.1,0.1,0);     // Set background color to White
    glClear(GL_COLOR_BUFFER_BIT);    // Clear the color buffer (background)

    glColor3f(0.0,1.0,0.0);

    glViewport(0 , 0 , 640 , 480);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0 , 640 , 0 , 480);
}


void ellipse(int xc, int yc, int rx, int ry, int theta)
{

    double x = xc + rx * cos(theta * PI/180.0);
    double y = yc + ry * sin(theta * PI/180.0);
    glBegin(GL_POINTS);
    {
        glVertex2d(x,y);

    }glEnd();

}

void WFMofGlobe() {

    Init();
    int xc=320, yc=240, rx=200,ry=200;
    glPointSize(2);
    int i;
    for(int theta =1;theta<=360;theta++)
       for(i = rx; i>=0; i-=20)
        ellipse(xc, yc, i, ry, theta);

    for(int theta =1;theta<=360;theta++)
       for(i=ry;i>=0;i-=20)
        ellipse(xc, yc, rx, i, theta);
    glFlush();
}

/* Main function: GLUT runs as a console application starting at main()  */
int main(int argc, char** argv) {
   glutInit(&argc, argv);                 // Initialize GLUT
   glutCreateWindow("OpenGL Wire frame model of globe"); // Create a window with the given title
   glutInitWindowSize(320, 320);   // Set the window's initial width & height
   glutInitWindowPosition(50, 50); // Position the window's initial top-left corner

   glutDisplayFunc(WFMofGlobe); // Register display callback handler for window re-paint

   glutMainLoop();           // Enter the event-processing loop
   return 0;
}

No comments:

Post a Comment