增加SceneTree范例测试节点矩阵变换
This commit is contained in:
parent
9bab8a97f1
commit
19e268050f
@ -8,6 +8,7 @@ CreateProject(1.indices_rect indices_rect.cpp)
|
|||||||
CreateProject(2.texture_rect texture_rect.cpp TGATexture.cpp)
|
CreateProject(2.texture_rect texture_rect.cpp TGATexture.cpp)
|
||||||
CreateProject(3.Geometry2D Geometry2D.cpp)
|
CreateProject(3.Geometry2D Geometry2D.cpp)
|
||||||
CreateProject(4.Geometry3D Geometry3D.cpp)
|
CreateProject(4.Geometry3D Geometry3D.cpp)
|
||||||
CreateProject(5.LoadModel LoadModel.cpp TGATexture.cpp AssimpLoaderMesh.h AssimpLoaderMesh.cpp)
|
CreateProject(5.SceneTree SceneTree.cpp)
|
||||||
|
|
||||||
target_link_libraries(5.LoadModel assimp)
|
CreateProject(6.LoadModel LoadModel.cpp TGATexture.cpp AssimpLoaderMesh.h AssimpLoaderMesh.cpp)
|
||||||
|
target_link_libraries(6.LoadModel assimp)
|
||||||
|
@ -117,15 +117,15 @@ private:
|
|||||||
void InitCamera()
|
void InitCamera()
|
||||||
{
|
{
|
||||||
math::vec center_point=model_data->bounding_box.CenterPoint();
|
math::vec center_point=model_data->bounding_box.CenterPoint();
|
||||||
math::vec max_point=model_data->bounding_box.maxPoint;
|
math::vec min_point=model_data->bounding_box.minPoint;
|
||||||
|
|
||||||
max_point.x*=2.0f;
|
min_point.x*=2.0f;
|
||||||
max_point.y=center_point.y;
|
min_point.y=center_point.y;
|
||||||
max_point.z=center_point.z;
|
min_point.z=center_point.z;
|
||||||
|
|
||||||
camera.type=CameraType::Perspective;
|
camera.type=CameraType::Perspective;
|
||||||
camera.center=center_point;
|
camera.center=center_point;
|
||||||
camera.eye=max_point;
|
camera.eye=min_point;
|
||||||
camera.width=SCREEN_WIDTH;
|
camera.width=SCREEN_WIDTH;
|
||||||
camera.height=SCREEN_HEIGHT;
|
camera.height=SCREEN_HEIGHT;
|
||||||
|
|
||||||
|
242
example/Vulkan/SceneTree.cpp
Normal file
242
example/Vulkan/SceneTree.cpp
Normal file
@ -0,0 +1,242 @@
|
|||||||
|
// 5.SceneTree
|
||||||
|
// 用于测试树形排列的场景中,每一级节点对变换矩阵的处理是否正确
|
||||||
|
|
||||||
|
#include"VulkanAppFramework.h"
|
||||||
|
#include<hgl/filesystem/FileSystem.h>
|
||||||
|
#include<hgl/graph/InlineGeometry.h>
|
||||||
|
#include<hgl/graph/SceneDB.h>
|
||||||
|
#include<hgl/graph/RenderableInstance.h>
|
||||||
|
#include<hgl/graph/RenderList.h>
|
||||||
|
#include<hgl/Time.h>
|
||||||
|
|
||||||
|
using namespace hgl;
|
||||||
|
using namespace hgl::graph;
|
||||||
|
|
||||||
|
constexpr uint32_t SCREEN_WIDTH=256;
|
||||||
|
constexpr uint32_t SCREEN_HEIGHT=256;
|
||||||
|
|
||||||
|
class TestApp:public VulkanApplicationFramework
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
double start_time;
|
||||||
|
|
||||||
|
uint swap_chain_count=0;
|
||||||
|
|
||||||
|
SceneDB * db =nullptr;
|
||||||
|
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;
|
||||||
|
vulkan::CommandBuffer ** cmd_buf =nullptr;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
TestApp()
|
||||||
|
{
|
||||||
|
start_time=GetDoubleTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
~TestApp()
|
||||||
|
{
|
||||||
|
SAFE_CLEAR(db);
|
||||||
|
|
||||||
|
SAFE_CLEAR_OBJECT_ARRAY(cmd_buf,swap_chain_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
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("OnlyPosition3D.vert.spv"),
|
||||||
|
OS_TEXT("FlatColor.frag.spv"));
|
||||||
|
if(!material)
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
descriptor_sets=material->CreateDescriptorSets();
|
||||||
|
|
||||||
|
db->Add(material);
|
||||||
|
db->Add(descriptor_sets);
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CreateRenderObject()
|
||||||
|
{
|
||||||
|
struct CubeCreateInfo cci;
|
||||||
|
|
||||||
|
ro_cube=CreateCube(db,material,&cci);
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
descriptor_sets->Update();
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool InitPipeline()
|
||||||
|
{
|
||||||
|
constexpr os_char PIPELINE_FILENAME[]=OS_TEXT("2DSolid.pipeline");
|
||||||
|
|
||||||
|
{
|
||||||
|
vulkan::PipelineCreater *pipeline_creater=new vulkan::PipelineCreater(device,material,device->GetRenderPass(),device->GetExtent());
|
||||||
|
pipeline_creater->SetDepthTest(true);
|
||||||
|
pipeline_creater->SetDepthWrite(true);
|
||||||
|
pipeline_creater->CloseCullFace();
|
||||||
|
pipeline_creater->SetPolygonMode(VK_POLYGON_MODE_LINE);
|
||||||
|
pipeline_creater->Set(PRIM_TRIANGLES);
|
||||||
|
|
||||||
|
pipeline_line=pipeline_creater->Create();
|
||||||
|
if(!pipeline_line)
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
db->Add(pipeline_line);
|
||||||
|
|
||||||
|
delete pipeline_creater;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pipeline_line;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool InitScene()
|
||||||
|
{
|
||||||
|
SceneNode *cur_node=&render_root;
|
||||||
|
|
||||||
|
uint size=1;
|
||||||
|
uint x=0;
|
||||||
|
|
||||||
|
const Vector3f axis(0,0,1);
|
||||||
|
|
||||||
|
for(uint i=0;i<360;i++)
|
||||||
|
{
|
||||||
|
render_root.Add(db->CreateRenderableInstance(pipeline_line,descriptor_sets,ro_cube),
|
||||||
|
rotate(i/5.0f,axis)*
|
||||||
|
translate(i/4.0f,0,0)*
|
||||||
|
scale((i+1)/100.0f));
|
||||||
|
}
|
||||||
|
|
||||||
|
render_root.RefreshMatrix();
|
||||||
|
render_root.ExpendToList(&render_list);
|
||||||
|
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool InitCommandBuffer()
|
||||||
|
{
|
||||||
|
cmd_buf=hgl_zero_new<vulkan::CommandBuffer *>(swap_chain_count);
|
||||||
|
|
||||||
|
for(uint i=0;i<swap_chain_count;i++)
|
||||||
|
{
|
||||||
|
cmd_buf[i]=device->CreateCommandBuffer();
|
||||||
|
|
||||||
|
BuildCommandBuffer(cmd_buf[i],device->GetFramebuffer(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BuildCommandBuffer(vulkan::CommandBuffer *cb,vulkan::Framebuffer *fb)
|
||||||
|
{
|
||||||
|
if (!cb)
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
cb->Begin();
|
||||||
|
cb->BeginRenderPass(device->GetRenderPass(),fb);
|
||||||
|
render_list.Render(cb);
|
||||||
|
cb->EndRenderPass();
|
||||||
|
cb->End();
|
||||||
|
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
bool Init()
|
||||||
|
{
|
||||||
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
swap_chain_count=device->GetSwapChainImageCount();
|
||||||
|
|
||||||
|
db=new SceneDB(device);
|
||||||
|
|
||||||
|
InitCamera();
|
||||||
|
|
||||||
|
if(!InitMaterial())
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
CreateRenderObject();
|
||||||
|
|
||||||
|
if(!InitUBO())
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
if(!InitPipeline())
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
if(!InitScene())
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
if(!InitCommandBuffer())
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Draw() override
|
||||||
|
{
|
||||||
|
const uint32_t frame_index=device->GetCurrentFrameIndices();
|
||||||
|
|
||||||
|
vulkan::Framebuffer * fb=device->GetFramebuffer(frame_index);
|
||||||
|
vulkan::CommandBuffer * cb=cmd_buf[frame_index];
|
||||||
|
|
||||||
|
Submit(*cb);
|
||||||
|
|
||||||
|
Matrix4f rot=rotate(GetDoubleTime()-start_time,camera.right_vector);
|
||||||
|
|
||||||
|
render_root.RefreshMatrix(&rot);
|
||||||
|
render_root.ExpendToList(&render_list);
|
||||||
|
|
||||||
|
BuildCommandBuffer(cb,fb);
|
||||||
|
}
|
||||||
|
};//class TestApp:public VulkanApplicationFramework
|
||||||
|
|
||||||
|
int main(int,char **)
|
||||||
|
{
|
||||||
|
TestApp app;
|
||||||
|
|
||||||
|
if(!app.Init())
|
||||||
|
return(-1);
|
||||||
|
|
||||||
|
while(app.Run());
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -58,10 +58,10 @@ public:
|
|||||||
ImageView *GetColorImageView (int index) {return attr->sc_image_views[index];}
|
ImageView *GetColorImageView (int index) {return attr->sc_image_views[index];}
|
||||||
ImageView *GetDepthImageView () {return attr->depth.view;}
|
ImageView *GetDepthImageView () {return attr->depth.view;}
|
||||||
|
|
||||||
const uint32_t GetCurrentFrameIndices () {return current_frame;}
|
|
||||||
|
|
||||||
RenderPass * GetRenderPass () {return main_rp;}
|
RenderPass * GetRenderPass () {return main_rp;}
|
||||||
Framebuffer * GetFramebuffer (int index) {return main_fb[index];}
|
Framebuffer * GetFramebuffer (int index) {return main_fb[index];}
|
||||||
|
const uint32_t GetCurrentFrameIndices () {return current_frame;}
|
||||||
|
Framebuffer * GetCurrentFramebuffer () {return main_fb[current_frame];}
|
||||||
|
|
||||||
bool Resize (uint,uint);
|
bool Resize (uint,uint);
|
||||||
|
|
||||||
|
@ -42,6 +42,8 @@ namespace hgl
|
|||||||
return(nullptr);
|
return(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
active=true;
|
||||||
|
|
||||||
return(device);
|
return(device);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user