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;
// Update camera.
cameraUpdate(deltaTime, m_mouseState);
cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );
// Set view and projection matrix for view 0.
{

View File

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

View File

@ -416,14 +416,14 @@ public:
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
{
imguiBeginFrame(m_mouseState.m_mx
, m_mouseState.m_my
, (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
| (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
| (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
, m_mouseState.m_mz
, uint16_t(m_width)
, uint16_t(m_height)
);
, m_mouseState.m_my
, (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
| (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
| (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
, m_mouseState.m_mz
, uint16_t(m_width)
, uint16_t(m_height)
);
showExampleDialog(this);
@ -437,17 +437,14 @@ public:
float time = (float)( (now-m_timeOffset)/freq);
ImGui::SetNextWindowPos(
ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
, ImGuiCond_FirstUseEver
);
);
ImGui::SetNextWindowSize(
ImVec2(m_width / 5.0f, m_height / 3.0f)
ImVec2(m_width / 5.0f, m_height / 3.0f)
, ImGuiCond_FirstUseEver
);
ImGui::Begin("Settings"
, NULL
, 0
);
);
ImGui::Begin("Settings", NULL, 0);
ImGui::SliderInt("Num lights", &m_numLights, 1, 2048);
ImGui::Checkbox("Show G-Buffer.", &m_showGBuffer);
@ -573,7 +570,7 @@ public:
}
// Update camera.
cameraUpdate(deltaTime, m_mouseState);
cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );
float view[16];
cameraGetViewMtx(view);

View File

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

View File

@ -203,8 +203,9 @@ public:
const float deltaTime = float(frameTime/freq);
// Update camera.
cameraUpdate(deltaTime, m_state.m_mouse, ImGui::MouseOverArea() );
float view[16];
cameraUpdate(deltaTime, m_state.m_mouse);
cameraGetViewMtx(view);
// 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();
imguiEndFrame();
// Update camera.
cameraUpdate(deltaTime, m_mouseState, ImGui::MouseOverArea() );
if (!ImGui::MouseOverArea() )
{
// Update camera.
cameraUpdate(deltaTime, m_mouseState);
if (!!m_mouseState.m_buttons[entry::MouseButton::Left])
{
mousePickTerrain();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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);
@ -100,8 +101,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 +129,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 +156,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 int32_t 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 +193,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);
}
@ -323,7 +326,7 @@ bx::Vec3 cameraGetAt()
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();
///
void cameraUpdate(float _deltaTime, const entry::MouseState& _mouseState);
void cameraUpdate(float _deltaTime, const entry::MouseState& _mouseState, bool _reset = false);
#endif // CAMERA_H_HEADER_GUARD