Don't update camera when mouse is over imgui.

This commit is contained in:
Бранимир Караџић 2020-12-26 22:46:26 -08:00
parent 534733fe3c
commit c95eca2a4b
16 changed files with 67 additions and 72 deletions

View File

@ -2027,7 +2027,7 @@ public:
s_uniforms.m_time = time; s_uniforms.m_time = time;
// Update camera. // Update camera.
cameraUpdate(deltaTime, m_mouseState); cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );
// Set view and projection matrix for view 0. // Set view and projection matrix for view 0.
{ {

View File

@ -2047,7 +2047,7 @@ public:
const float deltaTime = float(frameTime/freq); const float deltaTime = float(frameTime/freq);
// Update camera. // Update camera.
cameraUpdate(deltaTime, m_mouseState); cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );
// Update view mtx. // Update view mtx.
cameraGetViewMtx(m_viewState.m_view); cameraGetViewMtx(m_viewState.m_view);

View File

@ -444,10 +444,7 @@ public:
ImVec2(m_width / 5.0f, m_height / 3.0f) ImVec2(m_width / 5.0f, m_height / 3.0f)
, ImGuiCond_FirstUseEver , ImGuiCond_FirstUseEver
); );
ImGui::Begin("Settings" ImGui::Begin("Settings", NULL, 0);
, NULL
, 0
);
ImGui::SliderInt("Num lights", &m_numLights, 1, 2048); ImGui::SliderInt("Num lights", &m_numLights, 1, 2048);
ImGui::Checkbox("Show G-Buffer.", &m_showGBuffer); ImGui::Checkbox("Show G-Buffer.", &m_showGBuffer);
@ -573,7 +570,7 @@ public:
} }
// Update camera. // Update camera.
cameraUpdate(deltaTime, m_mouseState); cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );
float view[16]; float view[16];
cameraGetViewMtx(view); cameraGetViewMtx(view);

View File

@ -368,7 +368,7 @@ public:
bx::swap(m_prevPositionBuffer0, m_prevPositionBuffer1); bx::swap(m_prevPositionBuffer0, m_prevPositionBuffer1);
// Update camera. // Update camera.
cameraUpdate(deltaTime, m_mouseState); cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );
float view[16]; float view[16];
cameraGetViewMtx(view); cameraGetViewMtx(view);

View File

@ -203,8 +203,9 @@ public:
const float deltaTime = float(frameTime/freq); const float deltaTime = float(frameTime/freq);
// Update camera. // Update camera.
cameraUpdate(deltaTime, m_state.m_mouse, ImGui::MouseOverArea() );
float view[16]; float view[16];
cameraUpdate(deltaTime, m_state.m_mouse);
cameraGetViewMtx(view); cameraGetViewMtx(view);
// Set view and projection matrix for view 0. // Set view and projection matrix for view 0.

View File

@ -432,11 +432,11 @@ ExampleTerrain(const char* _name, const char* _description, const char* _url)
ImGui::End(); ImGui::End();
imguiEndFrame(); imguiEndFrame();
// Update camera.
cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );
if (!ImGui::MouseOverArea() ) if (!ImGui::MouseOverArea() )
{ {
// Update camera.
cameraUpdate(deltaTime, m_mouseState);
if (!!m_mouseState.m_buttons[entry::MouseButton::Left]) if (!!m_mouseState.m_buttons[entry::MouseButton::Left])
{ {
mousePickTerrain(); mousePickTerrain();

View File

@ -904,7 +904,7 @@ public:
const float deltaTime = float(frameTime/freq); const float deltaTime = float(frameTime/freq);
// Update camera. // Update camera.
cameraUpdate(deltaTime, m_mouseState); cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );
float view[16]; float view[16];
cameraGetViewMtx(view); cameraGetViewMtx(view);

View File

@ -451,7 +451,7 @@ public:
const float deltaTime = float(frameTime/freq); const float deltaTime = float(frameTime/freq);
// Update camera // Update camera
cameraUpdate(deltaTime*0.15f, m_mouseState); cameraUpdate(deltaTime*0.15f, m_mouseState, ImGui::MouseOverArea() );
// Set up matrices for gbuffer // Set up matrices for gbuffer
float view[16]; float view[16];

View File

@ -333,7 +333,7 @@ public:
const double freq = double(bx::getHPFrequency() ); const double freq = double(bx::getHPFrequency() );
const float deltaTime = float(frameTime/freq); const float deltaTime = float(frameTime/freq);
cameraUpdate(deltaTime, m_mouseState); cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );
float view[16]; float view[16];
cameraGetViewMtx(view); cameraGetViewMtx(view);

View File

@ -565,11 +565,8 @@ namespace
imguiEndFrame(); imguiEndFrame();
if (!ImGui::MouseOverArea())
{
// Update camera. // Update camera.
cameraUpdate(deltaTime, m_mouseState); cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );
}
// Set view 0 default viewport. // Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height)); bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height));

View File

@ -435,7 +435,7 @@ public:
ImGui::End(); ImGui::End();
// Update camera. // Update camera.
cameraUpdate(deltaTime, m_mouseState); cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );
float view[16]; float view[16];
cameraGetViewMtx(view); cameraGetViewMtx(view);

View File

@ -471,7 +471,7 @@ namespace
} }
// Update camera // Update camera
cameraUpdate(deltaTime*0.15f, m_mouseState); cameraUpdate(deltaTime*0.15f, m_mouseState, ImGui::MouseOverArea() );
// Set up matrices for gbuffer // Set up matrices for gbuffer
cameraGetViewMtx(m_view); cameraGetViewMtx(m_view);

View File

@ -261,7 +261,7 @@ public:
ImGui::End(); ImGui::End();
// Update camera. // Update camera.
cameraUpdate(deltaTime, m_mouseState); cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );
float view[16]; float view[16];
cameraGetViewMtx(view); cameraGetViewMtx(view);

View File

@ -511,11 +511,8 @@ namespace
ImGui::End(); ImGui::End();
if (!ImGui::MouseOverArea() )
{
// Update camera. // Update camera.
cameraUpdate(deltaTime*0.01f, m_mouseState); cameraUpdate(deltaTime*0.01f, m_mouseState, ImGui::MouseOverArea() );
}
bgfx::touch(0); bgfx::touch(0);
bgfx::touch(1); bgfx::touch(1);

View File

@ -79,13 +79,14 @@ struct Camera
{ {
int32_t m_mx; int32_t m_mx;
int32_t m_my; int32_t m_my;
int32_t m_mz;
}; };
Camera() Camera()
{ {
reset(); reset();
entry::MouseState mouseState; entry::MouseState mouseState;
update(0.0f, mouseState); update(0.0f, mouseState, true);
cmdAdd("move", cmdMove); cmdAdd("move", cmdMove);
inputAddBindings("camBindings", s_camBindings); inputAddBindings("camBindings", s_camBindings);
@ -100,8 +101,10 @@ struct Camera
{ {
m_mouseNow.m_mx = 0; m_mouseNow.m_mx = 0;
m_mouseNow.m_my = 0; m_mouseNow.m_my = 0;
m_mouseNow.m_mz = 0;
m_mouseLast.m_mx = 0; m_mouseLast.m_mx = 0;
m_mouseLast.m_my = 0; m_mouseLast.m_my = 0;
m_mouseLast.m_mz = 0;
m_eye.x = 0.0f; m_eye.x = 0.0f;
m_eye.y = 0.0f; m_eye.y = 0.0f;
m_eye.z = -35.0f; m_eye.z = -35.0f;
@ -126,8 +129,19 @@ struct Camera
m_keys |= _down ? _key : 0; 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) if (!m_mouseDown)
{ {
m_mouseLast.m_mx = _mouseState.m_mx; m_mouseLast.m_mx = _mouseState.m_mx;
@ -142,10 +156,15 @@ struct Camera
m_mouseNow.m_my = _mouseState.m_my; m_mouseNow.m_my = _mouseState.m_my;
} }
m_mouseLast.m_mz = m_mouseNow.m_mz;
m_mouseNow.m_mz = _mouseState.m_mz;
const int32_t deltaZ = float(m_mouseNow.m_mz - m_mouseLast.m_mz);
if (m_mouseDown) if (m_mouseDown)
{ {
int32_t deltaX = m_mouseNow.m_mx - m_mouseLast.m_mx; const 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 deltaY = m_mouseNow.m_my - m_mouseLast.m_my;
m_horizontalAngle += m_mouseSpeed * float(deltaX); m_horizontalAngle += m_mouseSpeed * float(deltaX);
m_verticalAngle -= m_mouseSpeed * float(deltaY); m_verticalAngle -= m_mouseSpeed * float(deltaY);
@ -174,63 +193,47 @@ struct Camera
const bx::Vec3 right = const bx::Vec3 right =
{ {
bx::sin(m_horizontalAngle - bx::kPiHalf), bx::sin(m_horizontalAngle - bx::kPiHalf),
0, 0.0f,
bx::cos(m_horizontalAngle - bx::kPiHalf), bx::cos(m_horizontalAngle - bx::kPiHalf),
}; };
const bx::Vec3 up = bx::cross(right, direction); 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) if (m_keys & CAMERA_KEY_FORWARD)
{ {
const bx::Vec3 pos = m_eye; m_eye = bx::mad(direction, _deltaTime * m_moveSpeed, m_eye);
const bx::Vec3 tmp = bx::mul(direction, _deltaTime * m_moveSpeed);
m_eye = bx::add(pos, tmp);
setKeyState(CAMERA_KEY_FORWARD, false); setKeyState(CAMERA_KEY_FORWARD, false);
} }
if (m_keys & CAMERA_KEY_BACKWARD) if (m_keys & CAMERA_KEY_BACKWARD)
{ {
const bx::Vec3 pos = m_eye; m_eye = bx::mad(direction, -_deltaTime * m_moveSpeed, m_eye);
const bx::Vec3 tmp = bx::mul(direction, _deltaTime * m_moveSpeed);
m_eye = bx::sub(pos, tmp);
setKeyState(CAMERA_KEY_BACKWARD, false); setKeyState(CAMERA_KEY_BACKWARD, false);
} }
if (m_keys & CAMERA_KEY_LEFT) if (m_keys & CAMERA_KEY_LEFT)
{ {
const bx::Vec3 pos = m_eye; m_eye = bx::mad(right, _deltaTime * m_moveSpeed, m_eye);
const bx::Vec3 tmp = bx::mul(right, _deltaTime * m_moveSpeed);
m_eye = bx::add(pos, tmp);
setKeyState(CAMERA_KEY_LEFT, false); setKeyState(CAMERA_KEY_LEFT, false);
} }
if (m_keys & CAMERA_KEY_RIGHT) if (m_keys & CAMERA_KEY_RIGHT)
{ {
const bx::Vec3 pos = m_eye; m_eye = bx::mad(right, -_deltaTime * m_moveSpeed, m_eye);
const bx::Vec3 tmp = bx::mul(right, _deltaTime * m_moveSpeed);
m_eye = bx::sub(pos, tmp);
setKeyState(CAMERA_KEY_RIGHT, false); setKeyState(CAMERA_KEY_RIGHT, false);
} }
if (m_keys & CAMERA_KEY_UP) if (m_keys & CAMERA_KEY_UP)
{ {
const bx::Vec3 pos = m_eye; m_eye = bx::mad(up, _deltaTime * m_moveSpeed, m_eye);
const bx::Vec3 tmp = bx::mul(up, _deltaTime * m_moveSpeed);
m_eye = bx::add(pos, tmp);
setKeyState(CAMERA_KEY_UP, false); setKeyState(CAMERA_KEY_UP, false);
} }
if (m_keys & CAMERA_KEY_DOWN) if (m_keys & CAMERA_KEY_DOWN)
{ {
const bx::Vec3 pos = m_eye; m_eye = bx::mad(up, -_deltaTime * m_moveSpeed, m_eye);
const bx::Vec3 tmp = bx::mul(up, _deltaTime * m_moveSpeed);
m_eye = bx::sub(pos, tmp);
setKeyState(CAMERA_KEY_DOWN, false); setKeyState(CAMERA_KEY_DOWN, false);
} }
@ -323,7 +326,7 @@ bx::Vec3 cameraGetAt()
return s_camera->m_at; 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);
} }

View File

@ -43,6 +43,6 @@ bx::Vec3 cameraGetPosition();
bx::Vec3 cameraGetAt(); bx::Vec3 cameraGetAt();
/// ///
void cameraUpdate(float _deltaTime, const entry::MouseState& _mouseState); void cameraUpdate(float _deltaTime, const entry::MouseState& _mouseState, bool _reset = false);
#endif // CAMERA_H_HEADER_GUARD #endif // CAMERA_H_HEADER_GUARD