RenderFramework接入键盘鼠标控制
This commit is contained in:
2
CMCore
2
CMCore
Submodule CMCore updated: 86ac0c37fc...333bec0a12
Submodule CMPlatform updated: 46ae04cd50...f0ff214289
Submodule CMSceneGraph updated: f866159787...d81a9f79f4
@@ -216,5 +216,5 @@ public:
|
|||||||
|
|
||||||
int os_main(int,os_char **)
|
int os_main(int,os_char **)
|
||||||
{
|
{
|
||||||
return RunFramework<TestApp>(OS_TEXT("AutoInstance"),1280,720);
|
return RunFramework<TestApp>(OS_TEXT("Billboard"),1280,720);
|
||||||
}
|
}
|
||||||
|
@@ -346,125 +346,6 @@ public:
|
|||||||
}
|
}
|
||||||
};//class VulkanApplicationFramework
|
};//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
|
class CameraAppFramework:public VulkanApplicationFramework
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
|
@@ -60,6 +60,10 @@ protected: //RenderContext,未来合并成一个RenderContext结构
|
|||||||
|
|
||||||
void CreateDefaultRenderer();
|
void CreateDefaultRenderer();
|
||||||
|
|
||||||
|
protected: //InputEvent
|
||||||
|
|
||||||
|
ObjectList<io::InputEvent> input_event;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Window * GetWindow ()const{return win;}
|
Window * GetWindow ()const{return win;}
|
||||||
@@ -101,6 +105,10 @@ public: // event
|
|||||||
virtual void OnActive(bool);
|
virtual void OnActive(bool);
|
||||||
virtual void OnClose();
|
virtual void OnClose();
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
void Tick();
|
||||||
|
|
||||||
public: // other
|
public: // other
|
||||||
|
|
||||||
RenderList *CreateRenderList()
|
RenderList *CreateRenderList()
|
||||||
|
@@ -138,6 +138,8 @@ bool RenderFramework::Init(uint w,uint h)
|
|||||||
|
|
||||||
void RenderFramework::CreateDefaultRenderer()
|
void RenderFramework::CreateDefaultRenderer()
|
||||||
{
|
{
|
||||||
|
input_event.Clear();
|
||||||
|
|
||||||
SAFE_CLEAR(default_renderer)
|
SAFE_CLEAR(default_renderer)
|
||||||
|
|
||||||
IRenderTarget *rt=GetSwapchainRenderTarget();
|
IRenderTarget *rt=GetSwapchainRenderTarget();
|
||||||
@@ -149,7 +151,15 @@ void RenderFramework::CreateDefaultRenderer()
|
|||||||
{
|
{
|
||||||
auto ubo_camera_info=device->CreateUBO<UBOCameraInfo>(&mtl::SBS_CameraInfo);
|
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);
|
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
|
VK_NAMESPACE_END
|
||||||
|
@@ -11,6 +11,7 @@ namespace hgl
|
|||||||
if(delta_time>=frame_time)
|
if(delta_time>=frame_time)
|
||||||
{
|
{
|
||||||
last_update_time=cur_time;
|
last_update_time=cur_time;
|
||||||
|
|
||||||
wo->Tick(delta_time);
|
wo->Tick(delta_time);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -70,6 +71,8 @@ namespace hgl
|
|||||||
{
|
{
|
||||||
cur_time=GetPreciseTime();
|
cur_time=GetPreciseTime();
|
||||||
|
|
||||||
|
render_framework->Tick();
|
||||||
|
|
||||||
if(cur_work_object->IsTickable())
|
if(cur_work_object->IsTickable())
|
||||||
Tick(cur_work_object);
|
Tick(cur_work_object);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user