Path: blob/master/samples/wp8/OcvRotatingCube/PhoneXamlDirect3DApp1/PhoneXamlDirect3DApp1Comp/Direct3DInterop.cpp
16354 views
#include "pch.h"1#include "Direct3DInterop.h"2#include "Direct3DContentProvider.h"3#include <windows.storage.streams.h>4#include <wrl.h>5#include <robuffer.h>6#include <opencv2\imgproc\types_c.h>78using namespace Windows::Storage::Streams;9using namespace Microsoft::WRL;10using namespace Windows::Foundation;11using namespace Windows::UI::Core;12using namespace Microsoft::WRL;13using namespace Windows::Phone::Graphics::Interop;14using namespace Windows::Phone::Input::Interop;1516namespace PhoneXamlDirect3DApp1Comp17{18void Direct3DInterop::ApplyGrayFilter(const cv::Mat& image)19{20cv::Mat intermediateMat;21cv::cvtColor(image, intermediateMat, COLOR_RGBA2GRAY);22cv::cvtColor(intermediateMat, image, COLOR_GRAY2BGRA);23}2425void Direct3DInterop::ApplyCannyFilter(const cv::Mat& image)26{27cv::Mat intermediateMat;28cv::Canny(image, intermediateMat, 80, 90);29cv::cvtColor(intermediateMat, image, COLOR_GRAY2BGRA);30}3132void Direct3DInterop::ApplySepiaFilter(const cv::Mat& image)33{34const float SepiaKernelData[16] =35{36/* B */0.131f, 0.534f, 0.272f, 0.f,37/* G */0.168f, 0.686f, 0.349f, 0.f,38/* R */0.189f, 0.769f, 0.393f, 0.f,39/* A */0.000f, 0.000f, 0.000f, 1.f40};4142const cv::Mat SepiaKernel(4, 4, CV_32FC1, (void*)SepiaKernelData);43cv::transform(image, image, SepiaKernel);44}4546Direct3DInterop::Direct3DInterop() :47m_timer(ref new BasicTimer())48{49}5051IDrawingSurfaceContentProvider^ Direct3DInterop::CreateContentProvider()52{53ComPtr<Direct3DContentProvider> provider = Make<Direct3DContentProvider>(this);54return reinterpret_cast<IDrawingSurfaceContentProvider^>(provider.Detach());55}5657// IDrawingSurfaceManipulationHandler58void Direct3DInterop::SetManipulationHost(DrawingSurfaceManipulationHost^ manipulationHost)59{60manipulationHost->PointerPressed +=61ref new TypedEventHandler<DrawingSurfaceManipulationHost^, PointerEventArgs^>(this, &Direct3DInterop::OnPointerPressed);6263manipulationHost->PointerMoved +=64ref new TypedEventHandler<DrawingSurfaceManipulationHost^, PointerEventArgs^>(this, &Direct3DInterop::OnPointerMoved);6566manipulationHost->PointerReleased +=67ref new TypedEventHandler<DrawingSurfaceManipulationHost^, PointerEventArgs^>(this, &Direct3DInterop::OnPointerReleased);68}6970void Direct3DInterop::RenderResolution::set(Windows::Foundation::Size renderResolution)71{72if (renderResolution.Width != m_renderResolution.Width ||73renderResolution.Height != m_renderResolution.Height)74{75m_renderResolution = renderResolution;7677if (m_renderer)78{79m_renderer->UpdateForRenderResolutionChange(m_renderResolution.Width, m_renderResolution.Height);80RecreateSynchronizedTexture();81}82}83}8485// Event Handlers8687void Direct3DInterop::OnPointerPressed(DrawingSurfaceManipulationHost^ sender, PointerEventArgs^ args)88{89// Insert your code here.90}9192void Direct3DInterop::OnPointerMoved(DrawingSurfaceManipulationHost^ sender, PointerEventArgs^ args)93{94// Insert your code here.95}9697void Direct3DInterop::OnPointerReleased(DrawingSurfaceManipulationHost^ sender, PointerEventArgs^ args)98{99// Insert your code here.100}101102// Interface With Direct3DContentProvider103HRESULT Direct3DInterop::Connect(_In_ IDrawingSurfaceRuntimeHostNative* host)104{105m_renderer = ref new CubeRenderer();106m_renderer->Initialize();107m_renderer->UpdateForWindowSizeChange(WindowBounds.Width, WindowBounds.Height);108m_renderer->UpdateForRenderResolutionChange(m_renderResolution.Width, m_renderResolution.Height);109110// Restart timer after renderer has finished initializing.111m_timer->Reset();112113return S_OK;114}115116void Direct3DInterop::Disconnect()117{118m_renderer = nullptr;119}120121HRESULT Direct3DInterop::PrepareResources(_In_ const LARGE_INTEGER* presentTargetTime, _Out_ BOOL* contentDirty)122{123*contentDirty = true;124125return S_OK;126}127128HRESULT Direct3DInterop::GetTexture(_In_ const DrawingSurfaceSizeF* size, _Out_ IDrawingSurfaceSynchronizedTextureNative** synchronizedTexture, _Out_ DrawingSurfaceRectF* textureSubRectangle)129{130m_timer->Update();131m_renderer->Update(m_timer->Total, m_timer->Delta);132m_renderer->Render();133134RequestAdditionalFrame();135136return S_OK;137}138139ID3D11Texture2D* Direct3DInterop::GetTexture()140{141return m_renderer->GetTexture();142}143144void Direct3DInterop::CreateTexture(const Platform::Array<int>^ buffer,int width,int height, OCVFilterType filter)145{146if (m_renderer)147{148cv::Mat Lena = cv::Mat(height, width, CV_8UC4);149memcpy(Lena.data, buffer->Data, 4 * height*width);150151switch (filter)152{153case OCVFilterType::ePreview:154break;155156case OCVFilterType::eGray:157ApplyGrayFilter(Lena);158break;159160case OCVFilterType::eCanny:161ApplyCannyFilter(Lena);162break;163164case OCVFilterType::eSepia:165ApplySepiaFilter(Lena);166break;167}168169m_renderer->CreateTextureFromByte(Lena.data, width, height);170}171}172173byte* GetPointerToPixelData( Windows::Storage::Streams::IBuffer ^ pixelBuffer)174{175// Query the IBufferByteAccess interface.176ComPtr<IBufferByteAccess> bufferByteAccess;177reinterpret_cast<IInspectable*>( pixelBuffer)->QueryInterface(IID_PPV_ARGS(&bufferByteAccess));178179// Retrieve the buffer data.180byte* pixels = nullptr;181bufferByteAccess->Buffer(&pixels);182return pixels;183}184185}186187188