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

📄 floordeck.cpp

📁 D3D做的天空盒地面加上了纹理混合!写的非常清晰
💻 CPP
字号:
#include "FloorDeck.h"

CFloorDeck::CFloorDeck(void)
:m_Device(NULL)
,Number(0)
,m_pFoot(NULL)
{
	memset(m_Water,0,30);
}

CFloorDeck::~CFloorDeck(void)
{
}

void CFloorDeck::ReadData()
{
	
	for(int i=0;i<30;i++)
	{
		string TextName;
		char number[5];
		memset(number,0,2);
		itoa(i+1,number,10);
		TextName += ".\\waterMap\\lake_a.";
		TextName += number;
		TextName += ".dds";
		D3DXCreateTextureFromFile( m_Device, TextName.c_str(), &m_Water[i]);
		
	}
	D3DXCreateTextureFromFile( m_Device, "cobblestone_quad_01.JPG", &m_pFoot);
}

HRESULT CFloorDeck::InitTexture(LPDIRECT3DDEVICE9 g_pd3dDevice)
{
	this->m_Device = g_pd3dDevice;

	// Create the vertex buffer.
	if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
		0, D3DFVF_CUSTOMVERTEX,
		D3DPOOL_DEFAULT, &m_VB, NULL ) ) )
	{
		return E_FAIL;
	}


	// Fill the vertex buffer. We are setting the tu and tv texture
	// coordinates, which range from 0.0 to 1.0
	CUSTOMVERTEX* pVertices;
	if( FAILED( m_VB->Lock( 0, 0, (void**)&pVertices, 0 ) ) )
	{
		return E_FAIL;

	}
	pVertices[0].position.x= 5.0f;
	pVertices[0].position.y= 0.0f;
	pVertices[0].position.z= 5.0f;
	pVertices[0].color = 0xffffffff;
	pVertices[0].tu = 1.0f;
	pVertices[0].tv = 0;

	pVertices[1].position.x= 5.0f;
	pVertices[1].position.y= 0.0f;
	pVertices[1].position.z= -5.0f;
	pVertices[1].color = 0xffffffff;
	pVertices[1].tu = 1;
	pVertices[1].tv = 1;

	pVertices[2].position.x= -5.0f;
	pVertices[2].position.y= 0.0f;
	pVertices[2].position.z= 5.0f;
	pVertices[2].color = 0xffffffff;
	pVertices[2].tu = 0;
	pVertices[2].tv = 0;

	pVertices[3].position.x= -5.0f;
	pVertices[3].position.y= 0.0f;
	pVertices[3].position.z= -5.0f;
	pVertices[3].color = 0xffffffff;
	pVertices[3].tu = 0;
	pVertices[3].tv = 1;

	m_VB->Unlock();
	return S_OK;

}

void CFloorDeck::UpdataTime(float speed)
{

	static float newtime,oldtime = timeGetTime()/1000.0f;
	newtime = timeGetTime()/1000.0f;
	if(newtime-oldtime>speed)
	{
		Number++;
		oldtime = newtime;
	}
	if(Number==30)
	{
		Number = 0;
	}
}
void CFloorDeck::Matrix()
{
	D3DXMATRIXA16 matWorld;
	D3DXMatrixIdentity( &matWorld );
	D3DXMatrixTranslation(&matWorld,0.0f,0.0f,0.0f);
	//D3DXMatrixRotationX( &matWorld, timeGetTime()/1000.0f );
	m_Device->SetTransform( D3DTS_WORLD, &matWorld );
}

void CFloorDeck::Render()
{
	Matrix();
	UpdataTime(0.001f);
	m_Device->SetTexture( 0, m_Water[Number]);
	m_Device->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
	m_Device->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
	m_Device->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
	m_Device->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
	m_Device->SetTexture(1, m_pFoot);
	m_Device->SetTextureStageState(1,D3DTSS_TEXCOORDINDEX,1);
	m_Device->SetTextureStageState( 1, D3DTSS_COLOROP,   D3DTOP_MODULATE4X  );
    m_Device->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
	m_Device->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_CURRENT );
	m_Device->SetTextureStageState( 1, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );

#ifdef SHOW_HOW_TO_USE_TCI
	// Note: to use D3D texture coordinate generation, use the stage state
	// D3DTSS_TEXCOORDINDEX, as shown below. In this example, we are using
	// the position of the vertex in camera space to generate texture
	// coordinates. The tex coord index (TCI) parameters are passed into a
	// texture transform, which is a 4x4 matrix which transforms the x,y,z
	// TCI coordinates into tu, tv texture coordinates.

	// In this example, the texture matrix is setup to 
	// transform the texture from (-1,+1) position coordinates to (0,1) 
	// texture coordinate space:
	//    tu =  0.5*x + 0.5
	//    tv = -0.5*y + 0.5
	D3DXMATRIXA16 mat;
	mat._11 = 0.25f; mat._12 = 0.00f; mat._13 = 0.00f; mat._14 = 0.00f;
	mat._21 = 0.00f; mat._22 =-0.25f; mat._23 = 0.00f; mat._24 = 0.00f;
	mat._31 = 0.00f; mat._32 = 0.00f; mat._33 = 1.00f; mat._34 = 0.00f;
	mat._41 = 0.50f; mat._42 = 0.50f; mat._43 = 0.00f; mat._44 = 1.00f;

	m_Device->SetTransform( D3DTS_TEXTURE0, &mat );
	m_Device->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );
	m_Device->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION );
#endif

	// Render the vertex buffer contents
	m_Device->SetStreamSource( 0, m_VB, 0, sizeof(CUSTOMVERTEX) );
	m_Device->SetFVF( D3DFVF_CUSTOMVERTEX );
	m_Device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

}

⌨️ 快捷键说明

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