FCT
载入中...
搜索中...
未找到
AutoViewport.cpp
浏览该文件的文档.
2#include "./Window.h"
3namespace FCT
4{
9
10 AutoViewport::AutoViewport(Vec2 windowSize, Vec2 viewportSize)
11 {
12 m_needReviewport = false;
13 resize(windowSize.x,windowSize.y);
14 m_dstViewportWidth = viewportSize.x;
15 m_dstViewPortHeight = viewportSize.y;
16 m_viewportOffsetX = 0.0f;
17 m_viewportOffsetY = 0.0f;
18 }
19
21 {
22 this->m_ctx = ctx;
23 }
24
26 {
27 m_wnd = wnd;
28 }
29
31 {
33 {
34 m_wnd->getCallBack()->removeResizeCallback(m_resizeCallBack);
35 }
36 m_resizeCallBack = m_wnd->getCallBack()->addResizeCallback([this](Window* w,int width,int height)
37 {
38 resize(width,height);
39 });
40 }
41
42 void AutoViewport::resize(int width, int height)
43 {
44 m_windowWidth = static_cast<float>(width);
45 m_windowHeight = static_cast<float>(height);
46 m_needReviewport = true;
47 }
48
50 {
51 float targetAspect = m_dstViewportWidth / m_dstViewPortHeight;
52 float windowAspect = m_windowWidth / m_windowHeight;
53
54 if (windowAspect > targetAspect) {
56 m_viewportWidth = m_windowHeight * targetAspect;
58 m_viewportOffsetY = 0.0f;
59 } else {
61 m_viewportHeight = m_windowWidth / targetAspect;
62 m_viewportOffsetX = 0.0f;
64 }
65 }
66
76
77
78 bool AutoViewport::wndPosToViewportPos(Vec2 wndPos, Vec2& viewportPos) const
79 {
80 Vec2 srcViewportPos(0.0f, 0.0f);
81 srcViewportPos.x = (wndPos.x - m_viewportOffsetX) / m_viewportWidth;
82 srcViewportPos.y = (wndPos.y - m_viewportOffsetY) / m_viewportHeight;
83 viewportPos = srcViewportPos * Vec2(m_dstViewportWidth, m_dstViewPortHeight);
84 return srcViewportPos.x >= 0.0f && srcViewportPos.x <= 1.0f && srcViewportPos.y >= 0.0f && srcViewportPos.y <= 1.0f;
85 }
86}
87
88
89
void ctx(FCT::Context *ctx)
bool wndPosToViewportPos(Vec2 wndPos, Vec2 &viewportPos) const
CallBackEventHandler::CallbackId m_resizeCallBack
void resize(int width, int height)
void window(Window *wnd)
void submit(RHI::CommandBuffer *cmdBuf)
void enable(bool enable)
virtual void scissor(Vec2 lt, Vec2 rb)=0
virtual void viewport(Vec2 lt, Vec2 rb)=0
float x
定义 Vec.h:10
float y
定义 Vec.h:10