增加WalkerCameraAppFramework,添加相机控制

This commit is contained in:
hyzboy 2019-06-14 17:13:30 +08:00
parent 734c428da2
commit f8569ed513
11 changed files with 159 additions and 113 deletions

View File

@ -34,7 +34,7 @@ private:
vulkan::Material * material =nullptr; vulkan::Material * material =nullptr;
vulkan::DescriptorSets * descriptor_sets =nullptr; vulkan::DescriptorSets * descriptor_sets =nullptr;
vulkan::Renderable *ro_rectangle =nullptr, vulkan::Renderable *ro_rectangle =nullptr,
*ro_circle =nullptr, *ro_circle =nullptr,
*ro_round_rectangle =nullptr; *ro_round_rectangle =nullptr;
@ -111,7 +111,7 @@ private:
descriptor_sets->Update(); descriptor_sets->Update();
return(true); return(true);
} }
bool InitPipeline() bool InitPipeline()
{ {
constexpr os_char PIPELINE_FILENAME[]=OS_TEXT("2DSolid.pipeline"); constexpr os_char PIPELINE_FILENAME[]=OS_TEXT("2DSolid.pipeline");
@ -157,7 +157,7 @@ public:
if(!InitUBO()) if(!InitUBO())
return(false); return(false);
if(!InitPipeline()) if(!InitPipeline())
return(false); return(false);
@ -169,7 +169,7 @@ public:
void Resize(int,int)override void Resize(int,int)override
{ {
BuildCommandBuffer(&render_list); BuildCommandBuffer(&render_list);
} }
};//class TestApp:public VulkanApplicationFramework };//class TestApp:public VulkanApplicationFramework

View File

@ -13,37 +13,22 @@ using namespace hgl::graph;
constexpr uint32_t SCREEN_WIDTH=128; constexpr uint32_t SCREEN_WIDTH=128;
constexpr uint32_t SCREEN_HEIGHT=128; constexpr uint32_t SCREEN_HEIGHT=128;
class TestApp:public VulkanApplicationFramework class TestApp:public WalkerCameraAppFramework
{ {
private: private:
SceneNode render_root; SceneNode render_root;
RenderList render_list; RenderList render_list;
Camera camera;
vulkan::Material * material =nullptr; vulkan::Material * material =nullptr;
vulkan::DescriptorSets * descriptor_sets =nullptr; vulkan::DescriptorSets * descriptor_sets =nullptr;
vulkan::Renderable *ro_plane_grid[3]; vulkan::Renderable *ro_plane_grid[3];
vulkan::Buffer * ubo_world_matrix =nullptr;
vulkan::Pipeline * pipeline_line =nullptr; vulkan::Pipeline * pipeline_line =nullptr;
private: 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() bool InitMaterial()
{ {
material=shader_manage->CreateMaterial(OS_TEXT("PositionColor3D.vert.spv"), material=shader_manage->CreateMaterial(OS_TEXT("PositionColor3D.vert.spv"),
@ -90,14 +75,7 @@ private:
bool InitUBO() bool InitUBO()
{ {
const VkExtent2D extent=device->GetExtent(); if(!InitCameraUBO(descriptor_sets,material->GetUBO("world")))
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))
return(false); return(false);
descriptor_sets->Update(); descriptor_sets->Update();
@ -147,11 +125,9 @@ public:
bool Init() bool Init()
{ {
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT)) if(!WalkerCameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false); return(false);
InitCamera();
if(!InitMaterial()) if(!InitMaterial())
return(false); return(false);
@ -171,9 +147,9 @@ public:
void Resize(int,int)override void Resize(int,int)override
{ {
BuildCommandBuffer(&render_list); BuildCommandBuffer(&render_list);
} }
};//class TestApp:public VulkanApplicationFramework };//class TestApp:public WalkerCameraAppFramework
int main(int,char **) int main(int,char **)
{ {

View File

@ -76,20 +76,16 @@ vulkan::Renderable *CreateMeshRenderable(SceneDB *db,vulkan::Material *mtl,const
return(render_obj); return(render_obj);
} }
class TestApp:public VulkanApplicationFramework class TestApp:public WalkerCameraAppFramework
{ {
private: private:
SceneNode render_root; SceneNode render_root;
RenderList render_list; RenderList render_list;
Camera camera;
vulkan::Material * material =nullptr; vulkan::Material * material =nullptr;
vulkan::DescriptorSets * descriptor_sets =nullptr; vulkan::DescriptorSets * descriptor_sets =nullptr;
vulkan::Buffer * ubo_world_matrix =nullptr;
vulkan::Pipeline * pipeline_line =nullptr; vulkan::Pipeline * pipeline_line =nullptr;
private: private:
@ -121,8 +117,6 @@ private:
camera.type=CameraType::Perspective; camera.type=CameraType::Perspective;
camera.center=center_point; camera.center=center_point;
camera.eye=min_point; camera.eye=min_point;
camera.width=SCREEN_WIDTH;
camera.height=SCREEN_HEIGHT;
camera.Refresh(); //更新矩阵计算 camera.Refresh(); //更新矩阵计算
} }
@ -159,14 +153,7 @@ private:
bool InitUBO() bool InitUBO()
{ {
const VkExtent2D extent=device->GetExtent(); if(!InitCameraUBO(descriptor_sets,material->GetUBO("world")))
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))
return(false); return(false);
descriptor_sets->Update(); descriptor_sets->Update();
@ -246,7 +233,7 @@ public:
model_data=md; model_data=md;
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT)) if(!WalkerCameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false); return(false);
if(!InitMaterial()) if(!InitMaterial())
@ -270,9 +257,9 @@ public:
void Resize(int,int)override void Resize(int,int)override
{ {
BuildCommandBuffer(&render_list); BuildCommandBuffer(&render_list);
} }
};//class TestApp:public VulkanApplicationFramework };//class TestApp:public WalkerCameraAppFramework
#ifdef _WIN32 #ifdef _WIN32
int wmain(int argc,wchar_t **argv) int wmain(int argc,wchar_t **argv)

View File

@ -1,5 +1,5 @@
// 5.SceneTree // 5.SceneTree
// 用于测试树形排列的场景中,每一级节点对变换矩阵的处理是否正确 // 用于测试树形排列的场景中,每一级节点对变换矩阵的处理是否正确
#include"VulkanAppFramework.h" #include"VulkanAppFramework.h"
#include<hgl/filesystem/FileSystem.h> #include<hgl/filesystem/FileSystem.h>
@ -15,7 +15,7 @@ using namespace hgl::graph;
constexpr uint32_t SCREEN_WIDTH=128; constexpr uint32_t SCREEN_WIDTH=128;
constexpr uint32_t SCREEN_HEIGHT=128; constexpr uint32_t SCREEN_HEIGHT=128;
class TestApp:public VulkanApplicationFramework class TestApp:public WalkerCameraAppFramework
{ {
private: private:
@ -24,15 +24,11 @@ private:
SceneNode render_root; SceneNode render_root;
RenderList render_list; RenderList render_list;
Camera camera;
vulkan::Material * material =nullptr; vulkan::Material * material =nullptr;
vulkan::DescriptorSets * descriptor_sets =nullptr; vulkan::DescriptorSets * descriptor_sets =nullptr;
vulkan::Renderable * ro_cube =nullptr; vulkan::Renderable * ro_cube =nullptr;
vulkan::Buffer * ubo_world_matrix =nullptr;
vulkan::Pipeline * pipeline_line =nullptr; vulkan::Pipeline * pipeline_line =nullptr;
public: public:
@ -46,17 +42,6 @@ public:
private: 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() bool InitMaterial()
{ {
material=shader_manage->CreateMaterial(OS_TEXT("OnlyPosition3D.vert.spv"), material=shader_manage->CreateMaterial(OS_TEXT("OnlyPosition3D.vert.spv"),
@ -80,14 +65,7 @@ private:
bool InitUBO() bool InitUBO()
{ {
const VkExtent2D extent=device->GetExtent(); if(!InitCameraUBO(descriptor_sets,material->GetUBO("world")))
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))
return(false); return(false);
descriptor_sets->Update(); descriptor_sets->Update();
@ -130,7 +108,7 @@ private:
for(uint i=0;i<360;i++) for(uint i=0;i<360;i++)
{ {
size=(i+1)/100.0f; size=(i+1)/100.0f;
cur_node=render_root.CreateSubNode( rotate(i/5.0f,camera.up_vector)* cur_node=render_root.CreateSubNode( rotate(i/5.0f,camera.up_vector)*
translate(i/4.0f,0,0)* translate(i/4.0f,0,0)*
scale(size)); scale(size));
@ -153,11 +131,9 @@ public:
bool Init() bool Init()
{ {
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT)) if(!WalkerCameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false); return(false);
InitCamera();
if(!InitMaterial()) if(!InitMaterial())
return(false); return(false);
@ -177,7 +153,7 @@ public:
void Draw() override void Draw() override
{ {
VulkanApplicationFramework::Draw(); WalkerCameraAppFramework::Draw();
Matrix4f rot=rotate(GetDoubleTime()-start_time,camera.up_vector); Matrix4f rot=rotate(GetDoubleTime()-start_time,camera.up_vector);
@ -190,7 +166,7 @@ public:
void Resize(int,int)override void Resize(int,int)override
{ {
BuildCommandBuffer(&render_list); BuildCommandBuffer(&render_list);
} }
};//class TestApp:public VulkanApplicationFramework };//class TestApp:public VulkanApplicationFramework

View File

@ -100,7 +100,7 @@ Texture2D *LoadTGATexture(const OSString &filename,Device *device)
{ {
if(header->bit==24) if(header->bit==24)
{ {
RGB8to565(pixel_data,header->width*header->height); RGB8to565(pixel_data,header->width*header->height);
format=FMT_RGB565; format=FMT_RGB565;
line_size=header->width*2; line_size=header->width*2;

View File

@ -28,6 +28,16 @@ private:
Window * win =nullptr; Window * win =nullptr;
vulkan::Instance * inst =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: protected:
vulkan::Device * device =nullptr; vulkan::Device * device =nullptr;
@ -38,11 +48,13 @@ private:
uint32_t swap_chain_count=0; uint32_t swap_chain_count=0;
vulkan::CommandBuffer ** cmd_buf =nullptr; vulkan::CommandBuffer ** cmd_buf =nullptr;
protected: protected:
SceneDB * db =nullptr; SceneDB * db =nullptr;
bool key_status[kbRangeSize];
public: public:
virtual ~VulkanApplicationFramework() virtual ~VulkanApplicationFramework()
@ -55,8 +67,10 @@ public:
SAFE_CLEAR(inst); SAFE_CLEAR(inst);
} }
virtual bool Init(int w,int h) virtual bool Init(int w,int h)
{ {
hgl_zero(key_status);
#ifdef _DEBUG #ifdef _DEBUG
if(!vulkan::CheckStrideBytesByFormat()) if(!vulkan::CheckStrideBytesByFormat())
return(false); return(false);
@ -86,18 +100,16 @@ public:
InitCommandBuffer(); 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); return(true);
} }
virtual void Resize(int,int)=0; virtual void Resize(int,int)=0;
virtual void KeyPress(KeyboardButton){}
void OnResize(int w,int h)
{
InitCommandBuffer();
Resize(w,h);
}
void InitCommandBuffer() void InitCommandBuffer()
{ {
@ -193,3 +205,92 @@ public:
return(true); return(true);
} }
};//class VulkanApplicationFramework };//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

View File

@ -135,15 +135,15 @@ public:
if(!InitPipeline()) if(!InitPipeline())
return(false); return(false);
BuildCommandBuffer(pipeline,descriptor_sets,render_obj); BuildCommandBuffer(pipeline,descriptor_sets,render_obj);
return(true); return(true);
} }
void Resize(int,int)override void Resize(int,int)override
{ {
BuildCommandBuffer(pipeline,descriptor_sets,render_obj); BuildCommandBuffer(pipeline,descriptor_sets,render_obj);
} }
};//class TestApp:public VulkanApplicationFramework };//class TestApp:public VulkanApplicationFramework

View File

@ -157,7 +157,7 @@ public:
void Resize(int,int)override void Resize(int,int)override
{ {
BuildCommandBuffer(pipeline,descriptor_sets,render_obj); BuildCommandBuffer(pipeline,descriptor_sets,render_obj);
} }
};//class TestApp:public VulkanApplicationFramework };//class TestApp:public VulkanApplicationFramework

View File

@ -180,7 +180,7 @@ public:
void Resize(int,int)override void Resize(int,int)override
{ {
BuildCommandBuffer(pipeline,descriptor_sets,render_obj); BuildCommandBuffer(pipeline,descriptor_sets,render_obj);
} }
};//class TestApp:public VulkanApplicationFramework };//class TestApp:public VulkanApplicationFramework

View File

@ -45,8 +45,10 @@ namespace hgl
/** /**
* *
*/ */
class WalkerCamera:public Camera struct WalkerCamera:public Camera
{ {
Vector4f foot; //脚位置
protected: protected:
/** /**
@ -55,8 +57,9 @@ namespace hgl
*/ */
void Move(const Vector4f &move_dist) void Move(const Vector4f &move_dist)
{ {
eye += move_dist; foot+=move_dist;
center += move_dist; eye+=move_dist;
center+=move_dist;
} }
/** /**
@ -64,8 +67,9 @@ namespace hgl
* @param ang * @param ang
* @param axis * @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); center=eye+(center-eye)*rotate(hgl_ang2rad(ang),axis);
} }
@ -74,28 +78,29 @@ namespace hgl
* @param ang * @param ang
* @param axis * @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); eye=center+(eye-center)*rotate(hgl_ang2rad(ang),axis);
} }
public: //方法 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 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 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);} ///<向左 virtual void Left(float step=0.01){Right(-step);} ///<向左
public: //以自身为中心旋转 public: //以自身为中心旋转
virtual void RightDumping(float ang=5){Rotate(ang,forward_vector);} ///<右倾倒 virtual void LeaningBack(float ang=5){Rotate(ang,cross(center-eye,up_vector));} ///<后仰
virtual void LeftDumping(float ang=5){RightDumping(-ang);} ///<左倾倒
virtual void LeaningBack(float ang=5){Rotate(ang,right_vector);} ///<后仰
virtual void ForwardTilt(float ang=5){LeaningBack(-ang);} ///<前倾 virtual void ForwardTilt(float ang=5){LeaningBack(-ang);} ///<前倾
virtual void RightRotate(float ang=5){Rotate(ang,up_vector);} ///<右转 virtual void RightRotate(float ang=5){Rotate(ang,up_vector);} ///<右转
@ -103,7 +108,7 @@ namespace hgl
public: //以目标点为中心旋转 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 WrapDownRotate(float ang=5){WrapUpRotate(-ang);} ///<以目标点为中心向下旋转
virtual void WrapRightRotate(float ang=5){WrapRotate(ang,up_vector);} ///<以目标点为中心向右旋转 virtual void WrapRightRotate(float ang=5){WrapRotate(ang,up_vector);} ///<以目标点为中心向右旋转
@ -117,7 +122,7 @@ namespace hgl
eye=center+(eye-center)*pos; eye=center+(eye-center)*pos;
} }
};//class WalkerCamera };//struct WalkerCamera
}//namespace graph }//namespace graph
}//namespace hgl }//namespace hgl
#endif//HGL_GRAPH_CAMERA_INCLUDE #endif//HGL_GRAPH_CAMERA_INCLUDE

View File

@ -149,7 +149,8 @@ namespace hgl
KeyboardButton ConvertOSKey(uint key) KeyboardButton ConvertOSKey(uint key)
{ {
if(key<kbBeginRange||key>=kbEndRange)return(kbBeginRange); if(key>=256)return(kbBeginRange);
if(KeyConvert[key]==0)return(kbBeginRange);
if(key==VK_SHIFT) if(key==VK_SHIFT)
{ {