增加新的ViewModelFramework专用于模型查看

This commit is contained in:
hyzboy 2019-06-16 02:58:12 +08:00
parent a4859f9ed3
commit edca9e7e67
6 changed files with 179 additions and 89 deletions

View File

@ -28,7 +28,7 @@ namespace
const UTF8String ext_name=filesystem::ClipFileExtName(filename);
#endif//
const aiScene *scene=aiImportFileFromMemory(filedata,filesize,aiProcessPreset_TargetRealtime_MaxQuality|aiProcess_FlipUVs,ext_name.c_str());
const aiScene *scene=aiImportFileFromMemory(filedata,filesize,aiProcessPreset_TargetRealtime_MaxQuality|aiProcess_FlipUVs|aiProcess_FlipWindingOrder|aiProcess_Triangulate,ext_name.c_str());
delete[] filedata;
@ -46,9 +46,9 @@ namespace
const Color4f COLOR_PURE_BLACK(0,0,0,1);
const Color4f COLOR_PURE_WHITE(1,1,1,1);
inline const vec pos_to_vec(const aiVector3D &v3d){return vec(v3d.x,-v3d.y,v3d.z,1.0);}
inline const vec pos_to_vec(const aiVector3D &v3d){return vec(v3d.x,v3d.y,v3d.z,1.0);}
void VecY2Z(List<Vector3f> &target,const aiVector3D *source,const uint count)
void VecFlipY(List<Vector3f> &target,const aiVector3D *source,const uint count)
{
target.SetCount(count);
@ -182,13 +182,13 @@ private:
aiVector3D tmp = mesh->mVertices[t];
aiTransformVecByMatrix4(&tmp,&cur_matrix);
min_pos->x = aisgl_min(min_pos->x,tmp.x);
min_pos->y = aisgl_min(min_pos->y,tmp.y);
min_pos->z = aisgl_min(min_pos->z,tmp.z);
min_pos->x = aisgl_min(min_pos->x, tmp.x);
min_pos->y = aisgl_min(min_pos->y,-tmp.y);
min_pos->z = aisgl_min(min_pos->z, tmp.z);
max_pos->x = aisgl_max(max_pos->x,tmp.x);
max_pos->y = aisgl_max(max_pos->y,tmp.y);
max_pos->z = aisgl_max(max_pos->z,tmp.z);
max_pos->x = aisgl_max(max_pos->x, tmp.x);
max_pos->y = aisgl_max(max_pos->y,-tmp.y);
max_pos->z = aisgl_max(max_pos->z, tmp.z);
}
}
@ -218,26 +218,8 @@ public:
GetBoundingBox(scene->mRootNode,&scene_min,&scene_max);
float tmp;
vec smin=pos_to_vec(scene_min);
vec smax=pos_to_vec(scene_max);
if(smin.y>smax.y)
{
tmp=smin.y;
smin.y=smax.y;
smax.y=tmp;
}
if(smin.z>smax.z)
{
tmp=smin.z;
smin.z=smax.z;
smax.z=tmp;
}
model_data->bounding_box.minPoint=smin;
model_data->bounding_box.maxPoint=smax;
model_data->bounding_box.minPoint=pos_to_vec(scene_min);
model_data->bounding_box.maxPoint=pos_to_vec(scene_max);
}
~AssimpLoaderMesh()=default;
@ -253,11 +235,11 @@ private:
md->name=mesh->mName.C_Str();
if(mesh->HasPositions())
VecY2Z(md->position,mesh->mVertices,mesh->mNumVertices);
VecFlipY(md->position,mesh->mVertices,mesh->mNumVertices);
if(mesh->HasNormals())
{
VecY2Z(md->normal,mesh->mNormals,mesh->mNumVertices);
VecFlipY(md->normal,mesh->mNormals,mesh->mNumVertices);
if(mesh->HasTangentsAndBitangents())
{

View File

@ -1,7 +1,7 @@
// 5.LoadModel
// 加载纯色无贴图模型
#include"VulkanAppFramework.h"
#include"ViewModelFramework.h"
#include<hgl/filesystem/FileSystem.h>
#include<hgl/graph/InlineGeometry.h>
#include<hgl/graph/RenderableInstance.h>
@ -76,13 +76,10 @@ vulkan::Renderable *CreateMeshRenderable(SceneDB *db,vulkan::Material *mtl,const
return(render_obj);
}
class TestApp:public CameraAppFramework
class TestApp:public ViewModelFramework
{
private:
SceneNode render_root;
RenderList render_list;
vulkan::Material * material =nullptr;
vulkan::DescriptorSets * descriptor_sets =nullptr;
@ -105,22 +102,6 @@ public:
private:
void InitCamera()
{
math::vec center_point=model_data->bounding_box.CenterPoint();
math::vec min_point=model_data->bounding_box.minPoint;
min_point.x*=2.0f;
min_point.y=center_point.y;
min_point.z=center_point.z;
camera.type=CameraType::Perspective;
camera.center=center_point;
camera.eye=min_point;
camera.Refresh(); //更新矩阵计算
}
bool InitMaterial()
{
material=shader_manage->CreateMaterial(OS_TEXT("OnlyPosition3D.vert.spv"),
@ -213,17 +194,6 @@ private:
}
}
bool InitScene()
{
CreateSceneNode(&render_root,model_data->root_node);
render_root.RefreshMatrix();
render_root.ExpendToList(&render_list);
BuildCommandBuffer(&render_list);
return(true);
}
public:
bool Init(ModelData *md)
@ -233,14 +203,12 @@ public:
model_data=md;
if(!CameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
if(!ViewModelFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT,model_data->bounding_box))
return(false);
if(!InitMaterial())
return(false);
InitCamera();
if(!InitUBO())
return(false);
@ -248,18 +216,12 @@ public:
return(false);
CreateRenderObject();
if(!InitScene())
return(false);
CreateSceneNode(&render_root,model_data->root_node);
return(true);
}
void Resize(int,int)override
{
BuildCommandBuffer(&render_list);
}
};//class TestApp:public CameraAppFramework
};//class TestApp:public ViewModelFramework
#ifdef _WIN32
int wmain(int argc,wchar_t **argv)

View File

@ -164,8 +164,10 @@ public:
BuildCommandBuffer(&render_list);
}
void Resize(int,int)override
void Resize(int w,int h)override
{
CameraAppFramework::Resize(w,h);
BuildCommandBuffer(&render_list);
}
};//class TestApp:public CameraAppFramework

View File

@ -0,0 +1,142 @@
#pragma once
#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;
Matrix4f view_model_matrix;
ControlCamera camera;
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;
view_model_matrix=identity();
InitCamera(w,h);
return(true);
}
virtual void InitCamera(int w,int h)
{
math::vec center_point=bounding_box.CenterPoint();
math::vec max_point=bounding_box.maxPoint;
max_point.x*=5.0f;
max_point.y=center_point.y;
max_point.z=center_point.z;
camera.type=CameraType::Perspective;
camera.width=w;
camera.height=h;
camera.center=center_point;
camera.eye=max_point;
camera.Refresh(); //更新矩阵计算
}
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
{
camera.Refresh(); //更新相机矩阵
ubo_world_matrix->Write(&camera.matrix); //写入缓冲区
render_root.RefreshMatrix(&view_model_matrix);
render_list.Clear();
render_root.ExpendToList(&render_list);
BuildCommandBuffer(&render_list);
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[kbR])camera.Up (move_speed);else
if(key_status[kbF])camera.Down (move_speed);else
if(key_status[kbLeft ])view_model_matrix=rotate(hgl_ang2rad(move_speed*10),camera.forward_vector)*view_model_matrix;else
if(key_status[kbRight])view_model_matrix=rotate(hgl_ang2rad(-move_speed*10),camera.forward_vector)*view_model_matrix;else
return;
}
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
{
if(!(mouse_key&(mbLeft|mbRight)))return;
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);
}
else
{
if(gap.x!=0)view_model_matrix=rotate(hgl_ang2rad(gap.x),camera.up_vector)*view_model_matrix;
if(gap.y!=0)view_model_matrix=rotate(hgl_ang2rad(gap.y),camera.right_vector)*view_model_matrix;
}
mouse_last_pos=mouse_pos;
}
virtual void MouseWheel(int v,int h,uint)
{
camera.Distance(1+(v/1000.0f));
}
};//class CameraAppFramework

View File

@ -235,29 +235,30 @@ public:
{
if(!VulkanApplicationFramework::Init(w,h))
return(false);
InitCamera();
InitCamera(w,h);
return(true);
}
virtual void InitCamera()
virtual void InitCamera(int w,int h)
{
camera.type=CameraType::Perspective;
camera.width=w;
camera.height=h;
camera.center.Set(0,0,0,1);
camera.eye.Set(100,100,100,1);
camera.Refresh(); //更新矩阵计算
}
void Resize(int w,int h)override
{
camera.width=w;
camera.height=h;
}
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)
@ -306,8 +307,8 @@ public:
Vector2f gap=mouse_pos-mouse_last_pos;
bool update=false;
if(gap.x!=0){update=true;if(mouse_key&mbLeft)camera.HorzRotate(-gap.x);else camera.WrapHorzRotate(gap.x);}
if(gap.y!=0){update=true;if(mouse_key&mbLeft)camera.VertRotate(-gap.y);else camera.WrapVertRotate(gap.y);}
if(gap.x!=0){update=true;if(mouse_key&mbLeft)camera.HorzRotate(-gap.x/10.0f);else camera.WrapHorzRotate(gap.x);}
if(gap.y!=0){update=true;if(mouse_key&mbLeft)camera.VertRotate(-gap.y/10.0f);else camera.WrapVertRotate(gap.y);}
mouse_last_pos=mouse_pos;
}

View File

@ -85,7 +85,8 @@ void Device::RecreateDevice()
}
texture_cmd_buf=CreateCommandBuffer();
current_frame=0;
current_fence=0;
}