RenderFramework接入键盘鼠标控制

This commit is contained in:
hyzboy 2025-06-10 01:34:13 +08:00
parent 3dfb2c65fd
commit b7627f392c
8 changed files with 39 additions and 124 deletions

2
CMCore

@ -1 +1 @@
Subproject commit 86ac0c37fcf6c4c4cb82446b89767561c9349517
Subproject commit 333bec0a122a962d0b7c55948a73ead6052ae9d2

@ -1 +1 @@
Subproject commit 46ae04cd50d09dfb00d9945c5f43d29a2744bcae
Subproject commit f0ff214289a1265898feecbdbbd2ddf50bff5dca

@ -1 +1 @@
Subproject commit f866159787c98e29029c2b4dba3de8f6455473da
Subproject commit d81a9f79f4f33a67d4c2938cb3acdd4ec18feeaf

View File

@ -216,5 +216,5 @@ public:
int os_main(int,os_char **)
{
return RunFramework<TestApp>(OS_TEXT("AutoInstance"),1280,720);
return RunFramework<TestApp>(OS_TEXT("Billboard"),1280,720);
}

View File

@ -346,125 +346,6 @@ public:
}
};//class VulkanApplicationFramework
class CameraKeyboardControl:public KeyboardStateEvent
{
FirstPersonCameraControl *camera;
float move_speed;
public:
CameraKeyboardControl(FirstPersonCameraControl *wc)
{
camera=wc;
move_speed=1.0f;
}
bool OnPressed(const KeyboardButton &kb)override
{
if(!KeyboardStateEvent::OnPressed(kb))
return(false);
if(kb==KeyboardButton::Minus )move_speed*=0.9f;else
if(kb==KeyboardButton::Equals )move_speed*=1.1f;
return(true);
}
void Update()
{
if(HasPressed(KeyboardButton::W ))camera->Forward (move_speed);else
if(HasPressed(KeyboardButton::S ))camera->Backward (move_speed);else
if(HasPressed(KeyboardButton::A ))camera->Left (move_speed);else
if(HasPressed(KeyboardButton::D ))camera->Right (move_speed);else
//if(HasPressed(KeyboardButton::R ))camera->Up (move_speed);else
//if(HasPressed(KeyboardButton::F ))camera->Down (move_speed);else
//if(HasPressed(KeyboardButton::Left ))camera->HoriRotate( move_speed);else
//if(HasPressed(KeyboardButton::Right ))camera->HoriRotate(-move_speed);else
//if(HasPressed(KeyboardButton::Up ))camera->VertRotate( move_speed);else
//if(HasPressed(KeyboardButton::Down ))camera->VertRotate(-move_speed);else
return;
}
};
class CameraMouseControl:public MouseEvent
{
FirstPersonCameraControl *camera;
double cur_time;
double last_time;
Vector2f mouse_pos;
Vector2f mouse_last_pos;
protected:
bool OnPressed(int x,int y,MouseButton) override
{
mouse_last_pos.x=x;
mouse_last_pos.y=y;
last_time=cur_time;
return(true);
}
bool OnWheel(int,int y) override
{
if(y==0)return(false);
camera->Forward(float(y)/10.0f);
return(true);
}
bool OnMove(int x,int y) override
{
mouse_pos.x=x;
mouse_pos.y=y;
bool left=HasPressed(MouseButton::Left);
bool right=HasPressed(MouseButton::Right);
Vector2f pos(x,y);
Vector2f gap=pos-mouse_last_pos;
if(left)
{
gap/=-5.0f;
camera->Rotate(gap);
}
else
if(right)
{
gap/=10.0f;
camera->Move(Vector3f(gap.x,0,gap.y));
}
last_time=cur_time;
mouse_last_pos=Vector2f(x,y);
return(true);
}
public:
CameraMouseControl(FirstPersonCameraControl *wc)
{
camera=wc;
cur_time=0;
last_time=0;
}
const Vector2f &GetMouseCoord()const{return mouse_pos;}
void Update()
{
cur_time=GetPreciseTime();
}
};
class CameraAppFramework:public VulkanApplicationFramework
{
protected:

View File

@ -60,6 +60,10 @@ protected: //RenderContext,未来合并成一个RenderContext结构
void CreateDefaultRenderer();
protected: //InputEvent
ObjectList<io::InputEvent> input_event;
public:
Window * GetWindow ()const{return win;}
@ -101,6 +105,10 @@ public: // event
virtual void OnActive(bool);
virtual void OnClose();
public:
void Tick();
public: // other
RenderList *CreateRenderList()

View File

@ -138,6 +138,8 @@ bool RenderFramework::Init(uint w,uint h)
void RenderFramework::CreateDefaultRenderer()
{
input_event.Clear();
SAFE_CLEAR(default_renderer)
IRenderTarget *rt=GetSwapchainRenderTarget();
@ -149,7 +151,15 @@ void RenderFramework::CreateDefaultRenderer()
{
auto ubo_camera_info=device->CreateUBO<UBOCameraInfo>(&mtl::SBS_CameraInfo);
default_camera_control=new FirstPersonCameraControl(rt->GetViewportInfo(),default_camera,ubo_camera_info);
auto fpcc=new FirstPersonCameraControl(rt->GetViewportInfo(),default_camera,ubo_camera_info);
auto ckc=new CameraKeyboardControl(fpcc);
auto cmc=new CameraMouseControl(fpcc);
this->Join(ckc);
this->Join(cmc);
default_camera_control=fpcc;
}
default_renderer->SetCameraControl(default_camera_control);
@ -172,4 +182,17 @@ void RenderFramework::OnClose()
{
}
void RenderFramework::Tick()
{
if(default_camera_control)
{
for(auto *ie:input_event)
{
ie->Update();
}
default_camera_control->Refresh();
}
}
VK_NAMESPACE_END

View File

@ -11,6 +11,7 @@ namespace hgl
if(delta_time>=frame_time)
{
last_update_time=cur_time;
wo->Tick(delta_time);
}
}
@ -70,6 +71,8 @@ namespace hgl
{
cur_time=GetPreciseTime();
render_framework->Tick();
if(cur_work_object->IsTickable())
Tick(cur_work_object);