Original Post
Greetings,
My whole day ran off while I tried to implement a quaternion based camera system with the following requirements: With LeftmouseDown allow the camera to rotate around it's focus on a sphere trackball. I'm okay with the mathematics part, I have problems with the implementation. Currently I've achieved the following:
I have the necessary handling functions:
[source lang="cpp"]XMFLOAT2 Previous_MousePosition;
void OnMouseDown(WPARAM KeyState, int x, int y)
{
if (KeyState & VM_LBUTTON)
Previous_MousePosition = XMFLOAT2(x, y);
}
void OnMouseMove(WPARAM KeyState, int x, int y)
{
if(KeyState & VM_LBUTTON)
{
XMFLOAT2 NewPosition(x, y);
XMFLOAT2 SphereLocation2D(x / (m_Width >> 1), y / (m_Height >> 1));
SphereLocation2D.x -= 1;
SphereLocation2D.y = 1 - SphereLocation2D.y;
double z = 1 - x * x - y * y;
XMFLOAT3 SphereLocation3D(SphereLocation2D.x, SphereLocation2D.y, z > 0 ? z : 0);
// no idea where to go from here...
}
}[/source]
And I have my camera currently defined like:
[source lang="java"]class Camera
{
XMFLOAT3 m_Position;
// If I'm not mistaken no need for rotation since my camera will always look at the same point.
XMFLOAT4X4 m_ViewMatrix;
XMFLOAT4X4 m_ProjectionMatrix;
public:
Camera(void) : m_Position(XMVectorZero())
{
m_ViewMatrix = XMMatrixIdentity();
m_ProjectionMatrix = XMMatrixIdentity();
}
~Camera(void) { }
void Update(XMFLOAT3 NewPosition, float Dt = 1.0f)
{
// Calc Axis between m_Position & NewPosition
// FLOAT Angle(m_Position, NewPosition);
// No idea how to continue from here
// m_Position = NewPosition;
}
void BuildViewMatrix(void)
{
XMVECTOR Eye = XMVectorSet(m_Position.x, m_Position.y, m_Position.z, 0.0f);
XMVECTOR At = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
XMVECTOR Up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
XMMATRIX ViewMatrix = XMMatrixLookAtLH(Eye, At, Up);
XMStoreFloat4x4(&m_ViewMatrix, ViewMatrix);
}
}
[/source]
So I really think I already messed up everything in here.
My whole day ran off while I tried to implement a quaternion based camera system with the following requirements: With LeftmouseDown allow the camera to rotate around it's focus on a sphere trackball. I'm okay with the mathematics part, I have problems with the implementation. Currently I've achieved the following:
I have the necessary handling functions:
[source lang="cpp"]XMFLOAT2 Previous_MousePosition;
void OnMouseDown(WPARAM KeyState, int x, int y)
{
if (KeyState & VM_LBUTTON)
Previous_MousePosition = XMFLOAT2(x, y);
}
void OnMouseMove(WPARAM KeyState, int x, int y)
{
if(KeyState & VM_LBUTTON)
{
XMFLOAT2 NewPosition(x, y);
XMFLOAT2 SphereLocation2D(x / (m_Width >> 1), y / (m_Height >> 1));
SphereLocation2D.x -= 1;
SphereLocation2D.y = 1 - SphereLocation2D.y;
double z = 1 - x * x - y * y;
XMFLOAT3 SphereLocation3D(SphereLocation2D.x, SphereLocation2D.y, z > 0 ? z : 0);
// no idea where to go from here...
}
}[/source]
And I have my camera currently defined like:
[source lang="java"]class Camera
{
XMFLOAT3 m_Position;
// If I'm not mistaken no need for rotation since my camera will always look at the same point.
XMFLOAT4X4 m_ViewMatrix;
XMFLOAT4X4 m_ProjectionMatrix;
public:
Camera(void) : m_Position(XMVectorZero())
{
m_ViewMatrix = XMMatrixIdentity();
m_ProjectionMatrix = XMMatrixIdentity();
}
~Camera(void) { }
void Update(XMFLOAT3 NewPosition, float Dt = 1.0f)
{
// Calc Axis between m_Position & NewPosition
// FLOAT Angle(m_Position, NewPosition);
// No idea how to continue from here
// m_Position = NewPosition;
}
void BuildViewMatrix(void)
{
XMVECTOR Eye = XMVectorSet(m_Position.x, m_Position.y, m_Position.z, 0.0f);
XMVECTOR At = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
XMVECTOR Up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
XMMATRIX ViewMatrix = XMMatrixLookAtLH(Eye, At, Up);
XMStoreFloat4x4(&m_ViewMatrix, ViewMatrix);
}
}
[/source]
So I really think I already messed up everything in here.