SceneNode矩阵变换,以及使用PushConstants传递LocalToWorld绘制成功

This commit is contained in:
hyzboy 2019-05-29 21:48:56 +08:00
parent 3155234629
commit c4c63a7add
19 changed files with 164 additions and 161 deletions

View File

@ -31,13 +31,11 @@ private:
vulkan::Material * material =nullptr;
vulkan::DescriptorSets * descriptor_sets =nullptr;
vulkan::Renderable *ro_plane_grid =nullptr,
*ro_cube =nullptr;
vulkan::Renderable *ro_plane_grid[3];
vulkan::Buffer * ubo_mvp =nullptr;
vulkan::Buffer * ubo_world_matrix =nullptr;
vulkan::Pipeline * pipeline_line =nullptr;
vulkan::Pipeline * pipeline_triangles =nullptr;
vulkan::CommandBuffer ** cmd_buf =nullptr;
public:
@ -69,7 +67,7 @@ private:
bool InitMaterial()
{
material=shader_manage->CreateMaterial(OS_TEXT("OnlyPosition3D.vert.spv"),
material=shader_manage->CreateMaterial(OS_TEXT("PositionColor3D.vert.spv"),
OS_TEXT("FlatColor.frag.spv"));
if(!material)
return(false);
@ -94,34 +92,40 @@ private:
pgci.step.u=20;
pgci.step.v=20;
ro_plane_grid=CreatePlaneGrid(db,material,&pgci);
}
pgci.side_step.u=10;
pgci.side_step.v=10;
{
struct CubeCreateInfo cci;
pgci.color.Set(0.75,0,0,1);
pgci.side_color.Set(1,0,0,1);
cci.tile.x=0;
cci.tile.y=1;
ro_plane_grid[0]=CreatePlaneGrid(db,material,&pgci);
ro_cube=CreateCube(db,material,&cci);
pgci.color.Set(0,0.75,0,1);
pgci.side_color.Set(0,1,0,1);
ro_plane_grid[1]=CreatePlaneGrid(db,material,&pgci);
pgci.color.Set(0,0,0.75,1);
pgci.side_color.Set(0,0,1,1);
ro_plane_grid[2]=CreatePlaneGrid(db,material,&pgci);
}
}
//bool InitUBO()
//{
// const VkExtent2D extent=device->GetExtent();
bool InitUBO()
{
const VkExtent2D extent=device->GetExtent();
// ubo_mvp=db->CreateUBO(sizeof(WorldConfig),&world);
ubo_world_matrix=db->CreateUBO(sizeof(WorldMatrix),&camera.matrix);
// if(!ubo_mvp)
// return(false);
if(!ubo_world_matrix)
return(false);
// if(!descriptor_sets->BindUBO(material->GetUBO("world"),*ubo_mvp))
// return(false);
if(!descriptor_sets->BindUBO(material->GetUBO("world"),*ubo_world_matrix))
return(false);
// descriptor_sets->Update();
// return(true);
//}
descriptor_sets->Update();
return(true);
}
bool InitPipeline()
{
@ -140,14 +144,6 @@ private:
db->Add(pipeline_line);
pipeline_creater->Set(PRIM_TRIANGLES);
pipeline_triangles=pipeline_creater->Create();
if(!pipeline_triangles)
return(false);
db->Add(pipeline_triangles);
delete pipeline_creater;
}
@ -156,12 +152,11 @@ private:
bool InitScene()
{
render_root.Add(db->CreateRenderableInstance(pipeline_line,descriptor_sets,ro_plane_grid));
render_root.Add(db->CreateRenderableInstance(pipeline_triangles,descriptor_sets,ro_cube),scale(50,50,50));
//render_root.Add(db->CreateRenderableInstance(pipeline,descriptor_sets,ro_circle));
render_root.Add(db->CreateRenderableInstance(pipeline_line,descriptor_sets,ro_plane_grid[0]));
render_root.Add(db->CreateRenderableInstance(pipeline_line,descriptor_sets,ro_plane_grid[1]),rotate(90,0,1,0));
render_root.Add(db->CreateRenderableInstance(pipeline_line,descriptor_sets,ro_plane_grid[2]),rotate(90,1,0,0));
render_root.RefreshMatrix(&(camera.mvp));
render_root.RefreshMatrix();
render_root.ExpendToList(&render_list);
return(true);
@ -206,8 +201,8 @@ public:
CreateRenderObject();
// if(!InitUBO())
// return(false);
if(!InitUBO())
return(false);
if(!InitPipeline())
return(false);

View File

@ -32,9 +32,7 @@ namespace hgl
public:
Matrix4f projection;
Matrix4f modelview;
Matrix4f mvp;
WorldMatrix matrix;
Frustum frustum;

View File

@ -4,6 +4,7 @@
#include<hgl/graph/vulkan/VK.h>
#include<hgl/math/Vector.h>
#include<hgl/type/RectScope.h>
#include<hgl/type/Color4f.h>
namespace hgl
{
namespace graph
@ -50,6 +51,11 @@ namespace hgl
{
Vector3f coord[4];
vec2<uint> step;
vec2<uint> side_step; //到边界的步数
Color4f color; //一般线条颜色
Color4f side_color; //边界线条颜色
};//struct PlaneGridCreateInfo
vulkan::Renderable *CreatePlaneGrid(SceneDB *db,vulkan::Material *mtl,const PlaneGridCreateInfo *pgci);

View File

@ -11,51 +11,28 @@ namespace hgl
{
class RenderableInstance;
struct UBOMatrixData
{
Matrix4f projection;
Matrix4f modelview;
Matrix4f mvp;
Matrix3f normal;
};//
struct UBOSkyLight
{
Color4f sun_color;
Vector4f sun_direction;
};//
class RenderList
{
vulkan::CommandBuffer *cmd_buf;
private:
Camera camera;
Frustum frustum;
private:
UBOMatrixData ubo_matrix;
UBOSkyLight ubo_skylight;
private:
List<SceneNode *> scene_node_list;
vulkan::PushConstant * last_pc;
vulkan::Pipeline * last_pipeline;
vulkan::DescriptorSets *last_desc_sets;
vulkan::Renderable * last_renderable;
void Render(RenderableInstance *);
void Render(List<RenderableInstance *> &);
void Render(SceneNode *,RenderableInstance *);
void Render(SceneNode *,List<RenderableInstance *> &);
public:
RenderList()
{
cmd_buf=nullptr;
last_pc=nullptr;
last_pipeline=nullptr;
last_desc_sets=nullptr;
last_renderable=nullptr;
@ -66,15 +43,6 @@ namespace hgl
void Add (SceneNode *node) {if(node)scene_node_list.Add(node);}
void Clear () {scene_node_list.ClearData();}
void SetCamera(const Camera &);
void SetMVP(const Matrix4f &proj,const Matrix4f &mv);
void SetSkyLightColor(const Color4f &c,const Vector4f &d)
{
ubo_skylight.sun_color=c;
ubo_skylight.sun_direction=d;
}
bool Render(vulkan::CommandBuffer *);
};//class RenderList
}//namespace graph

View File

@ -3,6 +3,7 @@
//#include<hgl/type/List.h>
#include<hgl/math/Math.h>
#include<hgl/graph/vulkan/VK.h>
//#include<hgl/graph/Transform.h>
namespace hgl
{
@ -23,11 +24,15 @@ namespace hgl
Matrix4f InverseLocalMatrix; ///<反向当前矩阵
Matrix4f InverseLocalToWorldMatrix; ///<反向当前到世界矩阵
vulkan::PushConstant pc;
public:
SceneOrient();
virtual ~SceneOrient()=default;
vulkan::PushConstant *GetPushConstant(){return &pc;}
Matrix4f & SetLocalMatrix (const Matrix4f &); ///<设定当前节点矩阵
Matrix4f & SetLocalToWorldMatrix (const Matrix4f &); ///<设定当前节点到世界矩阵

View File

@ -63,6 +63,11 @@ enum class ShaderType
Compute =VK_SHADER_STAGE_COMPUTE_BIT
};//
struct PushConstant
{
Matrix4f local_to_world;
};
#ifdef _DEBUG
bool CheckStrideBytesByFormat(); ///<检验所有数据类型长度数组是否符合规则
#endif//_DEBUG

View File

@ -7,21 +7,6 @@
VK_NAMESPACE_BEGIN
//push constant 一般只有128/256字节仅能存在矩阵。
//所以我们将每个对象的独立变换矩阵存在push constant中
struct PushConstant256
{
Matrix4f projection;
Matrix4f modelview;
Matrix4f mvp;
Matrix3f normal;
};//
struct PushConstant128
{
Matrix4f projection;
Matrix4f modelview;
};//
class CommandBuffer
{
VkDevice device;
@ -112,8 +97,7 @@ public:
vkCmdPushConstants(cmd_buf,pipeline_layout,(VkShaderStageFlagBits)shader_type,offset,size,pValues);
}
void PushConstants(const PushConstant256 *pc){vkCmdPushConstants(cmd_buf,pipeline_layout,VK_SHADER_STAGE_VERTEX_BIT,0,sizeof(PushConstant256),pc);}
void PushConstants(const PushConstant128 *pc){vkCmdPushConstants(cmd_buf,pipeline_layout,VK_SHADER_STAGE_VERTEX_BIT,0,sizeof(PushConstant128),pc);}
void PushConstants(const PushConstant *pc){vkCmdPushConstants(cmd_buf,pipeline_layout,VK_SHADER_STAGE_VERTEX_BIT,0,sizeof(PushConstant),pc);}
bool Bind(Renderable *);

View File

@ -11,6 +11,16 @@ namespace hgl
using Matrix3f=float3x3;
using Matrix4f=float4x4;
struct WorldMatrix
{
Matrix4f two_dim; //2D矩阵
Matrix4f projection;
Matrix4f modelview;
Matrix4f mvp;
Matrix3f normal;
};//
inline Matrix4f identity()
{
return Matrix4f::identity;

View File

@ -5,5 +5,5 @@ layout(location = 0) out vec4 FragColor;
void main()
{
FragColor=vec4(FragmentColor.rgb,1);
FragColor=FragmentColor;
}

Binary file not shown.

View File

@ -2,19 +2,20 @@
layout(location = 0) in vec3 Vertex;
layout(location = 1) in vec3 Color;
layout(location = 2) in vec3 Normal;
layout(binding = 0) uniform SunLightConfig
layout(binding = 0) uniform WorldMatrix
{
vec4 color;
vec4 direction;
} sun;
layout(push_constant) uniform Consts {
mat4 two_dim;
mat4 projection;
mat4 modelview;
mat4 mvp;
mat3 normal;
} matrix;
} world;
layout(push_constant) uniform Consts {
mat4 local_to_world;
} pc;
layout(location = 0) out vec4 FragmentColor;

View File

@ -1,22 +0,0 @@
#version 450 core
layout(location = 0) in vec3 Vertex;
layout(location = 1) in vec3 Normal;
layout(push_constant) uniform MatrixConstants {
mat4 projection;
mat4 modelview;
mat4 mvp;
mat3 normal;
}matrix;
layout(location = 0) out vec4 FragmentColor;
layout(location = 1) out vec3 FragmentNormal;
void main()
{
FragmentColor=vec4(Color,1.0);
FragmentNormal=Normal;
gl_Position=vec4(Vertex,1.0)*matrix.mvp;
}

View File

@ -0,0 +1,26 @@
#version 450 core
layout(location = 0) in vec3 Vertex;
layout(location = 1) in vec4 Color;
layout(binding = 0) uniform WorldMatrix
{
mat4 two_dim;
mat4 projection;
mat4 modelview;
mat4 mvp;
mat3 normal;
} world;
layout(push_constant) uniform Consts {
mat4 local_to_world;
} pc;
layout(location = 0) out vec4 FragmentColor;
void main()
{
FragmentColor=Color;
gl_Position=vec4(Vertex,1.0)*pc.local_to_world*world.mvp;
}

View File

@ -1,7 +1,10 @@
glslangValidator -V -o FlatColor.vert.spv FlatColor.vert
glslangValidator -V -o FlatColor3D.vert.spv FlatColor3D.vert
glslangValidator -V -o OnlyPosition.vert.spv OnlyPosition.vert
glslangValidator -V -o OnlyPosition3D.vert.spv OnlyPosition3D.vert
glslangValidator -V -o FlatColor.frag.spv FlatColor.frag
glslangValidator -V -o FlatTexture.vert.spv FlatTexture.vert
glslangValidator -V -o FlatTexture.frag.spv FlatTexture.frag
glslangValidator -V -o PositionColor3D.vert.spv PositionColor3D.vert

View File

@ -69,12 +69,18 @@ bool DescriptorSetLayoutCreater::CreatePipelineLayout()
if(vkCreateDescriptorSetLayout(*device,&descriptor_layout,nullptr,&dsl)!=VK_SUCCESS)
return(false);
VkPushConstantRange push_constant_rage;
push_constant_rage.stageFlags=VK_SHADER_STAGE_VERTEX_BIT;
push_constant_rage.size=sizeof(PushConstant);
push_constant_rage.offset=0;
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {};
pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pPipelineLayoutCreateInfo.pNext = nullptr;
pPipelineLayoutCreateInfo.pushConstantRangeCount = 0;
pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr;
pPipelineLayoutCreateInfo.pushConstantRangeCount = 1;
pPipelineLayoutCreateInfo.pPushConstantRanges = &push_constant_rage;
pPipelineLayoutCreateInfo.setLayoutCount = 1;
pPipelineLayoutCreateInfo.pSetLayouts = &dsl;

View File

@ -26,13 +26,15 @@ namespace hgl
void Camera::Refresh()
{
if(type==CameraType::Perspective)
projection=perspective(fov,width/height,znear,zfar);
matrix.projection=perspective(fov,width/height,znear,zfar);
else
projection=ortho(width,height,znear,zfar);
matrix.projection=ortho(width,height,znear,zfar); //这个算的不对
modelview=hgl::graph::LookAt(eye,center,up_vector);
matrix.modelview=hgl::graph::LookAt(eye,center,up_vector);
mvp=projection*modelview;
matrix.mvp=matrix.projection*matrix.modelview;
matrix.two_dim=ortho(width,height,znear,zfar);
frustum.SetVerticalFovAndAspectRatio(DegToRad(fov),width/height);
frustum.SetViewPlaneDistances(znear,zfar);

View File

@ -167,7 +167,7 @@ namespace hgl
VB3f *vertex=new VB3f(((pgci->step.u+1)+(pgci->step.v+1))*2);
vertex->Begin();
for(int row=0;row<=pgci->step.u;row++)
for(uint row=0;row<=pgci->step.u;row++)
{
float pos=float(row)/float(pgci->step.u);
@ -175,7 +175,7 @@ namespace hgl
to(pgci->coord[3],pgci->coord[2],pos));
}
for(int col=0;col<=pgci->step.v;col++)
for(uint col=0;col<=pgci->step.v;col++)
{
float pos=float(col)/float(pgci->step.v);
@ -188,6 +188,32 @@ namespace hgl
render_obj->Set(vertex_binding,db->CreateVBO(vertex));
render_obj->SetBoundingBox(vertex->GetAABB());
const int color_binding=vsm->GetStageInputBinding("Color");
if(color_binding!=-1)
{
VB4f *color=new VB4f(((pgci->step.u+1)+(pgci->step.v+1))*2);
color->Begin();
for(uint row=0;row<=pgci->step.u;row++)
{
if((row%pgci->side_step.u)==0)
color->Fill(pgci->side_color,2);
else
color->Fill(pgci->color,2);
}
for(uint col=0;col<=pgci->step.v;col++)
{
if((col%pgci->side_step.v)==0)
color->Fill(pgci->side_color,2);
else
color->Fill(pgci->color,2);
}
color->End();
render_obj->Set(color_binding,db->CreateVBO(color));
}
delete vertex;
db->Add(render_obj);
return render_obj;

View File

@ -26,42 +26,29 @@ namespace hgl
// return (((Frustum *)fc)->BoxIn(node->GetWorldBoundingBox())!=Frustum::OUTSIDE);
//}
void RenderList::SetCamera(const Camera &cam)
{
camera=cam;
ubo_matrix.projection=camera.projection;
ubo_matrix.modelview=camera.modelview;
ubo_matrix.mvp =ubo_matrix.projection*ubo_matrix.modelview;
ubo_matrix.normal =ubo_matrix.modelview.Float3x3Part(); //法线矩阵为3x3
frustum=camera.frustum;
}
void RenderList::SetMVP(const Matrix4f &proj,const Matrix4f &mv)
{
ubo_matrix.projection =proj;
ubo_matrix.modelview =mv;
ubo_matrix.mvp =proj*mv;
ubo_matrix.normal =ubo_matrix.modelview.Float3x3Part(); //法线矩阵为3x3
}
void RenderList::Render(RenderableInstance *ri)
void RenderList::Render(SceneNode *node,RenderableInstance *ri)
{
if(last_pipeline!=ri->GetPipeline())
{
cmd_buf->Bind(ri->GetPipeline());
last_pipeline=ri->GetPipeline();
cmd_buf->Bind(last_pipeline);
last_desc_sets=nullptr;
}
if(last_desc_sets!=ri->GetDescriptorSets())
{
cmd_buf->Bind(ri->GetDescriptorSets());
last_desc_sets=ri->GetDescriptorSets();
cmd_buf->Bind(last_desc_sets);
}
if(last_pc!=node->GetPushConstant())
{
last_pc=node->GetPushConstant();
cmd_buf->PushConstants(last_pc);
}
//更新fin_mvp
@ -87,14 +74,14 @@ namespace hgl
}
}
void RenderList::Render(List<RenderableInstance *> &ri_list)
void RenderList::Render(SceneNode *node,List<RenderableInstance *> &ri_list)
{
const int count=ri_list.GetCount();
RenderableInstance **ri=ri_list.GetData();
for(int i=0;i<count;i++)
{
Render(*ri);
Render(node,*ri);
++ri;
}
}
@ -115,7 +102,7 @@ namespace hgl
for(int i=0;i<count;i++)
{
Render((*node)->renderable_instances);
Render(*node,(*node)->renderable_instances);
++node;
}

View File

@ -7,6 +7,7 @@ namespace hgl
SceneOrient::SceneOrient()
{
pc.local_to_world=
LocalMatrix=
LocalToWorldMatrix=
InverseLocalMatrix=
@ -28,6 +29,8 @@ namespace hgl
InverseLocalToWorldMatrix=inverse(LocalToWorldMatrix);
pc.local_to_world=LocalToWorldMatrix;
return LocalToWorldMatrix;
}