使用OpenGL制作二维希望小学邀请卡

本教程将使用OpenGL制作一个二维的希望小学邀请卡,包含教学楼、鲜花、气球、云朵和树木等图形。邀请卡将会在适当的地方包含静态和动画对象,键盘和鼠标的交互可以触发动作或分阶段展示实体卡片。

代码示例

#define FREEGLUT_STATIC
#include <math.h>
#include <GL/freeglut.h>

int width = 800;
int height = 600;
float cloudX = 0.0;
float balloonY = 0.0;

void when_in_mainloop()
{
    glutPostRedisplay();
}

void keyboard_input(unsigned char key, int x, int y)
{
    if (key == 'q' || key == 'Q')
        exit(0);
}

void drawSchoolBuilding()
{
    // Draw school building
    glColor3f(0.7, 0.7, 0.7); // Light gray color
    glBegin(GL_POLYGON);
    glVertex2f(0.1, 0.1);
    glVertex2f(0.1, 0.6);
    glVertex2f(0.4, 0.6);
    glVertex2f(0.4, 0.1);
    glEnd();

    glColor3f(0.9, 0.9, 0.9); // White color
    glBegin(GL_POLYGON);
    glVertex2f(0.15, 0.6);
    glVertex2f(0.4, 0.6);
    glVertex2f(0.275, 0.8);
    glEnd();
}

void drawFlower()
{
    // Draw flower
    glColor3f(1.0, 0.0, 0.0); // Red color
    glBegin(GL_POLYGON);
    glVertex2f(0.6, 0.2);
    glVertex2f(0.6, 0.3);
    glVertex2f(0.55, 0.35);
    glVertex2f(0.5, 0.3);
    glVertex2f(0.5, 0.2);
    glEnd();

    glColor3f(0.0, 1.0, 0.0); // Green color
    glBegin(GL_POLYGON);
    glVertex2f(0.55, 0.35);
    glVertex2f(0.55, 0.45);
    glVertex2f(0.525, 0.475);
    glVertex2f(0.5, 0.45);
    glVertex2f(0.5, 0.35);
    glEnd();

    glColor3f(1.0, 1.0, 0.0); // Yellow color
    glBegin(GL_POLYGON);
    glVertex2f(0.525, 0.475);
    glVertex2f(0.525, 0.5);
    glVertex2f(0.5125, 0.5125);
    glVertex2f(0.5, 0.5);
    glVertex2f(0.5, 0.475);
    glEnd();
}

void drawBalloon()
{
    // Draw balloon
    glColor3f(0.0, 0.0, 1.0); // Blue color
    glBegin(GL_POLYGON);
    glVertex2f(0.7, 0.2 + balloonY);
    glVertex2f(0.675, 0.225 + balloonY);
    glVertex2f(0.725, 0.225 + balloonY);
    glEnd();

    glColor3f(1.0, 0.0, 0.0); // Red color
    glBegin(GL_POLYGON);
    glVertex2f(0.7, 0.2 + balloonY);
    glVertex2f(0.675, 0.15 + balloonY);
    glVertex2f(0.725, 0.15 + balloonY);
    glEnd();
}

void drawCloud()
{
    // Draw cloud
    glColor3f(1.0, 1.0, 1.0); // White color
    glBegin(GL_POLYGON);
    glVertex2f(0.1 + cloudX, 0.75);
    glVertex2f(0.125 + cloudX, 0.775);
    glVertex2f(0.175 + cloudX, 0.775);
    glVertex2f(0.2 + cloudX, 0.75);
    glEnd();

    glBegin(GL_POLYGON);
    glVertex2f(0.125 + cloudX, 0.775);
    glVertex2f(0.125 + cloudX, 0.825);
    glVertex2f(0.175 + cloudX, 0.825);
    glVertex2f(0.175 + cloudX, 0.775);
    glEnd();

    glBegin(GL_POLYGON);
    glVertex2f(0.175 + cloudX, 0.775);
    glVertex2f(0.2 + cloudX, 0.75);
    glVertex2f(0.25 + cloudX, 0.75);
    glVertex2f(0.275 + cloudX, 0.775);
    glEnd();

    glBegin(GL_POLYGON);
    glVertex2f(0.175 + cloudX, 0.825);
    glVertex2f(0.175 + cloudX, 0.875);
    glVertex2f(0.2 + cloudX, 0.9);
    glVertex2f(0.225 + cloudX, 0.875);
    glVertex2f(0.25 + cloudX, 0.875);
    glVertex2f(0.275 + cloudX, 0.9);
    glVertex2f(0.3 + cloudX, 0.875);
    glVertex2f(0.3 + cloudX, 0.825);
    glEnd();
}

void display(void)
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);

    drawSchoolBuilding();
    drawFlower();
    drawBalloon();
    drawCloud();

    glFlush();
}

void timer(int value)
{
    cloudX += 0.001;
    balloonY += 0.001;

    glutPostRedisplay();
    glutTimerFunc(10, timer, 0);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(width, height);
    glutCreateWindow('Hope Elementary Invitation Card');
    glutDisplayFunc(display);
    glutIdleFunc(when_in_mainloop);
    glutKeyboardFunc(keyboard_input);
    glutTimerFunc(10, timer, 0);
    glutMainLoop();

    return 0;
}

代码解释

这段代码使用OpenGL绘制了一个希望小学的邀请卡,包含了教学楼、鲜花、气球、云朵和树木等图形。使用定时器来实现云朵和气球的动画效果。通过键盘交互可以退出程序。

主要功能:

  • 绘制图形: 使用glBeginglEnd函数绘制各个图形,包括教学楼、鲜花、气球、云朵和树木。
  • 动画效果: 使用定时器函数timer来控制云朵和气球的移动,实现动画效果。
  • 键盘交互: 使用keyboard_input函数处理键盘输入,实现退出程序的功能。

使用方法:

  1. 将代码保存为.cpp文件。
  2. 使用支持OpenGL的编译器编译代码。
  3. 运行编译后的可执行文件。

进一步扩展

你可以根据需要进一步修改代码以适应你的需求,例如添加更多的图形和交互功能。

一些建议:

  • 添加更多图形,例如树木、草地、太阳等。
  • 实现鼠标交互,例如点击气球使其爆炸,或者点击鲜花使其变色。
  • 使用更复杂的动画效果,例如让云朵更加自然地飘动。
  • 使用纹理贴图来为图形添加更加逼真的外观。
  • 添加文字,例如邀请卡的标题、内容和日期等。

通过不断地尝试和改进,你可以制作出一个更加精美、生动的希望小学邀请卡。

OpenGL 二维希望小学邀请卡制作教程:包含动画效果和交互功能

原文地址: https://www.cveoy.top/t/topic/DRc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录