Advertisement
Rejuan706

Draw moving Circle

May 29th, 2023
1,602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <GL/gl.h>
  2. #include <GL/glu.h>
  3. #include <GL/glut.h>
  4. #include <cmath>
  5.  
  6. void init()
  7. {
  8.     glClearColor(0, 0 , 0 , 0);
  9.     glMatrixMode(GL_PROJECTION);
  10.     gluOrtho2D(0.0, 20.0, 0.0, 20.0 );
  11.     glMatrixMode(GL_MODELVIEW);
  12. }
  13.  
  14. void drawCircle(int r, int x_center, int y_center)
  15. {
  16.     int deg =0;
  17.     double theta, x, y;
  18.  
  19.     glBegin(GL_POLYGON);
  20.     while (deg<360)
  21.     {
  22.         theta = (deg*M_PI)/180;
  23.  
  24.         x = r*cos(theta) + x_center;
  25.         y = r*sin(theta) + y_center;
  26.         deg++;
  27.  
  28.         glVertex2d(x , y);
  29.     }
  30.     glEnd();
  31. }
  32.  
  33. int angle= 0;
  34. void display()
  35. {
  36.     glClear(GL_COLOR_BUFFER_BIT);
  37.  
  38.     glColor3ub(255,255,255);
  39.     drawCircle(3, 5, 5);
  40.  
  41.     glPushMatrix();
  42.     glColor3ub(0,0,0);
  43.     glTranslatef(5,5,0);
  44.     glRotatef(angle, 0, 0, 1);
  45.     glTranslatef(-5,-5,0);
  46.     glLineWidth(2.0);
  47.     glBegin(GL_LINES);
  48.     glVertex2i(2,5);
  49.     glVertex2i(8,5);
  50.  
  51.     glVertex2i(5,2);
  52.     glVertex2i(5,8);
  53.     glEnd();
  54.     glPopMatrix();
  55.  
  56.     glFlush();
  57.  
  58. }
  59. void update(int value)
  60. {
  61.     angle -= 10;
  62.  
  63.     glutPostRedisplay();
  64.     glutTimerFunc(50,update, 0);
  65. }
  66.  
  67.  
  68. int main(int argc, char** argv)
  69. {
  70.     glutInit(&argc, argv);
  71.     glutInitDisplayMode(GLUT_RGB);
  72.  
  73.     glutInitWindowPosition(300,200);
  74.     glutInitWindowSize(500,500);
  75.  
  76.     glutCreateWindow("Circle");
  77.     glutDisplayFunc(display);
  78.     glutTimerFunc(0,update, 0);
  79.     init();
  80.     glutMainLoop();
  81.  
  82.     return 0;
  83.  
  84. }
  85.  
Tags: opengl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement