diff --git a/example/Vulkan/Geometry2D.cpp b/example/Vulkan/Geometry2D.cpp index bd074cbf..6dae5e36 100644 --- a/example/Vulkan/Geometry2D.cpp +++ b/example/Vulkan/Geometry2D.cpp @@ -34,7 +34,7 @@ private: vulkan::Material * material =nullptr; vulkan::DescriptorSets * descriptor_sets =nullptr; - + vulkan::Renderable *ro_rectangle =nullptr, *ro_circle =nullptr, *ro_round_rectangle =nullptr; @@ -111,7 +111,7 @@ private: descriptor_sets->Update(); return(true); } - + bool InitPipeline() { constexpr os_char PIPELINE_FILENAME[]=OS_TEXT("2DSolid.pipeline"); @@ -157,7 +157,7 @@ public: if(!InitUBO()) return(false); - + if(!InitPipeline()) return(false); @@ -169,7 +169,7 @@ public: void Resize(int,int)override { - BuildCommandBuffer(&render_list); + BuildCommandBuffer(&render_list); } };//class TestApp:public VulkanApplicationFramework diff --git a/example/Vulkan/Geometry3D.cpp b/example/Vulkan/Geometry3D.cpp index ea7f40c0..5ecbb84b 100644 --- a/example/Vulkan/Geometry3D.cpp +++ b/example/Vulkan/Geometry3D.cpp @@ -13,37 +13,22 @@ using namespace hgl::graph; constexpr uint32_t SCREEN_WIDTH=128; constexpr uint32_t SCREEN_HEIGHT=128; -class TestApp:public VulkanApplicationFramework +class TestApp:public WalkerCameraAppFramework { private: SceneNode render_root; RenderList render_list; - Camera camera; - vulkan::Material * material =nullptr; vulkan::DescriptorSets * descriptor_sets =nullptr; vulkan::Renderable *ro_plane_grid[3]; - vulkan::Buffer * ubo_world_matrix =nullptr; - vulkan::Pipeline * pipeline_line =nullptr; private: - void InitCamera() - { - camera.type=CameraType::Perspective; - camera.center.Set(0,0,0,1); - camera.eye.Set(100,100,100,1); - camera.width=SCREEN_WIDTH; - camera.height=SCREEN_HEIGHT; - - camera.Refresh(); //更新矩阵计算 - } - bool InitMaterial() { material=shader_manage->CreateMaterial(OS_TEXT("PositionColor3D.vert.spv"), @@ -90,14 +75,7 @@ private: bool InitUBO() { - const VkExtent2D extent=device->GetExtent(); - - ubo_world_matrix=db->CreateUBO(sizeof(WorldMatrix),&camera.matrix); - - if(!ubo_world_matrix) - return(false); - - if(!descriptor_sets->BindUBO(material->GetUBO("world"),*ubo_world_matrix)) + if(!InitCameraUBO(descriptor_sets,material->GetUBO("world"))) return(false); descriptor_sets->Update(); @@ -147,11 +125,9 @@ public: bool Init() { - if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT)) + if(!WalkerCameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT)) return(false); - InitCamera(); - if(!InitMaterial()) return(false); @@ -171,9 +147,9 @@ public: void Resize(int,int)override { - BuildCommandBuffer(&render_list); + BuildCommandBuffer(&render_list); } -};//class TestApp:public VulkanApplicationFramework +};//class TestApp:public WalkerCameraAppFramework int main(int,char **) { diff --git a/example/Vulkan/LoadModel.cpp b/example/Vulkan/LoadModel.cpp index 38a4c9b5..e3cea4e0 100644 --- a/example/Vulkan/LoadModel.cpp +++ b/example/Vulkan/LoadModel.cpp @@ -76,20 +76,16 @@ vulkan::Renderable *CreateMeshRenderable(SceneDB *db,vulkan::Material *mtl,const return(render_obj); } -class TestApp:public VulkanApplicationFramework +class TestApp:public WalkerCameraAppFramework { private: SceneNode render_root; RenderList render_list; - Camera camera; - vulkan::Material * material =nullptr; vulkan::DescriptorSets * descriptor_sets =nullptr; - vulkan::Buffer * ubo_world_matrix =nullptr; - vulkan::Pipeline * pipeline_line =nullptr; private: @@ -121,8 +117,6 @@ private: camera.type=CameraType::Perspective; camera.center=center_point; camera.eye=min_point; - camera.width=SCREEN_WIDTH; - camera.height=SCREEN_HEIGHT; camera.Refresh(); //更新矩阵计算 } @@ -159,14 +153,7 @@ private: bool InitUBO() { - const VkExtent2D extent=device->GetExtent(); - - ubo_world_matrix=db->CreateUBO(sizeof(WorldMatrix),&camera.matrix); - - if(!ubo_world_matrix) - return(false); - - if(!descriptor_sets->BindUBO(material->GetUBO("world"),*ubo_world_matrix)) + if(!InitCameraUBO(descriptor_sets,material->GetUBO("world"))) return(false); descriptor_sets->Update(); @@ -246,7 +233,7 @@ public: model_data=md; - if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT)) + if(!WalkerCameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT)) return(false); if(!InitMaterial()) @@ -270,9 +257,9 @@ public: void Resize(int,int)override { - BuildCommandBuffer(&render_list); + BuildCommandBuffer(&render_list); } -};//class TestApp:public VulkanApplicationFramework +};//class TestApp:public WalkerCameraAppFramework #ifdef _WIN32 int wmain(int argc,wchar_t **argv) diff --git a/example/Vulkan/SceneTree.cpp b/example/Vulkan/SceneTree.cpp index 45968576..cfea1590 100644 --- a/example/Vulkan/SceneTree.cpp +++ b/example/Vulkan/SceneTree.cpp @@ -1,5 +1,5 @@ -// 5.SceneTree -// ڲеijУÿһڵԱ任ĴǷȷ +// 5.SceneTree +// 用于测试树形排列的场景中,每一级节点对变换矩阵的处理是否正确 #include"VulkanAppFramework.h" #include @@ -15,7 +15,7 @@ using namespace hgl::graph; constexpr uint32_t SCREEN_WIDTH=128; constexpr uint32_t SCREEN_HEIGHT=128; -class TestApp:public VulkanApplicationFramework +class TestApp:public WalkerCameraAppFramework { private: @@ -24,15 +24,11 @@ private: SceneNode render_root; RenderList render_list; - Camera camera; - vulkan::Material * material =nullptr; vulkan::DescriptorSets * descriptor_sets =nullptr; vulkan::Renderable * ro_cube =nullptr; - vulkan::Buffer * ubo_world_matrix =nullptr; - vulkan::Pipeline * pipeline_line =nullptr; public: @@ -46,17 +42,6 @@ public: private: - void InitCamera() - { - camera.type=CameraType::Perspective; - camera.center.Set(0,0,30,1); - camera.eye.Set(100,100,100,1); - camera.width=SCREEN_WIDTH; - camera.height=SCREEN_HEIGHT; - - camera.Refresh(); //¾ - } - bool InitMaterial() { material=shader_manage->CreateMaterial(OS_TEXT("OnlyPosition3D.vert.spv"), @@ -80,14 +65,7 @@ private: bool InitUBO() { - const VkExtent2D extent=device->GetExtent(); - - ubo_world_matrix=db->CreateUBO(sizeof(WorldMatrix),&camera.matrix); - - if(!ubo_world_matrix) - return(false); - - if(!descriptor_sets->BindUBO(material->GetUBO("world"),*ubo_world_matrix)) + if(!InitCameraUBO(descriptor_sets,material->GetUBO("world"))) return(false); descriptor_sets->Update(); @@ -130,7 +108,7 @@ private: for(uint i=0;i<360;i++) { size=(i+1)/100.0f; - + cur_node=render_root.CreateSubNode( rotate(i/5.0f,camera.up_vector)* translate(i/4.0f,0,0)* scale(size)); @@ -153,11 +131,9 @@ public: bool Init() { - if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT)) + if(!WalkerCameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT)) return(false); - InitCamera(); - if(!InitMaterial()) return(false); @@ -177,7 +153,7 @@ public: void Draw() override { - VulkanApplicationFramework::Draw(); + WalkerCameraAppFramework::Draw(); Matrix4f rot=rotate(GetDoubleTime()-start_time,camera.up_vector); @@ -190,7 +166,7 @@ public: void Resize(int,int)override { - BuildCommandBuffer(&render_list); + BuildCommandBuffer(&render_list); } };//class TestApp:public VulkanApplicationFramework diff --git a/example/Vulkan/TGATexture.cpp b/example/Vulkan/TGATexture.cpp index 9f3234a4..0812a60b 100644 --- a/example/Vulkan/TGATexture.cpp +++ b/example/Vulkan/TGATexture.cpp @@ -100,7 +100,7 @@ Texture2D *LoadTGATexture(const OSString &filename,Device *device) { if(header->bit==24) { - RGB8to565(pixel_data,header->width*header->height); + RGB8to565(pixel_data,header->width*header->height); format=FMT_RGB565; line_size=header->width*2; diff --git a/example/Vulkan/VulkanAppFramework.h b/example/Vulkan/VulkanAppFramework.h index 6f9ac7ff..dee49b6a 100644 --- a/example/Vulkan/VulkanAppFramework.h +++ b/example/Vulkan/VulkanAppFramework.h @@ -28,6 +28,16 @@ private: Window * win =nullptr; vulkan::Instance * inst =nullptr; + void OnResize(int w,int h) + { + InitCommandBuffer(); + Resize(w,h); + } + + void OnKeyDown(KeyboardButton kb){key_status[kb]=true;} + void OnKeyUp(KeyboardButton kb){key_status[kb]=false;} + void OnKeyPress(KeyboardButton kb){KeyPress(kb);} + protected: vulkan::Device * device =nullptr; @@ -38,11 +48,13 @@ private: uint32_t swap_chain_count=0; vulkan::CommandBuffer ** cmd_buf =nullptr; - + protected: SceneDB * db =nullptr; + bool key_status[kbRangeSize]; + public: virtual ~VulkanApplicationFramework() @@ -55,8 +67,10 @@ public: SAFE_CLEAR(inst); } - virtual bool Init(int w,int h) + virtual bool Init(int w,int h) { + hgl_zero(key_status); + #ifdef _DEBUG if(!vulkan::CheckStrideBytesByFormat()) return(false); @@ -86,18 +100,16 @@ public: InitCommandBuffer(); - SetEventCall(win->OnResize,this,VulkanApplicationFramework,OnResize); + SetEventCall(win->OnResize, this,VulkanApplicationFramework,OnResize ); + SetEventCall(win->OnKeyDown, this,VulkanApplicationFramework,OnKeyDown ); + SetEventCall(win->OnKeyUp, this,VulkanApplicationFramework,OnKeyUp ); + SetEventCall(win->OnKeyPress, this,VulkanApplicationFramework,OnKeyPress ); return(true); } virtual void Resize(int,int)=0; - - void OnResize(int w,int h) - { - InitCommandBuffer(); - Resize(w,h); - } + virtual void KeyPress(KeyboardButton){} void InitCommandBuffer() { @@ -193,3 +205,92 @@ public: return(true); } };//class VulkanApplicationFramework + +class WalkerCameraAppFramework:public VulkanApplicationFramework +{ +private: + + vulkan::Buffer * ubo_world_matrix =nullptr; + +protected: + + WalkerCamera camera; + float move_speed=0.01; + +public: + + virtual ~WalkerCameraAppFramework()=default; + + virtual bool Init(int w,int h) + { + if(!VulkanApplicationFramework::Init(w,h)) + return(false); + + InitCamera(); + return(true); + } + + virtual void InitCamera() + { + camera.type=CameraType::Perspective; + camera.center.Set(0,0,0,1); + camera.eye.Set(100,100,100,1); + + camera.Refresh(); //更新矩阵计算 + } + + bool InitCameraUBO(vulkan::DescriptorSets *desc_set,uint world_matrix_bindpoint) + { + InitCamera(); + + const VkExtent2D extent=device->GetExtent(); + + camera.width=extent.width; + camera.height=extent.height; + + 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 RefreshCameraUBO() + { + camera.Refresh(); //更新相机矩阵 + ubo_world_matrix->Write(&camera.matrix); //写入缓冲区 + } + + void Draw()override + { + VulkanApplicationFramework::Draw(); + + 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[kbLeft ])camera.WrapLeftRotate (move_speed*100);else + if(key_status[kbRight ])camera.WrapRightRotate(move_speed*100);else + if(key_status[kbUp ])camera.WrapUpRotate (move_speed*100);else + if(key_status[kbDown ])camera.WrapDownRotate (move_speed*100);else + + if(key_status[kbHome ])camera.ForwardTilt (move_speed*100);else + if(key_status[kbEnd ])camera.LeaningBack (move_speed*100);else + if(key_status[kbDelete ])camera.LeftRotate (move_speed*100);else + if(key_status[kbPageDown])camera.RightRotate (move_speed*100);else + return; + + RefreshCameraUBO(); + } + + void KeyPress(KeyboardButton kb)override + { + if(kb==kbMinus)move_speed*=0.9;else + if(kb==kbEquals)move_speed*=1.1;else + return; + + RefreshCameraUBO(); + } +};//class WalkerCameraAppFramework diff --git a/example/Vulkan/indices_rect.cpp b/example/Vulkan/indices_rect.cpp index 8abdcd7e..cd2f88db 100644 --- a/example/Vulkan/indices_rect.cpp +++ b/example/Vulkan/indices_rect.cpp @@ -135,15 +135,15 @@ public: if(!InitPipeline()) return(false); - + BuildCommandBuffer(pipeline,descriptor_sets,render_obj); return(true); } - + void Resize(int,int)override { - BuildCommandBuffer(pipeline,descriptor_sets,render_obj); + BuildCommandBuffer(pipeline,descriptor_sets,render_obj); } };//class TestApp:public VulkanApplicationFramework diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index 624dbe8b..2be4adb2 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -157,7 +157,7 @@ public: void Resize(int,int)override { - BuildCommandBuffer(pipeline,descriptor_sets,render_obj); + BuildCommandBuffer(pipeline,descriptor_sets,render_obj); } };//class TestApp:public VulkanApplicationFramework diff --git a/example/Vulkan/texture_rect.cpp b/example/Vulkan/texture_rect.cpp index 65548c0e..4e2ab82c 100644 --- a/example/Vulkan/texture_rect.cpp +++ b/example/Vulkan/texture_rect.cpp @@ -180,7 +180,7 @@ public: void Resize(int,int)override { - BuildCommandBuffer(pipeline,descriptor_sets,render_obj); + BuildCommandBuffer(pipeline,descriptor_sets,render_obj); } };//class TestApp:public VulkanApplicationFramework diff --git a/inc/hgl/graph/Camera.h b/inc/hgl/graph/Camera.h index ef993c95..80744ca8 100644 --- a/inc/hgl/graph/Camera.h +++ b/inc/hgl/graph/Camera.h @@ -45,8 +45,10 @@ namespace hgl /** * 行走模拟像机 */ - class WalkerCamera:public Camera + struct WalkerCamera:public Camera { + Vector4f foot; //脚位置 + protected: /** @@ -55,8 +57,9 @@ namespace hgl */ void Move(const Vector4f &move_dist) { - eye += move_dist; - center += move_dist; + foot+=move_dist; + eye+=move_dist; + center+=move_dist; } /** @@ -64,8 +67,9 @@ namespace hgl * @param ang 角度 * @param axis 旋转轴 */ - void Rotate(double ang,const Vector4f &axis) + void Rotate(double ang,Vector4f axis) { + normalize(axis); center=eye+(center-eye)*rotate(hgl_ang2rad(ang),axis); } @@ -74,28 +78,29 @@ namespace hgl * @param ang 角度 * @param axis 旋转轴 */ - void WrapRotate(double ang,const Vector4f &axis) + void WrapRotate(double ang,Vector4f axis) { + normalize(axis); eye=center+(eye-center)*rotate(hgl_ang2rad(ang),axis); } public: //方法 - virtual void Backward(float step=0.01){Move(forward_vector*step);} ///<向后 + virtual void Backward(float step=0.01) ///<向后 + { + Move(cross(cross(center-eye,up_vector),up_vector)*step); + } virtual void Forward(float step=0.01){Backward(-step);} ///<向前 - virtual void Up(float step=0.01){Move(up_vector*step);} ///<向上 + virtual void Up(float step=0.01){Move(length(eye,center)*step*up_vector);} ///<向上 virtual void Down(float step=0.01){Up(-step);} ///<向下 - virtual void Right(float step=0.01){Move(right_vector);} ///<向右 + virtual void Right(float step=0.01){Move(cross(center-eye,up_vector)*step);} ///<向右 virtual void Left(float step=0.01){Right(-step);} ///<向左 public: //以自身为中心旋转 - virtual void RightDumping(float ang=5){Rotate(ang,forward_vector);} ///<右倾倒 - virtual void LeftDumping(float ang=5){RightDumping(-ang);} ///<左倾倒 - - virtual void LeaningBack(float ang=5){Rotate(ang,right_vector);} ///<后仰 + virtual void LeaningBack(float ang=5){Rotate(ang,cross(center-eye,up_vector));} ///<后仰 virtual void ForwardTilt(float ang=5){LeaningBack(-ang);} ///<前倾 virtual void RightRotate(float ang=5){Rotate(ang,up_vector);} ///<右转 @@ -103,7 +108,7 @@ namespace hgl public: //以目标点为中心旋转 - virtual void WrapUpRotate(float ang=5){WrapRotate(ang,right_vector);} ///<以目标点为中心向上旋转 + virtual void WrapUpRotate(float ang=5){WrapRotate(ang,cross(center-eye,up_vector));} ///<以目标点为中心向上旋转 virtual void WrapDownRotate(float ang=5){WrapUpRotate(-ang);} ///<以目标点为中心向下旋转 virtual void WrapRightRotate(float ang=5){WrapRotate(ang,up_vector);} ///<以目标点为中心向右旋转 @@ -117,7 +122,7 @@ namespace hgl eye=center+(eye-center)*pos; } - };//class WalkerCamera + };//struct WalkerCamera }//namespace graph }//namespace hgl #endif//HGL_GRAPH_CAMERA_INCLUDE diff --git a/src/Platform/Win/WinMessage.cpp b/src/Platform/Win/WinMessage.cpp index 9c07bc64..50ccd15c 100644 --- a/src/Platform/Win/WinMessage.cpp +++ b/src/Platform/Win/WinMessage.cpp @@ -149,7 +149,8 @@ namespace hgl KeyboardButton ConvertOSKey(uint key) { - if(key=kbEndRange)return(kbBeginRange); + if(key>=256)return(kbBeginRange); + if(KeyConvert[key]==0)return(kbBeginRange); if(key==VK_SHIFT) {