21xrx.com
2024-12-22 21:58:11 Sunday
登录
文章检索 我的文章 写文章
C++语言制作烟花效果代码
2023-06-30 19:18:05 深夜i     --     --
C++ 烟花 效果 制作 代码

烟花是很多人喜爱的热门节日庆祝物,现在许多人也喜欢使用计算机技术制作虚拟烟花效果,本文将介绍如何使用C++语言制作烟花效果的代码。

首先,我们需要使用C++的 OpenGL 库来创建 3D 绘画窗口。代码如下:


#include <Windows.h>

#include <gl/Gl.h>

#include <gl/Glu.h>

#include <gl/glut.h>

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)

{

  if (height == 0)

  

    height = 1;

  

  glViewport(0, 0, width, height);

  glMatrixMode(GL_PROJECTION);

  glLoadIdentity();

  gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);

  glMatrixMode(GL_MODELVIEW);

  glLoadIdentity();

}

GLfloat xrot, yrot, zrot;

GLfloat xspeed, yspeed, zspeed;

GLfloat cx, cy, cz = -5.0f;

bool keys[255];

GLfloat rp = 0;

GLUquadricObj *hObj;

GLvoid InitGL(GLsizei width, GLsizei height)

{

  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

  glClearDepth(1.0f);

  glDepthFunc(GL_LESS);

  glEnable(GL_DEPTH_TEST);

  glEnable(GL_LIGHTING);

  glEnable(GL_LIGHT0);

  glEnable(GL_COLOR_MATERIAL);

  hObj = gluNewQuadric(); // Creating a new quadric object

  gluQuadricNormals(hObj, GL_SMOOTH); // Set the quadric normals

  gluQuadricTexture(hObj, GL_TRUE); // Set the quadric texture coordinates

  xrot = yrot = zrot = 0.0f;

  xspeed = yspeed = zspeed = 0.0f;

}

GLvoid DrawGLScene(GLvoid)

{

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glLoadIdentity();

  glTranslatef(0.0f, 0.0f, cz);

  glRotatef(xrot, 1.0f, 0.0f, 0.0f);

  glRotatef(yrot, 0.0f, 1.0f, 0.0f);

  glRotatef(zrot, 0.0f, 0.0f, 1.0f);

  glColor3f(0.7f, 0.7f, 0.7f);

  glPushMatrix(); // Save the current matrix

  glTranslatef(-1.0f, -1.0f, 0.0f);

  gluSphere(hObj, 1.0f, 16, 16); // Draw a sphere

  glPopMatrix(); // Restore the saved matrix

  glutSwapBuffers();

}

GLvoid KeyPress(unsigned char key, int x, int y)

{

  keys[key] = true;

}

GLvoid KeyUp(unsigned char key, int x, int y)

{

  keys[key] = false;

}

GLvoid SpecialKeyPress(int key, int x, int y)

{

  if (key == GLUT_KEY_UP)

  

    xspeed -= 0.1f;

  

  if (key == GLUT_KEY_DOWN)

  {

    xspeed += 0.1f;

  }

  if (key == GLUT_KEY_LEFT)

  

    yspeed -= 0.1f;

  

  if (key == GLUT_KEY_RIGHT)

  {

    yspeed += 0.1f;

  }

}

GLvoid SpecialKeyUp(int key, int x, int y)

{

  if (key == GLUT_KEY_UP || key == GLUT_KEY_DOWN)

  

    xspeed = 0.0f;

  

  if (key == GLUT_KEY_LEFT || key == GLUT_KEY_RIGHT)

  

    yspeed = 0.0f;

  

}

GLvoid UpdateGLScene(GLvoid)

{

  if (keys[27] == true)

  {

    PostQuitMessage(0);

  }

  xrot += xspeed;

  yrot += yspeed;

}

int main(int argc, char** argv)

{

  glutInit(&argc, argv);

  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);

  glutInitWindowSize(640, 480);

  glutCreateWindow("C++ Fireworks");

  glutDisplayFunc(&DrawGLScene);

  glutReshapeFunc(&ReSizeGLScene);

  glutKeyboardFunc(&KeyPress);

  glutKeyboardUpFunc(&KeyUp);

  glutSpecialFunc(&SpecialKeyPress);

  glutSpecialUpFunc(&SpecialKeyUp);

  glutIdleFunc(&UpdateGLScene);

  InitGL(640, 480);

  glutMainLoop();

  return 0;

}

以上代码创建了一个可以旋转的球体,我们接着来增加烟花效果。

我们需要在窗口中显示多个球体,来模仿烟花的爆炸效果。

我们先来定义一个包含球体的结构体:


struct FireBall

  GLfloat x;

  GLfloat y;

  GLfloat z;

  GLfloat dx;

  GLfloat dy;

  GLfloat dz;

  GLfloat r;

  GLfloat g;

  GLfloat b;

;

然后,我们需要创建一个 vector 将我们的球体添加到其中:


std::vector<FireBall> FireBallList;

接着,我们需要编写一个函数,该函数用于初始化一次烟花特效:


void InitFireworks()

{

  int numBalls = rand() % 10 + 10;

  FireBallList.clear();

  for (int i = 0; i < numBalls; i++)

  {

    FireBall fb;

    fb.x = rand() % 10 - 5.0f;

    fb.y = rand() % 5 + 5.0f;

    fb.z = rand() % 10 - 5.0f;

    fb.dx = fb.x - cx;

    fb.dy = fb.y - cy;

    fb.dz = fb.z - cz;

    fb.r = (GLfloat)(rand() % 255 + 1) / 255.0f;

    fb.g = (GLfloat)(rand() % 255 + 1) / 255.0f;

    fb.b = (GLfloat)(rand() % 255 + 1) / 255.0f;

    fb.r2 = fb.r / 2;

    fb.g2 = fb.g / 2;

    fb.b2 = fb.b / 2;

    fb.speed = (GLfloat)(rand() % 10 + 10.0f) / 20.0f;

    FireBallList.push_back(fb);

  }

}

我们将随机生成10到20个球体,每个球体都会随机设置它们的位置、速度和颜色。

最后,我们需要编写一个函数,它将绘制球体并通过它们的速度和方向实现烟花爆炸效果:


void DrawFireworks()

{

  glBegin(GL_QUADS);

  for (std::vector<FireBall>::iterator i = FireBallList.begin(); i != FireBallList.end(); i++)

  {

    glColor3f(i->r, i->g, i->b);

    GLfloat x1 = i->x - i->r2, x2 = i->x + i->r2;

    GLfloat y1 = i->y - i->r2, y2 = i->y + i->r2;

    GLfloat z1 = i->z - i->r2, z2 = i->z + i->r2;

    glVertex3f(x1, y1, z1);

    glVertex3f(x1, y1, z2);

    glVertex3f(x1, y2, z2);

    glVertex3f(x1, y2, z1);

    glVertex3f(x2, y1, z1);

    glVertex3f(x2, y1, z2);

    glVertex3f(x2, y2, z2);

    glVertex3f(x2, y2, z1);

    glVertex3f(x1, y1, z1);

    glVertex3f(x2, y1, z1);

    glVertex3f(x2, y2, z1);

    glVertex3f(x1, y2, z1);

    glVertex3f(x1, y1, z2);

    glVertex3f(x2, y1, z2);

    glVertex3f(x2, y2, z2);

    glVertex3f(x1, y2, z2);

    i->x += i->dx * i->speed;

    i->y += i->dy * i->speed;

    i->z += i->dz * i->speed;

    i->dy -= 0.01f;

  }

  glEnd();

}

void UpdateGLScene(GLvoid)

{

  if (keys[27] == true)

  {

    PostQuitMessage(0);

  }

  xrot += xspeed;

  yrot += yspeed;

  bool exploded = true;

  for (std::vector<FireBall>::iterator i = FireBallList.begin(); i != FireBallList.end(); i++)

  {

    if (i->y > 0.0f)

    

      exploded = false;

    

  }

  if (exploded)

  {

    InitFireworks();

  }

}

GLvoid DrawGLScene(GLvoid)

{

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glLoadIdentity();

  glTranslatef(0.0f, 0.0f, cz);

  glRotatef(xrot, 1.0f, 0.0f, 0.0f);

  glRotatef(yrot, 0.0f, 1.0f, 0.0f);

  glRotatef(zrot, 0.0f, 0.0f, 1.0f);

  DrawFireworks();

  glutSwapBuffers();

}

以上代码将在每次渲染时更新每个小球的位置,如果所有小球都已落地,我们就开始下一轮烟花。

现在,我们已经完成了 C++ 代码来创建烟花效果。我们可以添加更多绘图细节,如在每个球体上使用纹理等等。

以上就是在C++语言中制作烟花效果的所有代码。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复