Пример #1
0
/// @copydoc Entity::Attach()
void Camera::Attach()
{
    Base::Attach();

    // Update the scene view associated with this camera.
    if( IsValid( m_sceneViewId ) )
    {
        World* pWorld = GetWorld();
        HELIUM_ASSERT( pWorld );
        GraphicsScene* pScene = pWorld->GetGraphicsScene();
        if( pScene )
        {
            GraphicsSceneView* pSceneView = pScene->GetSceneView( m_sceneViewId );
            if( pSceneView )
            {
                Simd::Matrix44 rotationMatrix( Simd::Matrix44::INIT_ROTATION, GetRotation() );
                pSceneView->SetView(
                    GetPosition(),
                    Vector4ToVector3( rotationMatrix.GetRow( 2 ) ),
                    Vector4ToVector3( rotationMatrix.GetRow( 1 ) ) );
                pSceneView->SetHorizontalFov( m_fov );
            }
        }
    }
}
Пример #2
0
void Viewport::Draw()
{
	SCENE_GRAPH_RENDER_SCOPE_TIMER( ("") );

	uint64_t start = Helium::TimerGetClock();

	if (!m_World)
	{
		return;
	}

	Camera& camera = m_Cameras[m_CameraMode];

	GraphicsScene* pGraphicsScene = GetGraphicsScene();
	GraphicsSceneView* pSceneView = pGraphicsScene->GetSceneView( m_SceneViewId );
	BufferedDrawer* pDrawer = pGraphicsScene->GetSceneViewBufferedDrawer( m_SceneViewId );

	DrawArgs args;

	{
		SCENE_GRAPH_RENDER_SCOPE_TIMER( ("Setup Viewport and Projection") );

		Vector3 pos;
		camera.GetPosition( pos );

		const Matrix4& invView = camera.GetInverseView();

		pSceneView->SetView(
			Simd::Vector3( &pos.x ),
			-Simd::Vector3( &invView.z.x ),
			Simd::Vector3( &invView.y.x ) );

		pSceneView->SetHorizontalFov( Camera::FieldOfView * static_cast< float32_t >(HELIUM_RAD_TO_DEG) );
	}

	if (m_GridVisible)
	{
		m_GlobalPrimitives[GlobalPrimitives::StandardGrid]->Draw( pDrawer, &args );
	}

#ifdef VIEWPORT_REFACTOR
	// this seems like a bad place to do this
	if (m_Tool)
	{
		SCENE_GRAPH_RENDER_SCOPE_TIMER( ("Tool Evaluate") );
		m_Tool->Evaluate();
	}

	{
		SCENE_GRAPH_RENDER_SCOPE_TIMER( ("Clear and Reset Scene") );

		device->BeginScene();
		device->SetRenderTarget( 0, m_DeviceManager.GetBackBuffer() );
		device->SetDepthStencilSurface( m_DeviceManager.GetDepthBuffer() );
		device->Clear(NULL, NULL, D3DCLEAR_TARGET | D3DCLEAR_STENCIL | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(255, 80, 80, 80), 1.0f, 0);
		device->SetTransform(D3DTS_WORLD, (D3DMATRIX*)&Matrix4::Identity);

		m_ResourceTracker->ResetState();
	}

	{
		SCENE_GRAPH_RENDER_SCOPE_TIMER( ("Setup Viewport and Projection") );

		device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)&m_Cameras[m_CameraMode].SetProjection(m_Size.x, m_Size.y));
		device->SetTransform(D3DTS_VIEW, (D3DMATRIX*)&m_Cameras[m_CameraMode].GetViewport());
	}

	{
		SCENE_GRAPH_RENDER_SCOPE_TIMER( ("Setup RenderState (culling, lighting, and fill") );

		device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
		device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

		if (m_Cameras[m_CameraMode].IsBackFaceCulling())
		{
			device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
		}
		else
		{
			device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
		}

		device->SetRenderState(D3DRS_LIGHTING, TRUE);
		device->SetRenderState(D3DRS_COLORVERTEX, FALSE);
		device->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL);
		device->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
		device->SetRenderState(D3DRS_SPECULARMATERIALSOURCE, D3DMCS_MATERIAL);

		device->SetRenderState(D3DRS_ZENABLE, TRUE);
		device->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
		device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);

		device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
		device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
		device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

		device->SetPixelShader( NULL );
		device->SetVertexShader( NULL );

		D3DLIGHT9 light;    
		ZeroMemory(&light, sizeof(light));

		D3DCOLORVALUE ambient;
		D3DCOLORVALUE diffuse;
		D3DCOLORVALUE specular;
		if ( m_Cameras[m_CameraMode].GetShadingMode() == ShadingMode::Wireframe )
		{
			ambient = SceneGraph::Color::DIMGRAY;
			diffuse = SceneGraph::Color::BLACK;
			specular = SceneGraph::Color::BLACK;
		}
		else
		{
			ambient = SceneGraph::Color::DIMGRAY;
			diffuse = SceneGraph::Color::SILVER;
			specular = SceneGraph::Color::SILVER;
		}

		Vector3 dir;
		m_Cameras[m_CameraMode].GetDirection(dir);

		// setup light
		light.Type = D3DLIGHT_DIRECTIONAL;
		light.Ambient = ambient;
		light.Diffuse = diffuse;
		light.Specular = specular;

		// set light into runtime
		light.Direction = *(D3DVECTOR*)&dir;
		device->SetLight(0, &light);
		device->LightEnable(0, true);

		// light from the back
		dir *= -1.0f;

		// set light into runtime
		light.Direction = *(D3DVECTOR*)&dir;
		device->SetLight(1, &light);
		device->LightEnable(1, true);
	}

	{
		SCENE_GRAPH_RENDER_SCOPE_TIMER( ("PreRender") );

		device->SetTransform(D3DTS_WORLD, (D3DMATRIX*)&Matrix4::Identity);

		if (m_GridVisible)
		{
			m_GlobalPrimitives[GlobalPrimitives::StandardGrid]->Draw( &args );
		}
	}

	{
		SCENE_GRAPH_RENDER_SCOPE_TIMER( ("Render") );

		{
			SCENE_GRAPH_RENDER_SCOPE_TIMER( ("Render Setup") );
			m_RenderVisitor.Reset( &args, this );
		}

		{
			SCENE_GRAPH_RENDER_SCOPE_TIMER( ("Render Walk") );
			m_Render.Raise( &m_RenderVisitor );
		}

		if (m_Tool)
		{
			SCENE_GRAPH_RENDER_SCOPE_TIMER( ("Render Tool") );
			m_Tool->Draw( &args );
		}

		{
			SCENE_GRAPH_RENDER_SCOPE_TIMER( ("Render Draw") );
			m_RenderVisitor.Draw();
		}

		args.m_EntryCount = m_RenderVisitor.GetSize();
	}

	{
		SCENE_GRAPH_RENDER_SCOPE_TIMER( ("PostRender") );

		device->SetTransform(D3DTS_WORLD, (D3DMATRIX*)&Matrix4::Identity);
		device->Clear(NULL, NULL, D3DCLEAR_ZBUFFER, 0, 1.0f, 0);

		if (m_AxesVisible)
		{
			static_cast<SceneGraph::PrimitiveAxes*>(m_GlobalPrimitives[GlobalPrimitives::ViewportAxes])->DrawViewport( &args, &m_Cameras[m_CameraMode] );
		}

		if (m_Tool)
		{
			m_Tool->Draw( &args );
		}

		if ( m_Focused )
		{
			unsigned w = 3;
			unsigned x = m_Size.x;
			unsigned y = m_Size.y;

			uint32_t color = D3DCOLOR_ARGB(255, 200, 200, 200);

			std::vector< TransformedColored > vertices;

			//   <--
			//  | \  ^
			//  v  \ |
			//   -->

			// top
			vertices.push_back(TransformedColored ((float)0,    (float)0,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)0,    (float)w,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)x,    (float)w,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)x,    (float)0,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)0,    (float)0,     1.0f,   color));

			// bottom
			vertices.push_back(TransformedColored ((float)0,    (float)y-w,   1.0f,   color));
			vertices.push_back(TransformedColored ((float)0,    (float)y,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)x,    (float)y,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)x,    (float)y-w,   1.0f,   color));
			vertices.push_back(TransformedColored ((float)0,    (float)y-w,   1.0f,   color));

			// left
			vertices.push_back(TransformedColored ((float)0,    (float)0,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)0,    (float)y,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)w,    (float)y,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)w,    (float)0,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)0,    (float)0,     1.0f,   color));

			// right
			vertices.push_back(TransformedColored ((float)x-w,  (float)0,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)x-w,  (float)y,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)x,    (float)y,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)x,    (float)0,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)x-w,  (float)0,     1.0f,   color));

			device->SetRenderState(D3DRS_ZENABLE, FALSE);
			device->SetFVF(ElementFormats[ElementTypes::TransformedColored]);
			device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, &vertices.front(), sizeof(TransformedColored));
			device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, &(vertices[5]), sizeof(TransformedColored));
			device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, &(vertices[10]), sizeof(TransformedColored));
			device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, &(vertices[15]), sizeof(TransformedColored));
			device->SetRenderState(D3DRS_ZENABLE, TRUE);

			m_ResourceTracker->ResetState();
		}

		if (m_DragMode == DragModes::Select)
		{
			m_SelectionFrame->Draw( &args );
		}
	}

	{
		SCENE_GRAPH_RENDER_SCOPE_TIMER( ("Process Statistics") );

		m_Statistics->m_FrameNumber++;
		m_Statistics->m_FrameCount++;

		m_Statistics->m_RenderTime += Helium::CyclesToMillis( Helium::TimerGetClock() - start );
		m_Statistics->m_RenderWalkTime += args.m_WalkTime;
		m_Statistics->m_RenderSortTime += args.m_SortTime;
		m_Statistics->m_RenderCompareTime += args.m_CompareTime;
		m_Statistics->m_RenderDrawTime += args.m_DrawTime;

		m_Statistics->m_EntryCount += args.m_EntryCount;
		m_Statistics->m_TriangleCount += args.m_TriangleCount;
		m_Statistics->m_LineCount += args.m_LineCount;

		m_Statistics->Update();

		if (m_StatisticsVisible)
		{
			m_Statistics->Draw(&args);
		}
	}

	{
		SCENE_GRAPH_RENDER_SCOPE_TIMER( ("End Scene") );

		device->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
		device->SetRenderState( D3DRS_ZWRITEENABLE, TRUE );
		device->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
		device->EndScene();

	}

	{
		SCENE_GRAPH_RENDER_SCOPE_TIMER( ("Display") );

		if ( m_DeviceManager.Display( m_Window ) == D3DERR_DEVICELOST )
		{
			m_DeviceManager.SetDeviceLost();
		}
	}
#endif
}
Пример #3
0
void Viewport::Draw()
{
	EDITOR_SCENE_RENDER_SCOPE_TIMER( ("") );

	uint64_t start = Timer::GetTickCount();

	if (!m_World)
	{
		return;
	}

	Camera& camera = m_Cameras[m_CameraMode];

	GraphicsScene* pGraphicsScene = GetGraphicsScene();
	GraphicsSceneView* pSceneView = pGraphicsScene->GetSceneView( m_SceneViewId );
	BufferedDrawer* pDrawer = pGraphicsScene->GetSceneViewBufferedDrawer( m_SceneViewId );

	{
		EDITOR_SCENE_RENDER_SCOPE_TIMER( ("Setup Viewport and Projection") );

		Vector3 pos;
		camera.GetPosition( pos );

		const Matrix4& invView = camera.GetInverseView();

		pSceneView->SetView(
			Simd::Vector3( &pos.x ),
			-Simd::Vector3( &invView.z.x ),
			Simd::Vector3( &invView.y.x ) );

		pSceneView->SetHorizontalFov( Camera::FieldOfView * static_cast< float32_t >(HELIUM_RAD_TO_DEG) );
	}

	pGraphicsScene->SetActiveSceneView( m_SceneViewId );
	pGraphicsScene->Update( m_World.Get() );
	pGraphicsScene->SetActiveSceneView( Invalid< uint32_t >() );

	if (m_GridVisible)
	{
		m_GlobalPrimitives[GlobalPrimitives::StandardGrid]->Draw( pDrawer );
	}

	// this seems like a bad place to do this
	if (m_Tool)
	{
		EDITOR_SCENE_RENDER_SCOPE_TIMER( ("Tool Evaluate") );
		m_Tool->Evaluate();
	}

	{
		EDITOR_SCENE_RENDER_SCOPE_TIMER( ("PreRender") );

		if (m_GridVisible)
		{
			m_GlobalPrimitives[GlobalPrimitives::StandardGrid]->Draw( pDrawer );
		}
	}

	{
		EDITOR_SCENE_RENDER_SCOPE_TIMER( ("Render") );

		{
			EDITOR_SCENE_RENDER_SCOPE_TIMER( ("Render Setup") );
			m_RenderVisitor.Reset( this, pDrawer );
		}

		{
			EDITOR_SCENE_RENDER_SCOPE_TIMER( ("Render Walk") );
			m_Render.Raise( &m_RenderVisitor );
		}

		if (m_Tool)
		{
			EDITOR_SCENE_RENDER_SCOPE_TIMER( ("Render Tool") );
			m_Tool->Draw( pDrawer );
		}
	}

	{
		EDITOR_SCENE_RENDER_SCOPE_TIMER( ("PostRender") );

		//device->Clear(NULL, NULL, D3DCLEAR_ZBUFFER, 0, 1.0f, 0);

		if (m_AxesVisible)
		{
			static_cast<Editor::PrimitiveAxes*>(m_GlobalPrimitives[GlobalPrimitives::ViewportAxes])->DrawViewport( pDrawer, &m_Cameras[m_CameraMode] );
		}

		if (m_Tool)
		{
			m_Tool->Draw( pDrawer );
		}

#if  VIEWPORT_REFACTOR
		if ( m_Focused )
		{
			unsigned w = 3;
			unsigned x = m_Size.x;
			unsigned y = m_Size.y;

			uint32_t color = D3DCOLOR_ARGB(255, 200, 200, 200);

			std::vector< TransformedColored > vertices;

			//   <--
			//  | \  ^
			//  v  \ |
			//   -->

			// top
			vertices.push_back(TransformedColored ((float)0,    (float)0,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)0,    (float)w,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)x,    (float)w,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)x,    (float)0,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)0,    (float)0,     1.0f,   color));

			// bottom
			vertices.push_back(TransformedColored ((float)0,    (float)y-w,   1.0f,   color));
			vertices.push_back(TransformedColored ((float)0,    (float)y,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)x,    (float)y,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)x,    (float)y-w,   1.0f,   color));
			vertices.push_back(TransformedColored ((float)0,    (float)y-w,   1.0f,   color));

			// left
			vertices.push_back(TransformedColored ((float)0,    (float)0,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)0,    (float)y,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)w,    (float)y,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)w,    (float)0,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)0,    (float)0,     1.0f,   color));

			// right
			vertices.push_back(TransformedColored ((float)x-w,  (float)0,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)x-w,  (float)y,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)x,    (float)y,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)x,    (float)0,     1.0f,   color));
			vertices.push_back(TransformedColored ((float)x-w,  (float)0,     1.0f,   color));

			device->SetRenderState(D3DRS_ZENABLE, FALSE);
			device->SetFVF(ElementFormats[ElementTypes::TransformedColored]);
			device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, &vertices.front(), sizeof(TransformedColored));
			device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, &(vertices[5]), sizeof(TransformedColored));
			device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, &(vertices[10]), sizeof(TransformedColored));
			device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, &(vertices[15]), sizeof(TransformedColored));
			device->SetRenderState(D3DRS_ZENABLE, TRUE);
		}

		if (m_DragMode == DragModes::Select)
		{
			m_SelectionFrame->Draw( &args );
		}
#endif
	}
}