Snow.cpp
资源名称:3DRPG.rar [点击查看]
上传用户:cjwanglu
上传日期:2013-07-10
资源大小:4744k
文件大小:4k
源码类别:
游戏
开发平台:
Visual C++
- #include "stdafx.h"
- #include "Snow.h"
- CSnow::CSnow(LPDIRECT3DDEVICE9 d3dd,DWORD Lenth,DWORD Width,DWORD Height,DWORD Density,float offset)
- {
- m_pD3DDevice=d3dd;
- m_dwLenth=Lenth;
- m_dwWidth=Width;
- m_dwHeight=Height;
- m_dwDensity=Density;
- m_lSnowCount=(m_dwWidth*m_dwLenth*m_dwDensity*m_dwDensity);
- m_fOffset=offset;
- InitialSnow();
- }
- CSnow::~CSnow()
- {
- m_pVb->Release();
- m_pTx->Release();
- }
- void CSnow::InitialSnow()
- { SNOWVERTEX * vertex;
- D3DXCreateTextureFromFile( m_pD3DDevice, "./res/snow2.tga",&m_pTx);
- m_pD3DDevice->CreateVertexBuffer( m_lSnowCount*sizeof(SNOWVERTEX),D3DUSAGE_WRITEONLY,
- D3DFVF_SNOWVERTEX,D3DPOOL_MANAGED, &m_pVb,NULL );
- m_pVb->Lock( 0, m_lSnowCount*sizeof(SNOWVERTEX),(void**)&vertex, 0 );
- for( DWORD line = 0; line < (DWORD)(m_dwWidth*m_dwDensity) ; line++ )
- {
- //float offwidth = (float)1/m_dwDensity;
- for( DWORD seg=0; seg < (DWORD)(m_dwLenth*m_dwDensity); seg++ )
- {
- FLOAT fRandx = ((FLOAT)rand()/(FLOAT)RAND_MAX) *10.0f;
- FLOAT fRandy = ((FLOAT)rand()/(FLOAT)RAND_MAX) *10.0f;
- FLOAT fRandz = ((FLOAT)rand()/(FLOAT)RAND_MAX)*10.0f;
- //float offlenth = (float)1/m_dwDensity;
- vertex->p.x = -(float)m_dwWidth/2 + seg * ( fRandx);
- vertex->p.y = (float)m_dwHeight + fRandy;
- vertex->p.z = -(float)m_dwLenth/2 + line *(fRandz);
- vertex->color=D3DCOLOR_XRGB(255,255,255);
- vertex++;
- }
- }
- m_pVb->Unlock();
- }
- void CSnow::MoveSnow(D3DXVECTOR3 k,D3DXVECTOR3 j)
- {
- SNOWVERTEX * vertex=NULL;
- m_pVb->Lock(0,m_lSnowCount*sizeof(SNOWVERTEX),(void **)&vertex,0);
- /* for( DWORD seg=0; seg < m_lSnowCount; seg++ )
- {
- if(vertex->p.y < 0)
- vertex->p.y = (float)m_dwHeight;
- else
- vertex->p.y -= offset;
- vertex++;
- }*/
- /* for(DWORD i=0;i<m_lSnowCount;i++)
- {
- vertex->p.x=(i/10)*k.x;
- vertex->p.y=(i/10)*k.y;
- vertex->p.z=(i/10)*k.z;
- vertex->p.x=(i/10)*k.x+j.x;
- vertex->p.y=(i/10)*k.y+j.y;
- vertex->p.z=(i/10)*k.z+j.z;
- vertex++;
- }*/
- m_pVb->Unlock();
- }
- void CSnow::DrawSnow()
- {
- // D3DXMATRIX matWorld;
- //D3DXMatrixTranslation( &matWorld, x,y,z );
- //m_pD3DDevice->SetTransform( D3DTS_WORLD, &matWorld );
- SetMaterial();
- m_pD3DDevice->SetTexture( 0, m_pTx );
- m_pD3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
- m_pD3DDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
- m_pD3DDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
- m_pD3DDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
- m_pD3DDevice->SetRenderState( D3DRS_ALPHAREF, 0x0f);
- m_pD3DDevice->SetRenderState( D3DRS_ALPHAFUNC,D3DCMP_GREATEREQUAL );
- m_pD3DDevice->SetRenderState( D3DRS_POINTSPRITEENABLE, TRUE );
- m_pD3DDevice->SetRenderState( D3DRS_POINTSCALEENABLE, TRUE );
- m_pD3DDevice->SetRenderState( D3DRS_POINTSIZE, FtoDW(2.00f) );
- m_pD3DDevice->SetRenderState( D3DRS_POINTSIZE_MIN, FtoDW(0.00f) );
- m_pD3DDevice->SetRenderState( D3DRS_POINTSCALE_A, FtoDW(1.00f) );
- m_pD3DDevice->SetRenderState( D3DRS_POINTSCALE_B, FtoDW(1.00f) );
- m_pD3DDevice->SetRenderState( D3DRS_POINTSCALE_C, FtoDW(1.00f) );
- m_pD3DDevice->SetFVF( D3DFVF_SNOWVERTEX );
- m_pD3DDevice->SetStreamSource( 0, m_pVb,0, sizeof(SNOWVERTEX) );
- m_pD3DDevice->DrawPrimitive( D3DPT_POINTLIST,
- 0, m_lSnowCount);
- /*MoveSnow(0.3f);
- m_pD3DDevice->SetFVF( D3DFVF_SNOWVERTEX );
- m_pD3DDevice->SetStreamSource( 0, m_pVb,0, sizeof(SNOWVERTEX) );
- m_pD3DDevice->DrawPrimitive( D3DPT_POINTLIST,
- 0, m_lSnowCount);
- m_pD3DDevice->SetRenderState( D3DRS_ALPHATESTENABLE, FALSE );
- m_pD3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
- MoveSnow(m_fOffset);*/
- }
- void CSnow::RenderSnow()
- { // m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
- DrawSnow();
- }
- void CSnow::SetMaterial()
- {
- ZeroMemory(&mtrl,sizeof(mtrl));
- mtrl.Diffuse.r=mtrl.Ambient.r=1.0f;
- mtrl.Diffuse.g=mtrl.Ambient.g=1.0f;
- mtrl.Diffuse.b=mtrl.Ambient.b=1.0f;
- mtrl.Diffuse.a=mtrl.Ambient.a=1.0f;
- mtrl.Specular.r=0.0f;
- mtrl.Specular.g=0.0f;
- mtrl.Specular.b=0.0f;
- mtrl.Specular.a=0.0f;
- mtrl.Emissive.r=1.0f;
- mtrl.Emissive.g=1.0f;
- mtrl.Emissive.b=1.0f;
- mtrl.Emissive.a=1.0f;
- m_pD3DDevice->SetMaterial( &mtrl );
- }