VOOZH about

URL: https://www.geeksforgeeks.org/system-design/opengl-program-simple-animation-revolution-c/

⇱ OpenGL program for simple Animation (Revolution) in C - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

OpenGL program for simple Animation (Revolution) in C

Last Updated : 7 Oct, 2021

OpenGL is a cross-language, cross-platform API for rendering 2D and 3D Vector Graphics. Using this, we can make a lot of design as well as animations. Below is the simple animation made using OpenGL.
Approach : 
To make a picture moving, we need to understand the working procedure of a function used to display i.e glClear(GL_COLOR_BUFFER_BIT). Its task is to clear screen with default value after a certain time (normally, after 1/30 sec or 1/60 sec). So, if any change of coordinate happens, then it will appear to be moving as human eye can distinguish image only which is separated by 1/16 second (persistence of vision).
Now, the coordinates of circle are X = r*cos(?) and Y = r*sin(?) or for ellipse X = rx*cos(?) and Y = ry*cos(?) where rx and ry are radius in X- and Y- direction and ? is the angle. 
If we vary ? from 0 to 2*pi (360 degree) at very small increase (say of 1 degree) and draw point on that coordinate, we can make a complete circle or ellipse. We can also make semi-circle or any arc of circle or ellipse by varying the starting and ending value of ? (angle).
These concepts are used to draw the following Animation: 
 

  • 7 horizontal parts of ellipse and 3 vertical complete ellipse as well as 1 outer circle and one outer ellipse are used to visualise an orbit drawn by adjusting the ? as well as radius.
  • One vertical line is drawn to make the figure. Then to make it move, an other loop is given where value of j changes with very small amount to make the motion smoother.
  • Since, we had to make all point moving in same type of motion to keep the figure together, so equation of motion, that is, glVertex2i(x/2 - 600*cos(j), y/2 - 100*sin(j)) is given inside every inner for loop, so that it can be applied to all points altogether.


For working on Ubuntu operating system: 
 

gcc filename.c -lGL -lGLU -lglut -lm 
where filename.c is the name of the file
with which this program is saved.


 


Below is the implementation in C.
 


 

Comment
Article Tags:

Explore