图形学粒子系统例子--雪花
#include #include #include #include
#include
#include
#define MAX_PARTICLES 1000
GLuint texture[1]; typedef struct {
float life;//粒子的生命值
float fade;//消亡速度
//粒子的颜色
float r , g , b;
//粒子的位置
float x , y , z;
//粒子的速度
float v1 , v2 , v3;
//粒子的加速度
float ax;
float ay;
float az;
}particles;
GLsizei w = 500;
GLsizei h = 500;
void update();
void initparticles(); void display();
void reshape(int width, int height);
void keyboard(unsigned char key, int x, int y);
void init();
int LoadGLTextures();
particles particle[MAX_PARTICLES];
AUX_RGBImageRec *LoadBMP(char *Filename)
{
FILE * File=NULL;
if(!Filename)
{
return NULL;
}
File=fopen(Filename,"r");
if(File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}
return NULL;
}
int LoadGLTextures()
{
int Status=FALSE;
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage,0,sizeof(void *)*1);
if (TextureImage[0]=LoadBMP("snow.bmp"))
{
Status=TRUE; // 将 Status 设为 TRUE
glGenTextures(1, &texture[0]); // 创建纹理
// 使用来自位图数据生成 的典型纹理
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,
TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // 线形滤波
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // 线形滤波
}
if (TextureImage[0]) // 纹理是否存在
{
if (TextureImage[0]->data) // 纹理图像是否存在
{
free(TextureImage[0]->data); // 释放纹理图像占用的内存
}
free(TextureImage[0]); // 释放图像结构
}
return Status; // 返回 Status }
void init(){
glShadeModel(GL_SMOOTH);
glClearColor(0.0,0.0,1.0,0.0);
glClearDepth(1.0f);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
initparticles();
}
//初始化粒子
void initparticles(){
int i;
for(i=0;i10.0)
particle[i].x=-2.0;
particle[i].v2+=particle[i].ay; //更新速度
particle[i].life-=particle[i].fade;//更新死亡时间
if(particle[i].y<-50||particle[i].life<=0)
{
particle[i].x = -4.0f;
particle[i].y = 9.0f;
if((int)particle[i].x % 2 == 0)
particle[i].z = rand()%6;
else
particle[i].z = -rand()%3;
particle[i].life = 100;
particle[i].fade = 0.005 * (rand()%50);
particle[i].v1 = 0.00001 * (rand()%100);
particle[i].v2 = 0.0000002 * (rand()%28000);
particle[i].v3 = 0.0;
particle[i].r = 255;
particle[i].g = 255;
particle[i].b = 255;
particle[i].ax = 0.0f;
particle[i].ay = 0.0000005f;
particle[i].az = 0.0f;
}
}
}
void reshape(int width, int height){
glViewport(0, 0, width, height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 200.0f);
glMatrixMode(GL_MODELVIEW); // Select The Model view Matrix
glLoadIdentity(); // Reset The Model view Matrix
w = width;
h = height;
}
void keyboard(unsigned char key, int x, int y){
switch(key){
case 27:
exit(0);
break;
}
}
int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(500 , 500);
glutCreateWindow(*argv);
init();
LoadGLTextures();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
本文档为【图形学粒子系统例子--雪花】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。