SkyDome.cpp
上传用户:henghua
上传日期:2007-11-14
资源大小:7655k
文件大小:4k
- #include "SkyDome.h"
- CSkyDome::CSkyDome(LPDIRECT3DDEVICE9 device, CConfiguration *config, int detail, float tdelta, float cdelta)
- {
- this->device = device;
- this->detail = detail;
- this->config = config;
- this->config->set_float(p_bCloudiness, 0);
- this->time = 0;
- this->tdelta = tdelta;
- this->cdelta = cdelta;
- }
- bool CSkyDome::Init()
- {
- int i = 0;
- char *errortext;
- LPD3DXBUFFER errors;
- D3DXHANDLE hTechnique;
- unsigned int *indexbuffer;
- SURFACEVERTEX* pdVertices;
- if( FAILED( D3DXCreateTextureFromFile( device, "textures/clouds.png", &clouds ) ) )
- {
- MessageBox(NULL, "Could not find noise texture", "Textures.exe", MB_OK);
- return false;
- }
- if( FAILED( device->CreateVertexBuffer( detail*detail*sizeof(SURFACEVERTEX),
- D3DUSAGE_WRITEONLY, D3DFVF_SURFACEVERTEX,
- D3DPOOL_DEFAULT, &v_skydome, NULL ) ) )
- {
- return false;
- }
- if( FAILED( v_skydome->Lock( 0, 0, (void**)&pdVertices, 0 ) ) )
- return false;
- {
- for(int v=0; v<detail; v++){
- for(int u=0; u<detail; u++){
- float al = -2 * D3DX_PI * ((float)u/(detail-1.0f)),
- th = 0.6 * D3DX_PI * ((float)v/(detail-1.0f));
- pdVertices[v*detail+u].position.x = sin(th)*sin(al);
- pdVertices[v*detail+u].position.y = cos(th);
- pdVertices[v*detail+u].position.z = sin(th)*cos(al);
- pdVertices[v*detail+u].displacement = 0.0f;
- }
- }
- }
- v_skydome->Unlock();
- if( FAILED( device->CreateIndexBuffer( sizeof(unsigned int) * 6 * (detail-1)*(detail-1),
- D3DUSAGE_WRITEONLY,
- D3DFMT_INDEX32, D3DPOOL_DEFAULT,&i_skydome,NULL)))
- {
- return false;
- }
- if( FAILED( i_skydome->Lock(0,0,(void**)&indexbuffer,0 ) ) )
- return false;
- {
- for(int v=0; v<detail-1; v++){
- for(int u=0; u<detail-1; u++){
- // face 1 |/
- indexbuffer[i++] = v*detail + u;
- indexbuffer[i++] = v*detail + u + 1;
- indexbuffer[i++] = (v+1)*detail + u;
- // face 2 /|
- indexbuffer[i++] = (v+1)*detail + u;
- indexbuffer[i++] = v*detail + u + 1;
- indexbuffer[i++] = (v+1)*detail + u + 1;
- }
- }
- }
- i_skydome->Unlock();
- // same for skybox.fx
- D3DXCreateEffectFromFile(device, "fx\skydome.fx",
- NULL, NULL, 0, NULL, &e_skydome, &errors );
-
- if (errors != NULL){
- errortext = (char*) errors->GetBufferPointer();
- MessageBox(NULL, errortext, "Textures.exe", MB_OK);
- }
- e_skydome->FindNextValidTechnique(NULL, &hTechnique);
- e_skydome->SetTechnique(hTechnique);
- return true;
- }
- bool CSkyDome::Update()
- {
- this->time += this->tdelta;
- if(!config->get_bool(p_bisCloudy))
- {
- if(config->get_float(p_bCloudiness) > 0)
- config->set_float(p_bCloudiness, config->get_float(p_bCloudiness) - this->cdelta);
- }
- else
- {
- if(config->get_float(p_bCloudiness) < 1)
- config->set_float(p_bCloudiness, config->get_float(p_bCloudiness) + this->cdelta);
- }
- return true;
- }
- bool CSkyDome::Render()
- {
- device->SetStreamSource( 0, v_skydome, 0, sizeof(SURFACEVERTEX) );
- device->SetFVF( D3DFVF_SURFACEVERTEX);
- device->SetIndices(i_skydome);
- D3DXMATRIXA16 fvproj((*cam)->view);
- fvproj._41 = 0;
- fvproj._42 = 0;
- fvproj._43 = 0;
- fvproj = fvproj * (*cam)->proj;
- e_skydome->Begin(NULL,NULL);
- e_skydome->Pass(0);
-
- e_skydome->SetMatrix("mViewProj",&fvproj);
- e_skydome->SetMatrix("mInvViewProj",&((*cam)->invviewproj));
- e_skydome->SetMatrix("mInvView",&((*cam)->invview));
- e_skydome->SetFloat("sun_alfa", config->params[p_fSunPosAlpha].fData);
- e_skydome->SetFloat("sun_theta", config->params[p_fSunPosTheta].fData);
- e_skydome->SetFloat("sun_shininess", 4*config->params[p_fSunShininess].fData);
- e_skydome->SetFloat("sun_strength", config->params[p_fSunStrength].fData);
- e_skydome->SetFloat("t", time);
- e_skydome->SetVector("view_position", &D3DXVECTOR4((*cam)->position.x,(*cam)->position.y,(*cam)->position.z,1));
- e_skydome->SetTexture("Clouds",clouds);
- e_skydome->SetFloat("cloudiness", config->get_float(p_bCloudiness));
- e_skydome->SetFloat("night_day", config->get_float(p_bNight_Day));
- if(config->get_bool(p_bFogActive))
- {
- e_skydome->SetFloat("fog", 0.7f);
- }
- else
- e_skydome->SetFloat("fog", 1.0f);
- device->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, detail*detail, 0, 2*(detail-1)*(detail-1) );
- e_skydome->End();
- return true;
- }
- void CSkyDome::ChangeStatus()
- {
- config->set_bool(p_bisCloudy, !config->get_bool(p_bisCloudy));
- }