#pragma once
#include <d3d9.h>
#include <D3dx9core.h>
class CGameSkyBox
{
public:
CGameSkyBox(LPDIRECT3DDEVICE9 pD3DDevice);
~CGameSkyBox(void);
public:
BOOL LoadSkyBoxTextureFromFile(LPCSTR lpszFront, LPCSTR lpszBack, LPCSTR lpszLeft, LPCSTR lpszRight, LPCSTR lpszUp, LPCSTR lpszDown);
BOOL InitSkyBox(float fSideLength);
BOOL RenderSkyBox();
BOOL ShutDown();
private:
LPDIRECT3DDEVICE9 m_pD3DDevice;
LPDIRECT3DVERTEXBUFFER9 m_pSkyBoxVertex;
LPDIRECT3DINDEXBUFFER9 m_pSkyBoxIndex;
LPDIRECT3DTEXTURE9 m_pFrontTexture;
LPDIRECT3DTEXTURE9 m_pBackTexture;
LPDIRECT3DTEXTURE9 m_pLeftTexture;
LPDIRECT3DTEXTURE9 m_pRightTexture;
LPDIRECT3DTEXTURE9 m_pUpTexture;
LPDIRECT3DTEXTURE9 m_pDownTexture;
float m_fSideLength;
};
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
#include "GameSkyBox.h"
typedef struct _D3DVERTEX
{
float fX;
float fY;
float fZ;
//纹理坐标
float fU;
float fV;
}D3DVERTEX;
/*
*功能 :构造函数
*参数 :LPDIRECT3DDEVICE9
*返回 :
*描述 :
*/
CGameSkyBox::CGameSkyBox(LPDIRECT3DDEVICE9 pD3DDevice)
{
m_pD3DDevice = pD3DDevice;
m_pSkyBoxVertex = NULL;
m_pSkyBoxIndex = NULL;
m_pFrontTexture = NULL;
m_pBackTexture = NULL;
m_pLeftTexture = NULL;
m_pRightTexture = NULL;
m_pUpTexture = NULL;
m_pDownTexture = NULL;
m_fSideLength = 0;
}
/*
*功能 :析构函数
*参数 :void
*返回 :
*描述 :
*/
CGameSkyBox::~CGameSkyBox(void)
{
m_pD3DDevice = NULL;
ShutDown();
}
/*
*功能 :加载SkyBox纹理
*参数 :Front, Back, Left, Right, Up纹理路径
*返回 :成功返回TRUE, 否则返回FALSE
*描述 :
*/
BOOL CGameSkyBox::LoadSkyBoxTextureFromFile( LPCSTR lpszFront, LPCSTR lpszBack, LPCSTR lpszLeft, LPCSTR lpszRight, LPCSTR lpszUp, LPCSTR lpszDown )
{
HRESULT hResult;
hResult = D3DXCreateTextureFromFileA(m_pD3DDevice, lpszFront, &m_pFrontTexture);
if (FAILED(hResult))
return FALSE;
hResult = D3DXCreateTextureFromFileA(m_pD3DDevice, lpszBack, &m_pBackTexture);
if (FAILED(hResult))
return FALSE;
hResult = D3DXCreateTextureFromFileA(m_pD3DDevice, lpszLeft, &m_pLeftTexture);
if (FAILED(hResult))
return FALSE;
hResult = D3DXCreateTextureFromFileA(m_pD3DDevice, lpszRight, &m_pRightTexture);
if (FAILED(hResult))
return FALSE;
hResult = D3DXCreateTextureFromFileA(m_pD3DDevice, lpszUp, &m_pUpTexture);
if (FAILED(hResult))
return FALSE;
hResult = D3DXCreateTextureFromFileA(m_pD3DDevice, lpszDown, &m_pDownTexture);
if (FAILED(hResult))
return FALSE;
return TRUE;
}
/*
*功能 :初始化SkyBox
*参数 :nSideLength 天空盒的边长, pD3DDevice D3D设备
*返回 :成功返回TRUE, 否则返回FALSE
*描述 :创建顶点缓存, 索引缓存
*/
BOOL CGameSkyBox::InitSkyBox( float fSideLength)
{
//边长
if (fSideLength <= 0.0f)
return FALSE;
m_fSideLength = fSideLength;
//顶点缓存
D3DVERTEX VertexData[] =
{
//背面
{ m_fSideLength / 2.0f, m_fSideLength, -m_fSideLength / 2.0f, 0.0f, 0.0f},
{-m_fSideLength / 2.0f, m_fSideLength, -m_fSideLength / 2.0f, 1.0f, 0.0f},
{-m_fSideLength / 2.0f, 0.0f, -m_fSideLength / 2.0f, 1.0f, 1.0f},
{ m_fSideLength / 2.0f, 0.0f, -m_fSideLength / 2.0f, 0.0f, 1.0f},
//左面
{-m_fSideLength / 2.0f, m_fSideLength, -m_fSideLength / 2.0f, 0.0f, 0.0f},
{-m_fSideLength / 2.0f, m_fSideLength, m_fSideLength / 2.0f, 1.0f, 0.0f},
{-m_fSideLength / 2.0f, 0.0f, m_fSideLength / 2.0f, 1.0f, 1.0f},
{-m_fSideLength / 2.0f, 0.0f, -m_fSideLength / 2.0f, 0.0f, 1.0f},
//正面
{-m_fSideLength / 2.0f, m_fSideLength, m_fSideLength / 2.0f, 0.0f, 0.0f},
{ m_fSideLength / 2.0f, m_fSideLength, m_fSideLength / 2.0f, 1.0f, 0.0f},
{ m_fSideLength / 2.0f, 0.0f, m_fSideLength / 2.0f, 1.0f, 1.0f},
{-m_fSideLength / 2.0f, 0.0f, m_fSideLength / 2.0f, 0.0f, 1.0f},
//右面
{ m_fSideLength / 2.0f, m_fSideLength, m_fSideLength / 2.0f, 0.0f, 0.0f},
{ m_fSideLength / 2.0f, m_fSideLength, -m_fSideLength / 2.0f, 1.0f, 0.0f},
{ m_fSideLength / 2.0f, 0.0f, -m_fSideLength / 2.0f, 1.0f, 1.0f},
{ m_fSideLength / 2.0f, 0.0f, m_fSideLength / 2.0f, 0.0f, 1.0f},
//顶面
{-m_fSideLength / 2.0f, m_fSideLength, -m_fSideLength / 2.0f, 0.0f, 0.0f},
{ m_fSideLength / 2.0f, m_fSideLength, -m_fSideLength / 2.0f, 1.0f, 0.0f},
{ m_fSideLength / 2.0f, m_fSideLength, m_fSideLength / 2.0f, 1.0f, 1.0f},
{-m_fSideLength / 2.0f, m_fSideLength, m_fSideLength / 2.0f, 0.0f, 1.0f},
//底面
{-m_fSideLength / 2.0f, 0.0f, m_fSideLength / 2.0f, 0.0f, 0.0f},
{ m_fSideLength / 2.0f, 0.0f, m_fSideLength / 2.0f, 1.0f, 0.0f},
{ m_fSideLength / 2.0f, 0.0f, -m_fSideLength / 2.0f, 1.0f, 1.0f},
{-m_fSideLength / 2.0f, 0.0f, -m_fSideLength / 2.0f, 0.0f, 1.0f},
};
if (FAILED(m_pD3DDevice->CreateVertexBuffer(sizeof(VertexData), 0, D3DFVF_XYZ | D3DFVF_TEX1,
D3DPOOL_DEFAULT, &m_pSkyBoxVertex, NULL)))
return FALSE;
void *pVertex = NULL;
if (FAILED(m_pSkyBoxVertex->Lock(0, sizeof(VertexData), &pVertex, 0)))
return FALSE;
memcpy(pVertex, VertexData, sizeof(VertexData));
m_pSkyBoxVertex->Unlock();
//索引缓存
WORD IndexData[] = {0,1,2, 0,2,3, 4,5,6, 4,6,7, 8,9,10, 8,10,11, 12,13,14, 12,14,15, 16,17,18, 16,18,19, 20,21,22, 20,22,23};
if (FAILED(m_pD3DDevice->CreateIndexBuffer(sizeof(IndexData), 0, D3DFMT_INDEX16,
D3DPOOL_DEFAULT, &m_pSkyBoxIndex, NULL)))
return FALSE;
void *pIndex = NULL;
if (FAILED(m_pSkyBoxIndex->Lock(0, sizeof(IndexData), &pIndex, 0)))
return FALSE;
memcpy(pIndex, IndexData, sizeof(IndexData));
m_pSkyBoxIndex->Unlock();
return TRUE;
}
/*
*功能 :渲染SkyBox
*参数 :void
*返回 :成功返回TRUE, 否则返回FALSE
*描述 :
*/
BOOL CGameSkyBox::RenderSkyBox()
{
m_pD3DDevice->SetStreamSource(0, m_pSkyBoxVertex, 0, sizeof(D3DVERTEX));
m_pD3DDevice->SetFVF(D3DFVF_XYZ | D3DFVF_TEX1);
m_pD3DDevice->SetIndices(m_pSkyBoxIndex);
m_pD3DDevice->SetTexture(0, m_pBackTexture);
m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 2);
m_pD3DDevice->SetTexture(0, m_pLeftTexture);
m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 6, 2);
m_pD3DDevice->SetTexture(0, m_pFrontTexture);
m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 12, 2);
m_pD3DDevice->SetTexture(0, m_pRightTexture);
m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 18, 2);
m_pD3DDevice->SetTexture(0, m_pUpTexture);
m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 24, 2);
m_pD3DDevice->SetTexture(0, m_pDownTexture);
m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 30, 2);
return TRUE;
}
/*
*功能 :释放成员变量
*参数 :void
*返回 :成功返回TRUE, 否则返回FALSE
*描述 :
*/
BOOL CGameSkyBox::ShutDown()
{
if (m_pSkyBoxVertex != NULL)
{
m_pSkyBoxVertex->Release();
m_pSkyBoxVertex = NULL;
}
if (m_pSkyBoxIndex != NULL)
{
m_pSkyBoxIndex->Release();
m_pSkyBoxIndex = NULL;
}
if (m_pFrontTexture != NULL)
{
m_pFrontTexture->Release();
m_pFrontTexture = NULL;
}
if (m_pBackTexture != NULL)
{
m_pBackTexture->Release();
m_pBackTexture = NULL;
}
if (m_pLeftTexture != NULL)
{
m_pLeftTexture->Release();
m_pLeftTexture = NULL;
}
if (m_pRightTexture != NULL)
{
m_pRightTexture->Release();
m_pRightTexture = NULL;
}
if (m_pUpTexture != NULL)
{
m_pUpTexture->Release();
m_pUpTexture = NULL;
}
if (m_pDownTexture != NULL)
{
m_pDownTexture->Release();
m_pDownTexture = NULL;
}
m_fSideLength = 0.0f;
return TRUE;
}