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

📄 example.cpp

📁 3d 游戏 入门教程之例子源码-图像渲染
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			// 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 );

	HRESULT hr;

	TCHAR strFile[160];
	DXUtil_FindMediaFileCb(strFile, sizeof(strFile),_T("sphere.x"));
	LoadXFile(strFile);

	if( FAILED( hr = D3DXCreateEffectFromFile( m_pd3dDevice, "hlsl.fx", NULL, NULL, 
		D3DXSHADER_DEBUG, NULL, &m_pEffect, NULL ) ) )
		return hr;

	// load
	//  color map
	//  bump map
	//  cube normalization map
	if(FAILED(D3DXCreateTextureFromFile(m_pd3dDevice, m_cColorMap,&m_pColorMap)))
		return E_FAIL;
	if(FAILED(D3DXCreateTextureFromFile(m_pd3dDevice, m_cBumpMap,&m_pBumpMap)))
		return E_FAIL;

	return S_OK;
}

//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
	m_pFont->RestoreDeviceObjects();
    m_pFontSmall->RestoreDeviceObjects();
	if( m_pEffect != NULL ) m_pEffect->OnResetDevice();

    m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
	m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
	m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );

	D3DXMATRIX matProjection;
	D3DXMatrixPerspectiveFovLH(&m_matProj, 0.05f, 
		(FLOAT)m_d3dsdBackBuffer.Width /(FLOAT)m_d3dsdBackBuffer.Height, 
		5.0f, 500.0f);

	// init trackball 
	m_pTrackball.Init(m_d3dsdBackBuffer.Width, m_d3dsdBackBuffer.Height);

    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
    m_pFont->InvalidateDeviceObjects();
    m_pFontSmall->InvalidateDeviceObjects();
	if( m_pEffect != NULL ) m_pEffect->OnLostDevice();

	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_pEffect );
	SAFE_RELEASE( m_pD3DXMesh );

	SAFE_RELEASE(m_pBumpMap);
	SAFE_RELEASE(m_pColorMap);

	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()
{
    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( (dwBehavior & D3DCREATE_HARDWARE_VERTEXPROCESSING ) ||
		(dwBehavior & D3DCREATE_MIXED_VERTEXPROCESSING ) )
	{
		if(!( pCaps->VertexShaderVersion >= D3DVS_VERSION(1,1) ))
			return E_FAIL;
	}
	if(!( pCaps->PixelShaderVersion >= D3DPS_VERSION(1,1) ))
		return E_FAIL;

    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: Message proc function to handle key and menu input
//-----------------------------------------------------------------------------
LRESULT CMyD3DApplication::MsgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    // Trap the context menu
    if( WM_CONTEXTMENU==uMsg )
        return 0;

    switch(uMsg)
    {
    case WM_LBUTTONDOWN:
		SetCapture(hwnd);
        return 0;
    case WM_LBUTTONUP:
		ReleaseCapture();
        return 0;

	// track mouse movement to rotate sphere
    case WM_MOUSEMOVE:
	{
	    RECT r;
		FLOAT dx,dy;
		D3DXMATRIX mat1,mat2,mat3;

		GetWindowRect(m_hWnd,&r);
	    dx = ((FLOAT)m_xPos)/((FLOAT)r.right-r.left) * 2 - 1.0f;
	    dy = ((FLOAT)m_yPos)/((FLOAT)r.right-r.left) * 2 - 0.7f;
    
	    // left button rotates the object
	    if(m_bLMouseDown)
		{   
	      m_fTheta  = -dy+m_oPosY;
	      m_fTheta2 = -dx+m_oPosX;
    
	      D3DXMatrixRotationX(&mat1,m_fTheta * 0.1f);
	      D3DXMatrixRotationY(&mat2,m_fTheta2 * 0.1f);
	      D3DXMatrixMultiply(&mat3,&mat1,&mat2);
	      D3DXMatrixMultiply(&m_matWorld,&m_matWorld,&mat3);
		}
	}
    break;

    case WM_KEYUP:
        // mark a key up
        m_bKey[wParam] = FALSE;
        break;

    case WM_KEYDOWN:
        // mark a key down
         m_bKey[wParam] = TRUE;
       break;
   }

    return CD3DApplication::MsgProc( hwnd, uMsg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// Name: LoadXFile()
// Desc: loads the x-file
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::LoadXFile(TCHAR* name)
{
	HRESULT		hr;
	LPD3DXMESH	pMeshSysMem = NULL, pMeshSysMem2 = NULL;

	if (FAILED (D3DXLoadMeshFromX(name, D3DXMESH_SYSTEMMEM, 
				m_pd3dDevice, NULL, NULL, NULL, NULL, &pMeshSysMem)))
		return E_FAIL;

	D3DVERTEXELEMENT9 decl[]=
	{
		// stream, offset, type, method, semantic type (for example normal), ?
		{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
		{0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
		{0, 20, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
		{0, 32, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0},
		D3DDECL_END()
	};

	hr = pMeshSysMem->CloneMesh(D3DXMESH_MANAGED, decl, m_pd3dDevice, &pMeshSysMem2);

	// compute the normals
	hr = D3DXComputeNormals(pMeshSysMem2,NULL);
	if(FAILED(hr))
		return E_FAIL;

	// compute U (tangent)
	hr = D3DXComputeTangent(pMeshSysMem2,0,0,0,TRUE,NULL);
	if(FAILED(hr))
		return E_FAIL;

	//---------------------------------------------------------------------------
	// the HLSL compiler awaits as an input a float4 position
	// therefore we first calculate the normals and tangents with a float3 and
	// then clone the mesh with a float4 position
	//---------------------------------------------------------------------------
	D3DVERTEXELEMENT9 decl2[]=
	{
		// stream, offset, type, method, semantic type (for example normal), ?
		{0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
		{0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
		{0, 24, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
		{0, 36, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0},
		D3DDECL_END()
	};

	hr = pMeshSysMem2->CloneMesh(D3DXMESH_MANAGED, decl2, m_pd3dDevice, &m_pD3DXMesh);
		
	// cleanup
	SAFE_RELEASE(pMeshSysMem);
	SAFE_RELEASE(pMeshSysMem2);

	return S_OK;
}

⌨️ 快捷键说明

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