diff options
Diffstat (limited to '3rdparty/bgfx/examples/common/camera.cpp')
-rw-r--r-- | 3rdparty/bgfx/examples/common/camera.cpp | 90 |
1 files changed, 47 insertions, 43 deletions
diff --git a/3rdparty/bgfx/examples/common/camera.cpp b/3rdparty/bgfx/examples/common/camera.cpp index 3e48e57d4f7..2578d327726 100644 --- a/3rdparty/bgfx/examples/common/camera.cpp +++ b/3rdparty/bgfx/examples/common/camera.cpp @@ -1,6 +1,6 @@ /* * Copyright 2013 Dario Manesku. All rights reserved. - * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause + * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE */ #include <bx/timer.h> @@ -79,13 +79,14 @@ struct Camera { int32_t m_mx; int32_t m_my; + int32_t m_mz; }; Camera() { reset(); entry::MouseState mouseState; - update(0.0f, mouseState); + update(0.0f, mouseState, true); cmdAdd("move", cmdMove); inputAddBindings("camBindings", s_camBindings); @@ -93,6 +94,7 @@ struct Camera ~Camera() { + cmdRemove("move"); inputRemoveBindings("camBindings"); } @@ -100,8 +102,10 @@ struct Camera { m_mouseNow.m_mx = 0; m_mouseNow.m_my = 0; + m_mouseNow.m_mz = 0; m_mouseLast.m_mx = 0; m_mouseLast.m_my = 0; + m_mouseLast.m_mz = 0; m_eye.x = 0.0f; m_eye.y = 0.0f; m_eye.z = -35.0f; @@ -126,8 +130,19 @@ struct Camera m_keys |= _down ? _key : 0; } - void update(float _deltaTime, const entry::MouseState& _mouseState) + void update(float _deltaTime, const entry::MouseState& _mouseState, bool _reset) { + if (_reset) + { + m_mouseLast.m_mx = _mouseState.m_mx; + m_mouseLast.m_my = _mouseState.m_my; + m_mouseLast.m_mz = _mouseState.m_mz; + m_mouseNow = m_mouseLast; + m_mouseDown = false; + + return; + } + if (!m_mouseDown) { m_mouseLast.m_mx = _mouseState.m_mx; @@ -142,10 +157,15 @@ struct Camera m_mouseNow.m_my = _mouseState.m_my; } + m_mouseLast.m_mz = m_mouseNow.m_mz; + m_mouseNow.m_mz = _mouseState.m_mz; + + const float deltaZ = float(m_mouseNow.m_mz - m_mouseLast.m_mz); + if (m_mouseDown) { - int32_t deltaX = m_mouseNow.m_mx - m_mouseLast.m_mx; - int32_t deltaY = m_mouseNow.m_my - m_mouseLast.m_my; + const int32_t deltaX = m_mouseNow.m_mx - m_mouseLast.m_mx; + const int32_t deltaY = m_mouseNow.m_my - m_mouseLast.m_my; m_horizontalAngle += m_mouseSpeed * float(deltaX); m_verticalAngle -= m_mouseSpeed * float(deltaY); @@ -174,63 +194,47 @@ struct Camera const bx::Vec3 right = { bx::sin(m_horizontalAngle - bx::kPiHalf), - 0, + 0.0f, bx::cos(m_horizontalAngle - bx::kPiHalf), }; const bx::Vec3 up = bx::cross(right, direction); + m_eye = bx::mad(direction, deltaZ * _deltaTime * m_moveSpeed, m_eye); + if (m_keys & CAMERA_KEY_FORWARD) { - const bx::Vec3 pos = m_eye; - const bx::Vec3 tmp = bx::mul(direction, _deltaTime * m_moveSpeed); - - m_eye = bx::add(pos, tmp); + m_eye = bx::mad(direction, _deltaTime * m_moveSpeed, m_eye); setKeyState(CAMERA_KEY_FORWARD, false); } if (m_keys & CAMERA_KEY_BACKWARD) { - const bx::Vec3 pos = m_eye; - const bx::Vec3 tmp = bx::mul(direction, _deltaTime * m_moveSpeed); - - m_eye = bx::sub(pos, tmp); + m_eye = bx::mad(direction, -_deltaTime * m_moveSpeed, m_eye); setKeyState(CAMERA_KEY_BACKWARD, false); } if (m_keys & CAMERA_KEY_LEFT) { - const bx::Vec3 pos = m_eye; - const bx::Vec3 tmp = bx::mul(right, _deltaTime * m_moveSpeed); - - m_eye = bx::add(pos, tmp); + m_eye = bx::mad(right, _deltaTime * m_moveSpeed, m_eye); setKeyState(CAMERA_KEY_LEFT, false); } if (m_keys & CAMERA_KEY_RIGHT) { - const bx::Vec3 pos = m_eye; - const bx::Vec3 tmp = bx::mul(right, _deltaTime * m_moveSpeed); - - m_eye = bx::sub(pos, tmp); + m_eye = bx::mad(right, -_deltaTime * m_moveSpeed, m_eye); setKeyState(CAMERA_KEY_RIGHT, false); } if (m_keys & CAMERA_KEY_UP) { - const bx::Vec3 pos = m_eye; - const bx::Vec3 tmp = bx::mul(up, _deltaTime * m_moveSpeed); - - m_eye = bx::add(pos, tmp); + m_eye = bx::mad(up, _deltaTime * m_moveSpeed, m_eye); setKeyState(CAMERA_KEY_UP, false); } if (m_keys & CAMERA_KEY_DOWN) { - const bx::Vec3 pos = m_eye; - const bx::Vec3 tmp = bx::mul(up, _deltaTime * m_moveSpeed); - - m_eye = bx::sub(pos, tmp); + m_eye = bx::mad(up, -_deltaTime * m_moveSpeed, m_eye); setKeyState(CAMERA_KEY_DOWN, false); } @@ -240,12 +244,12 @@ struct Camera void getViewMtx(float* _viewMtx) { - bx::mtxLookAt(_viewMtx, bx::load(&m_eye.x), bx::load(&m_at.x), bx::load(&m_up.x) ); + bx::mtxLookAt(_viewMtx, bx::load<bx::Vec3>(&m_eye.x), bx::load<bx::Vec3>(&m_at.x), bx::load<bx::Vec3>(&m_up.x) ); } - void setPosition(const float* _pos) + void setPosition(const bx::Vec3& _pos) { - bx::memCopy(&m_eye.x, _pos, sizeof(float)*3); + m_eye = _pos; } void setVerticalAngle(float _verticalAngle) @@ -261,9 +265,9 @@ struct Camera MouseCoords m_mouseNow; MouseCoords m_mouseLast; - bx::Vec3 m_eye; - bx::Vec3 m_at; - bx::Vec3 m_up; + bx::Vec3 m_eye = bx::init::Zero; + bx::Vec3 m_at = bx::init::Zero; + bx::Vec3 m_up = bx::init::Zero; float m_horizontalAngle; float m_verticalAngle; @@ -288,7 +292,7 @@ void cameraDestroy() s_camera = NULL; } -void cameraSetPosition(const float* _pos) +void cameraSetPosition(const bx::Vec3& _pos) { s_camera->setPosition(_pos); } @@ -313,17 +317,17 @@ void cameraGetViewMtx(float* _viewMtx) s_camera->getViewMtx(_viewMtx); } -void cameraGetPosition(float* _pos) +bx::Vec3 cameraGetPosition() { - bx::memCopy(_pos, &s_camera->m_eye.x, 3*sizeof(float) ); + return s_camera->m_eye; } -void cameraGetAt(float* _at) +bx::Vec3 cameraGetAt() { - bx::memCopy(_at, &s_camera->m_at.x, 3*sizeof(float) ); + return s_camera->m_at; } -void cameraUpdate(float _deltaTime, const entry::MouseState& _mouseState) +void cameraUpdate(float _deltaTime, const entry::MouseState& _mouseState, bool _reset) { - s_camera->update(_deltaTime, _mouseState); + s_camera->update(_deltaTime, _mouseState, _reset); } |