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

游戏

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "Sky.h"
  3. CSky::CSky(LPDIRECT3DDEVICE9 d3dd)
  4. {
  5. m_pD3DDevice=d3dd;
  6. m_pVb=NULL;
  7. m_pSvb=NULL;
  8.     width=8000.0f;
  9.     segment=400;
  10. kkk=0;
  11. CreateSky();
  12. }
  13. CSky::~CSky()
  14. {
  15. m_pVb->Release();
  16. //m_pSvb->Release();
  17. m_pTxSizhou->Release();
  18. m_pTxDingbu->Release();
  19. m_pTxDibu->Release();
  20. //m_pTxSky->Release();
  21. }
  22. void CSky::CreateSky()
  23. {  
  24. void * pdata;
  25. //void * pcloud;
  26. VERTEX  ppbData[] ={
  27. {0.0F,width/10*3,0.0F,0.0F,0.0F,},
  28.     {0.0f,0.0f,0.0f,0.0f,1.0f,},
  29.     {width,width/10*3,0.0f,0.25f,0.0f,},
  30.     {width,0.0f,0.0f,0.25f,1.0f,},
  31.     {width,width/10*3,width,0.5f,0.0f,},
  32.     {width,0.0f,width,0.5f,1.0f,},
  33.     {0.0f,width/10*3,width,0.75f,0.0f,},
  34.     {0.0f,0.0f,width,0.75f,1.0f,},
  35.     {0.0f,width/10*3,0.0f,1.0f,0.0f,},
  36.     {0.0f,0.0f,0.0f,1.0f,1.0f,},//四周
  37. {0.0f,width/10*3,0.0f,0.0f,0.0f,},
  38. {width,width/10*3,0.0f,1.0f,0.0f,},
  39. {0.0f,width/10*3,width,0.0f,1.0f,},
  40. {width,width/10*3,width,1.0f,1.0f,},//顶部
  41. {0.0f,0.0f,0.0f,0.0f,0.0f,},
  42. {0.0f,0.0f,width,0.0f,1.0f,},
  43. {width,0.0f,0.0f,1.0f,0.0f,},
  44. {width,0.0f,width,1.0f,1.0f,}//底部
  45.                     };
  46. /*VERTEX  SkyArray[(400+1)*2];
  47.   int k=-1;
  48.       int j=-1;
  49.   bool flag=true;
  50.  
  51. for(int i=0;i<((segment+1)*2);i++)
  52. {
  53. SkyArray[i].y=width-10.0F;
  54. if(i%2==0)
  55. {
  56. SkyArray[i].tu=0.0f;
  57. SkyArray[i].x=0.0f;
  58. k++;
  59. if(flag==true)
  60. {j++;SkyArray[i].flag=true;}
  61. else
  62. {j--;SkyArray[i].flag=false;}
  63. }
  64. else
  65. {
  66. SkyArray[i].tu=1.0f;
  67. SkyArray[i].x=width;
  68. SkyArray[i].flag=SkyArray[i-1].flag;
  69. }
  70.         SkyArray[i].z=k*(width/segment);
  71.         SkyArray[i].tv=j*(1.0/(segment));
  72. if(j==segment)//SkyArray[i].tv>=1.0f
  73. {flag=false;SkyArray[i].flag=false;}//SkyArray[i].flag=false;
  74. if(j==0)//SkyArray[i].tv<=0.0f
  75. {flag=true;SkyArray[i].flag=true;}//SkyArray[i].flag=true;
  76.  
  77. }*/
  78. m_pD3DDevice->CreateVertexBuffer(sizeof(ppbData),D3DUSAGE_DYNAMIC,SKY_D3DFVF_VERTEXBUFFER,D3DPOOL_DEFAULT,&m_pVb,NULL);
  79. m_pVb->Lock(0,sizeof(ppbData),(void **)&pdata,D3DLOCK_DISCARD);
  80.     memcpy(pdata,ppbData,sizeof(ppbData));
  81. m_pVb->Unlock();
  82.     
  83. /*m_pD3DDevice->CreateVertexBuffer(sizeof(SkyArray),D3DUSAGE_DYNAMIC,SKY_D3DFVF_VERTEXBUFFER,D3DPOOL_DEFAULT,&m_pSvb,NULL);
  84.     m_pSvb->Lock(0,sizeof(SkyArray),(void **)&pcloud,D3DLOCK_DISCARD);
  85.     memcpy(pcloud,SkyArray,sizeof(SkyArray));
  86. m_pSvb->Unlock();*/
  87.     /*m_pD3DDevice->CreateVertexBuffer((segment+1)*2*sizeof(VERTEX),D3DUSAGE_DYNAMIC,SKY_D3DFVF_VERTEXBUFFER,D3DPOOL_DEFAULT,&m_pSvb,NULL);
  88.     m_pSvb->Lock(0,(segment+1)*2*sizeof(VERTEX),(void **)&SkyArray,D3DLOCK_DISCARD);
  89.     int j=-1;
  90. for(int i=0;i<((segment+1)*2);i++)
  91. {
  92. SkyArray[i].y=width-10.0f;
  93. if(i%2==0)
  94. {
  95. SkyArray[i].tu=0.0f;
  96. SkyArray[i].x=0.0f;
  97. j++;
  98. }
  99. else
  100. {
  101. SkyArray[i].tu=1.0f;
  102. SkyArray[i].x=width;
  103. }
  104.         SkyArray[i].z=j*(width/segment);
  105. SkyArray[i].tv=j*(1.0f/segment);
  106. }
  107. m_pSvb->Unlock();*/
  108.     
  109.     SetTexture();
  110. }
  111. void CSky::SetTexture()
  112. {
  113. D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/sky.jpg", 0, 0, 0, 0,
  114.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  115.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_pTxSizhou);
  116. D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/ding.jpg", 0, 0, 0, 0,
  117.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  118.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_pTxDingbu);
  119. D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/ding.jpg", 0, 0, 0, 0,
  120.   D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
  121.   D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_pTxDibu);
  122. //D3DXCreateTextureFromFile(m_pD3DDevice,"./res/Cloud.tga", &m_pTxSky);
  123. }
  124. void CSky::RenderSky()
  125. {   
  126. //VERTEX * pCloud;
  127.     SetMaterial();
  128. //m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);
  129.    // m_pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_BLENDTEXTUREALPHA); //使用透明贴图
  130. //m_pD3DDevice->SetRenderState( D3DRS_ZWRITEENABLE,FALSE); //关闭深度写入
  131.    // m_pD3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_DISABLE);
  132. m_pD3DDevice->SetStreamSource(0, m_pVb, 0, sizeof(VERTEX));   //设置顶点缓冲区
  133.     m_pD3DDevice->SetFVF(SKY_D3DFVF_VERTEXBUFFER); //设置自由顶点格式
  134. m_pD3DDevice->SetTexture(0,m_pTxSizhou);
  135. m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 8); //四周
  136. m_pD3DDevice->SetTexture(0,m_pTxDibu);
  137.     m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 14, 2);//底部
  138.     m_pD3DDevice->SetTexture(0,m_pTxDingbu);
  139.     m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 10, 2);//顶部
  140. /*m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
  141. m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
  142. m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
  143.     m_pD3DDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
  144.     m_pD3DDevice->SetRenderState( D3DRS_ALPHAREF,        D3DCOLOR_XRGB(0,0,0) );
  145.     m_pD3DDevice->SetRenderState( D3DRS_ALPHAFUNC,D3DCMP_GREATEREQUAL );
  146. m_pD3DDevice->SetStreamSource(0, m_pSvb, 0, sizeof(VERTEX));   //设置顶点缓冲区
  147.     m_pD3DDevice->SetFVF(SKY_D3DFVF_VERTEXBUFFER); //设置自由顶点格式
  148. m_pD3DDevice->SetTexture(0,m_pTxSky);
  149. m_pSvb->Lock(0,(segment+1)*2*sizeof(VERTEX),(void **)&pCloud,D3DLOCK_DISCARD);
  150. for(int i=0;i<((segment+1)*2);i++)
  151. {
  152.         if(pCloud[i].tv>=1.0f)
  153. {pCloud[i].flag=false;}//pCloud[i].tv=1.0f;
  154. if(pCloud[i].tv<=0.0f)
  155. {pCloud[i].flag=true;}//pCloud[i].tv=0.0f;
  156. if(pCloud[i].flag==true)
  157. {
  158. pCloud[i].tv+=(1.0/(segment));
  159. }
  160. else
  161. {
  162.          pCloud[i].tv-=(1.0/(segment));
  163. }
  164. }
  165. m_pSvb->Unlock();
  166.  
  167. m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,(segment*2));//cloud
  168.     m_pD3DDevice->SetRenderState( D3DRS_CLIPPLANEENABLE ,TRUE);
  169. m_pD3DDevice->SetRenderState( D3DRS_ZWRITEENABLE,TRUE);*/
  170.     
  171.     
  172. }
  173. void CSky::Translate(POSITION pos)
  174. {
  175.     D3DXMATRIX matTranslate,matWorld;
  176.     D3DXMATRIX matScale;
  177.     D3DXMatrixScaling(&matScale,5,5,5);
  178. D3DXMatrixTranslation(&matTranslate,-2080.0f,0.0f,-2080.0f);
  179. m_pD3DDevice->SetTransform(D3DTS_WORLD,&matTranslate);
  180. }
  181. void CSky::SetMaterial()
  182. {
  183. ZeroMemory(&material,sizeof(material));
  184. material.Diffuse.r=material.Ambient.r=0.0f;
  185. material.Diffuse.g=material.Ambient.g=0.0f;
  186. material.Diffuse.b=material.Ambient.b=0.0f;
  187. material.Diffuse.a=material.Ambient.a=0.0f;
  188. material.Specular.r=0.0f;
  189. material.Specular.g=0.0f;
  190. material.Specular.b=0.0f;
  191. material.Specular.a=0.0f;
  192. material.Emissive.r=1.0f;
  193. material.Emissive.g=1.0f;
  194. material.Emissive.b=1.0f;
  195. material.Emissive.a=0.5f;
  196. m_pD3DDevice->SetMaterial(&material);
  197. }