HRESULT CameraMoveTool::GetViewWidthAndHeight(__in IViewController *pView, __out UINT &width, __out UINT &height) { HRESULT hr; SmartPtr<IViewControllerScene> spViewSceneController; hr = pView->QueryInterface(&spViewSceneController); if (SUCCEEDED(hr)) { SmartPtr<IRenderer> spRenderer; hr = spViewSceneController->GetRenderer(&spRenderer); if (SUCCEEDED(hr)) { spRenderer->GetWindowWidthAndHeight(width, height); } } return hr; }
HRESULT RenderableText::Draw(UINT passIndex, __in IRendererHandler *pRenderer) { UNREFERENCED_PARAMETER(passIndex); XMMATRIX viewMatrix, projectionMatrix; HRESULT hr = pRenderer->GetCameraMatrices(&viewMatrix, &projectionMatrix); IF_FAILED_RETURN(hr); // Get suitable renderer SmartPtr<IRenderer> spRenderer; hr = pRenderer->QueryInterface(&spRenderer); IF_FAILED_RETURN(hr); UINT screenWidth = 0, screenHeight = 0; spRenderer->GetWindowWidthAndHeight(screenWidth, screenHeight); // Transform text position to screen coordinates XMFLOAT3 screenCoordinatesPosition; XMMATRIX worldMatrix = m_spSceneObjectText.CastTo<ISceneObject>()->GetWorldTransform(); XMVECTOR textPosition = XMLoadFloat3(&m_spSceneObjectText->GetPosition()); XMVECTOR transformedTextPosition = XMVector3Project(textPosition, 0, 0, (FLOAT)screenWidth, (FLOAT)screenHeight, 0.0f, 1.0f, projectionMatrix, viewMatrix, worldMatrix); XMStoreFloat3(&screenCoordinatesPosition, transformedTextPosition); // Draw only if within view port if (screenCoordinatesPosition.x < 0.0f || screenCoordinatesPosition.x > screenWidth || screenCoordinatesPosition.y < 0.0f || screenCoordinatesPosition.y > screenHeight || screenCoordinatesPosition.z > 1.0f || screenCoordinatesPosition.z < 0.0f) { return hr; } // Update text vertices according to new screen coordinates hr = UpdateDynamicVertexBuffer(screenCoordinatesPosition, screenWidth, screenHeight); IF_FAILED_RETURN(hr); // Return if no text graphics created if (m_IndexCount == 0 || m_VertexCount == 0) return hr; // Get font shader wrapper SmartPtr<IShader> spShaderWrapper; hr = pRenderer->GetShaderInterface(__uuidof(IFontShader), &spShaderWrapper); IF_FAILED_RETURN(hr); // Get 2D projection matrices projectionMatrix = XMMatrixOrthographicLH((FLOAT)screenWidth, (FLOAT)screenHeight, 1.0f, 1000.0f); viewMatrix = XMMatrixLookAtLH(XMLoadFloat3(&XMFLOAT3(0.0f, 0.0f, -1.0f)), XMLoadFloat3(&XMFLOAT3(0.0f, 0.0f, 0.0f)), XMLoadFloat3(&XMFLOAT3(0.0f, 1.0f, 0.0f))); // Fill shader constants with transformation matrices hr = spShaderWrapper.CastTo<IFontShader>()->SetShaderParametersAndTexture(worldMatrix, viewMatrix, projectionMatrix, m_spFontSheet); IF_FAILED_RETURN(hr); spShaderWrapper->SetVertextAndPixelShader(); pRenderer->TurnOnAlphaBlending(); pRenderer->DrawIndexedPrimitive(&m_spVertexBuffer.p, sizeof(TexturedVertexType), m_spIndexBuffer, DXGI_FORMAT_R32_UINT, m_IndexCount, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); pRenderer->TurnOffAlphaBlending(); return hr; }
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; }