📄 multitexture.cpp
字号:
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_ADD);
}
/*****************************************
Darkmap blended with material color
*****************************************/
if (m_bTex4 == TRUE)
{
m_pd3dDevice->SetTexture( 0, m_pWallTexture);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE);
// Set darkmap
m_pd3dDevice->SetTexture(1, m_pEnvTexture);
m_pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0 );
m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
}
/*****************************************
Glow Mapping
******************************************/
if (m_bTex5 == TRUE)
{
m_pd3dDevice->SetTexture( 0, m_pWallTexture);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
// Set glow map
m_pd3dDevice->SetTexture(1, m_pEnvTexture);
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 0 );
m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT );
m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD);
}
/*****************************************
Detail Mapping
******************************************/
if (m_bTex6 == TRUE)
{
m_pd3dDevice->SetTexture( 0, m_pWallTexture);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
// Set detail map
m_pd3dDevice->SetTexture(1, m_pDetailTexture);
m_pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0 );
m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT );
m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADDSIGNED);
}
/*****************************************
Night view: Modulate Alpha
******************************************/
if (m_bTex7 == TRUE)
{
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
m_pd3dDevice->SetTexture( 0, m_pWallTexture);
}
/****************************************
Blending with the frame buffer
*****************************************/
if (m_bTex8 == TRUE)
{
m_pd3dDevice->SetRenderState (D3DRS_ALPHABLENDENABLE, TRUE);
m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCCOLOR);
m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCCOLOR);
m_pd3dDevice->SetTexture( 0, m_pWallTexture);
}
/***************************************
Bilinear filtering
***************************************/
/* if (m_bFil1 == TRUE)
{
// bilineaer filtering
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
}
*/
/***************************************
Trilinear filtering
***************************************/
if (m_bFil1 == TRUE)
{
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR );
}
/***************************************
Anisotropic filtering
***************************************/
if (m_bFil2 == TRUE)
{
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_ANISOTROPIC);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_ANISOTROPIC);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MIPFILTER, D3DTEXF_ANISOTROPIC);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAXANISOTROPY, 16);
}
// Display the object
m_pd3dDevice->SetStreamSource( 0, m_pCubeVB, sizeof(CUBEVERTEX) );
m_pd3dDevice->SetVertexShader( FVF_CUBEVERTEX );
m_pd3dDevice->SetIndices( m_pCubeIB, 0 );
m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
0,
24, // number of vertices
0,
36/3); // number of primitives
if (m_bTex8 == TRUE)
{
// switch off alpha blending
m_pd3dDevice->SetRenderState (D3DRS_ALPHABLENDENABLE, FALSE);
}
// Output statistics
m_pFont->DrawText( 2, 0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
// Draw help
if( m_bDrawHelp )
{
m_pFontSmall->DrawText( 2, 40, 0xffaaffff,
_T("Keyboard controls:") );
m_pFontSmall->DrawText( 20, 52, 0xffaaffff,
_T("Dark Mapping\n"
"Animated Dark Mapping\n"
"Blending with Material Diffuse Color\n"
"Darkmap with Material Diffuse Color\n"
"Glow Mapping\n"
"Detail Mapping\n"
"Modulate Alpha\n"
"Blending with Frame Buffer\n\n"
"Trilinear Filtering\n"
"Anisotropic Filtering\n\n"
"Clamp Addressing Mode\n"
"Mirror Once (new in DX8)\n"
"Border Color (not supp.: Geforce)\n\n"
"Exit\n"
"Toggle Menu") );
m_pFontSmall->DrawText( 260, 52, 0xffaaffff,
_T("1\n"
"2\n"
"3\n"
"4\n"
"5\n"
"6\n"
"7\n"
"8\n\n"
"F5\n"
"F6\n\n"
"F9\n"
"F11\n"
"F12\n\n"
"ESC\n"
"F1"));
}
else
m_pFontSmall->DrawText( 2, 40, 0xffffffff,
_T("Press F1 for help") );
// End the scene.
m_pd3dDevice->EndScene();
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
m_pFont->InitDeviceObjects( m_pd3dDevice );
m_pFontSmall->InitDeviceObjects( m_pd3dDevice );
// Load in textures
if( FAILED( D3DUtil_CreateTexture(m_pd3dDevice,
_T("Lake.bmp"), &m_pBackgroundTexture)))
return D3DAPPERR_MEDIANOTFOUND;
if( FAILED( D3DUtil_CreateTexture(m_pd3dDevice,
_T("wall.bmp"), &m_pWallTexture)))
return D3DAPPERR_MEDIANOTFOUND;
if( FAILED( D3DUtil_CreateTexture(m_pd3dDevice,
_T("env0.bmp"), &m_pEnvTexture)))
return D3DAPPERR_MEDIANOTFOUND;
if( FAILED( D3DUtil_CreateTexture(m_pd3dDevice,
_T("detail.bmp"), &m_pDetailTexture)))
return D3DAPPERR_MEDIANOTFOUND;
// Generate the cube data
CreateCube( m_pCubeVertices, m_pCubeIndices );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
m_pFont->RestoreDeviceObjects();
m_pFontSmall->RestoreDeviceObjects();
// Set the transform matrices
D3DXVECTOR3 vEyePt = D3DXVECTOR3( 0.0f, 0.0f, 5.0f );
D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMATRIX matView, matProj;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 500.0f );
m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
// Material
D3DMATERIAL8 Material;
D3DUtil_InitMaterial( Material, 1.0f, 1.0f, 1.0f, 1.0f );
m_pd3dDevice->SetMaterial( &Material );
// if the viewport has changed ... fill the vertex buffer with the new
// data
D3DVIEWPORT8 vp;
m_pd3dDevice->GetViewport(&vp);
// Initialize four vertices to render two triangles
CUSTOMVERTEX cvVertices[] =
{
{ 0.0f, 0.0f, 1.0f, 1.0f, -0.5f, -0.5f, }, // x, y, z, rhw, tu, tv
{ (float)vp.Width, 0.0f, 1.0f, 1.0f, 1.5f, -0.5f, },
{ (float)vp.Width, (float)vp.Height, 1.0f, 1.0f, 1.5f, 1.5f, },
{ 0.0f, (float)vp.Height, 1.0f, 1.0f, -0.5f, 1.5f},
};
m_dwSizeofVertices = sizeof(cvVertices);
// Create the vertex buffer
if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofVertices,
0, FVF_CUSTOMVERTEX,
D3DPOOL_MANAGED, &m_pVB ) ) )
return E_FAIL;
VOID* pVertices;
if( FAILED( m_pVB->Lock( 0, m_dwSizeofVertices, (BYTE**)&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, cvVertices, m_dwSizeofVertices);
m_pVB->Unlock();
// Initialize the Index buffer
WORD dwIndices[] = {0, 1, 2, 0, 2, 3};
m_dwSizeofIndices = sizeof(dwIndices);
// Create the index buffer
if( FAILED( m_pd3dDevice->CreateIndexBuffer( m_dwSizeofIndices,
0, D3DFMT_INDEX16,
D3DPOOL_MANAGED, &m_pIB ) ) )
return E_FAIL;
VOID* pIndices;
if( FAILED( m_pIB->Lock( 0, m_dwSizeofIndices, (BYTE**)&pIndices, 0 ) ) )
return E_FAIL;
memcpy( pIndices, dwIndices, sizeof(dwIndices) );
m_pIB->Unlock();
//
// vertex and index buffer for the cube
//
m_dwSizeofCubeVertices = sizeof(m_pCubeVertices);
// Create the vertex buffer
if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofCubeVertices,
0, FVF_CUBEVERTEX,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -