⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 skybox.cpp

📁 3D游戏展示程序
💻 CPP
字号:
//--------------------------------------------------
//  Desc: SkyBox
//  Author: artsylee/2007.1.25
//--------------------------------------------------

#include "SkyBox.h"
#include "Common.h"
#include "ase/ASEngine.h"

const DWORD SkyBoxVertex::FVF = D3DFVF_XYZ | D3DFVF_TEX1;

CSkyBox::CSkyBox()
{
	m_XLen = 100;
	m_YLen = 100;
	m_ZLen = 100;
	m_CenterPosition = D3DXVECTOR3(0, 0, 0);

	m_pBoxVB = NULL;
}

CSkyBox::~CSkyBox()
{
	m_pBoxVB->Release();
	m_pBoxVB = NULL;
	if(g_pTextureManager == NULL)	return;

	for(int i=0; i<6; i++)
	{
		g_pTextureManager->ReleaseTexture(m_hSkyTex[i]);
	}
}

void CSkyBox::InitSkyBox(float x, float y, float z)
{
	m_XLen = x;
	m_YLen = y;
	m_ZLen = z;

	char filename[256];
	for(int i=0; i<5; i++)
	{
		sprintf(filename, "Picture\\skyb%d.bmp", i);
		g_pTextureManager->ReleaseTexture(m_hSkyTex[i]);
		m_hSkyTex[i] = g_pTextureManager->LoadTexture(filename, MM_KEEPINMEMORY);
	}
	m_hSkyTex[5] = g_pTextureManager->LoadTexture("Picture\\art_test.bmp", MM_KEEPINMEMORY);
	if(m_pBoxVB != NULL)
	{
		m_pBoxVB->Release();
		m_pBoxVB = NULL;
	}

	SkyBoxVertex pV[36];
	int point = 0;
	pV[point++] = SkyBoxVertex(-m_XLen, m_YLen, -m_ZLen, 0.0f, 0.0f);
	pV[point++] = SkyBoxVertex(-m_XLen, m_YLen,  m_ZLen, 1.0f, 0.0f);
	pV[point++] = SkyBoxVertex(-m_XLen, 0,      -m_ZLen, 0.0f, 1.0f);
	pV[point++] = SkyBoxVertex(-m_XLen, 0,	    -m_ZLen, 0.0f, 1.0f);
	pV[point++] = SkyBoxVertex(-m_XLen, m_YLen,  m_ZLen, 1.0f, 0.0f);
	pV[point++] = SkyBoxVertex(-m_XLen, 0,		 m_ZLen, 1.0f, 1.0f);

	pV[point++] = SkyBoxVertex(-m_XLen, m_YLen,  m_ZLen, 0.0f, 0.0f);
	pV[point++] = SkyBoxVertex( m_XLen, m_YLen,  m_ZLen, 1.0f, 0.0f);
	pV[point++] = SkyBoxVertex(-m_XLen, 0,		 m_ZLen, 0.0f, 1.0f);
	pV[point++] = SkyBoxVertex(-m_XLen, 0,		 m_ZLen, 0.0f, 1.0f);
	pV[point++] = SkyBoxVertex( m_XLen, m_YLen,  m_ZLen, 1.0f, 0.0f);
	pV[point++] = SkyBoxVertex( m_XLen, 0,	     m_ZLen, 1.0f, 1.0f);

	pV[point++] = SkyBoxVertex( m_XLen, m_YLen,  m_ZLen, 0.0f, 0.0f);
	pV[point++] = SkyBoxVertex( m_XLen, m_YLen, -m_ZLen, 1.0f, 0.0f);
	pV[point++] = SkyBoxVertex( m_XLen, 0,	     m_ZLen, 0.0f, 1.0f);
	pV[point++] = SkyBoxVertex( m_XLen, 0,		 m_ZLen, 0.0f, 1.0f);
	pV[point++] = SkyBoxVertex( m_XLen, m_YLen, -m_ZLen, 1.0f, 0.0f);
	pV[point++] = SkyBoxVertex( m_XLen, 0,      -m_ZLen, 1.0f, 1.0f);

	pV[point++] = SkyBoxVertex( m_XLen, m_YLen, -m_ZLen, 0.0f, 0.0f);
	pV[point++] = SkyBoxVertex(-m_XLen, m_YLen, -m_ZLen, 1.0f, 0.0f);
	pV[point++] = SkyBoxVertex( m_XLen, 0,      -m_ZLen, 0.0f, 1.0f);
	pV[point++] = SkyBoxVertex( m_XLen, 0,      -m_ZLen, 0.0f, 1.0f);
	pV[point++] = SkyBoxVertex(-m_XLen, m_YLen, -m_ZLen, 1.0f, 0.0f);
	pV[point++] = SkyBoxVertex(-m_XLen, 0,		-m_ZLen, 1.0f, 1.0f);
	// top
	pV[point++] = SkyBoxVertex(-m_XLen, m_YLen, -m_ZLen, 0.0f, 1.0f);
	pV[point++] = SkyBoxVertex( m_XLen, m_YLen, -m_ZLen, 0.0f, 0.0f);
	pV[point++] = SkyBoxVertex(-m_XLen, m_YLen,  m_ZLen, 1.0f, 1.0f);
	pV[point++] = SkyBoxVertex(-m_XLen, m_YLen,  m_ZLen, 1.0f, 1.0f);
	pV[point++] = SkyBoxVertex( m_XLen, m_YLen, -m_ZLen, 0.0f, 0.0f);
	pV[point++] = SkyBoxVertex( m_XLen, m_YLen,  m_ZLen, 1.0f, 0.0f);
	// bottom
	pV[point++] = SkyBoxVertex(-m_XLen, 0, -m_ZLen, 0.0f, 1.0f);
	pV[point++] = SkyBoxVertex(-m_XLen, 0,  m_ZLen, 0.0f, 0.0f);
	pV[point++] = SkyBoxVertex( m_XLen, 0,  m_ZLen, 1.0f, 0.0f);
	pV[point++] = SkyBoxVertex( m_XLen, 0,  m_ZLen, 1.0f, 0.0f);
	pV[point++] = SkyBoxVertex( m_XLen, 0, -m_ZLen, 1.0f, 1.0f);
	pV[point++] = SkyBoxVertex(-m_XLen, 0, -m_ZLen, 0.0f, 1.0f);
	

	g_pD3DDevice->CreateVertexBuffer(36* sizeof(SkyBoxVertex), D3DUSAGE_WRITEONLY,
		SkyBoxVertex::FVF, D3DPOOL_MANAGED, &m_pBoxVB, 0);

	void* pBuffer = NULL;
	m_pBoxVB->Lock(0, 36*sizeof(SkyBoxVertex), &pBuffer,0);
	memcpy(pBuffer, pV, 36*sizeof(SkyBoxVertex));
	m_pBoxVB->Unlock();
}

void CSkyBox::SetCenter(D3DXVECTOR3 center)
{
	m_CenterPosition = center;
}

void CSkyBox::DrawSkyBox()
{
	D3DXMATRIX matWorld;
	D3DXMatrixTranslation(&matWorld, m_CenterPosition.x, m_CenterPosition.y, m_CenterPosition.z);
	g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
	g_pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
	g_pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);

	g_pD3DDevice->SetStreamSource(0, m_pBoxVB, 0, sizeof(SkyBoxVertex));
	g_pD3DDevice->SetFVF(SkyBoxVertex::FVF);
	for(int i=0; i<6; i++)
	{
		SET_TEXTURE_TYPE(0, m_hSkyTex[i]);
		g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, i*6, 2);
	}
	g_pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
	g_pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -