Tuesday, July 24, 2012

Circles Within Circle OpenGL Code

We can create multiple circles within a Circles in OpenGL, with the following code, you can change the radius Color and width according to your own requirement.




   1:  #include<Windows.h>
   2:  #include<glut.h>
   3:  #include<math.h>
   4:   
   5:  void DrawCircle(float x, float y, float radius)
   6:  {
   7:      float DEG_RAD = 3.14159/180;
   8:      glBegin(GL_LINE_LOOP);
   9:   
  10:     for (int z=0; z<=360; z++)
  11:     {
  12:        float deg = z*DEG_RAD;
  13:        glVertex2f(x+cos(deg)*radius,y+sin(deg)*radius);
  14:     }
  15:     glEnd();
  16:  }
  17:  void Draw()
  18:  {   
  19:      glClear(GL_COLOR_BUFFER_BIT);
  20:      glColor3f(1.0 , 0.0, 0.9);
  21:      DrawCircle(0.0,0.0,0.9);
  22:      glColor3f(0.0 , 1.0, 0.0);
  23:      DrawCircle(0.0,0.0,0.7);
  24:      glColor3f(1.0 , 1.0, 0.0);
  25:      DrawCircle(0.0,0.0,0.5);
  26:      glColor3f(1.0 , 1.0, 1.0);
  27:      DrawCircle(0.0,0.0,0.3);
  28:      glEnd();
  29:      glFlush();
  30:  }
  31:   
  32:  void Initialize()
  33:  {
  34:      glClearColor(0,0,0,1);
  35:      glMatrixMode(GL_PROJECTION);
  36:      glLoadIdentity();
  37:      glOrtho(0.0, 0.0, 0.0, 0.0, -1.0, 1.0);
  38:  }
  39:   
  40:  void main(int iArgc,char** cppArgv)
  41:  {
  42:      glutInit(&iArgc,cppArgv);
  43:      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  44:      glutInitWindowPosition(400,200);
  45:      glutInitWindowSize(500,400);
  46:      glutCreateWindow("Circles Within Circle by Muhammad Taqi :: www.techgulf.blogspot.com");
  47:      Initialize();   
  48:      glutDisplayFunc(Draw);
  49:      glutMainLoop();
  50:  } 

No comments:

Post a Comment