Weahter.cpp
上传用户:henghua
上传日期:2007-11-14
资源大小:7655k
文件大小:8k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. #include "Weather.h"
  2. #ifdef ENABLE_RAIN
  3. CRain::CRain(LPDIRECT3DDEVICE9 device,CConfiguration *config, int particlenum, D3DXVECTOR3 &v, float radius )
  4. {
  5. this->particlenum = particlenum;
  6. this->particlearray = new CRainParticle[this->particlenum * MAX_LEVEL];
  7. D3DXVec3Normalize(&this->dir, &v);
  8. this->device = device;
  9. this->radius = radius;
  10. this->config = config;
  11. }
  12. CRain::~CRain()
  13. {
  14. delete []this->particlearray;
  15. this->pRainVB->Release();
  16. }
  17. bool CRain::Init()
  18. {
  19. int i;
  20. float length = 2;
  21. float angle;
  22. float r;
  23. CUSTOMVERTEX * pRainVertices;
  24. for( i = 0; i < this->particlenum * MAX_LEVEL; i++)
  25. {
  26. angle = float(rand() ) * 2 * D3DX_PI / (float)RAND_MAX;
  27. r = radius * float(rand()) / (float)RAND_MAX;
  28. this->particlearray[i].pos.x = (*cam)->position.x + r * cos(angle);
  29. this->particlearray[i].pos.z = (*cam)->position.z + r * sin(angle);
  30. this->particlearray[i].pos.y = 300;
  31. this->particlearray[i].speed = 30 + rand() % 30;
  32. }
  33. if(FAILED(( device->CreateVertexBuffer( numofvtx * sizeof( CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &this->pRainVB, NULL))))
  34. {
  35. return false;
  36. }
  37. //Set the Shape of the Rain Particle
  38. if(FAILED( this->pRainVB->Lock(0, numofvtx * sizeof(CUSTOMVERTEX), (void**)&pRainVertices, 0)))
  39. {
  40. return false;
  41. }
  42. {
  43. pRainVertices[0].x = 0;
  44. pRainVertices[0].y = 0;
  45. pRainVertices[0].z = 0;
  46. pRainVertices[0].color = 0x7fffffff;
  47. pRainVertices[1].x = this->dir.x * length;
  48. pRainVertices[1].y = this->dir.y * length;
  49. pRainVertices[1].z = this->dir.z * length;
  50. pRainVertices[1].color = 0x7fffffff;
  51. }
  52. this->pRainVB->Unlock();
  53. return true;
  54. }
  55. bool  CRain::Update(float fElapsedTime)
  56. {
  57. int i;
  58. float angle;
  59. float r;
  60. for(i = 0; i < this->particlenum * config->get_float(p_bRainLevel); i++)
  61. {
  62. this->particlearray[i].pos += this->particlearray[i].speed * fElapsedTime * this->dir;
  63. if(this->particlearray[i].pos.y < -10)
  64. {
  65. angle = float(rand() ) * 2 * D3DX_PI / (float)RAND_MAX;
  66. r = radius * float(rand()) / (float)RAND_MAX;
  67. this->particlearray[i].pos.x = r * cos(angle) + (*cam)->position.x;
  68. this->particlearray[i].pos.z = r * sin(angle) + (*cam)->position.z;
  69. this->particlearray[i].pos.y = 300;
  70. }
  71. }
  72. return true;
  73. }
  74. bool CRain::Render()
  75. {
  76. if(config->get_bool(p_bisRaining))
  77. {
  78. device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
  79. device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  80. device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
  81. D3DXMATRIXA16 matWorld, tmp;
  82. device->GetTransform(D3DTS_WORLD, &matWorld);
  83. tmp = matWorld;
  84. D3DXMATRIXA16 fvproj((*cam)->view);
  85. fvproj._41 = 0;
  86. fvproj._42 = 0;
  87. fvproj._43 = 0;
  88. fvproj = fvproj * (*cam)->proj;
  89. device->SetTransform(D3DTS_PROJECTION, &fvproj);
  90. device->SetTexture(0, NULL);
  91. device->SetStreamSource(0, this->pRainVB, 0, sizeof(CUSTOMVERTEX));
  92. device->SetFVF(D3DFVF_CUSTOMVERTEX);
  93. for(int i = 0; i < this->particlenum * config->get_float(p_bRainLevel); i++)
  94. {
  95. D3DXMatrixTranslation( &matWorld, this->particlearray[i].pos.x, this->particlearray[i].pos.y, this->particlearray[i].pos.z);
  96. device->SetTransform(D3DTS_WORLD, &matWorld);
  97. device->DrawPrimitive(D3DPT_LINELIST, 0, 1);
  98. }
  99. device->SetTransform(D3DTS_WORLD, &tmp);
  100. device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
  101. return true;
  102. }
  103. return false;
  104. }
  105. #endif
  106. CSnow::CSnow(LPDIRECT3DDEVICE9 device,CConfiguration *config, int particlenum, D3DXVECTOR3 &v, float radius )
  107. {
  108. this->particlenum = particlenum;
  109. D3DXVec3Normalize(&this->dir, &v);
  110. this->device = device;
  111. this->radius = radius;
  112. this->config = config;
  113. this->inited = false;
  114. }
  115. CSnow::~CSnow()
  116. {
  117. delete []this->particlearray;
  118. this->pSnowVB->Release();
  119. }
  120. bool CSnow::Init()
  121. {
  122. float length = 2;
  123. float angle;
  124. float r;
  125. this->particlearray = new CSnowParticle[this->particlenum ];
  126. //创建雪花粒子顶点缓冲区
  127.     if( FAILED( device->CreateVertexBuffer( 4*sizeof(SNOWVERTEX), 0, D3DFVF_SNOWVERTEX, D3DPOOL_MANAGED, &pSnowVB, NULL )))
  128. return false;   
  129.     //填充雪花粒子顶点缓冲区
  130. SNOWVERTEX vertices[] =
  131.     {
  132. { -0.1, 0, 0,   0.0f, 1.0f, },
  133.         { -0.1, 0.2, 0,   0.0f, 0.0f, },
  134.         {  0.1, 0, 0,   1.0f, 1.0f, }, 
  135. {  0.1, 0.2, 0,   1.0f, 0.0f, }
  136.     };
  137.     VOID* pVertices;
  138.     if( FAILED( pSnowVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
  139.         return false;
  140.     memcpy( pVertices, vertices, sizeof(vertices) );
  141. pSnowVB->Unlock();
  142. //创建雪花纹理
  143. if(FAILED(D3DXCreateTextureFromFile( device, "textures\snow1.bmp", &pSnowTex[0] )))
  144. {
  145. MessageBox(NULL, "ERROR WHILE LOADING SNOW TEXTURE1", "OceanFlame", MB_OK);
  146. return false;
  147. }
  148. if(FAILED(D3DXCreateTextureFromFile( device, "textures\snow2.bmp", &pSnowTex[1] )))
  149. {
  150. MessageBox(NULL, "ERROR WHILE LOADING SNOW TEXTURE2", "OceanFlame", MB_OK);
  151. return false;
  152. }
  153. if(FAILED(D3DXCreateTextureFromFile( device, "textures\snow3.bmp", &pSnowTex[2] )))
  154. {
  155. MessageBox(NULL, "ERROR WHILE LOADING SNOW TEXTURE3", "OceanFlame", MB_OK);
  156. return false;
  157. }
  158. return true;
  159. }
  160. void CSnow::InitPos()
  161. {
  162. float angle, r;
  163. for(int i=0; i<this->particlenum ; i++)
  164. {
  165. angle = float(rand() ) * 2 * D3DX_PI / (float)RAND_MAX;
  166. r = radius * float(rand()) / (float)RAND_MAX;
  167. this->particlearray[i].x = r * cos(angle) + (*cam)->position.x;
  168. this->particlearray[i].z = r * sin(angle) + (*cam)->position.z;
  169. this->particlearray[i].y        = float(rand()%30);
  170. this->particlearray[i].fYaw     = (rand()%100)/50.0f*D3DX_PI;
  171. this->particlearray[i].fPitch   = (rand()%100)/50.0f*D3DX_PI;
  172. this->particlearray[i].Dspeed   = 10.0f + rand()%10;
  173. this->particlearray[i].Rspeed   = 1.0f +  rand()%10/10.0f;
  174. this->particlearray[i].TexIndex = rand()%3;
  175. }
  176. }
  177. bool  CSnow::Update(float fElapsedTime)
  178. {
  179. float r, angle;
  180. for(int i=0; i<this->particlenum ; i++)
  181. {
  182. this->particlearray[i].y -= this->particlearray[i].Dspeed * fElapsedTime;
  183. if(this->particlearray[i].y<0)
  184. {
  185. this->particlearray[i].y = 30.0f;
  186. angle = float(rand() ) * 2 * D3DX_PI / (float)RAND_MAX;
  187. r = radius * float(rand()) / (float)RAND_MAX;
  188. this->particlearray[i].x = r * cos(angle) + (*cam)->position.x;
  189. this->particlearray[i].z = r * sin(angle) + (*cam)->position.z;
  190. }
  191. this->particlearray[i].fYaw    += this->particlearray[i].Rspeed * fElapsedTime;
  192. this->particlearray[i].fPitch  += this->particlearray[i].Rspeed * fElapsedTime;
  193. }
  194. return true;
  195. }
  196. bool CSnow::Render()
  197. {
  198. static D3DXMATRIX matYaw, matPitch, matTrans;
  199. if(config->get_bool(p_bisSnowing))
  200. {
  201. if(!inited)
  202. {
  203. this->InitPos();
  204. inited = true;
  205. }
  206. device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
  207. device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  208. device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
  209. D3DXMATRIXA16 matWorld, tmp;
  210. device->GetTransform(D3DTS_WORLD, &matWorld);
  211. tmp = matWorld;
  212. device->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  213. device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  214. device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  215. device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  216. device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
  217. device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
  218. device->SetRenderState( D3DRS_LIGHTING, false);
  219. device->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE);
  220. for(int i=0; i < this->particlenum ; i++)
  221. {
  222.     //构造并设置当前雪花粒子的世界矩阵
  223. D3DXMatrixRotationY(&matYaw, this->particlearray[i].fYaw);
  224. D3DXMatrixRotationX(&matPitch, this->particlearray[i].fPitch);
  225. D3DXMatrixTranslation(&matTrans, this->particlearray[i].x, this->particlearray[i].y, this->particlearray[i].z);
  226. matWorld = matYaw * matPitch * matTrans;
  227. device->SetTransform( D3DTS_WORLD,  &matWorld);
  228. //渲染当前雪花粒子
  229. device->SetTexture( 0, pSnowTex[this->particlearray[i].TexIndex] ); 
  230. device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
  231. device->SetStreamSource(0, pSnowVB, 0, sizeof(SNOWVERTEX));
  232. device->SetFVF(D3DFVF_SNOWVERTEX);
  233. device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
  234. device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
  235. }
  236. device->SetTransform(D3DTS_WORLD, &tmp);
  237. return true;
  238. }
  239. else
  240. {
  241. if(inited = true)
  242. inited = false;
  243. }
  244. return false;
  245. }