Snow.cpp
上传用户:cjwanglu
上传日期:2013-07-10
资源大小:4744k
文件大小:4k
源码类别:

游戏

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "Snow.h"
  3. CSnow::CSnow(LPDIRECT3DDEVICE9 d3dd,DWORD Lenth,DWORD Width,DWORD Height,DWORD Density,float offset)
  4. {
  5. m_pD3DDevice=d3dd;
  6. m_dwLenth=Lenth;
  7. m_dwWidth=Width;
  8. m_dwHeight=Height;
  9. m_dwDensity=Density;
  10. m_lSnowCount=(m_dwWidth*m_dwLenth*m_dwDensity*m_dwDensity);
  11. m_fOffset=offset;
  12.    
  13.     InitialSnow();
  14. }
  15. CSnow::~CSnow()
  16. {
  17. m_pVb->Release();
  18. m_pTx->Release();
  19. }
  20. void CSnow::InitialSnow()
  21. {   SNOWVERTEX * vertex;
  22.     D3DXCreateTextureFromFile( m_pD3DDevice, "./res/snow2.tga",&m_pTx);
  23.     m_pD3DDevice->CreateVertexBuffer( m_lSnowCount*sizeof(SNOWVERTEX),D3DUSAGE_WRITEONLY,
  24.                                D3DFVF_SNOWVERTEX,D3DPOOL_MANAGED, &m_pVb,NULL );
  25.    m_pVb->Lock( 0, m_lSnowCount*sizeof(SNOWVERTEX),(void**)&vertex, 0 );
  26.      for( DWORD line = 0; line < (DWORD)(m_dwWidth*m_dwDensity) ; line++ )
  27.     {
  28. //float offwidth = (float)1/m_dwDensity;
  29.         for( DWORD seg=0; seg < (DWORD)(m_dwLenth*m_dwDensity); seg++ )
  30.         {
  31.             FLOAT fRandx = ((FLOAT)rand()/(FLOAT)RAND_MAX) *10.0f;
  32. FLOAT fRandy = ((FLOAT)rand()/(FLOAT)RAND_MAX) *10.0f;
  33.         FLOAT fRandz = ((FLOAT)rand()/(FLOAT)RAND_MAX)*10.0f;
  34.     
  35. //float offlenth = (float)1/m_dwDensity;
  36.             vertex->p.x =  -(float)m_dwWidth/2 + seg * ( fRandx);
  37.             vertex->p.y = (float)m_dwHeight + fRandy;
  38.             vertex->p.z =  -(float)m_dwLenth/2 + line *(fRandz);
  39. vertex->color=D3DCOLOR_XRGB(255,255,255);
  40. vertex++;
  41. }
  42. }
  43. m_pVb->Unlock();
  44.                                      
  45. }
  46. void CSnow::MoveSnow(D3DXVECTOR3 k,D3DXVECTOR3 j)
  47. {    
  48.      SNOWVERTEX * vertex=NULL;
  49.  m_pVb->Lock(0,m_lSnowCount*sizeof(SNOWVERTEX),(void **)&vertex,0);
  50. /* for( DWORD seg=0; seg < m_lSnowCount; seg++ )
  51.     {
  52.         if(vertex->p.y < 0)
  53. vertex->p.y = (float)m_dwHeight;
  54. else
  55. vertex->p.y -= offset;
  56.         vertex++;
  57. }*/
  58. /* for(DWORD i=0;i<m_lSnowCount;i++)
  59.  {
  60.  vertex->p.x=(i/10)*k.x;
  61.  vertex->p.y=(i/10)*k.y;
  62.  vertex->p.z=(i/10)*k.z;
  63.          vertex->p.x=(i/10)*k.x+j.x;
  64.  vertex->p.y=(i/10)*k.y+j.y;
  65.  vertex->p.z=(i/10)*k.z+j.z;
  66.            vertex++;
  67.  }*/
  68.     m_pVb->Unlock();
  69. }
  70. void CSnow::DrawSnow()
  71. {
  72.    // D3DXMATRIX matWorld;
  73. //D3DXMatrixTranslation( &matWorld, x,y,z );
  74. //m_pD3DDevice->SetTransform( D3DTS_WORLD, &matWorld );
  75.   SetMaterial();
  76. m_pD3DDevice->SetTexture( 0, m_pTx );
  77.     
  78.     
  79.    
  80.     m_pD3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE,   TRUE );
  81.     m_pD3DDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_SRCALPHA );
  82.     m_pD3DDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
  83.     m_pD3DDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
  84.     m_pD3DDevice->SetRenderState( D3DRS_ALPHAREF,        0x0f);
  85.     m_pD3DDevice->SetRenderState( D3DRS_ALPHAFUNC,D3DCMP_GREATEREQUAL );
  86.     m_pD3DDevice->SetRenderState( D3DRS_POINTSPRITEENABLE, TRUE );
  87.     m_pD3DDevice->SetRenderState( D3DRS_POINTSCALEENABLE,  TRUE );
  88.     m_pD3DDevice->SetRenderState( D3DRS_POINTSIZE,     FtoDW(2.00f) );
  89.     m_pD3DDevice->SetRenderState( D3DRS_POINTSIZE_MIN, FtoDW(0.00f) );
  90.     m_pD3DDevice->SetRenderState( D3DRS_POINTSCALE_A,  FtoDW(1.00f) );
  91.     m_pD3DDevice->SetRenderState( D3DRS_POINTSCALE_B,  FtoDW(1.00f) );
  92.     m_pD3DDevice->SetRenderState( D3DRS_POINTSCALE_C,  FtoDW(1.00f) );
  93.     m_pD3DDevice->SetFVF( D3DFVF_SNOWVERTEX );
  94.     m_pD3DDevice->SetStreamSource( 0, m_pVb,0, sizeof(SNOWVERTEX) );
  95.    m_pD3DDevice->DrawPrimitive( D3DPT_POINTLIST,
  96.                                    0, m_lSnowCount);
  97. /*MoveSnow(0.3f);
  98. m_pD3DDevice->SetFVF( D3DFVF_SNOWVERTEX );
  99.     m_pD3DDevice->SetStreamSource( 0, m_pVb,0, sizeof(SNOWVERTEX) );
  100.     m_pD3DDevice->DrawPrimitive( D3DPT_POINTLIST,
  101.                                    0, m_lSnowCount);
  102. m_pD3DDevice->SetRenderState( D3DRS_ALPHATESTENABLE, FALSE );
  103.     m_pD3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
  104.     
  105. MoveSnow(m_fOffset);*/
  106. }
  107. void CSnow::RenderSnow()
  108. {  // m_pD3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
  109. DrawSnow();
  110. }
  111. void CSnow::SetMaterial()
  112. {
  113.     ZeroMemory(&mtrl,sizeof(mtrl));
  114.     mtrl.Diffuse.r=mtrl.Ambient.r=1.0f;
  115. mtrl.Diffuse.g=mtrl.Ambient.g=1.0f;
  116. mtrl.Diffuse.b=mtrl.Ambient.b=1.0f;
  117. mtrl.Diffuse.a=mtrl.Ambient.a=1.0f;
  118.     mtrl.Specular.r=0.0f;
  119. mtrl.Specular.g=0.0f;
  120. mtrl.Specular.b=0.0f;
  121. mtrl.Specular.a=0.0f;
  122. mtrl.Emissive.r=1.0f;
  123. mtrl.Emissive.g=1.0f;
  124. mtrl.Emissive.b=1.0f;
  125. mtrl.Emissive.a=1.0f;
  126.      m_pD3DDevice->SetMaterial( &mtrl );
  127. }