C에서 간단한 애니메이션(Revolution)을 위한 OpenGL 프로그램
OpenGL 2D 및 3D 벡터 그래픽을 렌더링하기 위한 교차 언어 교차 플랫폼 API입니다. 이를 활용하면 애니메이션뿐만 아니라 디자인도 많이 만들 수 있습니다. 아래는 다음을 사용하여 만든 간단한 애니메이션입니다. OpenGL .
접근하다 :
움직이는 그림을 만들려면 표시하는 데 사용되는 기능의 작동 절차를 이해해야 합니다. glClear(GL_COLOR_BUFFER_BIT) . 그 임무는 특정 시간(보통 1/30초 또는 1/60초 후)이 지나면 기본값으로 화면을 지우는 것입니다. 따라서 좌표가 변경되면 인간의 눈은 1/16초 간격으로 분리된 이미지만 구별할 수 있으므로 움직이는 것처럼 보입니다(시력 지속성).
이제 원의 좌표는 X = r*cos(?) 및 Y = r*sin(?) 또는 타원의 경우 X = rx*cos(?) 및 Y = ry*cos(?)입니다. 여기서 rx 및 ry는 X 및 Y 방향의 반경이고 ? 각도입니다.
우리가 다양하다면 ? 0에서 2*pi(360도)까지 매우 작은 증가(예: 1도)로 해당 좌표에 점을 그리면 완전한 원이나 타원을 만들 수 있습니다. 또한 시작 및 끝 값을 변경하여 반원이나 원 또는 타원의 호를 만들 수도 있습니다. ? (각도).
이러한 개념은 다음 애니메이션을 그리는 데 사용됩니다.
- 타원의 수평 부분 7개와 수직 완전 타원 3개, 외부 원 1개와 외부 타원 1개를 사용하여 ? 반경도 그렇고.
- 하나의 수직선을 그려 그림을 만듭니다. 그런 다음 이를 움직이게 하기 위해 j 값이 아주 작은 양으로 변경되어 모션을 더 부드럽게 만드는 다른 루프가 제공됩니다.
- 그림을 함께 유지하려면 모든 점을 동일한 유형의 동작으로 움직여야 했기 때문에 운동 방정식은 다음과 같습니다. Glyx2i(x/2 - 600*cos(j)/2 - 100*sin(j)) 모든 내부 내부에 제공됩니다. for 루프 모든 지점에 일괄적으로 적용할 수 있도록 말이죠.
Ubuntu 운영 체제에서 작업하는 경우:
gcc filename.c -lGL -lGLU -lglut -lm where filename.c is the name of the file with which this program is saved.
아래는 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 (); }