Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed non-intuitive camera orientation #849

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions Samples/360VideoPlayback/cpp/PlaybackPage.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ quaternion CreateFromHeadingPitchRoll(double flHeading, double flPitch, double f

PlaybackPage::PlaybackPage()
{
m_centerX = 0;
m_centerY = 0;
m_pitch = 0;
m_heading = 0;
m_rotationdelta = 1.8f;
Expand All @@ -72,8 +70,6 @@ PlaybackPage::PlaybackPage()

void PlaybackPage::OnLayoutUpdated(Object^ sender, Object^ args)
{
m_centerX = this->VideoElement->ActualWidth / 2;
m_centerY = this->VideoElement->ActualHeight / 2;
if (this->m_mtcGrid == nullptr)
{
FrameworkElement^ transportControlsTemplateRoot = (FrameworkElement^)(VisualTreeHelper::GetChild(this->VideoElement->TransportControls, 0));
Expand Down Expand Up @@ -105,14 +101,15 @@ void PlaybackPage::OnPointerPressed(Object^ sender, PointerRoutedEventArgs^ e)
if (e->OriginalSource != m_mtcGrid && IsMediaAlreadyOpened())
{
m_isPointerPress = true;
m_origin = e->GetCurrentPoint(VideoElement)->Position;
}
e->Handled = true;
}


void PlaybackPage::OnPointerReleased(Object^ sender, PointerRoutedEventArgs^ e)
{
ResetState();
m_isPointerPress = false;
e->Handled = true;
}

Expand All @@ -132,14 +129,23 @@ void PlaybackPage::OnPointerWheelChanged(Object^ sender, PointerRoutedEventArgs^
}


float PlaybackPage::PixelToAngle(float pixel) const
{
return float(pixel * videoProjection->HorizontalFieldOfViewInDegrees / VideoElement->ActualWidth);
}

void PlaybackPage::OnPointerMoved(Object^ sender, PointerRoutedEventArgs^ e)
{
if (e->OriginalSource != m_mtcGrid && m_isPointerPress)
{
double ChangeX = e->GetCurrentPoint(this->VideoElement)->Position.X - m_centerX;
double ChangeY = m_centerY - (e->GetCurrentPoint(VideoElement)->Position.Y);
this->videoProjection->ViewOrientation = CreateFromHeadingPitchRoll(ChangeX, ChangeY, 0);
}
auto current = e->GetCurrentPoint(this->VideoElement)->Position;

m_heading += PixelToAngle(m_origin.X - current.X);
m_pitch += PixelToAngle(current.Y - m_origin.Y);

this->videoProjection->ViewOrientation = CreateFromHeadingPitchRoll(m_heading, m_pitch, 0);
m_origin = current;
}
e->Handled = true;
}

Expand Down
5 changes: 3 additions & 2 deletions Samples/360VideoPlayback/cpp/PlaybackPage.xaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ namespace _360VideoPlayback
private:

Windows::Media::Playback::MediaPlayer^ m_player;
// positions hold the center of the window
double m_centerX = 0, m_centerY = 0;
// positions hold the origin of the movement
Windows::Foundation::Point m_origin;
// MTC grid to ignore mouse event on the MTC Panel
Windows::UI::Xaml::Controls::Grid^ m_mtcGrid;
double m_pitch;
Expand All @@ -45,6 +45,7 @@ namespace _360VideoPlayback
void ResetState();
void StartInputLoop();

float PixelToAngle(float pixel) const;
void OnPointerPressed(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e);
Windows::Foundation::EventRegistrationToken OnPointerPressedToken;
Windows::Foundation::EventRegistrationToken OnMTCPointerPressedToken;
Expand Down
20 changes: 12 additions & 8 deletions Samples/360VideoPlayback/cs/PlaybackPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace _360VideoPlayback
public sealed partial class PlaybackPage : Page
{
private MediaPlayer mediaPlayer;
// positions hold the center of the window
private double centerX = 0, centerY = 0;
// positions hold the start of the movement
private Point origin;
// MTC grid to ignore mouse event on the MTC Panel
Grid mtcGrid = null;
private double pitch = 0, heading = 0;
Expand All @@ -48,8 +48,6 @@ public PlaybackPage()

private void PlaybackPage_LayoutUpdated(object sender, object e)
{
centerX = VideoElement.ActualWidth / 2;
centerY = VideoElement.ActualHeight / 2;
if (mtcGrid == null)
{
FrameworkElement transportControlsTemplateRoot = (FrameworkElement)VisualTreeHelper.GetChild(VideoElement.TransportControls, 0);
Expand Down Expand Up @@ -150,20 +148,25 @@ private void OnKeyDown(CoreWindow sender, KeyEventArgs args)
}
}

private double PixelToAngle(double pixel) => pixel * videoProjection.HorizontalFieldOfViewInDegrees / VideoElement.ActualWidth;

private void OnPointerMoved(object sender, PointerRoutedEventArgs e)
{
if (e.OriginalSource != mtcGrid && isPointerPress)
{
double ChangeX = e.GetCurrentPoint(VideoElement).Position.X - centerX;
double ChangeY = centerY - e.GetCurrentPoint(VideoElement).Position.Y;
videoProjection.ViewOrientation = Helpers.CreateFromHeadingPitchRoll(ChangeX, ChangeY, 0);
var current = e.GetCurrentPoint(VideoElement).Position;

heading += PixelToAngle(origin.X - current.X);
pitch += PixelToAngle(current.Y - origin.Y);
videoProjection.ViewOrientation = Helpers.CreateFromHeadingPitchRoll(heading, pitch, 0);
origin = current;
}
e.Handled = true;
}

private void OnPointerReleased(object sender, PointerRoutedEventArgs e)
{
ResetState();
isPointerPress = false;
e.Handled = true;
}

Expand All @@ -172,6 +175,7 @@ private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
if (e.OriginalSource != mtcGrid && IsMediaAlreadyOpened())
{
isPointerPress = true;
origin = e.GetCurrentPoint(VideoElement).Position;
}
e.Handled = true;
}
Expand Down