基于opengl的场景漫游,粒子系统出问题了怎么不下雨啊?求大神指教!!救命啊!!!
#include <windows.h>//windows头文件
#include <math.h>
#include <GL/glu.h>//实用库
#include <gl/glaux.h>
#include <gl/gl.h>
#include <stdio.h>
#include <io.h>
//**************************888888888888888888888888888888888888888
const float p=0.1f; //绘图比例尺 P个单位长度:1米
const float q=0.5f; //移动速度 1次移动q个单位长度
//粒子系统部分
#define MAX_PARTICLES 3000 //粒子数量
#define FADE 2000.0
#define SOURCE 30
bool keys[256]; //键盘数组
bool active=TRUE; //窗口是否最小化标志,缺省为ture
bool sp;
bool rp;
bool bRender = TRUE; // Polygon Flag Set To TRUE By Default (NEW)
float slowdown=2.0f; // 粒子减速
float xspeed; // X初速度
float yspeed; // Y初速度
float zoom=-40.0f; // Used To Zoom Out
GLuint loop; // 循环变量
GLuint col; // 当前颜色
GLuint texture[140]; // 存储粒子的纹理空间
typedef struct // 粒子结构
{
bool active; // 是否活跃
float life; // 粒子生命
float fade; // 褪色速度
float r; // Red
float g; // Green
float b; // Blue
float x; // X Position
float y; // Y Position
float z; // Z Position
float xi; // X Direction
float yi; // Y Direction
float zi; // Z Direction
float xg; // X Gravity
float yg; // Y Gravity
float zg; // Z Gravity
}
particles;
particles particle[MAX_PARTICLES]; //粒子数组
static GLfloat colors[3]={1.0,1.0,1.0}; // Rainbow Of Colors
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
AUX_RGBImageRec *LoadBMP(char *Filename) // 读取位图图象
{
FILE *File=NULL; // 文件句柄
if (!Filename) // 确定文件名已给出
{
return NULL; // 如果文件名未给出则返回NULL
}
File=fopen(Filename,"r"); // 检测文件是否存在
if (File) // 文件是否存在?
{
fclose(File); // 关闭文件
return auxDIBImageLoad(Filename); // 读取位图并返回一个指针
}
return NULL; //如果调用文件失败则返回NULL
}
int LoadGLTextures() //调用Bitmap并转换成纹理
{
int Status=FALSE; //状态确定
AUX_RGBImageRec *TextureImage[2]; //为纹理创建存储空间
memset(TextureImage,0,sizeof(void *)*2); //将指针设为NULL
//读取位图,检查错误。如果位图不存在则退出
//粒子的纹理
if (TextureImage[1]=LoadBMP("tupian/rain.bmp")) //创建雨滴纹理
{
Status=TRUE;
glGenTextures(1, &texture[22]);
glBindTexture(GL_TEXTURE_2D, texture[22]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);
}
if (TextureImage[0]) //纹理是否存在
{
if (TextureImage[0]->data) //纹理图象是否存在
{
free(TextureImage[0]->data); //释放纹理图象所占用内存
}
free(TextureImage[0]); //释放图象结构
}
if (TextureImage[1])
{
if (TextureImage[1]->data)
{
free(TextureImage[1]->data);
}
free(TextureImage[1]);
}
return Status; //返回Status的值
}
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) //重置opengl窗口大小
{
if (height==0) //防止被零除
{
height=1; //设为1
}
glViewport(0,0,width,height); //重置当前视口
glMatrixMode(GL_PROJECTION); //选择投影矩阵
glLoadIdentity(); //重置投影矩阵
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,200.0f);//设置视口大小,实现近大远小
glMatrixMode(GL_MODELVIEW); // 选择Modelview Matrix
glLoadIdentity(); // 重置 Modelview Matrix
}
//粒子部分
int InitGL(GLvoid) // 此处开始对OpenGL进行所有设置
{
if (!LoadGLTextures()) // 载入纹理
{
return FALSE;
}
glShadeModel(GL_SMOOTH); // 启用阴影平滑
glClearColor(0.0f,0.0f,0.0f,0.0f); // 黑色背景
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);
glEnable(GL_TEXTURE_2D);
for (loop=0;loop<MAX_PARTICLES;loop++) // 初始化每个粒子
{
particle[loop].active=true;
particle[loop].life=1.0f;
particle[loop].fade=float(rand()%100)/FADE+0.0025f; // 随机产生淡出速度(用作透明度)
particle[loop].r=colors[0];
particle[loop].g=colors[1];
particle[loop].b=colors[2];
particle[loop].xi=float((rand()%50)-26.0f)*3.0f; // 随机产生X速度
particle[loop].yi=float((rand()%50)-25.0f)*3.0f; // 随机产生Y速度
particle[loop].zi=float((rand()%50)-25.0f)*3.0f; // 随机产生Z速度
particle[loop].xg=0.0f;
particle[loop].yg=-0.8f;
particle[loop].zg=0.0f;
}
return TRUE;
}
//此部分和下边有些重复,待修改**************************************************************************************************************
//int DrawGLScene(GLvoid) // 从这里开始进行所有的绘制
// 绘制场景
//{
//}
//***********************************************************************************************************************************************
//窗口部分
HWND hWnd=NULL; //窗口句柄
HDC hDC=NULL; //设备描述表句柄
HGLRC hRC=NULL; //窗口着色描述句柄
HINSTANCE hInstance; //保存程序实例
RECT rect;
int sw=800; //设定窗口宽度为800
int sh=600; //设定窗口高度为600
const double pi=3.1415926536; //定义常量π
int i=1; //花坛墙循环变量
bool fullscreen=1; //是否全屏
GLfloat aspect;
const float piover180=0.0174532925f; //角度转换到弧度
float heading;
float xpos;
float zpos=7.0f;
//按键部分
GLfloat yup; //y平移|
GLfloat xup; //X平移 --
GLfloat zup; //z平移 --
GLfloat xrot; //x轴旋转
GLfloat yrot; //y轴旋转
//GLfloat walkbias=0; //模拟行走的参数变量
//GLfloat walkbiasangle=0; //模拟行走的参数变量
GLfloat lookupdown=-5.0f;
//GLfloat xspeed; //x轴的旋转速度
//GLfloat yspeed; //y轴的旋转速度
bool goodlight; //路灯开关
bool light; //光源-开关
bool blend; //混合 开关
bool lp; //L键是否按下
//bool fp; //F键是否按下
bool bp; //B键是否按下
GLfloat LightAmbient[]={ 0.5f, 0.5f, 0.5f, 1.0f}; //环境光的值RGBA
GLfloat LightDiffuse[]={ 1.0f, 1.0f, 1.0f, 1.0f}; //散射光的值
GLfloat LightPosition[]={ 0.0f, 0.0f, -p*20.0f, 1.0f}; //光照位置XYZA
GLuint filter; //过滤器的选择
//GLuint texture[140]; //纹理的存储空间共140个纹理空间
#pragma comment( lib, "opengl32.lib") //连接时使用的OpenGL库
#pragma comment( lib, "glu32.lib")
#pragma comment( lib, "glaux.lib")
//读取位图部分