コード例 #1
0
ファイル: 3DCanvas.cpp プロジェクト: LiXizhi/NPLRuntime
void C3DCanvas::Draw(float fDeltaTime)
{
	CBaseObject* pActor = GetActor();
	if(!IsEnabled() || pActor==0)
		return;
	if(m_bNeedUpdate == false)
		return;

	if(!m_bInitialized)
	{
		InitDeviceObjects();
		RestoreDeviceObjects();
	}
	if(m_pCanvasSurface == 0 || m_pDepthStencilSurface==0)
		return;

	if(m_bAutoRotate)
	{
		m_camera.Rotate(0,0.3f*fDeltaTime, 0);
	}

	m_bNeedUpdate = false;
	
	EffectManager* pEffectManager = CGlobals::GetEffectManager();
	LPDIRECT3DDEVICE9 pd3dDevice = CGlobals::GetRenderDevice();

	LPDIRECT3DSURFACE9 pOldRenderTarget =  CGlobals::GetDirectXEngine().GetRenderTarget();
	CGlobals::GetDirectXEngine().SetRenderTarget(0, m_pCanvasSurface);

	// save old render origin
	Vector3 vOldRenderOrigin = CGlobals::GetScene()->GetRenderOrigin();

	// set depth surface
	LPDIRECT3DSURFACE9 pOldZBuffer = NULL;
	if(FAILED(pd3dDevice->GetDepthStencilSurface(&pOldZBuffer)))
		return;
	pd3dDevice->SetDepthStencilSurface( m_pDepthStencilSurface );

	// save old actor position
	pActor->PushParam();

	pActor->SetPosition(m_vActorPosition);
	pActor->SetFacing(m_fActorFacing);
	

	// animate the camera and push result to transformation stack
	m_camera.FrameMove(fDeltaTime);

	CGlobals::GetProjectionMatrixStack().push(*m_camera.GetProjMatrix());
	CGlobals::GetViewMatrixStack().push(*m_camera.GetViewMatrix());
	CGlobals::GetEffectManager()->UpdateD3DPipelineTransform(true,true, true);

	// clear 
	pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, COLOR_RGBA(0, 0, 0, 0), 1.0f, 0L);

	// disable fog
	pEffectManager->EnableFog(false);

	
	if(pEffectManager->BeginEffect(pActor->GetPrimaryTechniqueHandle(), &(CGlobals::GetSceneState()->m_pCurrentEffect)))
	{
		/** draw the main stage object */
		pActor->Draw(CGlobals::GetSceneState());
		CGlobals::GetSceneState()->GetFaceGroups()->Clear();
	}
	

	if(m_pMask!=0 && pEffectManager->BeginEffect(TECH_GUI, &(CGlobals::GetSceneState()->m_pCurrentEffect)))
	{
		/** draw the mask square in front of the screen. */
		CEffectFile* pEffectFile = CGlobals::GetEffectManager()->GetCurrentEffectFile();
		if(pEffectFile == 0)
		{
			//////////////////////////////////////////////////////////////////////////
			// fixed programming pipeline
			float sx = (float)m_nTextureWidth,sy = (float)m_nTextureHeight;
			
			DXUT_SCREEN_VERTEX v[10];
			v[0].x = 0;  v[0].y = sy; v[0].tu = 0;  v[0].tv = 1.0f;
			v[1].x = 0;  v[1].y = 0;  v[1].tu = 0;  v[1].tv = 0;
			v[2].x = sx; v[2].y = sy; v[2].tu = 1.f; v[2].tv = 1.f;
			v[3].x = sx; v[3].y = 0;  v[3].tu = 1.f; v[3].tv = 0;

			DWORD dwColor = LinearColor(1.f,1.f,1.f,1.f);
			int i;
			for(i=0;i<4;i++)
			{
				v[i].color = dwColor;
				v[i].z = 0;
				v[i].h = 1.0f;
			}
			CGlobals::GetRenderDevice()->SetTexture(0, m_pMask->GetTexture());
			RenderDevice::DrawPrimitiveUP( CGlobals::GetRenderDevice(), RenderDevice::DRAW_PERF_TRIANGLES_UI, D3DPT_TRIANGLESTRIP, 2, v, sizeof(DXUT_SCREEN_VERTEX) );
		}
	}
	
	//////////////////////////////////////////////////////////////////////////
	// Restore state for the normal pipeline

	// enable fog
	pEffectManager->EnableFog(CGlobals::GetScene()->IsFogEnabled());

	// restore old actor position
	pActor->PopParam();
	
	// restore transformations
	CGlobals::GetProjectionMatrixStack().pop();
	CGlobals::GetViewMatrixStack().pop();
	CGlobals::GetEffectManager()->UpdateD3DPipelineTransform(true,true, true);

	// restore old depth surface
	pd3dDevice->SetDepthStencilSurface( pOldZBuffer);
	SAFE_RELEASE(pOldZBuffer);
	// Restore the old render target: i.e. the backbuffer
	HRESULT hr = CGlobals::GetDirectXEngine().SetRenderTarget(0, pOldRenderTarget);
	assert(hr == D3D_OK);

	// restore render origin
	CGlobals::GetScene()->RegenerateRenderOrigin(vOldRenderOrigin);

#ifdef _DEBUG
	//SaveToFile("E:\\cavas_obj.png");
#endif

}