Exemplo n.º 1
0
HRESULT CameraMoveTool::GetCameraFromViewController(__in IViewController *pView, __deref_out_opt ISceneObjectCamera **ppCamera)
{
	HRESULT hr;

	SmartPtr<IViewControllerScene> spViewSceneController;
	hr = pView->QueryInterface(&spViewSceneController);

	if (SUCCEEDED(hr))
	{
		SmartPtr<IRenderer> spRenderer;
		hr = spViewSceneController->GetRenderer(&spRenderer);
		if (SUCCEEDED(hr))
		{
			SmartPtr<ISceneObjectCamera> spCamera;
			hr = spRenderer->GetCamera(&spCamera);

			if (SUCCEEDED(hr))
			{
				hr = spCamera.CopyToOpt(ppCamera);
			}
		}
	}
	return hr;
}
Exemplo n.º 2
0
HRESULT RenderableImage::Draw(UINT passIndex, __in IRendererHandler *pRenderer)
{	
    UNREFERENCED_PARAMETER(passIndex);
    HRESULT hr;

    // Get suitable renderer
    SmartPtr<IRenderer> spRenderer;
    hr = pRenderer->QueryInterface(&spRenderer);
    IF_FAILED_RETURN(hr);
    
    // Calibrate
    //
    SmartPtr<ICalibrable> spCalibrable = m_spSceneObject.CastTo<ICalibrable>();
    if (spCalibrable && !spCalibrable->IsCalibrated())
    {
        XMMATRIX viewMatrix, projectionMatrix;
        SmartPtr<ISceneObjectCamera> spCamera;
        hr = spRenderer->GetCamera(&spCamera);
        IF_FAILED_RETURN(hr);

        hr = CalibrateCamera(spCamera);
        IF_FAILED_RETURN(hr);

        spCalibrable->SetCalibrated();
    }

    SmartPtr<IRendererHandler> spRendererHandler(pRenderer);

    XMMATRIX viewMatrix, projectionMatrix;
    hr = spRendererHandler->GetCameraMatrices(&viewMatrix, &projectionMatrix);
    IF_FAILED_RETURN(hr);

    // Get object world transform
	XMMATRIX worldMatrix = m_spSceneObject->GetWorldTransform();

    UINT screenWidth = 0, screenHeight = 0;
    spRenderer->GetWindowWidthAndHeight(screenWidth, screenHeight);

    SmartPtr<ISceneObjectCamera> spCamera;
    hr = spRenderer->GetCamera(&spCamera);
    IF_FAILED_RETURN(hr);

    UINT level = 0;
    hr = DetermineLevelFromCamera(spCamera, (FLOAT)screenWidth, (FLOAT)screenHeight, level);
    IF_FAILED_RETURN(hr);

    // Get suitable shader
	SmartPtr<IShader> spShader;
    hr = pRenderer->GetShaderInterface(__uuidof(ITextureShader), &spShader);
    IF_FAILED_RETURN(hr);
		

    for (UINT i = 0; i < m_LevelTiles[level].Length(); ++i)
    {
        if (m_LevelTiles[level][i]->IsVisible(viewMatrix, projectionMatrix, worldMatrix, (FLOAT)screenWidth, (FLOAT)screenHeight))
        {
            SmartPtr<ID3D11ShaderResourceView> spTexture;
            hr = m_LevelTiles[level][i]->LockTexture(&spTexture);
            IF_FAILED_RETURN(hr);

            // Fill shader constants with transformation matrices
            hr = spShader.CastTo<ITextureShader>()->SetShaderParametersAndTexture(worldMatrix, viewMatrix, projectionMatrix, spTexture);
            IF_FAILED_RETURN(hr);

            spShader->SetVertextAndPixelShader();

            D3D_PRIMITIVE_TOPOLOGY primitiveTopology;
            UINT vertexCount = 0, indexCount = 0;
            SmartPtr<ID3D11Buffer> spVertexBuffer, spIndexBuffer;
            hr = m_LevelTiles[level][i]->GetBuffersAndPrimitiveTopology(&spVertexBuffer, vertexCount, &spIndexBuffer, indexCount, primitiveTopology); 
            IF_FAILED_RETURN(hr);      

            ID3D11Buffer* vertexBuffer = spVertexBuffer;
            pRenderer->DrawIndexedPrimitive(&vertexBuffer, sizeof(TexturedVertexType), spIndexBuffer, DXGI_FORMAT_R32_UINT, 
                                            indexCount, primitiveTopology);
        }
        else
        {
            m_LevelTiles[level][i]->ReleaseTexture();
        }

    }

    return hr;
}