DirectX骨骼动画,地形,级球形天空体的演示

来源:互联网 发布:网络布线施工 编辑:程序博客网 时间:2024/05/10 17:31

 

天空体类:

头文件:

Code:
  1. #pragma once  
  2.   
  3. #include <d3d9.h>  
  4. #include <d3dx9.h>  
  5.   
  6. #ifndef SAFE_DELETE  
  7. #define SAFE_DELETE(p)  { if(p) { delete (p); (p)=NULL; } }  
  8. #endif   
  9. #ifndef SAFE_RELEASE  
  10. #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }  
  11. #endif  
  12.   
  13. //--------------------------------------------------------------------------------------  
  14. // Name: class CSkybox  
  15. // Desc: 球形地形  
  16. //--------------------------------------------------------------------------------------  
  17. class CSkybox  
  18. {  
  19. private:  
  20.     LPDIRECT3DDEVICE9       m_pd3dDevice;  
  21.     LPDIRECT3DTEXTURE9      m_pTexture;  
  22.     LPDIRECT3DINDEXBUFFER9  m_pIndexBuf;  
  23.     LPDIRECT3DVERTEXBUFFER9 m_pVertexBuf;  
  24.   
  25.     INT     m_nNumLatitudes;  
  26.     INT     m_nNumLongitudes;  
  27.     INT     m_nVertsPerLati;  
  28.     INT     m_nVertsPerLongi;  
  29.     INT     m_nNumVertices;     // 顶点数  
  30.     FLOAT   m_fSkyboxRadius;    // 半径  
  31.   
  32.     struct SKYBOXVERTEX  
  33.     {  
  34.         FLOAT _x, _y, _z;  
  35.         FLOAT _u, _v;  
  36.         SKYBOXVERTEX(FLOAT x, FLOAT y, FLOAT z, FLOAT u, FLOAT v)   
  37.             : _x(x), _y(y), _z(z), _u(u), _v(v) {}  
  38.         static const DWORD FVF = D3DFVF_XYZ | D3DFVF_TEX1;  
  39.     };  
  40.   
  41. public:  
  42.     CSkybox(IDirect3DDevice9 *pd3dDevice);  
  43.     virtual ~CSkybox(void);  
  44.   
  45. public:  
  46.     BOOL LoadSkybox(LPCSTR pTextureFile);  
  47.     BOOL InitSkybox(INT nAlpha, INT nBeta, FLOAT nRadius);  
  48.     BOOL DrawSkybox(D3DXMATRIX *pMatWorld, bool bDrawFrame=0);  
  49. };  

CPP文件:

Code:
  1. #include "Skybox.h"  
  2.   
  3. CSkybox::CSkybox(IDirect3DDevice9 *pd3dDevice)  
  4. {  
  5.     m_pd3dDevice     = pd3dDevice;  
  6.     m_pTexture       = NULL;  
  7.     m_pIndexBuf      = NULL;  
  8.     m_pVertexBuf     = NULL;  
  9.     m_nNumVertices   = 0;  
  10.     m_nNumLatitudes  = 0;  
  11.     m_nNumLongitudes = 0;  
  12.     m_nVertsPerLongi = 0;  
  13.     m_nVertsPerLati  = 0;  
  14.     m_fSkyboxRadius  = 0;  
  15. }  
  16.   
  17. CSkybox::~CSkybox(void)  
  18. {  
  19.     SAFE_RELEASE(m_pTexture);  
  20.     SAFE_RELEASE(m_pIndexBuf);  
  21.     SAFE_RELEASE(m_pVertexBuf);  
  22. }  
  23.   
  24. BOOL CSkybox::LoadSkybox(LPCSTR pTextureFile)   
  25. {  
  26.     // 加载天空纹理  
  27.     if (FAILED(D3DXCreateTextureFromFile(m_pd3dDevice, pTextureFile, &m_pTexture)))  
  28.         return FALSE;  
  29.     return TRUE;  
  30. }  
  31.   
  32. BOOL CSkybox::InitSkybox(INT nAlpha, INT nBeta, FLOAT fRadius)   
  33. {  
  34.     m_fSkyboxRadius  = fRadius;                 // 半球体的半径  
  35.     m_nNumLatitudes  = 360 / nAlpha;            // 维度线的条数  
  36.     m_nNumLongitudes =  90 / nBeta;             // 经度线的条数  
  37.     m_nVertsPerLongi = m_nNumLatitudes + 1;     // 每条经度线上的顶点数  
  38.     m_nVertsPerLati  = m_nNumLongitudes + 1;    // 每条维度线上的顶点数  
  39.     m_nNumVertices   = m_nVertsPerLati * m_nVertsPerLongi;  
  40.   
  41.     // 计算天空的灵活顶点  
  42.     if (FAILED(m_pd3dDevice->CreateVertexBuffer(m_nNumVertices * sizeof(SKYBOXVERTEX),   
  43.         D3DUSAGE_WRITEONLY, SKYBOXVERTEX::FVF, D3DPOOL_MANAGED, &m_pVertexBuf, 0)))  
  44.         return FALSE;  
  45.   
  46.     SKYBOXVERTEX *pVertices = NULL;  
  47.     m_pVertexBuf->Lock(0, 0, (void**)&pVertices, 0);  
  48.   
  49.     int nIndex = 0;  
  50.     FLOAT fAlpha = 2.0f * D3DX_PI * nAlpha / 360.0f;    // 经度角转换为弧度表示  
  51.     FLOAT fBeta  = 2.0f * D3DX_PI * nBeta  / 360.0f;    // 维度角转换为弧度表示  
  52.     for (int row = 0; row < m_nNumLongitudes+1; row++)   
  53.     {  
  54.         for (int col = 0; col < m_nNumLatitudes+1; col++)   
  55.         {  
  56.             // 计算顶点的坐标  
  57.             pVertices[nIndex]._x = fRadius * cosf(row * fBeta) * cosf(col * fAlpha);   
  58.             pVertices[nIndex]._y = fRadius * sinf(row * fBeta);  
  59.             pVertices[nIndex]._z = fRadius * cosf(row * fBeta) * sinf(col * fAlpha);  
  60.             // 计算顶点的纹理坐标  
  61.             pVertices[nIndex]._u = col * fAlpha / (2.0f * D3DX_PI);  
  62.             pVertices[nIndex]._v = row * fBeta  / (D3DX_PI / 2.0f);  
  63.   
  64.             nIndex++;  
  65.         }  
  66.     }  
  67.     m_pVertexBuf->Unlock();  
  68.   
  69.     // 计算天空的顶点索引  
  70.     if (FAILED(m_pd3dDevice->CreateIndexBuffer(m_nNumVertices * 6 *sizeof(WORD),   
  71.         D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_pIndexBuf, 0)))  
  72.         return FALSE;  
  73.   
  74.     WORD* pIndices = NULL;  
  75.     m_pIndexBuf->Lock(0, 0, (void**)&pIndices, 0);  
  76.   
  77.     nIndex = 0;   
  78.     for (int row = 0; row < m_nNumLongitudes; row++)   
  79.     {  
  80.         for (int col = 0; col < m_nNumLatitudes; col++)   
  81.         {  
  82.             pIndices[nIndex+0] =   row   * m_nVertsPerLongi + col;  
  83.             pIndices[nIndex+1] = (row+1) * m_nVertsPerLongi + col;  
  84.             pIndices[nIndex+2] = (row+1) * m_nVertsPerLongi + col + 1;  
  85.   
  86.             pIndices[nIndex+3] =   row   * m_nVertsPerLongi + col;  
  87.             pIndices[nIndex+4] = (row+1) * m_nVertsPerLongi + col + 1;  
  88.             pIndices[nIndex+5] =   row   * m_nVertsPerLongi + col + 1;  
  89.             nIndex += 6;  
  90.         }  
  91.     }  
  92.     m_pIndexBuf->Unlock();  
  93.   
  94.     return TRUE;  
  95. }  
  96.   
  97. BOOL CSkybox::DrawSkybox(D3DXMATRIX *pMatWorld, bool bDrawFrame)   
  98. {  
  99.     //纹理模式渲染  
  100.     m_pd3dDevice->SetStreamSource(0, m_pVertexBuf, 0, sizeof(SKYBOXVERTEX));  
  101.     m_pd3dDevice->SetFVF(SKYBOXVERTEX::FVF);  
  102.     m_pd3dDevice->SetIndices(m_pIndexBuf);  
  103.     m_pd3dDevice->SetTexture(0, m_pTexture);  
  104.   
  105.     m_pd3dDevice->SetTransform(D3DTS_WORLD, pMatWorld);  
  106.   
  107.     m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);  
  108.     m_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);  
  109.   
  110.     m_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0,   
  111.         m_nNumVertices, 0, m_nNumVertices * 2);  
  112.   
  113.     m_pd3dDevice->SetTexture(0, 0);  
  114.     m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE);  
  115.     m_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);  
  116.   
  117.     //网格模式渲染  
  118.     if (bDrawFrame)  
  119.     {  
  120.         m_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);  
  121.         m_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0,   
  122.             m_nNumVertices, 0, m_nNumVertices * 2);  
  123.         m_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);  
  124.     }  
  125.   
  126.     return TRUE;  
  127. }  

 

原创粉丝点击