OpenGL-Programm für einfache Animation (Revolution) in C

OpenGL ist eine sprachübergreifende, plattformübergreifende API zum Rendern von 2D- und 3D-Vektorgrafiken. Damit können wir viele Designs und Animationen erstellen. Unten sehen Sie die einfache Animation, die mit erstellt wurde OpenGL .
Ansatz :  
Um ein Bild in Bewegung zu setzen, müssen wir den Arbeitsablauf einer Funktion verstehen, die zum Anzeigen verwendet wird, d. h glClear(GL_COLOR_BUFFER_BIT) . Seine Aufgabe besteht darin, den Bildschirm nach einer bestimmten Zeit (normalerweise nach 1/30 Sek. oder 1/60 Sek.) mit dem Standardwert zu löschen. Wenn sich also die Koordinaten ändern, scheint es sich zu bewegen, da das menschliche Auge nur Bilder unterscheiden kann, die um 1/16 Sekunde voneinander getrennt sind (Persistenz der Sicht).
Nun sind die Koordinaten des Kreises X = r*cos(?) und Y = r*sin(?) oder für die Ellipse X = rx*cos(?) und Y = ry*cos(?), wobei rx und ry der Radius in ? ist der Winkel. 
Wenn wir variieren ? von 0 auf 2*pi (360 Grad) bei sehr geringer Vergrößerung (z. B. 1 Grad) und zeichnen Sie einen Punkt auf dieser Koordinate, um einen vollständigen Kreis oder eine Ellipse zu erstellen. Wir können auch einen Halbkreis oder einen beliebigen Kreis- oder Ellipsenbogen erstellen, indem wir den Anfangs- und Endwert variieren ? (Winkel).
Diese Konzepte werden verwendet, um die folgende Animation zu zeichnen: 
 

  • 7 horizontale Ellipsenteile und 3 vertikale vollständige Ellipsen sowie 1 äußerer Kreis und eine äußere Ellipse dienen zur Visualisierung einer durch Anpassen der gezeichneten Umlaufbahn ? sowie Radius.
  • Für die Figur wird eine vertikale Linie gezeichnet. Um es dann zu bewegen, wird eine weitere Schleife erstellt, in der sich der Wert von j mit einem sehr kleinen Betrag ändert, um die Bewegung gleichmäßiger zu machen.
  • Da wir alle Punkte in der gleichen Bewegungsart bewegen mussten, um die Figur zusammenzuhalten, ist das eine Bewegungsgleichung Glyx2i(x/2 - 600*cos(j) of the/2 - 100*sin(j)) ist in jedem Inneren gegeben for-Schleife sodass es auf alle Punkte insgesamt angewendet werden kann.


Für die Arbeit mit dem Ubuntu-Betriebssystem:  
 

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


 


Nachfolgend finden Sie die Implementierung in C.
 

C
   // C Program to illustrate    // OpenGL animation for revolution   #include      #include      #include      // global declaration   int     x       y  ;   float     i       j  ;   // Initialization function   void     myInit     (  void  )   {      // Reset background color with black (since all three argument is 0.0)      glClearColor  (  0.0       0.0       0.0       1.0  );          // Set picture color to green (in RGB model)      // as only argument corresponding to G (Green) is 1.0 and rest are 0.0      glColor3f  (  0.0       1.0       0.0  );          // Set width of point to one unit      glPointSize  (  1.0  );      glMatrixMode  (  GL_PROJECTION  );      glLoadIdentity  ();          // Set window size in X- and Y- direction      gluOrtho2D  (  -780       780       -420       420  );   }   // Function to display animation   void     display     (  void  )   {      // Outer loop to make figure moving      // loop variable j iterated up to 10000      // indicating that figure will be in motion for large amount of time      // around 10000/6.29 = 1590 time it will revolve      // j is incremented by small value to make motion smoother      for     (  j     =     0  ;     j      <     10000  ;     j     +=     0.01  )      {      glClear  (  GL_COLOR_BUFFER_BIT  );      glBegin  (  GL_POINTS  );          // Iterate i up to 2*pi i.e. 360 degree      // plot point with slight increment in angle      // so it will look like a continuous figure      // Loop is to draw outer circle      for     (  i     =     0  ;  i      <     6.29  ;  i     +=     0.001  )      {      x     =     200     *     cos  (  i  );      y     =     200     *     sin  (  i  );      glVertex2i  (  x       y  );          // For every loop 2nd glVertex function is      // to make smaller figure in motion      glVertex2i  (  x     /     2     -     600     *     cos  (  j  )     y     /     2     -     100     *     sin  (  j  ));      }          // 7 loops to draw parallel latitude      for     (  i     =     1.17  ;     i      <     1.97  ;     i     +=     0.001  )      {      x     =     400     *     cos  (  i  );      y     =     -150     +     300     *     sin  (  i  );      glVertex2i  (  x       y  );      glVertex2i  (  x     /     2     -     600     *     cos  (  j  )     y     /     2     -     100     *     sin  (  j  ));      }          for     (  i     =     1.07  ;     i      <     2.07  ;     i     +=     0.001  )      {      x     =     400     *     cos  (  i  );      y     =     -200     +     300     *     sin  (  i  );      glVertex2i  (  x       y  );      glVertex2i  (  x     /     2     -     600     *     cos  (  j  )     y     /     2     -     100     *     sin  (  j  ));      }          for     (  i     =     1.05  ;     i      <     2.09  ;     i     +=     0.001  )      {      x     =     400     *     cos  (  i  );      y     =     -250     +     300     *     sin  (  i  );      glVertex2i  (  x       y  );      glVertex2i  (  x     /     2     -     600     *     cos  (  j  )     y     /     2     -     100     *     sin  (  j  ));      }          for     (  i     =     1.06  ;     i      <     2.08  ;     i     +=     0.001  )      {      x     =     400     *     cos  (  i  );      y     =     -300     +     300     *     sin  (  i  );      glVertex2i  (  x       y  );      glVertex2i  (  x     /     2     -     600     *     cos  (  j  )     y     /     2     -     100     *     sin  (  j  ));      }          for     (  i     =     1.10  ;     i      <     2.04  ;     i     +=     0.001  )      {      x     =     400     *     cos  (  i  );      y     =     -350     +     300     *     sin  (  i  );      glVertex2i  (  x       y  );      glVertex2i  (  x     /     2     -     600     *     cos  (  j  )     y     /     2     -     100     *     sin  (  j  ));      }          for     (  i     =     1.16  ;     i      <     1.98  ;     i     +=     0.001  )      {      x     =     400     *     cos  (  i  );      y     =     -400     +     300     *     sin  (  i  );      glVertex2i  (  x       y  );      glVertex2i  (  x     /     2     -     600     *     cos  (  j  )     y     /     2     -     100     *     sin  (  j  ));      }          for     (  i     =     1.27  ;     i      <     1.87  ;     i     +=     0.001  )      {      x     =     400     *     cos  (  i  );      y     =     -450     +     300     *     sin  (  i  );      glVertex2i  (  x       y  );      glVertex2i  (  x     /     2     -     600     *     cos  (  j  )     y     /     2     -     100     *     sin  (  j  ));      }          // Loop is to draw vertical line      for     (  i     =     200  ;     i     >=-     200  ;     i  --  )      {      glVertex2i  (  0       i  );      glVertex2i  (  -600     *     cos  (  j  )     i     /     2     -     100     *     sin  (  j  ));      }          // 3 loops to draw vertical ellipse (similar to longitude)      for     (  i     =     0  ;  i      <     6.29  ;     i     +=     0.001  )      {      x     =     70     *     cos  (  i  );      y     =     200     *     sin  (  i  );      glVertex2i  (  x       y  );      glVertex2i  (  x     /     2     -     600     *     cos  (  j  )     y     /     2     -     100     *     sin  (  j  ));      }          for     (  i     =     0  ;     i      <     6.29  ;     i     +=     0.001  )      {      x     =     120     *     cos  (  i  );      y     =     200     *     sin  (  i  );      glVertex2i  (  x       y  );      glVertex2i  (  x     /     2     -     600     *     cos  (  j  )     y     /     2     -     100     *     sin  (  j  ));      }          for     (  i     =     0  ;     i      <     6.29  ;     i     +=     0.001  )      {      x     =     160     *     cos  (  i  );      y     =     200     *     sin  (  i  );      glVertex2i  (  x       y  );      glVertex2i  (  x     /     2     -     600     *     cos  (  j  )     y     /     2     -     100     *     sin  (  j  ));      }          // Loop to make orbit of revolution      for     (  i     =     0  ;     i      <     6.29  ;     i     +=     0.001  )      {      x     =     600     *     cos  (  i  );      y     =     100     *     sin  (  i  );      glVertex2i  (  x       y  );      }      glEnd  ();      glFlush  ();      }   }   // Driver Program   int     main     (  int     argc       char  **     argv  )   {      glutInit  (  &  argc       argv  );          // Display mode which is of RGB (Red Green Blue) type      glutInitDisplayMode  (  GLUT_SINGLE     |     GLUT_RGB  );          // Declares window size      glutInitWindowSize  (  1360       768  );          // Declares window position which is (0 0)      // means lower left corner will indicate position (0 0)      glutInitWindowPosition  (  0       0  );      // Name to window      glutCreateWindow  (  'Revolution'  );      // Call to myInit()      myInit  ();      glutDisplayFunc  (  display  );      glutMainLoop  ();   }   


 

Quiz erstellen