2019-06-24 20:18:01 +08:00
|
|
|
|
#pragma once
|
2019-06-16 02:58:12 +08:00
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
class ViewModelFramework:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
vulkan::Buffer * ubo_world_matrix =nullptr;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
AABB bounding_box;
|
|
|
|
|
|
2019-06-25 01:36:47 +08:00
|
|
|
|
Matrix4f object_matrix,origin_matrix;
|
2019-06-16 02:58:12 +08:00
|
|
|
|
|
2019-06-25 01:36:47 +08:00
|
|
|
|
ControlCamera camera,origin_camera;
|
2019-06-16 02:58:12 +08:00
|
|
|
|
float move_speed=1;
|
|
|
|
|
|
|
|
|
|
Vector2f mouse_last_pos;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
SceneNode render_root;
|
|
|
|
|
RenderList render_list;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
virtual ~ViewModelFramework()=default;
|
|
|
|
|
|
|
|
|
|
virtual bool Init(int w,int h,const AABB &aabb)
|
|
|
|
|
{
|
|
|
|
|
if(!VulkanApplicationFramework::Init(w,h))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
bounding_box=aabb;
|
|
|
|
|
|
|
|
|
|
InitCamera(w,h);
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void InitCamera(int w,int h)
|
|
|
|
|
{
|
2019-06-25 01:36:47 +08:00
|
|
|
|
math::vec center_point =bounding_box.CenterPoint();
|
|
|
|
|
math::vec min_point =bounding_box.minPoint;
|
|
|
|
|
math::vec max_point =bounding_box.maxPoint;
|
|
|
|
|
math::vec size =bounding_box.Size();
|
2019-06-16 02:58:12 +08:00
|
|
|
|
|
2019-06-25 01:36:47 +08:00
|
|
|
|
math::vec eye( center_point.x,
|
|
|
|
|
center_point.y-size.y*2,
|
2019-06-24 20:18:01 +08:00
|
|
|
|
center_point.z,
|
|
|
|
|
1.0f);
|
2019-06-16 02:58:12 +08:00
|
|
|
|
|
2019-06-24 20:18:01 +08:00
|
|
|
|
camera.type =CameraType::Perspective;
|
|
|
|
|
camera.width =w;
|
|
|
|
|
camera.height =h;
|
|
|
|
|
camera.center =center_point;
|
|
|
|
|
camera.eye =eye;
|
|
|
|
|
|
|
|
|
|
camera.Refresh(); //更新矩阵计算
|
2019-06-16 02:58:12 +08:00
|
|
|
|
|
2019-06-25 01:36:47 +08:00
|
|
|
|
origin_camera=camera;
|
|
|
|
|
|
2019-06-24 20:18:01 +08:00
|
|
|
|
move_speed=length(min_point,center_point)/100.0f;
|
2019-06-16 17:18:10 +08:00
|
|
|
|
|
2019-06-24 20:18:01 +08:00
|
|
|
|
object_matrix=translate(-center_point.xyz());
|
2019-06-25 01:36:47 +08:00
|
|
|
|
origin_matrix=object_matrix;
|
2019-06-16 02:58:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitCameraUBO(vulkan::DescriptorSets *desc_set,uint world_matrix_bindpoint)
|
|
|
|
|
{
|
|
|
|
|
ubo_world_matrix=db->CreateUBO(sizeof(WorldMatrix),&camera.matrix);
|
|
|
|
|
|
|
|
|
|
if(!ubo_world_matrix)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return desc_set->BindUBO(world_matrix_bindpoint,*ubo_world_matrix);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resize(int w,int h)override
|
|
|
|
|
{
|
|
|
|
|
camera.width=w;
|
|
|
|
|
camera.height=h;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void Draw()override
|
|
|
|
|
{
|
2019-06-18 00:48:05 +08:00
|
|
|
|
const uint32_t index=AcquireNextImage();
|
|
|
|
|
|
2019-06-24 20:18:01 +08:00
|
|
|
|
camera.Refresh(); //更新相机矩阵
|
|
|
|
|
ubo_world_matrix->Write(&camera.matrix); //写入缓冲区
|
2019-06-16 02:58:12 +08:00
|
|
|
|
|
2019-06-24 20:18:01 +08:00
|
|
|
|
render_root.RefreshMatrix(&object_matrix);
|
2019-06-16 02:58:12 +08:00
|
|
|
|
render_list.Clear();
|
|
|
|
|
render_root.ExpendToList(&render_list);
|
|
|
|
|
|
2019-06-18 00:48:05 +08:00
|
|
|
|
BuildCommandBuffer(index,&render_list);
|
2019-06-16 02:58:12 +08:00
|
|
|
|
|
2019-06-18 00:48:05 +08:00
|
|
|
|
SubmitDraw(index);
|
2019-06-16 02:58:12 +08:00
|
|
|
|
|
2019-06-25 01:36:47 +08:00
|
|
|
|
if(key_status[kbEnter])
|
|
|
|
|
{
|
|
|
|
|
origin_camera.width=camera.width;
|
|
|
|
|
origin_camera.height=camera.height;
|
|
|
|
|
camera=origin_camera;
|
|
|
|
|
object_matrix=origin_matrix;
|
|
|
|
|
}
|
|
|
|
|
else
|
2019-06-16 02:58:12 +08:00
|
|
|
|
if(key_status[kbW])camera.Forward (move_speed);else
|
|
|
|
|
if(key_status[kbS])camera.Backward (move_speed);else
|
|
|
|
|
if(key_status[kbA])camera.Left (move_speed);else
|
|
|
|
|
if(key_status[kbD])camera.Right (move_speed);else
|
|
|
|
|
if(key_status[kbR])camera.Up (move_speed);else
|
|
|
|
|
if(key_status[kbF])camera.Down (move_speed);else
|
2019-06-25 01:36:47 +08:00
|
|
|
|
{
|
|
|
|
|
auto axis=normalized(camera.center-camera.eye);
|
|
|
|
|
float rotate_rad=hgl_ang2rad(move_speed)*10;
|
|
|
|
|
|
|
|
|
|
if(key_status[kbLeft ])
|
|
|
|
|
object_matrix=rotate(-rotate_rad,camera.forward_vector)*object_matrix;else
|
|
|
|
|
if(key_status[kbRight])
|
|
|
|
|
object_matrix=rotate( rotate_rad,camera.forward_vector)*object_matrix;else
|
2019-06-16 02:58:12 +08:00
|
|
|
|
|
|
|
|
|
return;
|
2019-06-25 01:36:47 +08:00
|
|
|
|
}
|
2019-06-16 02:58:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void KeyPress(KeyboardButton kb)override
|
|
|
|
|
{
|
|
|
|
|
if(kb==kbMinus)move_speed*=0.9f;else
|
|
|
|
|
if(kb==kbEquals)move_speed*=1.1f;else
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void MouseDown(uint) override
|
|
|
|
|
{
|
|
|
|
|
mouse_last_pos=mouse_pos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void MouseMove() override
|
|
|
|
|
{
|
2019-06-16 17:18:10 +08:00
|
|
|
|
if(!mouse_key)return;
|
2019-06-16 02:58:12 +08:00
|
|
|
|
|
|
|
|
|
Vector2f gap=mouse_pos-mouse_last_pos;
|
|
|
|
|
|
|
|
|
|
if(mouse_key&mbLeft)
|
|
|
|
|
{
|
|
|
|
|
if(gap.x!=0)camera.HorzRotate(-gap.x/10.0f);
|
|
|
|
|
if(gap.y!=0)camera.VertRotate(-gap.y/10.0f);
|
|
|
|
|
}
|
2019-06-16 17:18:10 +08:00
|
|
|
|
else if(mouse_key&mbRight)
|
2019-06-16 02:58:12 +08:00
|
|
|
|
{
|
2019-06-24 20:18:01 +08:00
|
|
|
|
if(gap.x!=0)object_matrix=rotate(hgl_ang2rad(gap.x),camera.up_vector)*object_matrix;
|
2019-06-25 01:36:47 +08:00
|
|
|
|
if(gap.y!=0)object_matrix=rotate(hgl_ang2rad(gap.y),camera.right_vector)*object_matrix;
|
2019-06-16 02:58:12 +08:00
|
|
|
|
}
|
2019-06-16 17:18:10 +08:00
|
|
|
|
else if(mouse_key&mbMid)
|
|
|
|
|
{
|
2019-06-25 01:36:47 +08:00
|
|
|
|
if(gap.x!=0)camera.Left(gap.x*move_speed/5.0f);
|
|
|
|
|
if(gap.y!=0)camera.Up(gap.y*move_speed/5.0f);
|
2019-06-16 17:18:10 +08:00
|
|
|
|
}
|
2019-06-16 02:58:12 +08:00
|
|
|
|
|
|
|
|
|
mouse_last_pos=mouse_pos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void MouseWheel(int v,int h,uint)
|
|
|
|
|
{
|
|
|
|
|
camera.Distance(1+(v/1000.0f));
|
|
|
|
|
}
|
|
|
|
|
};//class CameraAppFramework
|