Weahter.cpp
上传用户:henghua
上传日期:2007-11-14
资源大小:7655k
文件大小:8k
- #include "Weather.h"
- #ifdef ENABLE_RAIN
- CRain::CRain(LPDIRECT3DDEVICE9 device,CConfiguration *config, int particlenum, D3DXVECTOR3 &v, float radius )
- {
- this->particlenum = particlenum;
- this->particlearray = new CRainParticle[this->particlenum * MAX_LEVEL];
- D3DXVec3Normalize(&this->dir, &v);
- this->device = device;
- this->radius = radius;
- this->config = config;
- }
- CRain::~CRain()
- {
- delete []this->particlearray;
- this->pRainVB->Release();
- }
- bool CRain::Init()
- {
- int i;
- float length = 2;
- float angle;
- float r;
-
- CUSTOMVERTEX * pRainVertices;
- for( i = 0; i < this->particlenum * MAX_LEVEL; i++)
- {
- angle = float(rand() ) * 2 * D3DX_PI / (float)RAND_MAX;
- r = radius * float(rand()) / (float)RAND_MAX;
- this->particlearray[i].pos.x = (*cam)->position.x + r * cos(angle);
- this->particlearray[i].pos.z = (*cam)->position.z + r * sin(angle);
- this->particlearray[i].pos.y = 300;
-
- this->particlearray[i].speed = 30 + rand() % 30;
- }
- if(FAILED(( device->CreateVertexBuffer( numofvtx * sizeof( CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &this->pRainVB, NULL))))
- {
- return false;
- }
- //Set the Shape of the Rain Particle
- if(FAILED( this->pRainVB->Lock(0, numofvtx * sizeof(CUSTOMVERTEX), (void**)&pRainVertices, 0)))
- {
- return false;
- }
- {
- pRainVertices[0].x = 0;
- pRainVertices[0].y = 0;
- pRainVertices[0].z = 0;
- pRainVertices[0].color = 0x7fffffff;
- pRainVertices[1].x = this->dir.x * length;
- pRainVertices[1].y = this->dir.y * length;
- pRainVertices[1].z = this->dir.z * length;
- pRainVertices[1].color = 0x7fffffff;
- }
- this->pRainVB->Unlock();
- return true;
- }
- bool CRain::Update(float fElapsedTime)
- {
- int i;
- float angle;
- float r;
-
- for(i = 0; i < this->particlenum * config->get_float(p_bRainLevel); i++)
- {
- this->particlearray[i].pos += this->particlearray[i].speed * fElapsedTime * this->dir;
- if(this->particlearray[i].pos.y < -10)
- {
- angle = float(rand() ) * 2 * D3DX_PI / (float)RAND_MAX;
- r = radius * float(rand()) / (float)RAND_MAX;
-
- this->particlearray[i].pos.x = r * cos(angle) + (*cam)->position.x;
- this->particlearray[i].pos.z = r * sin(angle) + (*cam)->position.z;
- this->particlearray[i].pos.y = 300;
- }
- }
- return true;
- }
- bool CRain::Render()
- {
- if(config->get_bool(p_bisRaining))
- {
- device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
- device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
- device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
- D3DXMATRIXA16 matWorld, tmp;
- device->GetTransform(D3DTS_WORLD, &matWorld);
- tmp = matWorld;
- D3DXMATRIXA16 fvproj((*cam)->view);
- fvproj._41 = 0;
- fvproj._42 = 0;
- fvproj._43 = 0;
- fvproj = fvproj * (*cam)->proj;
- device->SetTransform(D3DTS_PROJECTION, &fvproj);
- device->SetTexture(0, NULL);
- device->SetStreamSource(0, this->pRainVB, 0, sizeof(CUSTOMVERTEX));
- device->SetFVF(D3DFVF_CUSTOMVERTEX);
- for(int i = 0; i < this->particlenum * config->get_float(p_bRainLevel); i++)
- {
- D3DXMatrixTranslation( &matWorld, this->particlearray[i].pos.x, this->particlearray[i].pos.y, this->particlearray[i].pos.z);
- device->SetTransform(D3DTS_WORLD, &matWorld);
- device->DrawPrimitive(D3DPT_LINELIST, 0, 1);
- }
- device->SetTransform(D3DTS_WORLD, &tmp);
- device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
- return true;
- }
- return false;
- }
- #endif
- CSnow::CSnow(LPDIRECT3DDEVICE9 device,CConfiguration *config, int particlenum, D3DXVECTOR3 &v, float radius )
- {
- this->particlenum = particlenum;
- D3DXVec3Normalize(&this->dir, &v);
- this->device = device;
- this->radius = radius;
- this->config = config;
- this->inited = false;
- }
- CSnow::~CSnow()
- {
- delete []this->particlearray;
- this->pSnowVB->Release();
- }
- bool CSnow::Init()
- {
- float length = 2;
- float angle;
- float r;
- this->particlearray = new CSnowParticle[this->particlenum ];
-
- //创建雪花粒子顶点缓冲区
- if( FAILED( device->CreateVertexBuffer( 4*sizeof(SNOWVERTEX), 0, D3DFVF_SNOWVERTEX, D3DPOOL_MANAGED, &pSnowVB, NULL )))
- return false;
- //填充雪花粒子顶点缓冲区
- SNOWVERTEX vertices[] =
- {
- { -0.1, 0, 0, 0.0f, 1.0f, },
- { -0.1, 0.2, 0, 0.0f, 0.0f, },
- { 0.1, 0, 0, 1.0f, 1.0f, },
- { 0.1, 0.2, 0, 1.0f, 0.0f, }
- };
- VOID* pVertices;
- if( FAILED( pSnowVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
- return false;
- memcpy( pVertices, vertices, sizeof(vertices) );
- pSnowVB->Unlock();
- //创建雪花纹理
- if(FAILED(D3DXCreateTextureFromFile( device, "textures\snow1.bmp", &pSnowTex[0] )))
- {
- MessageBox(NULL, "ERROR WHILE LOADING SNOW TEXTURE1", "OceanFlame", MB_OK);
- return false;
- }
- if(FAILED(D3DXCreateTextureFromFile( device, "textures\snow2.bmp", &pSnowTex[1] )))
- {
- MessageBox(NULL, "ERROR WHILE LOADING SNOW TEXTURE2", "OceanFlame", MB_OK);
- return false;
- }
- if(FAILED(D3DXCreateTextureFromFile( device, "textures\snow3.bmp", &pSnowTex[2] )))
- {
- MessageBox(NULL, "ERROR WHILE LOADING SNOW TEXTURE3", "OceanFlame", MB_OK);
- return false;
- }
- return true;
- }
- void CSnow::InitPos()
- {
- float angle, r;
- for(int i=0; i<this->particlenum ; i++)
- {
- angle = float(rand() ) * 2 * D3DX_PI / (float)RAND_MAX;
- r = radius * float(rand()) / (float)RAND_MAX;
- this->particlearray[i].x = r * cos(angle) + (*cam)->position.x;
- this->particlearray[i].z = r * sin(angle) + (*cam)->position.z;
- this->particlearray[i].y = float(rand()%30);
- this->particlearray[i].fYaw = (rand()%100)/50.0f*D3DX_PI;
- this->particlearray[i].fPitch = (rand()%100)/50.0f*D3DX_PI;
- this->particlearray[i].Dspeed = 10.0f + rand()%10;
- this->particlearray[i].Rspeed = 1.0f + rand()%10/10.0f;
- this->particlearray[i].TexIndex = rand()%3;
- }
- }
- bool CSnow::Update(float fElapsedTime)
- {
- float r, angle;
- for(int i=0; i<this->particlenum ; i++)
- {
-
- this->particlearray[i].y -= this->particlearray[i].Dspeed * fElapsedTime;
- if(this->particlearray[i].y<0)
- {
- this->particlearray[i].y = 30.0f;
- angle = float(rand() ) * 2 * D3DX_PI / (float)RAND_MAX;
- r = radius * float(rand()) / (float)RAND_MAX;
- this->particlearray[i].x = r * cos(angle) + (*cam)->position.x;
- this->particlearray[i].z = r * sin(angle) + (*cam)->position.z;
- }
- this->particlearray[i].fYaw += this->particlearray[i].Rspeed * fElapsedTime;
- this->particlearray[i].fPitch += this->particlearray[i].Rspeed * fElapsedTime;
- }
- return true;
- }
- bool CSnow::Render()
- {
- static D3DXMATRIX matYaw, matPitch, matTrans;
- if(config->get_bool(p_bisSnowing))
- {
- if(!inited)
- {
- this->InitPos();
- inited = true;
- }
- device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
- device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
- device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
- D3DXMATRIXA16 matWorld, tmp;
- device->GetTransform(D3DTS_WORLD, &matWorld);
- tmp = matWorld;
- device->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
- device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
- device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
- device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
- device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
- device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
- device->SetRenderState( D3DRS_LIGHTING, false);
- device->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE);
- for(int i=0; i < this->particlenum ; i++)
- {
- //构造并设置当前雪花粒子的世界矩阵
-
- D3DXMatrixRotationY(&matYaw, this->particlearray[i].fYaw);
- D3DXMatrixRotationX(&matPitch, this->particlearray[i].fPitch);
- D3DXMatrixTranslation(&matTrans, this->particlearray[i].x, this->particlearray[i].y, this->particlearray[i].z);
- matWorld = matYaw * matPitch * matTrans;
- device->SetTransform( D3DTS_WORLD, &matWorld);
-
-
- //渲染当前雪花粒子
- device->SetTexture( 0, pSnowTex[this->particlearray[i].TexIndex] );
- device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
- device->SetStreamSource(0, pSnowVB, 0, sizeof(SNOWVERTEX));
- device->SetFVF(D3DFVF_SNOWVERTEX);
- device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
- device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
- }
- device->SetTransform(D3DTS_WORLD, &tmp);
- return true;
- }
- else
- {
- if(inited = true)
- inited = false;
- }
- return false;
-
- }