Assimp模型导入不在手动变动数据,改为在根矩阵增加旋转变换使用变换为Vulkan坐标系

This commit is contained in:
hyzboy 2019-06-24 20:18:01 +08:00
parent 10c047a975
commit e989a62d9c
5 changed files with 102 additions and 61 deletions

View File

@ -58,9 +58,9 @@ namespace
for(uint i=0;i<count;i++) for(uint i=0;i<count;i++)
{ {
tp->x= source->x; tp->x=source->x;
tp->y=-source->y; tp->y=source->y;
tp->z= source->z; tp->z=source->z;
++source; ++source;
++tp; ++tp;
@ -143,7 +143,7 @@ namespace
return AABB(POINT_VEC(minPoint),POINT_VEC(maxPoint)); return AABB(POINT_VEC(minPoint),POINT_VEC(maxPoint));
} }
Matrix4f MatrixRotate(const aiMatrix4x4 &s) Matrix4f MatrixConvert(const aiMatrix4x4 &s)
{ {
return Matrix4f(s.a1,s.a2,s.a3,s.a4, return Matrix4f(s.a1,s.a2,s.a3,s.a4,
s.b1,s.b2,s.b3,s.b4, s.b1,s.b2,s.b3,s.b4,
@ -159,19 +159,19 @@ class AssimpLoaderMesh
ModelData *model_data; ModelData *model_data;
Matrix4f OpenGLCoord2VulkanCoordMatrix;
private: private:
void GetBoundingBox(const aiNode * node, void GetBoundingBox(const aiNode * node,
aiVector3D *min_pos, aiVector3D *min_pos,
aiVector3D *max_pos, aiVector3D *max_pos,
const aiMatrix4x4 &up_matrix) const Matrix4f &up_matrix)
{ {
aiMatrix4x4 cur_matrix; Matrix4f cur_matrix;
uint n = 0, t; uint n = 0, t;
cur_matrix = up_matrix; cur_matrix=up_matrix*MatrixConvert(node->mTransformation);
aiMultiplyMatrix4(&cur_matrix,&node->mTransformation);
for (; n < node->mNumMeshes; ++n) for (; n < node->mNumMeshes; ++n)
{ {
@ -179,16 +179,20 @@ private:
for (t = 0; t < mesh->mNumVertices; ++t) for (t = 0; t < mesh->mNumVertices; ++t)
{ {
aiVector3D tmp = mesh->mVertices[t]; aiVector3D gl_tmp = mesh->mVertices[t];
aiTransformVecByMatrix4(&tmp,&cur_matrix);
Vector4f tmp=cur_matrix*Vector4f(gl_tmp.x,
gl_tmp.y,
gl_tmp.z,
1.0f);
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); max_pos->x = aisgl_max(max_pos->x,tmp.x);
min_pos->y = aisgl_min(min_pos->y,-tmp.y); max_pos->y = aisgl_max(max_pos->y,tmp.y);
min_pos->z = aisgl_min(min_pos->z, tmp.z); 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);
} }
} }
@ -198,9 +202,7 @@ private:
void AssimpLoaderMesh::GetBoundingBox(const aiNode *node,aiVector3D *min_pos,aiVector3D *max_pos) void AssimpLoaderMesh::GetBoundingBox(const aiNode *node,aiVector3D *min_pos,aiVector3D *max_pos)
{ {
aiMatrix4x4 root_matrix; Matrix4f root_matrix=OpenGLCoord2VulkanCoordMatrix;
aiIdentityMatrix4(&root_matrix);
min_pos->x = min_pos->y = min_pos->z = 1e10f; min_pos->x = min_pos->y = min_pos->z = 1e10f;
max_pos->x = max_pos->y = max_pos->z = -1e10f; max_pos->x = max_pos->y = max_pos->z = -1e10f;
@ -212,6 +214,8 @@ public:
AssimpLoaderMesh(const OSString &fn,const aiScene *s):filename(fn),scene(s) AssimpLoaderMesh(const OSString &fn,const aiScene *s):filename(fn),scene(s)
{ {
OpenGLCoord2VulkanCoordMatrix=rotate(hgl_ang2rad(90),Vector3f(0,0,1))*rotate(hgl_ang2rad(90),Vector3f(1,0,0));
model_data=new ModelData; model_data=new ModelData;
aiVector3D scene_min,scene_max; aiVector3D scene_min,scene_max;
@ -288,7 +292,7 @@ private:
bool Load(ModelSceneNode *msn,const aiNode *node) bool Load(ModelSceneNode *msn,const aiNode *node)
{ {
msn->name=node->mName.C_Str(); msn->name=node->mName.C_Str();
msn->local_matrix=MatrixRotate(node->mTransformation); msn->local_matrix=MatrixConvert(node->mTransformation);
if(node->mNumMeshes>0) if(node->mNumMeshes>0)
{ {
@ -321,7 +325,11 @@ public:
{ {
model_data->root_node=new ModelSceneNode; model_data->root_node=new ModelSceneNode;
return Load(model_data->root_node,scene->mRootNode); if(!Load(model_data->root_node,scene->mRootNode))
return(false);
model_data->root_node->local_matrix=OpenGLCoord2VulkanCoordMatrix*model_data->root_node->local_matrix;
return(true);
} }
};//class AssimpLoaderMesh };//class AssimpLoaderMesh

View File

@ -1,4 +1,4 @@
// 8.大气渲染 // 8.大气渲染
// 画一个球纯粹使用shader计算出颜色 // 画一个球纯粹使用shader计算出颜色
#include"VulkanAppFramework.h" #include"VulkanAppFramework.h"

View File

@ -1,4 +1,4 @@
// 7.InlineGeometryScene // 7.InlineGeometryScene
// 全内置几何体场景 // 全内置几何体场景
#include"VulkanAppFramework.h" #include"VulkanAppFramework.h"

View File

@ -83,7 +83,8 @@ private:
vulkan::Material * material =nullptr; vulkan::Material * material =nullptr;
vulkan::DescriptorSets * descriptor_sets =nullptr; vulkan::DescriptorSets * descriptor_sets =nullptr;
vulkan::Pipeline * pipeline_line =nullptr; vulkan::Pipeline * pipeline_wireframe =nullptr;
vulkan::Pipeline * pipeline_lines =nullptr;
private: private:
@ -92,13 +93,11 @@ private:
vulkan::Renderable **mesh_renderable; vulkan::Renderable **mesh_renderable;
RenderableInstance **mesh_renderable_instance; RenderableInstance **mesh_renderable_instance;
public: vulkan::Renderable *axis_renderable;
RenderableInstance *axis_renderable_instance;
~TestApp() vulkan::Renderable *bbox_renderable;
{ RenderableInstance *bbox_renderable_instance;
delete[] mesh_renderable_instance;
delete[] mesh_renderable;
}
private: private:
@ -127,9 +126,29 @@ private:
for(uint i=0;i<count;i++) for(uint i=0;i<count;i++)
{ {
mesh_renderable[i]=CreateMeshRenderable(db,material,*md); mesh_renderable[i]=CreateMeshRenderable(db,material,*md);
mesh_renderable_instance[i]=db->CreateRenderableInstance(pipeline_line,descriptor_sets,mesh_renderable[i]); mesh_renderable_instance[i]=db->CreateRenderableInstance(pipeline_wireframe,descriptor_sets,mesh_renderable[i]);
++md; ++md;
} }
{
AxisCreateInfo aci;
aci.root=model_data->bounding_box.CenterPoint().xyz();
aci.size=model_data->bounding_box.HalfSize().xyz();
axis_renderable=CreateRenderableAxis(db,material,&aci);
axis_renderable_instance=db->CreateRenderableInstance(pipeline_lines,descriptor_sets,axis_renderable);
}
{
CubeCreateInfo cci;
cci.center=model_data->bounding_box.CenterPoint().xyz();
cci.size=model_data->bounding_box.Size().xyz();
bbox_renderable=CreateRenderableBoundingBox(db,material,&cci);
bbox_renderable_instance=db->CreateRenderableInstance(pipeline_lines,descriptor_sets,bbox_renderable);
}
} }
bool InitUBO() bool InitUBO()
@ -153,16 +172,23 @@ private:
pipeline_creater->CloseCullFace(); pipeline_creater->CloseCullFace();
pipeline_creater->Set(PRIM_TRIANGLES); pipeline_creater->Set(PRIM_TRIANGLES);
pipeline_line=pipeline_creater->Create(); pipeline_wireframe=pipeline_creater->Create();
if(!pipeline_line)
return(false);
db->Add(pipeline_line); if(pipeline_wireframe)
db->Add(pipeline_wireframe);
pipeline_creater->SetPolygonMode(VK_POLYGON_MODE_FILL);
pipeline_creater->Set(PRIM_LINES);
pipeline_lines=pipeline_creater->Create();
if(pipeline_lines)
db->Add(pipeline_lines);
delete pipeline_creater; delete pipeline_creater;
} }
return pipeline_line; return pipeline_wireframe;
} }
void CreateSceneNode(SceneNode *scene_node,ModelSceneNode *model_node) void CreateSceneNode(SceneNode *scene_node,ModelSceneNode *model_node)
@ -216,8 +242,11 @@ public:
return(false); return(false);
CreateRenderObject(); CreateRenderObject();
CreateSceneNode(&render_root,model_data->root_node); render_root.Add(axis_renderable_instance);
render_root.Add(bbox_renderable_instance);
CreateSceneNode(render_root.CreateSubNode(),model_data->root_node);
return(true); return(true);
} }

View File

@ -1,4 +1,4 @@
#pragma once #pragma once
#include"VulkanAppFramework.h" #include"VulkanAppFramework.h"
@ -15,7 +15,7 @@ protected:
AABB bounding_box; AABB bounding_box;
Matrix4f view_model_matrix; Matrix4f object_matrix;
ControlCamera camera; ControlCamera camera;
float move_speed=1; float move_speed=1;
@ -38,8 +38,6 @@ public:
bounding_box=aabb; bounding_box=aabb;
view_model_matrix=identity();
InitCamera(w,h); InitCamera(w,h);
return(true); return(true);
} }
@ -47,21 +45,25 @@ public:
virtual void InitCamera(int w,int h) virtual void InitCamera(int w,int h)
{ {
math::vec center_point=bounding_box.CenterPoint(); math::vec center_point=bounding_box.CenterPoint();
math::vec min_point=bounding_box.minPoint;
math::vec max_point=bounding_box.maxPoint; math::vec max_point=bounding_box.maxPoint;
max_point.x*=5.0f; math::vec eye( max_point.x*5,
max_point.y=center_point.y; center_point.y,
max_point.z=center_point.z; center_point.z,
1.0f);
camera.type=CameraType::Perspective; camera.type =CameraType::Perspective;
camera.width=w; camera.width =w;
camera.height=h; camera.height =h;
camera.center=center_point; camera.center =center_point;
camera.eye=max_point; camera.eye =eye;
camera.Refresh(); //更新矩阵计算 camera.Refresh(); //更新矩阵计算
move_speed=length(max_point,center_point)/100.0f; move_speed=length(min_point,center_point)/100.0f;
object_matrix=translate(-center_point.xyz());
} }
bool InitCameraUBO(vulkan::DescriptorSets *desc_set,uint world_matrix_bindpoint) bool InitCameraUBO(vulkan::DescriptorSets *desc_set,uint world_matrix_bindpoint)
@ -84,10 +86,10 @@ public:
{ {
const uint32_t index=AcquireNextImage(); const uint32_t index=AcquireNextImage();
camera.Refresh(); //更新相机矩阵 camera.Refresh(); //更新相机矩阵
ubo_world_matrix->Write(&camera.matrix); //写入缓冲区 ubo_world_matrix->Write(&camera.matrix); //写入缓冲区
render_root.RefreshMatrix(&view_model_matrix); render_root.RefreshMatrix(&object_matrix);
render_list.Clear(); render_list.Clear();
render_root.ExpendToList(&render_list); render_root.ExpendToList(&render_list);
@ -102,8 +104,10 @@ public:
if(key_status[kbR])camera.Up (move_speed);else if(key_status[kbR])camera.Up (move_speed);else
if(key_status[kbF])camera.Down (move_speed);else if(key_status[kbF])camera.Down (move_speed);else
if(key_status[kbLeft ])view_model_matrix=rotate(hgl_ang2rad(move_speed*100),normalized(camera.center-camera.eye))*view_model_matrix;else // if(key_status[kbLeft ])object_matrix=rotate(hgl_ang2rad(move_speed),normalized(camera.center-camera.eye))*object_matrix;else
if(key_status[kbRight])view_model_matrix=rotate(hgl_ang2rad(-move_speed*100),normalized(camera.center-camera.eye))*view_model_matrix;else //if(key_status[kbRight])object_matrix=rotate(hgl_ang2rad(-move_speed),normalized(camera.center-camera.eye))*object_matrix;else
if(key_status[kbLeft ])camera.WrapHorzRotate(move_speed);else
if(key_status[kbRight])camera.WrapHorzRotate(-move_speed);else
return; return;
} }
@ -132,8 +136,8 @@ public:
} }
else if(mouse_key&mbRight) else if(mouse_key&mbRight)
{ {
if(gap.x!=0)view_model_matrix=rotate(hgl_ang2rad(gap.x),camera.up_vector)*view_model_matrix; if(gap.x!=0)object_matrix=rotate(hgl_ang2rad(gap.x),camera.up_vector)*object_matrix;
if(gap.y!=0)view_model_matrix=rotate(hgl_ang2rad(gap.y),camera.right_vector)*view_model_matrix; if(gap.y!=0)object_matrix=rotate(hgl_ang2rad(-gap.y),camera.forward_vector)*object_matrix;
} }
else if(mouse_key&mbMid) else if(mouse_key&mbMid)
{ {