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

📄 multitexture.cpp

📁 3d 游戏编程入门教程之例子源码--飞机绕茶壶
💻 CPP
📖 第 1 页 / 共 3 页
字号:
                                                  D3DPOOL_MANAGED, &m_pCubeVB, NULL ) ) )
        return E_FAIL;

    VOID* tmpVertices;
    if( FAILED( m_pCubeVB->Lock( 0, m_dwSizeofCubeVertices, (VOID**)&tmpVertices, 0 ) ) )
        return E_FAIL;
    memcpy( tmpVertices, m_pCubeVertices, m_dwSizeofCubeVertices);
    m_pCubeVB->Unlock();

	m_dwSizeofCubeIndices = sizeof(m_pCubeIndices);

	// Create the index buffer
	if( FAILED( m_pd3dDevice->CreateIndexBuffer( m_dwSizeofCubeIndices,
	                                             0, D3DFMT_INDEX16,
		                                         D3DPOOL_MANAGED, &m_pCubeIB, NULL ) ) )
		return E_FAIL;

	VOID* tmpIndices;
	if( FAILED( m_pCubeIB->Lock( 0, m_dwSizeofCubeIndices, (VOID**)&tmpIndices, 0 ) ) )
	   return E_FAIL;
	memcpy( tmpIndices, m_pCubeIndices, sizeof(m_pCubeIndices) );
	m_pCubeIB->Unlock();

  
    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
    m_pFont->InvalidateDeviceObjects();
    m_pFontSmall->InvalidateDeviceObjects();

    SAFE_RELEASE( m_pCubeVB );
    SAFE_RELEASE( m_pCubeIB );
    SAFE_RELEASE( m_pVB );

	return S_OK;
}

//-----------------------------------------------------------------------------
// Name: DeleteDeviceObjects()
// Desc: Called when the app is exiting, or the device is being changed,
//       this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::DeleteDeviceObjects()
{
    m_pFont->DeleteDeviceObjects();
    m_pFontSmall->DeleteDeviceObjects();

    SAFE_RELEASE( m_pBackgroundTexture );
    SAFE_RELEASE( m_pWallTexture );
    SAFE_RELEASE( m_pEnvTexture );
    SAFE_RELEASE( m_pDetailTexture );

	return S_OK;
}

//-----------------------------------------------------------------------------
// Name: FinalCleanup()
// Desc: Called before the app exits, this function gives the app the chance
//       to cleanup after itself.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FinalCleanup()
{
    SAFE_DELETE( m_pFont );
    SAFE_DELETE( m_pFontSmall );

    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: ConfirmDevice()
// Desc: Called during device initialization, this code checks the device
//       for some minimum set of capabilities
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS9* pCaps, DWORD dwBehavior,
                                          D3DFORMAT adapterFormat, D3DFORMAT backBufferFormat )
{
    if( (0 == ( pCaps->DestBlendCaps & D3DPBLENDCAPS_INVSRCCOLOR)) &&  // alpha blending
		(0 == ( pCaps->SrcBlendCaps & D3DPBLENDCAPS_SRCCOLOR)) &&
		(0 == ( pCaps->TextureOpCaps & D3DTEXOPCAPS_ADDSIGNED )) &&   // texture blending
		(0 == ( pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE )) &&
		(0 == ( pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE2X)) &&
		(0 == ( pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE4X)) &&
		(0 == ( pCaps->TextureOpCaps & D3DTEXOPCAPS_ADD )) &&
		(0 == ( pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_CLAMP)) &&		// texture addressing
		(0 == ( pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_BORDER )) &&
		(0 == ( pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_MIRRORONCE)))
        return E_FAIL;

    if( pCaps->MaxSimultaneousTextures < 2 )
        return E_FAIL;


    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: CreateCube()
// Desc: Sets up the vertices for a cube.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::CreateCube( CUBEVERTEX* pVertices, WORD* pIndices )
{

    D3DXVECTOR3 p1 = D3DXVECTOR3(-1.0f, 1.0f,-1.0f);
    D3DXVECTOR3 p2 = D3DXVECTOR3( 1.0f, 1.0f,-1.0f);
    D3DXVECTOR3 p3 = D3DXVECTOR3( 1.0f,-1.0f,-1.0f);
    D3DXVECTOR3 p4 = D3DXVECTOR3(-1.0f,-1.0f,-1.0f);
    D3DXVECTOR3 p5 = D3DXVECTOR3(-1.0f, 1.0f, 1.0f);
    D3DXVECTOR3 p6 = D3DXVECTOR3(-1.0f,-1.0f, 1.0f);
    D3DXVECTOR3 p7 = D3DXVECTOR3( 1.0f,-1.0f, 1.0f);
    D3DXVECTOR3 p8 = D3DXVECTOR3( 1.0f, 1.0f, 1.0f);

    // Define the normals for the cube
    D3DXVECTOR3 n0( 0.0f, 0.0f,-1.0f ); // Front face
    D3DXVECTOR3 n1( 0.0f, 0.0f, 1.0f ); // Back face
    D3DXVECTOR3 n2( 0.0f, 1.0f, 0.0f ); // Bottom face
    D3DXVECTOR3 n3( 0.0f,-1.0f, 0.0f ); // Top face
    D3DXVECTOR3 n4( 1.0f, 0.0f, 0.0f ); // Left face
    D3DXVECTOR3 n5(-1.0f, 0.0f, 0.0f ); // Right face

    // Set up the vertices for the cube. Note: to prevent tiling problems,
    // the u/v coords are knocked slightly inwards.

    // Front face
    FILL_CUBEVERTEX( pVertices[0], p1, n0, 0.01f, 0.01f );
    FILL_CUBEVERTEX( pVertices[1], p2, n0, 0.99f, 0.01f );
    FILL_CUBEVERTEX( pVertices[2], p3, n0, 0.99f, 0.99f );
    FILL_CUBEVERTEX( pVertices[3], p4, n0, 0.01f, 0.99f );

    // Back face
    FILL_CUBEVERTEX( pVertices[4], p5, n1, 0.99f, 0.01f );
    FILL_CUBEVERTEX( pVertices[5], p6, n1, 0.99f, 0.99f );
    FILL_CUBEVERTEX( pVertices[6], p7, n1, 0.01f, 0.99f );
    FILL_CUBEVERTEX( pVertices[7], p8, n1, 0.01f, 0.01f );

    // Top face
    FILL_CUBEVERTEX( pVertices[8], p5, n2, 0.01f, 0.01f );
    FILL_CUBEVERTEX( pVertices[9], p8, n2, 0.99f, 0.01f );
    FILL_CUBEVERTEX( pVertices[10], p2, n2, 0.99f, 0.99f );
    FILL_CUBEVERTEX( pVertices[11], p1, n2, 0.01f, 0.99f );

    // Bottom face
    FILL_CUBEVERTEX( pVertices[12], p6, n3, 0.01f, 0.99f );
    FILL_CUBEVERTEX( pVertices[13], p4, n3, 0.01f, 0.01f );
    FILL_CUBEVERTEX( pVertices[14], p3, n3, 0.99f, 0.01f );
    FILL_CUBEVERTEX( pVertices[15], p7, n3, 0.99f, 0.99f );

    // Left face
    FILL_CUBEVERTEX( pVertices[16], p2, n4, 0.01f, 0.01f );
    FILL_CUBEVERTEX( pVertices[17], p8, n4, 0.99f, 0.01f );
    FILL_CUBEVERTEX( pVertices[18], p7, n4, 0.99f, 0.99f );
    FILL_CUBEVERTEX( pVertices[19], p3, n4, 0.01f, 0.99f );

    // Right face
    FILL_CUBEVERTEX( pVertices[20], p1, n5, 0.99f, 0.01f );
    FILL_CUBEVERTEX( pVertices[21], p4, n5, 0.99f, 0.99f );
    FILL_CUBEVERTEX( pVertices[22], p6, n5, 0.01f, 0.99f );
    FILL_CUBEVERTEX( pVertices[23], p5, n5, 0.01f, 0.01f );


    // Set up the indices for the cube
    *pIndices++ =  0+0;   *pIndices++ =  0+1;   *pIndices++ =  0+2;
    *pIndices++ =  0+2;   *pIndices++ =  0+3;   *pIndices++ =  0+0;
    *pIndices++ =  4+0;   *pIndices++ =  4+1;   *pIndices++ =  4+2;
    *pIndices++ =  4+2;   *pIndices++ =  4+3;   *pIndices++ =  4+0;
    *pIndices++ =  8+0;   *pIndices++ =  8+1;   *pIndices++ =  8+2;
    *pIndices++ =  8+2;   *pIndices++ =  8+3;   *pIndices++ =  8+0;
    *pIndices++ = 12+0;   *pIndices++ = 12+1;   *pIndices++ = 12+2;
    *pIndices++ = 12+2;   *pIndices++ = 12+3;   *pIndices++ = 12+0;
    *pIndices++ = 16+0;   *pIndices++ = 16+1;   *pIndices++ = 16+2;
    *pIndices++ = 16+2;   *pIndices++ = 16+3;   *pIndices++ = 16+0;
    *pIndices++ = 20+0;   *pIndices++ = 20+1;   *pIndices++ = 20+2;
    *pIndices++ = 20+2;   *pIndices++ = 20+3;   *pIndices++ = 20+0;

   return S_OK;
}

//----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: App custom WndProc function for handling mouse and keyboard input.
//----------------------------------------------------------------------------
LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
                                    LPARAM lParam )
{
    // Perform commands when keys are rleased
    if( WM_KEYUP == uMsg )
    {
		HMENU hMenu = GetMenu(m_hWnd);

        switch( wParam )
        {
            case VK_F5:
                m_bFil1 = TRUE;
				m_bFil2 = FALSE;
                return 1;

            case VK_F6:
                m_bFil2 = TRUE;
				m_bFil1 = FALSE;
                return 1;

            case VK_F9:
                m_bAdd1 = TRUE;
				m_bAdd2 = m_bAdd3 = FALSE;
                return 1;

            case VK_F11:
                m_bAdd2 = TRUE;
				m_bAdd1 = m_bAdd3 = FALSE;
                return 1;

            case VK_F12:
                m_bAdd3 = TRUE;
				m_bAdd1 = m_bAdd2 = FALSE;
                return 1;

            case VK_F1:
                m_bDrawHelp = !m_bDrawHelp;
                return 1;

			case '1':
					m_bTex1 = TRUE; 
					m_bTex2 = m_bTex3 = m_bTex4 = m_bTex5 = m_bTex6 = m_bTex7 = m_bTex8 =FALSE;
					SetLights();
				return 1;

			case '2':
					m_bTex2 = TRUE; 
					m_bTex1 = m_bTex3 = m_bTex4 = m_bTex5 = m_bTex6 = m_bTex7 = m_bTex8 =FALSE;
					SetLights();
				return 1;

            case '3':   
					m_bTex3 = TRUE; 
					m_bTex2 = m_bTex1 = m_bTex4 = m_bTex5 = m_bTex6 = m_bTex7 = m_bTex8 =FALSE;
					SetLights();
				return 1;

            case '4':   
					m_bTex4 = TRUE;
					m_bTex2 = m_bTex3 = m_bTex1 = m_bTex5 = m_bTex6 = m_bTex7 = m_bTex8 =FALSE;
					SetLights();
			    return 1;

            case '5':
					m_bTex5 = TRUE; 
					m_bTex2 = m_bTex3 = m_bTex4 = m_bTex1 = m_bTex6 = m_bTex7 = m_bTex8 =FALSE;
					SetLights();
				return 1;

            case '6':   
					m_bTex6 = TRUE; 
					m_bTex2 = m_bTex3 = m_bTex4 = m_bTex5 = m_bTex1 = m_bTex7 = m_bTex8 = FALSE;
					SetLights();
				return 1;

            case '7':   
					m_bTex7 = TRUE; 
					m_bTex2 = m_bTex3 = m_bTex4 = m_bTex5 = m_bTex6 = m_bTex1 = m_bTex8 = FALSE;
					SetLights();
				return 1;

            case '8':   
					m_bTex8 = TRUE; 
					m_bTex2 = m_bTex3 = m_bTex4 = m_bTex5 = m_bTex6 = m_bTex7 = m_bTex1 = FALSE;
					SetLights();
				return 1;
		}
	}

    // Fall through to the app's main windows proc function
    return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}

HRESULT CMyD3DApplication::SetLights()
{
	if (m_bTex3 == TRUE || m_bTex4 == TRUE)
	{
	    m_pd3dDevice->SetRenderState(D3DRS_AMBIENT, 0);

		D3DLIGHT9 light;
		D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.0f, -5.0f, -5.0f );
		m_pd3dDevice->SetLight( 0, &light );
		m_pd3dDevice->LightEnable( 0, TRUE );
	}
	else if (m_bTex7 == TRUE)
	{
		m_pd3dDevice->LightEnable( 0, FALSE);

		// Set the ambient light.
		m_pd3dDevice->SetRenderState(D3DRS_AMBIENT, 0x00aaffaa);
	}
  return S_OK;
}

⌨️ 快捷键说明

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