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

📄 shader_fog.cpp

📁 游戏编程精华02-含有几十个游戏编程例子
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	m_pD3DDev->SetTransform(D3DTS_WORLD, &matWorld);
	m_pD3DDev->SetTransform(D3DTS_VIEW, &matView);
	m_pD3DDev->SetTransform(D3DTS_PROJECTION, &matProj);


	return S_OK;

}

HRESULT CShaderFog::DrawRoom()
{
	D3DXMATRIX matTemp;
	D3DXMATRIX matWorld;

	D3DXVECTOR4 vLightAmbient(0.5f, 0.5f, 0.5f, 1.0f);
	D3DXVECTOR4 vLightDiffuse(1.0f, 1.0f, 1.0f, 1.0f);
	m_pD3DDev->SetVertexShaderConstant(CV_LIGHT1_AMBIENT, &vLightAmbient.x, 1);
	m_pD3DDev->SetVertexShaderConstant(CV_LIGHT1_DIFFUSE, &vLightDiffuse.x, 1);

	// Draw the room
	// Translate to center and scale
	D3DXMatrixTranslation(&matWorld, -m_RoomBounds.m_vecCenter.x, -m_RoomBounds.m_vecCenter.y, -m_RoomBounds.m_vecCenter.z);
	
	D3DXVECTOR3 vecScale = m_RoomBounds.m_vecMaxExtents - m_RoomBounds.m_vecMinExtents;
	vecScale.x = (1.0f / (float)fabs(vecScale.x)) * (m_vecRoomDimensions.x + 1.0f);
	vecScale.y = (1.0f / (float)fabs(vecScale.y)) * (m_vecRoomDimensions.y + 1.0f);
	vecScale.z = (1.0f / (float)fabs(vecScale.z)) * (m_vecRoomDimensions.z + 1.0f);
	
	D3DXMatrixScaling(&matTemp, vecScale.x, vecScale.y, vecScale.z);
	matWorld = matWorld * matTemp;

	m_pNVDevice->SetWorldTransform(&matWorld);
	SetTransform();

	m_pD3DDev->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
    m_pD3DDev->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
	m_pD3DDev->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR );
	m_pD3DDev->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP);

	m_dwCurrentShader = m_dwFogShader;
	m_pD3DDev->SetVertexShader(m_dwFogShader);

	// Set room state.  Want to apply fog in second pass
	m_pD3DDev->SetTextureStageState(0, D3DTSS_COLOROP,   D3DTOP_MODULATE);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
	m_pD3DDev->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
	m_pD3DDev->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
			
	m_pD3DDev->SetTexture(1, m_pFogMap);
	m_pD3DDev->SetTextureStageState(1, D3DTSS_COLOROP,   D3DTOP_BLENDTEXTUREALPHA);
	m_pD3DDev->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	m_pD3DDev->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
	m_pD3DDev->SetTextureStageState(1, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1);
	m_pD3DDev->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
	m_pD3DDev->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_CURRENT );
	m_pD3DDev->SetTextureStageState(1, D3DTSS_MIPFILTER, D3DTEXF_NONE);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
    m_pD3DDev->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
	m_pD3DDev->SetTextureStageState(1, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
	m_pD3DDev->SetTextureStageState(1, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);
	m_pD3DDev->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);
	
	// Draw the room
	m_pRoom->Render(m_pNVDevice);

	// Add the chairs & table
	m_pD3DDev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
	m_pD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

	vLightAmbient = D3DXVECTOR4(0.0f, 0.0f, 0.0f, 1.0f);
	m_pD3DDev->SetVertexShaderConstant(CV_LIGHT1_AMBIENT, &vLightAmbient.x, 1);

	// First, the chairs
	D3DXMATRIX matTranslate1;
	D3DXMATRIX matScale;
	D3DXMATRIX matRotate;
	D3DXMATRIX matTranslate2;
	
	D3DXMatrixTranslation(&matTranslate1, -m_ChairBounds.m_vecCenter.x, 
		-m_ChairBounds.m_vecCenter.y,
		-m_ChairBounds.m_vecCenter.z);

	vecScale = m_ChairBounds.m_vecMaxExtents - m_ChairBounds.m_vecMinExtents;
	vecScale.x = (1.0f / (float)fabs(vecScale.x)) * m_vecChairDimensions.x;
	vecScale.y = (1.0f / (float)fabs(vecScale.y)) * m_vecChairDimensions.y;
	vecScale.z = (1.0f / (float)fabs(vecScale.z)) * m_vecChairDimensions.z;
	D3DXMatrixScaling(&matScale, vecScale.x, vecScale.y, vecScale.z);

	for (int i = 0; i < NUM_CHAIRS; i++)
	{
		D3DXMatrixRotationY(&matRotate, m_vecChairs[i].w);

		D3DXMatrixTranslation(&matTranslate2, m_vecChairs[i].x, m_vecChairs[i].y, m_vecChairs[i].z);
		matWorld = matTranslate1 * matRotate * matScale * matTranslate2;

		m_pNVDevice->SetWorldTransform(&matWorld);
		SetTransform();

		// Draw the chair
		m_pChair->Render(m_pNVDevice);
	}	

	// Second, the table
	D3DXMatrixTranslation(&matTranslate1, -m_TableBounds.m_vecCenter.x, 
		-m_TableBounds.m_vecCenter.y,
		-m_TableBounds.m_vecCenter.z);

	vecScale = m_TableBounds.m_vecMaxExtents - m_TableBounds.m_vecMinExtents;
	vecScale.x = (1.0f / (float)fabs(vecScale.x)) * m_vecTableDimensions.x;
	vecScale.y = (1.0f / (float)fabs(vecScale.y)) * m_vecTableDimensions.y;
	vecScale.z = (1.0f / (float)fabs(vecScale.z)) * m_vecTableDimensions.z;
	D3DXMatrixScaling(&matScale, vecScale.x, vecScale.y, vecScale.z);
	D3DXMatrixTranslation(&matTranslate2, 0.0f, -(m_vecRoomDimensions.y * 0.5f) + (m_vecTableDimensions.y * 0.5f), 0.0f);

	matWorld = matTranslate1 * matScale * matTranslate2;
	m_pNVDevice->SetWorldTransform(&matWorld);
	SetTransform();

	m_pTable->Render(m_pNVDevice);


    return S_OK;
}

HRESULT CShaderFog::DrawWorldBox()
{
	D3DXVECTOR3 vecScale;
	D3DXMATRIX matWorld;

	// Scale the world box and put it outside the world
	vecScale.x = (m_vecRoomDimensions.x * 0.5f) + 5.0f;
	vecScale.y = (m_vecRoomDimensions.y * 0.5f) + 5.0f;
	vecScale.z = (m_vecRoomDimensions.z * 0.5f) + 5.0f;
	D3DXMatrixScaling(&matWorld, vecScale.x, vecScale.y, vecScale.z);

	m_pNVDevice->SetWorldTransform(&matWorld);
	SetTransform();
	
	m_pD3DDev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
    m_pD3DDev->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
	m_pD3DDev->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_NONE);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);

	m_pD3DDev->SetTexture(1, m_pFogMap);
	m_pD3DDev->SetTextureStageState(1, D3DTSS_COLOROP,   D3DTOP_BLENDTEXTUREALPHA);
	m_pD3DDev->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	m_pD3DDev->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
	m_pD3DDev->SetTextureStageState(1, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1);
	m_pD3DDev->SetTextureStageState(1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
	m_pD3DDev->SetTextureStageState(1, D3DTSS_ALPHAARG2, D3DTA_CURRENT );
	m_pD3DDev->SetTextureStageState(1, D3DTSS_MIPFILTER, D3DTEXF_NONE);
	m_pD3DDev->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
    m_pD3DDev->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
	m_pD3DDev->SetTextureStageState(1, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
	m_pD3DDev->SetTextureStageState(1, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);
	m_pD3DDev->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);

	m_pD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    
	m_pD3DDev->SetStreamSource(0, m_pWorldBoxVertices, sizeof(WorldBoxVertex));
	m_pD3DDev->SetIndices(m_pWorldBoxIndices, 0);
	m_pD3DDev->SetVertexShader(m_dwWorldShader);

	m_pD3DDev->SetTexture(0, m_pWorldTextures[D3DCUBEMAP_FACE_NEGATIVE_Z]);
	m_pD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, NUM_CUBE_VERTICES, 0, 2);

	m_pD3DDev->SetTexture(0, m_pWorldTextures[D3DCUBEMAP_FACE_POSITIVE_Z]);
	m_pD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, NUM_CUBE_VERTICES, 6, 2);
	
	m_pD3DDev->SetTexture(0, m_pWorldTextures[D3DCUBEMAP_FACE_NEGATIVE_Y]);
	m_pD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, NUM_CUBE_VERTICES, 12, 2);

	m_pD3DDev->SetTexture(0, m_pWorldTextures[D3DCUBEMAP_FACE_POSITIVE_Y]);
	m_pD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, NUM_CUBE_VERTICES, 18, 2);

	m_pD3DDev->SetTexture(0, m_pWorldTextures[D3DCUBEMAP_FACE_NEGATIVE_X]);
	m_pD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, NUM_CUBE_VERTICES, 24, 2);

	m_pD3DDev->SetTexture(0, m_pWorldTextures[D3DCUBEMAP_FACE_POSITIVE_X]);
	m_pD3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, NUM_CUBE_VERTICES, 30, 2);
		
	m_pD3DDev->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
	
	return S_OK;
}

HRESULT CShaderFog::Tick(EBTimer* pTimer)
{
	HRESULT hr = S_OK;

	hr = m_pD3DDev->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0xFF, 0x0, 0x0 ), 1.0, 0);
	
	m_pD3DDev->SetRenderState(D3DRS_FILLMODE, m_bWireframe ? D3DFILL_WIREFRAME : D3DFILL_SOLID);

	m_pCamera->CheckKeyboard();
	m_pNVDevice->SetViewTransform(&m_pCamera->GetCameraMatrix());

	// Build the fog map.  Only needs to be done when the eye changes height
	BuildFogMap();

	// Create a directional light
	D3DXVECTOR3 vLightToEye;
	D3DXVECTOR3 vLight(1.0f, -1.0f, 0.35f);
	D3DXVECTOR4 vLightEye;

	// Transform direction vector into eye space
	D3DXVec3Normalize(&vLightToEye, &vLight);
	D3DXVec3TransformNormal(&vLightToEye, &vLightToEye, &m_pCamera->GetCameraMatrix());
	D3DXVec3Normalize(&vLightToEye, &vLightToEye);

	// Shader math requires that the vector is to the light
	vLightEye.x = -vLightToEye.x;
	vLightEye.y = -vLightToEye.y;
	vLightEye.z = -vLightToEye.z;
	vLightEye.w = 1.0f;
	
	m_pD3DDev->SetVertexShaderConstant(CV_LIGHT1_DIRECTION, &vLightEye.x, 1);

	D3DXVECTOR3 CameraPosition = m_pCamera->GetCameraPosition();

	m_pD3DDev->SetVertexShaderConstant(CV_WORLD_EYE, D3DXVECTOR4(CameraPosition.x, CameraPosition.y, CameraPosition.z, 1.0f), 1);

	// Set the current transform
	SetTransform();

	// Draw the room and the chairs/table
	DrawRoom();

	// Draw the 'outside' world
	DrawWorldBox();

	// Show the fog map
	if (m_bShowFogMap)
		DrawFogMap();

	return hr;
}

// Overrides for mesh renderer
HRESULT NVFogDevice::SetVertexShader(DWORD dwVertexShader)
{
	if (m_pFogShader->m_dwCurrentShader != (DWORD)-1)
		dwVertexShader = m_pFogShader->m_dwCurrentShader;

	return m_pD3DDev->SetVertexShader(dwVertexShader);
}

HRESULT NVFogDevice::SetMaterial(D3DMATERIAL8* pMat)
{
	m_pD3DDev->SetVertexShaderConstant(CV_LIGHT1_DIFFUSE, D3DXVECTOR4(pMat->Diffuse.r, pMat->Diffuse.g, pMat->Diffuse.b, pMat->Diffuse.a), 1);

	return S_OK;
}

// Device API's
void NVFogDevice::SetWorldTransform(const D3DXMATRIX* pValue) 
{
	NVDevice::SetWorldTransform(pValue);
	m_pFogShader->SetTransform();
}

void CShaderFog::Keyboard(DWORD vKey, UINT nFlags, bool bDown)
{
	eEBKeyAction Action = TranslateEffectKey(vKey, nFlags, bDown);
	
	static const float fTranslateMagnitude = 0.1f;
    switch ( Action )
    {
		case EB_HELP:
		{
			::MessageBoxEx( NULL, " Help : F1 - Help \n\n Home - Reset To Defaults \n\n W - Wireframe Toggle \n\n Space\\Pause - Toggle Pause/Resume \n\n Left Button & Mouse - Rotate Object\n\n Shift Left Button & Mouse - Pan Camera \n\n Ctrl Left Button & Mouse - Move Camera In & Out\n\n",
				   "Help", MB_ICONINFORMATION | MB_TASKMODAL, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ) );
		}
		break;

		case EB_WIREFRAME:
        {
            m_bWireframe = !m_bWireframe;
			m_dwEffectDirtyFlags |= EBEFFECT_DIRTY_PUBLICSTATE;
            
        }
		break;

		case EB_RESET:
        {
            m_pCamera->Reset();
            m_bWireframe = false;
			m_dwEffectDirtyFlags |= EBEFFECT_DIRTY_PUBLICSTATE;
            
        }
		break;

        default :
            break;
    }
}

void CShaderFog::MouseButton(HWND hWnd, eButtonID button, bool bDown, int x, int y)
{
	if(button == MOUSE_LEFTBUTTON)
	{
		if(bDown)
		{
			m_pCamera->OnLButtonDown(hWnd, x, y);
		}
		else
		{
			m_pCamera->OnLButtonUp(hWnd, x, y);
		}
	}
	return;
}

void CShaderFog::MouseMove(HWND hWnd, int x, int y)
{
	m_pCamera->OnMouseMove(hWnd, x, y);
	return;
}

// ------------------------------------------------------------------------------
// CShaderSineWave::ConfirmDevice
//
// Description: Performs caps-bit checking to make sure the selected device 
//      supports this demo. 
// ------------------------------------------------------------------------------ 
HRESULT CShaderFog::ConfirmDevice(D3DCAPS8* pCaps, DWORD dwBehavior, D3DFORMAT Format)
{
	return S_OK;
}

⌨️ 快捷键说明

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