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

游戏引擎

开发平台:

Visual C++

  1. #include "SkyDome.h"
  2. CSkyDome::CSkyDome(LPDIRECT3DDEVICE9 device, CConfiguration *config, int detail, float tdelta, float cdelta)
  3. {
  4. this->device = device;
  5. this->detail = detail;
  6. this->config = config;
  7. this->config->set_float(p_bCloudiness, 0);
  8. this->time = 0;
  9. this->tdelta = tdelta;
  10. this->cdelta = cdelta;
  11. }
  12. bool CSkyDome::Init()
  13. {
  14. int i = 0;
  15. char *errortext;
  16. LPD3DXBUFFER errors;
  17. D3DXHANDLE hTechnique;
  18. unsigned int *indexbuffer;
  19. SURFACEVERTEX* pdVertices;
  20. if( FAILED( D3DXCreateTextureFromFile( device, "textures/clouds.png", &clouds ) ) )
  21. {
  22. MessageBox(NULL, "Could not find noise texture", "Textures.exe", MB_OK);
  23. return false;
  24. }
  25. if( FAILED( device->CreateVertexBuffer( detail*detail*sizeof(SURFACEVERTEX),
  26. D3DUSAGE_WRITEONLY, D3DFVF_SURFACEVERTEX,
  27. D3DPOOL_DEFAULT, &v_skydome, NULL ) ) )
  28. {
  29. return false;
  30. }
  31. if( FAILED( v_skydome->Lock( 0, 0, (void**)&pdVertices, 0 ) ) )
  32. return false;
  33. {
  34. for(int v=0; v<detail; v++){
  35. for(int u=0; u<detail; u++){
  36. float al = -2 * D3DX_PI * ((float)u/(detail-1.0f)),
  37. th = 0.6 * D3DX_PI * ((float)v/(detail-1.0f));
  38. pdVertices[v*detail+u].position.x = sin(th)*sin(al);
  39. pdVertices[v*detail+u].position.y = cos(th);
  40. pdVertices[v*detail+u].position.z = sin(th)*cos(al);
  41. pdVertices[v*detail+u].displacement = 0.0f;
  42. }
  43. }
  44. }
  45. v_skydome->Unlock();
  46. if( FAILED( device->CreateIndexBuffer( sizeof(unsigned int) * 6 * (detail-1)*(detail-1),
  47. D3DUSAGE_WRITEONLY,
  48. D3DFMT_INDEX32, D3DPOOL_DEFAULT,&i_skydome,NULL)))
  49. {
  50. return false;
  51. }
  52. if( FAILED( i_skydome->Lock(0,0,(void**)&indexbuffer,0 ) ) )
  53. return false;
  54. {
  55. for(int v=0; v<detail-1; v++){
  56. for(int u=0; u<detail-1; u++){
  57. // face 1 |/
  58. indexbuffer[i++] = v*detail + u;
  59. indexbuffer[i++] = v*detail + u + 1;
  60. indexbuffer[i++] = (v+1)*detail + u;
  61. // face 2 /|
  62. indexbuffer[i++] = (v+1)*detail + u;
  63. indexbuffer[i++] = v*detail + u + 1;
  64. indexbuffer[i++] = (v+1)*detail + u + 1;
  65. }
  66. }
  67. }
  68. i_skydome->Unlock();
  69. // same for skybox.fx
  70. D3DXCreateEffectFromFile(device, "fx\skydome.fx", 
  71. NULL, NULL, 0, NULL, &e_skydome, &errors );
  72. if (errors != NULL){
  73. errortext = (char*) errors->GetBufferPointer();
  74. MessageBox(NULL, errortext, "Textures.exe", MB_OK);
  75. }
  76. e_skydome->FindNextValidTechnique(NULL, &hTechnique);    
  77. e_skydome->SetTechnique(hTechnique);
  78. return true;
  79. }
  80. bool CSkyDome::Update()
  81. {
  82. this->time += this->tdelta;
  83. if(!config->get_bool(p_bisCloudy))
  84. {
  85. if(config->get_float(p_bCloudiness) > 0)
  86. config->set_float(p_bCloudiness, config->get_float(p_bCloudiness) - this->cdelta);
  87. }
  88. else
  89. {
  90. if(config->get_float(p_bCloudiness) < 1)
  91. config->set_float(p_bCloudiness, config->get_float(p_bCloudiness) + this->cdelta);
  92. }
  93. return true;
  94. }
  95. bool CSkyDome::Render()
  96. {
  97. device->SetStreamSource( 0, v_skydome, 0, sizeof(SURFACEVERTEX) );
  98. device->SetFVF( D3DFVF_SURFACEVERTEX);
  99. device->SetIndices(i_skydome);
  100. D3DXMATRIXA16 fvproj((*cam)->view);
  101. fvproj._41 = 0;
  102. fvproj._42 = 0;
  103. fvproj._43 = 0;
  104. fvproj = fvproj * (*cam)->proj;
  105. e_skydome->Begin(NULL,NULL);
  106. e_skydome->Pass(0);
  107. e_skydome->SetMatrix("mViewProj",&fvproj);
  108. e_skydome->SetMatrix("mInvViewProj",&((*cam)->invviewproj));
  109. e_skydome->SetMatrix("mInvView",&((*cam)->invview));
  110. e_skydome->SetFloat("sun_alfa", config->params[p_fSunPosAlpha].fData);
  111. e_skydome->SetFloat("sun_theta", config->params[p_fSunPosTheta].fData);
  112. e_skydome->SetFloat("sun_shininess", 4*config->params[p_fSunShininess].fData);
  113. e_skydome->SetFloat("sun_strength", config->params[p_fSunStrength].fData);
  114. e_skydome->SetFloat("t", time);
  115. e_skydome->SetVector("view_position", &D3DXVECTOR4((*cam)->position.x,(*cam)->position.y,(*cam)->position.z,1));
  116. e_skydome->SetTexture("Clouds",clouds);
  117. e_skydome->SetFloat("cloudiness", config->get_float(p_bCloudiness));
  118. e_skydome->SetFloat("night_day", config->get_float(p_bNight_Day));
  119. if(config->get_bool(p_bFogActive))
  120. {
  121. e_skydome->SetFloat("fog", 0.7f);
  122. }
  123. else
  124. e_skydome->SetFloat("fog", 1.0f);
  125. device->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, detail*detail, 0, 2*(detail-1)*(detail-1) );
  126. e_skydome->End();
  127. return true;
  128. }
  129. void CSkyDome::ChangeStatus()
  130. {
  131. config->set_bool(p_bisCloudy, !config->get_bool(p_bisCloudy));
  132. }