📜  用于C语言中简单动画(革命)的OpenGL程序

📅  最后修改于: 2021-05-26 00:41:07             🧑  作者: Mango

OpenGL是一种跨语言,跨平台的API,用于渲染2D和3D矢量图形。使用此功能,我们可以制作很多设计以及动画。以下是使用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方向上, θ是角度。
如果我们以很小的增量(例如1度)将θ从0更改为2 * pi(360度),并在该坐标上绘制点,则可以制作一个完整的圆形或椭圆形。我们还可以通过改变θ (角度)的起始值和终止值来制作半圆或任何圆弧或椭圆形的圆弧。

这些概念用于绘制以下动画:

  • 使用7个水平椭圆和3个垂直完整椭圆以及1个外圆和1个外椭圆来可视化通过调整θ和半径绘制的轨道。
  • 绘制一条垂直线以制作该图。然后使其运动,给出一个水獭循环,其中j的值以很小的量变化以使运动更平滑。
  • 由于必须使所有点以相同的运动方式运动才能使图形保持在一起,所以运动方程即glVertex2i(x / 2 – 600 * cos(j),y / 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();
}
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。