Sky.cpp
资源名称:3DRPG.rar [点击查看]
上传用户:cjwanglu
上传日期:2013-07-10
资源大小:4744k
文件大小:7k
源码类别:
游戏
开发平台:
Visual C++
- #include "stdafx.h"
- #include "Sky.h"
- CSky::CSky(LPDIRECT3DDEVICE9 d3dd)
- {
- m_pD3DDevice=d3dd;
- m_pVb=NULL;
- m_pSvb=NULL;
- width=8000.0f;
- segment=400;
- kkk=0;
- CreateSky();
- }
- CSky::~CSky()
- {
- m_pVb->Release();
- //m_pSvb->Release();
- m_pTxSizhou->Release();
- m_pTxDingbu->Release();
- m_pTxDibu->Release();
- //m_pTxSky->Release();
- }
- void CSky::CreateSky()
- {
- void * pdata;
- //void * pcloud;
- VERTEX ppbData[] ={
- {0.0F,width/10*3,0.0F,0.0F,0.0F,},
- {0.0f,0.0f,0.0f,0.0f,1.0f,},
- {width,width/10*3,0.0f,0.25f,0.0f,},
- {width,0.0f,0.0f,0.25f,1.0f,},
- {width,width/10*3,width,0.5f,0.0f,},
- {width,0.0f,width,0.5f,1.0f,},
- {0.0f,width/10*3,width,0.75f,0.0f,},
- {0.0f,0.0f,width,0.75f,1.0f,},
- {0.0f,width/10*3,0.0f,1.0f,0.0f,},
- {0.0f,0.0f,0.0f,1.0f,1.0f,},//四周
- {0.0f,width/10*3,0.0f,0.0f,0.0f,},
- {width,width/10*3,0.0f,1.0f,0.0f,},
- {0.0f,width/10*3,width,0.0f,1.0f,},
- {width,width/10*3,width,1.0f,1.0f,},//顶部
- {0.0f,0.0f,0.0f,0.0f,0.0f,},
- {0.0f,0.0f,width,0.0f,1.0f,},
- {width,0.0f,0.0f,1.0f,0.0f,},
- {width,0.0f,width,1.0f,1.0f,}//底部
- };
- /*VERTEX SkyArray[(400+1)*2];
- int k=-1;
- int j=-1;
- bool flag=true;
- for(int i=0;i<((segment+1)*2);i++)
- {
- SkyArray[i].y=width-10.0F;
- if(i%2==0)
- {
- SkyArray[i].tu=0.0f;
- SkyArray[i].x=0.0f;
- k++;
- if(flag==true)
- {j++;SkyArray[i].flag=true;}
- else
- {j--;SkyArray[i].flag=false;}
- }
- else
- {
- SkyArray[i].tu=1.0f;
- SkyArray[i].x=width;
- SkyArray[i].flag=SkyArray[i-1].flag;
- }
- SkyArray[i].z=k*(width/segment);
- SkyArray[i].tv=j*(1.0/(segment));
- if(j==segment)//SkyArray[i].tv>=1.0f
- {flag=false;SkyArray[i].flag=false;}//SkyArray[i].flag=false;
- if(j==0)//SkyArray[i].tv<=0.0f
- {flag=true;SkyArray[i].flag=true;}//SkyArray[i].flag=true;
- }*/
- m_pD3DDevice->CreateVertexBuffer(sizeof(ppbData),D3DUSAGE_DYNAMIC,SKY_D3DFVF_VERTEXBUFFER,D3DPOOL_DEFAULT,&m_pVb,NULL);
- m_pVb->Lock(0,sizeof(ppbData),(void **)&pdata,D3DLOCK_DISCARD);
- memcpy(pdata,ppbData,sizeof(ppbData));
- m_pVb->Unlock();
- /*m_pD3DDevice->CreateVertexBuffer(sizeof(SkyArray),D3DUSAGE_DYNAMIC,SKY_D3DFVF_VERTEXBUFFER,D3DPOOL_DEFAULT,&m_pSvb,NULL);
- m_pSvb->Lock(0,sizeof(SkyArray),(void **)&pcloud,D3DLOCK_DISCARD);
- memcpy(pcloud,SkyArray,sizeof(SkyArray));
- m_pSvb->Unlock();*/
- /*m_pD3DDevice->CreateVertexBuffer((segment+1)*2*sizeof(VERTEX),D3DUSAGE_DYNAMIC,SKY_D3DFVF_VERTEXBUFFER,D3DPOOL_DEFAULT,&m_pSvb,NULL);
- m_pSvb->Lock(0,(segment+1)*2*sizeof(VERTEX),(void **)&SkyArray,D3DLOCK_DISCARD);
- int j=-1;
- for(int i=0;i<((segment+1)*2);i++)
- {
- SkyArray[i].y=width-10.0f;
- if(i%2==0)
- {
- SkyArray[i].tu=0.0f;
- SkyArray[i].x=0.0f;
- j++;
- }
- else
- {
- SkyArray[i].tu=1.0f;
- SkyArray[i].x=width;
- }
- SkyArray[i].z=j*(width/segment);
- SkyArray[i].tv=j*(1.0f/segment);
- }
- m_pSvb->Unlock();*/
- SetTexture();
- }
- void CSky::SetTexture()
- {
- D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/sky.jpg", 0, 0, 0, 0,
- D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
- D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_pTxSizhou);
- D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/ding.jpg", 0, 0, 0, 0,
- D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
- D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_pTxDingbu);
- D3DXCreateTextureFromFileEx(m_pD3DDevice, "./res/ding.jpg", 0, 0, 0, 0,
- D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT,
- D3DX_DEFAULT, D3DCOLOR_XRGB(0,0,0), NULL, NULL, &m_pTxDibu);
- //D3DXCreateTextureFromFile(m_pD3DDevice,"./res/Cloud.tga", &m_pTxSky);
- }
- void CSky::RenderSky()
- {
- //VERTEX * pCloud;
- SetMaterial();
- //m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);
- // m_pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_BLENDTEXTUREALPHA); //使用透明贴图
- //m_pD3DDevice->SetRenderState( D3DRS_ZWRITEENABLE,FALSE); //关闭深度写入
- // m_pD3DDevice->SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_DISABLE);
- m_pD3DDevice->SetStreamSource(0, m_pVb, 0, sizeof(VERTEX)); //设置顶点缓冲区
- m_pD3DDevice->SetFVF(SKY_D3DFVF_VERTEXBUFFER); //设置自由顶点格式
- m_pD3DDevice->SetTexture(0,m_pTxSizhou);
- m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 8); //四周
- m_pD3DDevice->SetTexture(0,m_pTxDibu);
- m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 14, 2);//底部
- m_pD3DDevice->SetTexture(0,m_pTxDingbu);
- m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 10, 2);//顶部
- /*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, D3DCOLOR_XRGB(0,0,0) );
- m_pD3DDevice->SetRenderState( D3DRS_ALPHAFUNC,D3DCMP_GREATEREQUAL );
- m_pD3DDevice->SetStreamSource(0, m_pSvb, 0, sizeof(VERTEX)); //设置顶点缓冲区
- m_pD3DDevice->SetFVF(SKY_D3DFVF_VERTEXBUFFER); //设置自由顶点格式
- m_pD3DDevice->SetTexture(0,m_pTxSky);
- m_pSvb->Lock(0,(segment+1)*2*sizeof(VERTEX),(void **)&pCloud,D3DLOCK_DISCARD);
- for(int i=0;i<((segment+1)*2);i++)
- {
- if(pCloud[i].tv>=1.0f)
- {pCloud[i].flag=false;}//pCloud[i].tv=1.0f;
- if(pCloud[i].tv<=0.0f)
- {pCloud[i].flag=true;}//pCloud[i].tv=0.0f;
- if(pCloud[i].flag==true)
- {
- pCloud[i].tv+=(1.0/(segment));
- }
- else
- {
- pCloud[i].tv-=(1.0/(segment));
- }
- }
- m_pSvb->Unlock();
- m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,(segment*2));//cloud
- m_pD3DDevice->SetRenderState( D3DRS_CLIPPLANEENABLE ,TRUE);
- m_pD3DDevice->SetRenderState( D3DRS_ZWRITEENABLE,TRUE);*/
- }
- void CSky::Translate(POSITION pos)
- {
- D3DXMATRIX matTranslate,matWorld;
- D3DXMATRIX matScale;
- D3DXMatrixScaling(&matScale,5,5,5);
- D3DXMatrixTranslation(&matTranslate,-2080.0f,0.0f,-2080.0f);
- m_pD3DDevice->SetTransform(D3DTS_WORLD,&matTranslate);
- }
- void CSky::SetMaterial()
- {
- ZeroMemory(&material,sizeof(material));
- material.Diffuse.r=material.Ambient.r=0.0f;
- material.Diffuse.g=material.Ambient.g=0.0f;
- material.Diffuse.b=material.Ambient.b=0.0f;
- material.Diffuse.a=material.Ambient.a=0.0f;
- material.Specular.r=0.0f;
- material.Specular.g=0.0f;
- material.Specular.b=0.0f;
- material.Specular.a=0.0f;
- material.Emissive.r=1.0f;
- material.Emissive.g=1.0f;
- material.Emissive.b=1.0f;
- material.Emissive.a=0.5f;
- m_pD3DDevice->SetMaterial(&material);
- }