Expose ui scaling and orientation

This commit is contained in:
Georg Hagen
2024-07-27 14:30:59 +02:00
parent 6a1a76f4d1
commit eb81c777bf
7 changed files with 45 additions and 43 deletions

View File

@@ -84,6 +84,9 @@ namespace OpenVulkano
[[nodiscard]] virtual uint32_t GetWindowId() const = 0;
virtual void* GetNativeWindowHandle() = 0;
virtual float GetContentScale() const { return 1; }
virtual float GetInterfaceOrientation() const { return 0; }
protected:
static uint32_t CreateWindowId()
{

View File

@@ -45,7 +45,7 @@ namespace OpenVulkano
if (w > 0 && h > 0)
io.DisplayFramebufferScale = ImVec2((float)display_w / (float)w, (float)display_h / (float)h);*/
io.DisplaySize = { static_cast<float>(m_window->GetWidth()), static_cast<float>(m_window->GetHeight()) };
io.DisplayFramebufferScale = { 1, 1 };
io.DisplayFramebufferScale = { m_window->GetContentScale(), m_window->GetContentScale() };
//if (bd->WantUpdateMonitors)
//ImGui_ImplGlfw_UpdateMonitors();
@@ -68,4 +68,4 @@ namespace OpenVulkano
io.BackendPlatformUserData = nullptr;
io.BackendFlags = ImGuiBackendFlags_None;
}
}
}

View File

@@ -16,6 +16,8 @@ namespace OpenVulkano
IWindowHandler* handler = nullptr;
void* caMetalLayer = nullptr;
bool tickHandler = true;
float contentScale = 1;
float orientation = 0;
public:
void Init(RenderAPI::RenderApi renderApi) override {}
@@ -82,6 +84,14 @@ namespace OpenVulkano
void Close() override {}
void OnResize(uint32_t width, uint32_t height);
virtual float GetContentScale() const override { return contentScale; }
virtual float GetInterfaceOrientation() const override { return orientation; }
void SetContentScale(float scale) { contentScale = scale; }
void SetOrientation(float orientation) { this->orientation = orientation; }
void TickHandler();
};

View File

@@ -21,4 +21,6 @@
-(void)WillDisappear;
-(void)DidDisappear;
-(void)DidUnload;
-(void)SetInterfaceOrientation:(float)orientation;
@end

View File

@@ -35,6 +35,7 @@ using namespace OpenVulkano;
- (void) mtkView:(MTKView *) view drawableSizeWillChange:(CGSize) size
{
window->SetContentScale(UIScreen.mainScreen.nativeScale);
window->OnResize(size.width, size.height);
}
@@ -82,9 +83,8 @@ using namespace OpenVulkano;
- (void)commonInit {
[self setMultipleTouchEnabled:YES];
self.contentScaleFactor = UIScreen.mainScreen.nativeScale / 1.5f;
self.contentScaleFactor = UIScreen.mainScreen.nativeScale;
auto size = self.bounds.size;
auto sizeX = size.width * self.contentScaleFactor;
auto sizeY = size.height * self.contentScaleFactor;
@@ -126,10 +126,17 @@ using namespace OpenVulkano;
auto size = self.bounds.size;
auto sizeX = size.width * self.contentScaleFactor;
auto sizeY = size.height * self.contentScaleFactor;
m_window.SetContentScale(self.contentScaleFactor);
m_window.OnResize(sizeX, sizeY);
}
}
- (void)SetInterfaceOrientation:(float)orientation
{
m_window.SetOrientation(orientation);
}
- (Math::Vector2f)getTouchPosition:(UITouch*)touch
{
CGPoint uitouchLocation = [touch locationInView:touch.view];

View File

@@ -17,7 +17,7 @@ namespace OpenVulkano::Scene
{
public:
ICloseable* renderCamera = nullptr;
static constexpr inline size_t SIZE = sizeof(Math::Matrix4f) * 3 + sizeof(Math::Vector4f) + sizeof(float) * 8 + 16;
static constexpr inline size_t SIZE = sizeof(Math::Matrix4f) * 3 + sizeof(Math::Vector4f) + sizeof(float) * 12;
static constexpr inline DescriptorSetLayoutBinding DESCRIPTOR_SET_LAYOUT_BINDING = { 0, DescriptorSetLayoutBinding::Type::TYPE_UNIFORM_BUFFER_DYNAMIC, 1, ShaderProgramType::ALL_GRAPHICS };
protected:
@@ -26,7 +26,8 @@ namespace OpenVulkano::Scene
float m_nearPlane, m_farPlane, m_width, m_height;
float m_fov = 0, m_aspect = 0, m_scaleFactor = 0, m_perPixelScaleFactor = 0;
float m_contentScaleFactor = 1, m_zoom = 1; // For use with ortho camera
std::array<uint8_t, 8> m_userData{};
float m_interfaceOrientation = 0;
float m_padding = 0; //Unused
Camera() : m_nearPlane(0), m_farPlane(0), m_width(0), m_height(0) {}
@@ -55,32 +56,19 @@ namespace OpenVulkano::Scene
virtual void SetSize(const float width, const float height)
{
if (m_width == width && m_height == height) return;
if (m_width == width && m_height == height) [[likely]] return;
m_width = width;
m_height = height;
UpdateProjectionMatrix();
}
void SetNearPlane(float nearPlane)
{
m_nearPlane = nearPlane;
}
void SetNearPlane(float nearPlane) { m_nearPlane = nearPlane; }
void SetFarPlane(float farPlane)
{
m_farPlane = farPlane;
}
void SetFarPlane(float farPlane) { m_farPlane = farPlane; }
[[nodiscard]] float NearPlane() const { return m_nearPlane; }
[[nodiscard]] float NearPlane() const
{
return m_nearPlane;
}
[[nodiscard]] float FarPlane() const
{
return m_farPlane;
}
[[nodiscard]] float FarPlane() const { return m_farPlane; }
void SetContentScaleFactor(float contentScale = 1)
{
@@ -88,6 +76,10 @@ namespace OpenVulkano::Scene
}
[[nodiscard]] float GetContentScaleFactor() const { return 1.0f / m_contentScaleFactor; }
void SetInterfaceOrientation(float orientation) { m_interfaceOrientation = orientation; }
float GetInterfaceOrientation() const { return m_interfaceOrientation; }
void SetZoom(float zoom) { m_zoom = 1.0f / zoom; }
@@ -126,10 +118,7 @@ namespace OpenVulkano::Scene
return m_viewProjection;
}
[[nodiscard]] const Math::Vector4f& GetPosition() const
{
return m_camPosition;
}
[[nodiscard]] const Math::Vector4f& GetPosition() const { return m_camPosition; }
[[nodiscard]] Math::Vector3f GetRightVector() const
{
@@ -155,12 +144,6 @@ namespace OpenVulkano::Scene
return {m_viewProjection};
}
/**
* The 16 byte of user data can be used to transmit additional data about the camera to the shader.
* @return reference to the custom data array
*/
[[nodiscard]] std::array<uint8_t, 8>& GetUserData() { return m_userData; }
[[nodiscard]] float GetScaleFactor() const { return m_scaleFactor; }
[[nodiscard]] float GetPixelScaleFactor() const { return m_perPixelScaleFactor; }
@@ -202,6 +185,7 @@ namespace OpenVulkano::Scene
void SetSize(const float width, const float height) override
{
if (m_width == width && m_height == height) [[likely]] return;
m_aspect = width / height;
Camera::SetSize(width, height);
m_perPixelScaleFactor = m_height / m_scaleFactor;
@@ -235,20 +219,14 @@ namespace OpenVulkano::Scene
m_perPixelScaleFactor = m_height / m_scaleFactor;
}
[[nodiscard]] float GetFov() const
{
return Math::Utils::degrees(m_fov);
}
[[nodiscard]] float GetFov() const { return Math::Utils::degrees(m_fov); }
[[nodiscard]] float GetFovX() const
{
return 2.0f * atanf(tanf(GetFov() * 0.5f) * m_aspect);
}
[[nodiscard]] float GetFovRad() const
{
return m_fov;
}
[[nodiscard]] float GetFovRad() const { return m_fov; }
[[nodiscard]] float GetFovXRad() const
{

View File

@@ -69,6 +69,8 @@ namespace OpenVulkano::Vulkan
{
currentImageId = context.swapChain.AcquireNextImage();
scene->GetCamera()->SetSize(context.window->GetWidth(), context.window->GetHeight());
scene->GetCamera()->SetContentScaleFactor(context.window->GetContentScale());
scene->GetCamera()->SetInterfaceOrientation(context.window->GetInterfaceOrientation());
Render();
}