C++ OpenGL交互式实体卡片演示:键盘和鼠标控制

这篇博客文章将展示一个简单的C++ OpenGL程序,该程序创建了一个交互式实体卡片演示。用户可以使用键盘和鼠标与卡片元素进行交互,例如触发动画或显示隐藏的内容。

代码实现cpp#define FREEGLUT_STATIC#include <math.h>#include <GL/freeglut.h>

int width = 800;int height = 600;float cloudX = 0.0;float balloonY = 0.0;bool showCard = false; // 标志,指示是否显示整个卡片

// 在主循环中调用的函数void when_in_mainloop(){ glutPostRedisplay();}

// 处理键盘输入的函数void keyboard_input(unsigned char key, int x, int y){ if (key == 'q' || key == 'Q') exit(0); else if (key == 'c' || key == 'C') showCard = !showCard;}

// 绘制学校建筑的函数void drawSchoolBuilding(){ // 绘制学校建筑 glColor3f(0.7, 0.7, 0.7); // 浅灰色 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); // 白色    glBegin(GL_POLYGON);    glVertex2f(0.15, 0.6);    glVertex2f(0.4, 0.6);    glVertex2f(0.275, 0.8);    glEnd();}

// 绘制花朵的函数void drawFlower(){ // 绘制花朵 glColor3f(1.0, 0.0, 0.0); // 红色 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); // 绿色    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); // 黄色    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(){ // 绘制气球 glColor3f(0.0, 0.0, 1.0); // 蓝色 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); // 红色    glBegin(GL_POLYGON);    glVertex2f(0.7, 0.2 + balloonY);    glVertex2f(0.675, 0.15 + balloonY);    glVertex2f(0.725, 0.15 + balloonY);    glEnd();}

// 绘制云的函数void drawCloud(){ // 绘制云 glColor3f(1.0, 1.0, 1.0); // 白色 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.529, 0.808, 0.922, 0); // 浅蓝色背景 glClear(GL_COLOR_BUFFER_BIT);

if (showCard)    {        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;}

代码解释

  • 该代码使用 FreeGLUT 库来创建 OpenGL 窗口和处理用户输入。

  • keyboard_input() 函数处理键盘输入。按下 'C' 键将切换 showCard 标志的值,该标志控制是否显示整个卡片。

  • display() 函数清除屏幕,并根据 showCard 标志的值绘制卡片元素。

  • timer() 函数更新云和气球的位置,从而创建动画效果。

鼠标交互

为了添加鼠标交互,可以使用 glutMouseFunc() 函数注册一个回调函数。例如,以下代码展示了如何在鼠标左键点击时切换 showCard 标志的值:cpp// 处理鼠标点击的函数void mouse_input(int button, int state, int x, int y){ if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) showCard = !showCard;}

// 在 main 函数中注册鼠标回调函数glutMouseFunc(mouse_input);

总结

这个简单的示例展示了如何使用 C++ 和 OpenGL 创建交互式图形应用程序。通过使用键盘和鼠标回调函数,您可以创建各种交互式元素,从而增强应用程序的用户体验。


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

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