Circles in the GL/OpenGL



Source code:
#include<GL/freeglut.h>
#include <math.h>



void drawCircle(float cx, float cy, float r, int num_segments) {

      

       const float PI = 3.14159;
       glBegin(GL_TRIANGLE_FAN);
       for (int i = 0; i < num_segments; i++)  {
             
              float theta = i * (2.0f * PI / num_segments);
              float x = r * cos(theta);
              float y = r * sin(theta);
              glVertex2f(x + cx, y + cy);
       }
       glEnd();
}

void display(void) {
      
       glClear(GL_COLOR_BUFFER_BIT);

       drawCircle(0.0, 0.0, 0.5, 36);
      
       glColor3b(2.0, 2.0, 2.0);
       drawCircle(0.0, 0.0, 0.4, 36);

       glColor3b(100.0, 100.0, 100.0);
       drawCircle(0.0, 0.0, 0.4, 36);
      
       glColor3b(100.0, 200.0 , 100.0);
       drawCircle(0.0, 0.0, 0.3, 36);

       glColor3b(200.0, 100.0, 100.0);
       drawCircle(0.0, 0.0, 0.2, 36);

       glColor3b(200.0, 100.0, 200.0);
       drawCircle(0.0, 0.0, 0.2, 36);
      
       glEnd();
       glFlush();
}

//



void init(void) {
       // select background color
       glClearColor(0.0, 0.0, 0.0, 0.0); // (red, green, blue, alpha), used by glClear

       // Define world coordinate frame
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); // model in real word units
       // (left, right, bottom, top, near, far)
}


int main(int argc, char** argv) {
       //Step#1 Create and initialize glut windowing system
       glutInit(&argc, argv);
       glutInitWindowSize(600, 400);
       glutInitWindowPosition(100, 100);
       glutCreateWindow("Let's Programming");
       //Step#2 Do Open GL related initializations
       init();
       //Step#3 Register Call back methods or Event Handler methods
      
       glutDisplayFunc(display);
       //Step#4 Enter in  main loop (an infinite loop listening to registered events and then call their registered methods if any)
       glutMainLoop();
       return 0;
}


Comments

Popular posts from this blog

Umbrella activities in Software Engineering

Operating System | Best Definition of Opetating System