From 31efb2991ff165b6724599f7fb16d3ab88b19f92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sat, 8 Nov 2014 20:57:47 -0800 Subject: [PATCH] Updated to OVR 0.4.3. Added support for event char to read keyboard text input. --- examples/10-font/font.cpp | 9 ++-- examples/common/entry/entry.cpp | 19 ++++++- examples/common/entry/entry_p.h | 17 +++++++ examples/common/entry/entry_windows.cpp | 20 ++++++++ examples/common/entry/input.cpp | 53 ++++++++++++++++++++ examples/common/entry/input.h | 9 ++++ examples/common/font/text_buffer_manager.cpp | 10 +++- examples/common/font/text_metrics.cpp | 4 +- examples/common/imgui/imgui.cpp | 4 +- include/bgfx.h | 2 +- src/ovr.cpp | 12 ++--- src/renderer_d3d11.cpp | 6 +-- src/renderer_gl.cpp | 6 +-- 13 files changed, 146 insertions(+), 25 deletions(-) diff --git a/examples/10-font/font.cpp b/examples/10-font/font.cpp index 2662d770d..56b79e69a 100644 --- a/examples/10-font/font.cpp +++ b/examples/10-font/font.cpp @@ -12,6 +12,7 @@ #include "font/font_manager.h" #include "font/text_buffer_manager.h" +#include "entry/input.h" #include #include @@ -192,8 +193,8 @@ int _main_(int /*_argc*/, char** /*_argv*/) textBufferManager->appendText(transientText, visitor10, L"text buffer\n"); textBufferManager->appendText(transientText, visitor10, fpsText); - float at[3] = { 0, 0, 0.0f }; - float eye[3] = {0, 0, -1.0f }; + float at[3] = { 0, 0, 0.0f }; + float eye[3] = { 0, 0, -1.0f }; float view[16]; bx::mtxLookAt(view, eye, at); @@ -211,8 +212,8 @@ int _main_(int /*_argc*/, char** /*_argv*/) time += 0.05f; const float dist = 10.0f; - const float offset0 = -proj[8] + (hmd->eye[0].adjust[0] / dist * proj[0]); - const float offset1 = -proj[8] + (hmd->eye[1].adjust[0] / dist * proj[0]); + const float offset0 = -proj[8] + (hmd->eye[0].viewOffset[0] / dist * proj[0]); + const float offset1 = -proj[8] + (hmd->eye[1].viewOffset[0] / dist * proj[0]); float ortho[2][16]; const float offsetx = width/2.0f; diff --git a/examples/common/entry/entry.cpp b/examples/common/entry/entry.cpp index 210588cc2..70a619f95 100644 --- a/examples/common/entry/entry.cpp +++ b/examples/common/entry/entry.cpp @@ -242,12 +242,12 @@ namespace entry if (_shiftModifier) { // Big letters. - if(ascii >= 'a' && ascii <= 'z') + if (ascii >= 'a' && ascii <= 'z') { ascii += 'A' - 'a'; } // Special cases. - else if('-' == ascii) + else if ('-' == ascii) { ascii = '_'; } @@ -322,6 +322,13 @@ namespace entry } break; + case Event::Char: + { + const CharEvent* chev = static_cast(ev); + inputChar(chev->m_len, chev->m_char); + } + break; + case Event::Size: { const SizeEvent* size = static_cast(ev); @@ -440,6 +447,14 @@ namespace entry } break; + case Event::Char: + { + const CharEvent* chev = static_cast(ev); + win.m_handle = chev->m_handle; + inputChar(chev->m_len, chev->m_char); + } + break; + case Event::Size: { const SizeEvent* size = static_cast(ev); diff --git a/examples/common/entry/entry_p.h b/examples/common/entry/entry_p.h index bf67a30db..62cb3775b 100644 --- a/examples/common/entry/entry_p.h +++ b/examples/common/entry/entry_p.h @@ -45,6 +45,7 @@ namespace entry { Exit, Key, + Char, Mouse, Size, Window, @@ -75,6 +76,14 @@ namespace entry bool m_down; }; + struct CharEvent : public Event + { + ENTRY_IMPLEMENT_EVENT(CharEvent, Event::Char); + + uint8_t m_len; + uint8_t m_char[4]; + }; + struct MouseEvent : public Event { ENTRY_IMPLEMENT_EVENT(MouseEvent, Event::Mouse); @@ -124,6 +133,14 @@ namespace entry m_queue.push(ev); } + void postCharEvent(WindowHandle _handle, uint8_t _len, const uint8_t _char[4]) + { + CharEvent* ev = new CharEvent(_handle); + ev->m_len = _len; + memcpy(ev->m_char, _char, 4); + m_queue.push(ev); + } + void postMouseEvent(WindowHandle _handle, int32_t _mx, int32_t _my, int32_t _mz) { MouseEvent* ev = new MouseEvent(_handle); diff --git a/examples/common/entry/entry_windows.cpp b/examples/common/entry/entry_windows.cpp index 1d8d51f2e..ce0f911b8 100644 --- a/examples/common/entry/entry_windows.cpp +++ b/examples/common/entry/entry_windows.cpp @@ -532,6 +532,26 @@ namespace entry } break; + case WM_CHAR: + { + uint8_t utf8[4] = {}; + uint8_t len = (uint8_t)WideCharToMultiByte(CP_UTF8 + , 0 + , (LPCWSTR)&_wparam + , 1 + , (LPSTR)utf8 + , BX_COUNTOF(utf8) + , NULL + , NULL + ); + if (0 != len) + { + WindowHandle handle = findHandle(_hwnd); + m_eventQueue.postCharEvent(handle, len, utf8); + } + } + break; + default: break; } diff --git a/examples/common/entry/input.cpp b/examples/common/entry/input.cpp index 110e3cde1..bd3c2bbdc 100644 --- a/examples/common/entry/input.cpp +++ b/examples/common/entry/input.cpp @@ -8,6 +8,7 @@ #include "entry_p.h" #include "input.h" +#include #include #include namespace stl = tinystl; @@ -68,6 +69,7 @@ struct Mouse struct Keyboard { Keyboard() + : m_ring(BX_COUNTOF(m_char) ) { } @@ -97,8 +99,44 @@ struct Keyboard m_once[_key] = false; } + void pushChar(uint8_t _len, const uint8_t _char[4]) + { + for (uint32_t len = m_ring.reserve(4) + ; len < _len + ; len = m_ring.reserve(4) + ) + { + popChar(); + } + + memcpy(&m_char[m_ring.m_write], _char, 4); + m_ring.commit(4); + } + + const uint8_t* popChar() + { + if (0 < m_ring.available() ) + { + uint8_t* utf8 = &m_char[m_ring.m_read]; + m_ring.consume(4); + return utf8; + } + + return NULL; + } + + void charFlush() + { + m_ring.m_current = 0; + m_ring.m_write = 0; + m_ring.m_read = 0; + } + uint32_t m_key[256]; bool m_once[256]; + + bx::RingBufferControl m_ring; + uint8_t m_char[256]; }; struct Input @@ -204,6 +242,21 @@ void inputSetKeyState(entry::Key::Enum _key, uint8_t _modifiers, bool _down) s_input.m_keyboard.setKeyState(_key, _modifiers, _down); } +void inputChar(uint8_t _len, const uint8_t _char[4]) +{ + s_input.m_keyboard.pushChar(_len, _char); +} + +const uint8_t* inputGetChar() +{ + return s_input.m_keyboard.popChar(); +} + +void inputCharFlush() +{ + s_input.m_keyboard.charFlush(); +} + void inputSetMousePos(int32_t _mx, int32_t _my, int32_t _mz) { s_input.m_mouse.setPos(_mx, _my, _mz); diff --git a/examples/common/entry/input.h b/examples/common/entry/input.h index b16dcfd18..a48a182dc 100644 --- a/examples/common/entry/input.h +++ b/examples/common/entry/input.h @@ -34,6 +34,15 @@ void inputProcess(); /// void inputSetKeyState(entry::Key::Enum _key, uint8_t _modifiers, bool _down); +/// Adds single UTF-8 encoded character into input buffer. +void inputChar(uint8_t _len, const uint8_t _char[4]); + +/// Returns single UTF-8 encoded character from input buffer. +const uint8_t* inputGetChar(); + +/// Flush internal input buffer. +void inputCharFlush(); + /// void inputSetMouseResolution(uint16_t _width, uint16_t _height); diff --git a/examples/common/font/text_buffer_manager.cpp b/examples/common/font/text_buffer_manager.cpp index 0d5a8bdc0..654d1cabe 100644 --- a/examples/common/font/text_buffer_manager.cpp +++ b/examples/common/font/text_buffer_manager.cpp @@ -236,7 +236,13 @@ void TextBuffer::appendText(FontHandle _fontHandle, const char* _string, const c CodePoint codepoint = 0; uint32_t state = 0; - for (; *_string && _string<_end ; ++_string) + if (_end == NULL) + { + _end = _string + strlen(_string); + } + BX_CHECK(_end >= _string); + + for (; *_string && _string < _end ; ++_string) { if (utf8_decode(&state, (uint32_t*)&codepoint, *_string) == UTF8_ACCEPT ) { @@ -260,7 +266,7 @@ void TextBuffer::appendText(FontHandle _fontHandle, const wchar_t* _string, cons if (_end == NULL) { - _end = _string + (uint32_t) wcslen(_string); + _end = _string + wcslen(_string); } BX_CHECK(_end >= _string); diff --git a/examples/common/font/text_metrics.cpp b/examples/common/font/text_metrics.cpp index 841fa08d3..d48035fb8 100644 --- a/examples/common/font/text_metrics.cpp +++ b/examples/common/font/text_metrics.cpp @@ -162,9 +162,9 @@ void TextLineMetrics::getSubText(const char* _string, uint32_t _firstLine, uint3 { for (; *_string; ++_string) { - if(utf8_decode(&state, (uint32_t*)&codepoint, *_string) == UTF8_ACCEPT) + if (utf8_decode(&state, (uint32_t*)&codepoint, *_string) == UTF8_ACCEPT) { - if(codepoint == L'\n') + if (codepoint == L'\n') { ++currentLine; ++_string; diff --git a/examples/common/imgui/imgui.cpp b/examples/common/imgui/imgui.cpp index 61a7bb2e1..064729544 100644 --- a/examples/common/imgui/imgui.cpp +++ b/examples/common/imgui/imgui.cpp @@ -788,8 +788,8 @@ struct Imgui time += 0.05f; const float dist = 10.0f; - const float offset0 = -proj[8] + (hmd->eye[0].adjust[0] / dist * proj[0]); - const float offset1 = -proj[8] + (hmd->eye[1].adjust[0] / dist * proj[0]); + const float offset0 = -proj[8] + (hmd->eye[0].viewOffset[0] / dist * proj[0]); + const float offset1 = -proj[8] + (hmd->eye[1].viewOffset[0] / dist * proj[0]); float ortho[2][16]; const float viewOffset = _width/4.0f; diff --git a/include/bgfx.h b/include/bgfx.h index d9516f704..e40cfe283 100644 --- a/include/bgfx.h +++ b/include/bgfx.h @@ -378,7 +378,7 @@ namespace bgfx float rotation[4]; //!< Eye rotation represented as quaternion. float translation[3]; //!< Eye translation. float fov[4]; //!< Field of view (up, down, left, right). - float adjust[3]; //!< Eye view matrix translation adjustment. + float viewOffset[3]; //!< Eye view matrix translation adjustment. float pixelsPerTanAngle[2]; //!< }; diff --git a/src/ovr.cpp b/src/ovr.cpp index 4181e3328..52255fb38 100644 --- a/src/ovr.cpp +++ b/src/ovr.cpp @@ -153,8 +153,8 @@ ovrError: m_timing = ovrHmd_BeginFrame(m_hmd, 0); - m_pose[0] = ovrHmd_GetEyePose(m_hmd, ovrEye_Left); - m_pose[1] = ovrHmd_GetEyePose(m_hmd, ovrEye_Right); + m_pose[0] = ovrHmd_GetHmdPosePerEye(m_hmd, ovrEye_Left); + m_pose[1] = ovrHmd_GetHmdPosePerEye(m_hmd, ovrEye_Right); return true; } @@ -175,7 +175,7 @@ ovrError: for (int ii = 0; ii < 2; ++ii) { ovrPosef& pose = m_pose[ii]; - pose = ovrHmd_GetEyePose(m_hmd, eye[ii]); + pose = ovrHmd_GetHmdPosePerEye(m_hmd, eye[ii]); HMD::Eye& eye = _hmd.eye[ii]; eye.rotation[0] = pose.Orientation.x; @@ -191,9 +191,9 @@ ovrError: eye.fov[1] = erd.Fov.DownTan; eye.fov[2] = erd.Fov.LeftTan; eye.fov[3] = erd.Fov.RightTan; - eye.adjust[0] = erd.ViewAdjust.x; - eye.adjust[1] = erd.ViewAdjust.y; - eye.adjust[2] = erd.ViewAdjust.z; + eye.viewOffset[0] = erd.HmdToEyeViewOffset.x; + eye.viewOffset[1] = erd.HmdToEyeViewOffset.y; + eye.viewOffset[2] = erd.HmdToEyeViewOffset.z; eye.pixelsPerTanAngle[0] = erd.PixelsPerTanAngleAtCenter.x; eye.pixelsPerTanAngle[1] = erd.PixelsPerTanAngleAtCenter.y; } diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index 2dfcf4e26..288dfcdbc 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -2967,9 +2967,9 @@ RENDERDOC_IMPORT for (uint32_t eye = 0; eye < 2; ++eye) { const HMD::Eye& hmdEye = hmd.eye[eye]; - viewAdjust.un.val[12] = hmdEye.adjust[0]; - viewAdjust.un.val[13] = hmdEye.adjust[1]; - viewAdjust.un.val[14] = hmdEye.adjust[2]; + viewAdjust.un.val[12] = hmdEye.viewOffset[0]; + viewAdjust.un.val[13] = hmdEye.viewOffset[1]; + viewAdjust.un.val[14] = hmdEye.viewOffset[2]; for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii) { diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index 769f16098..d988eeea1 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -4136,9 +4136,9 @@ namespace bgfx for (uint32_t eye = 0; eye < 2; ++eye) { const HMD::Eye& hmdEye = hmd.eye[eye]; - viewAdjust.un.val[12] = hmdEye.adjust[0]; - viewAdjust.un.val[13] = hmdEye.adjust[1]; - viewAdjust.un.val[14] = hmdEye.adjust[2]; + viewAdjust.un.val[12] = hmdEye.viewOffset[0]; + viewAdjust.un.val[13] = hmdEye.viewOffset[1]; + viewAdjust.un.val[14] = hmdEye.viewOffset[2]; for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii) {