dx粒子效果雪花飘

前端之家收集整理的这篇文章主要介绍了dx粒子效果雪花飘前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

简单修改一下也能变成玻璃效果,天上掉玻璃了

文件

Code:
  1. //////////////////////////////////////////////////////////////////////////////////////////////////
  2. //文件:main.cpp
  3. //作者:CYM
  4. //*雾特效,旋转的立方体,底面!~简单演示
  5. //*方向键控制摄像头
  6. //*shift使摄像头向上ctrl使摄像头向下
  7. //////////////////////////////////////////////////////////////////////////////////////////////////
  8. #include"D3DFrame.h"
  9. #include"Box.h"
  10. #include"Ground.h"
  11. #include"Drawtext.h"
  12. #include"Sprite.h"
  13. #include<d3d9.h>
  14. #include<d3dx9.h>
  15. voidSetFogEffects(D3DCAPS9caps,boolisFogEnable);//设置雾效果函数声明
  16. //
  17. //全局设置
  18. //
  19. IDirect3DDevice9*Device=0;//定义设备对象
  20. IDirect3DTexture9*Texture1;//立方体纹理
  21. IDirect3DTexture9*Texture2;//地面地图纹理
  22. IDirect3DTexture9*Texture3;//简单的粒子文理纹理
  23. LPD3DXFONTFont;//定义字体
  24. CD3DFrameFrame;//定义框架类
  25. CGround*Ground=0;//定义场地类
  26. CBox*Box1=0;//定义Box对象
  27. CBox*Box2=0;
  28. CDrawtext*Text=0;//定义文字绘制对象
  29. CSprite*Point;//定义sprite点对象
  30. D3DMATERIAL9material;
  31. D3DXMATRIXMap;//地图矩阵
  32. D3DXMATRIXV;//视图矩阵
  33. D3DXVECTOR3Txyz1(-3,2,-3);//矩阵平移坐标
  34. D3DXVECTOR3Rxyz1(0,0);//矩阵旋转坐标
  35. constintWidth=640;//窗口的宽高
  36. constintHeight=480;
  37. staticRECTFontRect={0,Width,Height};//绘制文字的矩形
  38. staticD3DXVECTOR3position(0.0f,3.0f,3.0f);//摄像机位置
  39. staticD3DXVECTOR3target(0.0f,0.0f,0.0f);//摄像机观察目标
  40. staticD3DXVECTOR3up(0.0f,1.0f,0.0f);//摄像机高度
  41. //staticD3DXVECTOR3SpritePos(10.0f,10.0f,-10.0f);//Sprite点的坐标
  42. D3DCAPS9caps;//设备功能支持的结构体
  43. boolisFogEnable=true;//判断雾效果是否开启
  44. staticfloattimeConversion=0.1f;//时间转换(速度同步)数值
  45. //
  46. //框架函数
  47. //
  48. boolSetup()
  49. {
  50. //创造对象
  51. Box1=newCBox(Device);
  52. Box2=newCBox(Device);
  53. Ground=newCGround(Device);
  54. Text=newCDrawtext(Device,Font);
  55. Point=newCSprite(Device);
  56. Device->GetDeviceCaps(&caps);//获取设备功能支持信息的结构体
  57. //设置纹理
  58. //D3DMATERIAL9material;
  59. ::ZeroMemory(&material,sizeof(material));
  60. material.Ambient=D3DXCOLOR(1.0f,1.0f);
  61. material.Diffuse=D3DXCOLOR(1.0f,0.1f,1.0f);
  62. material.Emissive=D3DXCOLOR(1.0f,1.0f);
  63. material.Power=0.1f;
  64. material.Specular=D3DXCOLOR(0.0f,1.0f);
  65. //设置平行光
  66. D3DLIGHT9light;
  67. ::ZeroMemory(&light,sizeof(light));
  68. light.Type=D3DLIGHT_DIRECTIONAL;
  69. light.Ambient=D3DXCOLOR(1.0f,1.0f);
  70. light.Diffuse=D3DXCOLOR(1.0f,1.0f);
  71. light.Specular=D3DXCOLOR(1.0f,1.0f);
  72. light.Direction=D3DXVECTOR3(1.0f,-1.0f,0.0f);
  73. Device->SetLight(0,&light);
  74. Device->LightEnable(0,true);
  75. //设置平行光
  76. D3DLIGHT9light2;
  77. ::ZeroMemory(&light2,sizeof(light2));
  78. light2.Type=D3DLIGHT_DIRECTIONAL;
  79. light2.Ambient=D3DXCOLOR(1.0f,1.0f);
  80. light2.Diffuse=D3DXCOLOR(0.1f,0.5f,0.4f,1.0f);
  81. light2.Specular=D3DXCOLOR(1.0f,1.0f);
  82. light2.Direction=D3DXVECTOR3(0.0f,0.0f);
  83. Device->SetLight(1,&light2);
  84. Device->LightEnable(1,true);
  85. Device->SetRenderState(D3DRS_NORMALIZENORMALS,true);//正常的渲染等级
  86. Device->SetRenderState(D3DRS_SPECULARENABLE,true);//镜面高光
  87. D3DXMatrixLookAtLH(&V,&position,&target,&up);//设置摄像机
  88. Device->SetTransform(D3DTS_VIEW,&V);//视图转换
  89. //
  90. //Createtexture.
  91. //
  92. //载入立方体纹理
  93. D3DXCreateTextureFromFile(
  94. Device,
  95. "crate.jpg",
  96. &Texture1);
  97. //载入地面纹理
  98. D3DXCreateTextureFromFile(
  99. Device,
  100. "河流.jpg",
  101. &Texture2);
  102. D3DXCreateTextureFromFile(
  103. Device,
  104. "雪球.tga",
  105. &Texture3);
  106. //
  107. //SetTextureFilterStates.
  108. //
  109. Device->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
  110. Device->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
  111. Device->SetSamplerState(0,D3DSAMP_MIPFILTER,D3DTEXF_LINEAR);
  112. Device->SetSamplerState(1,D3DTEXF_LINEAR);
  113. Device->SetSamplerState(1,D3DTEXF_LINEAR);
  114. Device->SetSamplerState(1,D3DTEXF_LINEAR);
  115. Device->SetSamplerState(3,D3DTEXF_LINEAR);
  116. Device->SetSamplerState(3,D3DTEXF_LINEAR);
  117. Device->SetSamplerState(3,D3DTEXF_LINEAR);
  118. //
  119. //Settheprojectionmatrix.
  120. //
  121. D3DXMATRIXproj;
  122. D3DXMatrixPerspectiveFovLH(
  123. &proj,
  124. D3DX_PI*0.5f,//90-degree
  125. (float)Width/(float)Height,
  126. 1.0f,
  127. 1000.0f);
  128. Device->SetTransform(D3DTS_PROJECTION,&proj);
  129. //设置雾效果
  130. SetFogEffects(caps,true);
  131. Device->SetRenderState(D3DRS_LIGHTING,true);
  132. Device->SetRenderState(D3DRS_AMBIENT,D3DXCOLOR(0.3f,1.0f));
  133. Device->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);//不剔除任何面
  134. returntrue;
  135. }
  136. voidCleanup()
  137. {
  138. Frame.Delete<CSprite*>(Point);
  139. Frame.Delete<CBox*>(Box2);
  140. Frame.Delete<CBox*>(Box1);
  141. Frame.Delete<CGround*>(Ground);
  142. Frame.Delete<CDrawtext*>(Text);
  143. Frame.Release<LPD3DXFONT>(Font);
  144. Frame.Release<IDirect3DTexture9*>(Texture3);
  145. Frame.Release<IDirect3DTexture9*>(Texture2);
  146. Frame.Release<IDirect3DTexture9*>(Texture1);
  147. }
  148. //设置摄像机函数
  149. voidSetMatrixLookAt(D3DXMATRIX*V,D3DXVECTOR3*position,D3DXVECTOR3*target,D3DXVECTOR3*up)
  150. {
  151. D3DXMatrixLookAtLH(V,position,target,up);//设置摄像机
  152. Device->SetTransform(D3DTS_VIEW,V);//视图转换
  153. }
  154. //设置雾特效函数
  155. voidSetFogEffects(D3DCAPS9caps,boolisFogEnable)
  156. {
  157. //雾特效的开始和结束距离数值
  158. floatstart=12,end=15;
  159. //设置雾的相关属性
  160. Device->SetRenderState(D3DRS_FOGENABLE,isFogEnable);//启用雾效果
  161. //设置要渲染的雾的期望颜色(这里为灰色)
  162. Device->SetRenderState(D3DRS_FOGCOLOR,D3DCOLOR_XRGB(128,128,128));
  163. //使用顶点雾
  164. Device->SetRenderState(D3DRS_FOGVERTEXMODE,D3DFOG_LINEAR);
  165. //指定雾的开始距离和终点距离
  166. Device->SetRenderState(D3DRS_FOGSTART,*(DWORD*)(&start));
  167. Device->SetRenderState(D3DRS_FOGEND,*(DWORD*)(&end));
  168. //PixelFog像素雾
  169. //Device->SetRenderState(D3DRS_FOGTABLEMODE,D3DFOG_LINEAR);
  170. //如果支持,则启用基于距离的雾
  171. if(caps.RasterCaps&D3DPRASTERCAPS_FOGRANGE)
  172. Device->SetRenderState(D3DRS_RANGEFOGENABLE,true);
  173. }
  174. //数值计算函数
  175. boolCalculation(void)
  176. {
  177. //*start:立方体1(箱子)的矩阵变化数值计算
  178. Rxyz1.x+=0.005f;
  179. Rxyz1.y+=0.005f;
  180. Rxyz1.z+=0.005f;
  181. if(Rxyz1.x>=2*D3DX_PI)//当角度达到360时,赋值为零
  182. Rxyz1.x=0.0f;
  183. if(Rxyz1.y>=2*D3DX_PI)//当角度达到360时,赋值为零
  184. Rxyz1.y=0.0f;
  185. if(Rxyz1.z>=2*D3DX_PI)//当角度达到360时,赋值为零
  186. Rxyz1.z=0.0f;
  187. //end:立方体1(箱子)数值计算;
  188. //*start:文字绘制矩形区域数值变化
  189. timeConversion+=0.1f;//速度同步数值累加
  190. if(timeConversion>=2.0f)//timeConversion累加到2.0f是,执行文字矩形框的数值计算
  191. {
  192. timeConversion=0.1f;//重置初始值
  193. FontRect.left+=1;
  194. FontRect.top+=1;
  195. FontRect.right-=1;
  196. FontRect.bottom-=1;
  197. if(FontRect.left>=Width/2)//当矩形left大于100是重置为零
  198. FontRect.left=0;
  199. if(FontRect.top>=Height/2)//当矩形top大于100是重置为零
  200. FontRect.top=0;
  201. if(FontRect.right<=Width/2)//当矩形right小于400是重置为Width(窗口宽度)
  202. FontRect.right=Width;
  203. if(FontRect.bottom<=Height/2)//当矩形bottom小于200是重置为Height(窗口高度)
  204. FontRect.bottom=Height;
  205. }
  206. returntrue;
  207. }
  208. boolDisplay(floattimeDelta)
  209. {
  210. if(Device)//如果设备对象不为空
  211. {
  212. //数值计算;
  213. Calculation();
  214. Device->Clear(0,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(00,00,00),0);
  215. //开始渲染
  216. Device->BeginScene();
  217. Device->SetTransform(D3DTS_VIEW,&V);
  218. //绘制立方体
  219. Box1->SetCalculation(Txyz1,Rxyz1);//设置数值计算后的值
  220. Box1->Draw(&material,Texture1);//绘制最终立方体
  221. Box2->SetCalculation(D3DXVECTOR3(3,-3),D3DXVECTOR3(0,0));
  222. Box2->Draw(&material,Texture1);//绘制最终立方体
  223. Ground->DrawGround(&Map,&material,Texture2);//绘制地形(地图)
  224. //显示文字
  225. Text->DrawTextA(NULL,"雾特效,河流底面,雪花飘!~简单演示~",
  226. -1,&FontRect,DT_CENTER,D3DCOLOR_XRGB(255,255,255));
  227. //绘制sprite点
  228. Point->Draw(Texture3);
  229. //结束渲染
  230. Device->EndScene();
  231. Device->Present(0,0);
  232. }
  233. returntrue;
  234. }
  235. //
  236. //消息处理
  237. //
  238. LRESULTCALLBACKWndProc(HWNDhwnd,UINTmsg,WPARAMwParam,LPARAMlParam)
  239. {
  240. switch(msg)
  241. {
  242. caseWM_DESTROY:
  243. ::PostQuitMessage(0);
  244. break;
  245. caseWM_KEYDOWN:
  246. switch(wParam)
  247. {
  248. caseVK_ESCAPE:
  249. ::DestroyWindow(hwnd);
  250. break;
  251. caseVK_LEFT:
  252. target.x+=0.2f;//摄像机观察目标向左移动
  253. position.x+=0.2f;//摄像机位置向左移动
  254. SetMatrixLookAt(&V,&up);//设置摄像机函数
  255. break;
  256. caseVK_RIGHT:
  257. target.x-=0.2f;//摄像机观察目标向右移动
  258. position.x-=0.2f;//摄像机位置向右移动
  259. SetMatrixLookAt(&V,&up);//设置摄像机函数
  260. break;
  261. caseVK_UP:
  262. target.z-=0.2f;//摄像机观察目标向前移动
  263. position.z-=0.2f;//摄像机向前移动
  264. SetMatrixLookAt(&V,&up);//设置摄像机函数
  265. break;
  266. caseVK_DOWN:
  267. target.z+=0.2f;//摄像机观察目标向后移动
  268. position.z+=0.2f;//摄像机位置向后移动
  269. SetMatrixLookAt(&V,&up);//设置摄像机函数
  270. break;
  271. caseVK_SHIFT:
  272. position.y+=0.2f;//摄像机位置向上移动
  273. SetMatrixLookAt(&V,&up);//设置摄像机函数
  274. break;
  275. caseVK_CONTROL:
  276. position.y-=0.2f;//摄像机位置向下移动
  277. SetMatrixLookAt(&V,&up);//设置摄像机函数
  278. break;
  279. caseVK_SPACE:
  280. isFogEnable=(isFogEnable==1?0:1);//启动或关闭效果
  281. SetFogEffects(caps,isFogEnable);
  282. break;
  283. }
  284. }
  285. return::DefWindowProc(hwnd,msg,wParam,lParam);
  286. }
  287. //
  288. //入口函数
  289. //
  290. intWINAPIWinMain(HINSTANCEhinstance,
  291. HINSTANCEprevInstance,
  292. PSTRcmdLine,
  293. intshowCmd)
  294. {
  295. WNDCLASSwc;
  296. wc.style=CS_HREDRAW|CS_VREDRAW;
  297. wc.lpfnWndProc=(WNDPROC)WndProc;
  298. wc.cbClsExtra=0;
  299. wc.cbWndExtra=0;
  300. wc.hInstance=hinstance;
  301. wc.hIcon=LoadIcon(0,IDI_APPLICATION);
  302. wc.hCursor=LoadCursor(0,IDC_ARROW);
  303. wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
  304. wc.lpszMenuName=0;
  305. wc.lpszClassName="Direct3D9App";
  306. if(!RegisterClass(&wc))
  307. {
  308. ::MessageBox(0,"RegisterClass()-Failed",0);
  309. returnfalse;
  310. }
  311. //初始化D3D
  312. if(!Frame.InitD3D(hinstance,
  313. 640,480,true,D3DDEVTYPE_HAL,&Device))
  314. {
  315. ::MessageBox(0,"InitD3D()-Failed",0);
  316. return0;
  317. }
  318. if(!Setup())
  319. {
  320. ::MessageBox(0,"Setup()-Failed",0);
  321. return0;
  322. }
  323. //进入游戏循环
  324. Frame.EnterMsgLoop(Display);
  325. Cleanup();
  326. Device->Release();
  327. return0;
  328. }

两个粒子系统文件

Code:
  1. //////////////////////////////////////////////////////////////////////////////////////////////////
  2. //文件:Sprite.cpp
  3. //作者:CYM
  4. //*简单的粒子类
  5. //////////////////////////////////////////////////////////////////////////////////////////////////
  6. #pragmaonce
  7. #include<d3d9.h>
  8. #include<d3dx9.h>
  9. #include"Vertex.h"
  10. #definecount1000
  11. structSPRITE
  12. {
  13. D3DXVECTOR3pos;//位置
  14. floatspeed;//粒子速度
  15. };
  16. classCSprite
  17. {
  18. public:
  19. CSprite(IDirect3DDevice9*device);
  20. ~CSprite(void);
  21. public:
  22. voidDraw(IDirect3DTexture9*texture);//显示图像函数
  23. protected:
  24. voidUpdate(void);//[内]更新数据函数
  25. inlineunsignedlongFtoDW(floatv);//[内]数值转换函数
  26. public:
  27. IDirect3DDevice9*_device;//定义D3D设备
  28. IDirect3DVertexBuffer9*_vb;//定义顶点缓存
  29. IDirect3DTexture9*_texture;//定义文理
  30. protected:
  31. //sprite属性
  32. SPRITE_sprite[count];//定义1000个粒子
  33. //int_count;//粒子数量
  34. unsignedlong_col;//颜色
  35. //D3DXMATRIX*matrix;//移动矩阵
  36. };
Code:
  1. #include"Sprite.h"
  2. CSprite::CSprite(IDirect3DDevice9*device)
  3. {
  4. _device=device;//获取设备对象
  5. _col=D3DCOLOR_XRGB(255,00);//初始化颜色
  6. for(inti=0;i<=count;i++)
  7. {
  8. //初始化位置坐标和速度
  9. _sprite[i].pos=D3DXVECTOR3(0.0f,0.0f);
  10. _sprite[i].speed=0.0f;
  11. }
  12. _device->CreateVertexBuffer(
  13. count*sizeof(Vertex3),
  14. D3DUSAGE_WRITEONLY,
  15. FVF_VERTEX3,
  16. D3DPOOL_MANAGED,
  17. &_vb,
  18. 0);
  19. }
  20. CSprite::~CSprite(void)
  21. {
  22. if(_vb){_vb->Release();_vb=0;}
  23. }
  24. //数值转换函数
  25. unsignedlongCSprite::FtoDW(floatv)
  26. {
  27. return*((unsignedlong*)&v);
  28. }
  29. //显示图像函数
  30. voidCSprite::Draw(IDirect3DTexture9*texture)
  31. {
  32. _texture=texture;
  33. _device->SetTexture(0,_texture);
  34. Update();//更新数据
  35. //启用点状sprite
  36. _device->SetRenderState(D3DRS_POINTSPRITEENABLE,TRUE);
  37. //启用点状sprite的比例
  38. _device->SetRenderState(D3DRS_POINTSCALEENABLE,TRUE);
  39. //开启混合因子
  40. _device->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
  41. //设置目标混合因子
  42. _device->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ONE);
  43. //设置sprite的尺寸
  44. _device->SetRenderState(D3DRS_POINTSIZE,FtoDW(0.1f));
  45. //设置点状sprite的最小尺寸
  46. _device->SetRenderState(D3DRS_POINTSIZE_MIN,FtoDW(0.0f));
  47. //根据距离更改点状sprite的形状
  48. _device->SetRenderState(D3DRS_POINTSCALE_A,FtoDW(0.0f));
  49. //根据距离更改点状sprite的形状
  50. _device->SetRenderState(D3DRS_POINTSCALE_B,FtoDW(0.0f));
  51. //根据距离更改点状sprite的形状
  52. _device->SetRenderState(D3DRS_POINTSCALE_C,FtoDW(1.0f));
  53. _device->SetFVF(FVF_VERTEX3);
  54. _device->SetStreamSource(0,_vb,sizeof(Vertex3));
  55. _device->DrawPrimitive(D3DPT_TRIANGLESTRIP,count);
  56. _device->SetRenderState(D3DRS_POINTSPRITEENABLE,FALSE);
  57. _device->SetRenderState(D3DRS_POINTSCALEENABLE,FALSE);
  58. _device->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);
  59. }
  60. //更新数据函数
  61. voidCSprite::Update()
  62. {
  63. Vertex3*v;//顶点
  64. _vb->Lock(0,(void**)&v,0);
  65. //填充数据X正半轴,Z负半轴粒子顶点缓存数据
  66. for(inti=0;i<count/4;i++)
  67. {
  68. _sprite[i].pos.y-=0.01f*_sprite[i].speed;
  69. if(_sprite[i].pos.y<0.0f)
  70. {
  71. ::ZeroMemory(&v[i],sizeof(v[i]));//清空数据
  72. ::ZeroMemory(&_sprite[i],sizeof(_sprite[i]));//清空数据
  73. ::ZeroMemory(&_sprite[i].speed,sizeof(_sprite[i].speed));//清空数据
  74. _sprite[i].speed=((rand()%20)*0.1f);
  75. _sprite[i].pos.x=((rand()%100)*0.1f);//随机获得x轴坐标
  76. _sprite[i].pos.y=5;//获得固定高度5
  77. _sprite[i].pos.z=((rand()%100)*-0.1f);//随机获得z轴坐标
  78. }
  79. v[i]=Vertex3(_sprite[i].pos.x,_sprite[i].pos.y,_sprite[i].pos.z,255));
  80. }
  81. //填充数据X负半轴,Z负半轴粒子顶点缓存数据
  82. for(inti=count/4;i<count/2;i++)
  83. {
  84. _sprite[i].pos.y-=0.01f*_sprite[i].speed;
  85. if(_sprite[i].pos.y<0.0f)
  86. {
  87. ::ZeroMemory(&v[i],sizeof(_sprite[i].speed));//清空数据
  88. _sprite[i].speed=((rand()%20)*0.1f);
  89. _sprite[i].pos.x=((rand()%100)*-0.1f);//随机获得x轴坐标
  90. _sprite[i].pos.y=5.0f;//获得固定高度5
  91. _sprite[i].pos.z=((rand()%100)*-0.1f);//随机获得z轴坐标
  92. }
  93. v[i]=Vertex3(_sprite[i].pos.x,255));
  94. }
  95. //填充数据X正半轴,Z正半轴粒子顶点缓存数据
  96. for(inti=count/2;i<count*3/4;i++)
  97. {
  98. _sprite[i].pos.y-=0.01f*_sprite[i].speed;
  99. if(_sprite[i].pos.y<0.0f)
  100. {
  101. ::ZeroMemory(&v[i],sizeof(v[i]));//清空数据
  102. ::ZeroMemory(&_sprite[i],sizeof(_sprite[i]));//清空数据
  103. ::ZeroMemory(&_sprite[i].speed,sizeof(_sprite[i].speed));//清空数据
  104. _sprite[i].speed=((rand()%20)*0.1f);
  105. _sprite[i].pos.x=((rand()%100)*0.1f);//随机获得x轴坐标
  106. _sprite[i].pos.y=5.0f;//获得固定高度5
  107. _sprite[i].pos.z=((rand()%100)*0.1f);//随机获得z轴坐标
  108. }
  109. v[i]=Vertex3(_sprite[i].pos.x,Z正半轴粒子顶点缓存数据
  110. for(inti=count*3/4;i<=count;i++)
  111. {
  112. _sprite[i].pos.y-=0.01f*_sprite[i].speed;
  113. if(_sprite[i].pos.y<0.0f)
  114. {
  115. ::ZeroMemory(&v[i],sizeof(_sprite[i].speed));//清空数据
  116. _sprite[i].speed=((rand()%20)*0.1f);
  117. _sprite[i].pos.x=((rand()%100)*-0.1f);//随机获得x轴坐标
  118. _sprite[i].pos.y=5.0f;//获得固定高度5
  119. _sprite[i].pos.z=((rand()%100)*0.1f);//随机获得z轴坐标
  120. }
  121. v[i]=Vertex3(_sprite[i].pos.x,255));
  122. }
  123. _vb->Unlock();
  124. }

这个代码有点重复,但也不修改了,只是测试一下,还是有漏洞

猜你在找的VB相关文章