2023-09-27 11:36:39 +08:00
|
|
|
|
// 该范例主要演示使用一个材质下的不同材质实例传递颜色参数绘制三角形,并依赖RenderList中的自动合并功能,让同一材质下所有不同材质实例的对象一次渲染完成。
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/math/Math.h>
|
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
|
|
|
|
#include<hgl/graph/VKRenderablePrimitiveCreater.h>
|
2023-10-07 20:59:44 +08:00
|
|
|
|
#include<hgl/graph/mtl/Material2DCreateConfig.h>
|
2023-05-22 15:29:07 +08:00
|
|
|
|
#include<hgl/graph/RenderList.h>
|
2023-09-20 21:53:30 +08:00
|
|
|
|
#include<hgl/color/Color.h>
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=1024;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=1024;
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t VERTEX_COUNT=3;
|
|
|
|
|
|
|
|
|
|
constexpr float position_data[VERTEX_COUNT*2]=
|
|
|
|
|
{
|
|
|
|
|
0.0, 0.0,
|
|
|
|
|
-0.1, 0.9,
|
|
|
|
|
0.1, 0.9
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-20 21:53:30 +08:00
|
|
|
|
constexpr uint DRAW_OBJECT_COUNT=12;
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
2023-10-10 10:57:29 +08:00
|
|
|
|
#define USE_MATERIAL_FILE true //是否使用材质文件
|
|
|
|
|
|
2023-05-22 15:29:07 +08:00
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
SceneNode render_root;
|
|
|
|
|
RenderList * render_list =nullptr;
|
|
|
|
|
|
2023-09-20 21:53:30 +08:00
|
|
|
|
Material * material =nullptr;
|
|
|
|
|
|
|
|
|
|
struct
|
|
|
|
|
{
|
|
|
|
|
MaterialInstance * mi;
|
|
|
|
|
Renderable * r;
|
|
|
|
|
}render_obj[DRAW_OBJECT_COUNT]{};
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
|
|
|
|
Pipeline * pipeline =nullptr;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
|
|
|
|
{
|
2023-09-26 21:49:37 +08:00
|
|
|
|
mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"PureColor2D",Prim::Triangles);
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
|
|
|
|
cfg.coordinate_system=CoordinateSystem2D::NDC;
|
|
|
|
|
cfg.local_to_world=true;
|
|
|
|
|
|
2023-10-10 10:57:29 +08:00
|
|
|
|
#ifndef USE_MATERIAL_FILE
|
|
|
|
|
AutoDelete<mtl::MaterialCreateInfo> mci=mtl::CreatePureColor2D(&cfg); //走程序内置材质创建函数
|
2023-10-11 19:02:17 +08:00
|
|
|
|
material=db->CreateMaterial(mci);
|
2023-10-10 10:57:29 +08:00
|
|
|
|
#else
|
2023-10-11 19:02:17 +08:00
|
|
|
|
material=db->LoadMaterial("Std2D/PureColor2D",&cfg); //走材质文件加载
|
2023-10-10 10:57:29 +08:00
|
|
|
|
#endif//USE_MATERIAL_FILE
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
2023-09-20 21:53:30 +08:00
|
|
|
|
if(!material)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
for(uint i=0;i<DRAW_OBJECT_COUNT;i++)
|
2023-06-05 21:39:12 +08:00
|
|
|
|
{
|
2023-09-20 21:53:30 +08:00
|
|
|
|
render_obj[i].mi=db->CreateMaterialInstance(material);
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
2023-09-20 21:53:30 +08:00
|
|
|
|
if(!render_obj[i].mi)
|
2023-06-05 21:39:12 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2023-09-20 21:53:30 +08:00
|
|
|
|
Color4f color=GetColor4f((COLOR)(i+int(COLOR::Blue)),1.0);
|
|
|
|
|
|
2023-09-22 01:30:44 +08:00
|
|
|
|
render_obj[i].mi->WriteMIData(color); //设置MaterialInstance的数据
|
2023-09-19 21:09:09 +08:00
|
|
|
|
}
|
2023-06-05 21:39:12 +08:00
|
|
|
|
}
|
2023-09-05 10:28:03 +08:00
|
|
|
|
|
2023-09-20 21:53:30 +08:00
|
|
|
|
pipeline=CreatePipeline(material,InlinePipeline::Solid2D,Prim::Triangles);
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
|
|
|
|
return pipeline;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-22 01:08:32 +08:00
|
|
|
|
bool InitVBOAndRenderList()
|
2023-05-22 15:29:07 +08:00
|
|
|
|
{
|
2023-10-12 10:39:43 +08:00
|
|
|
|
RenderablePrimitiveCreater rpc(db,"Triangle",VERTEX_COUNT);
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
2024-04-24 01:44:01 +08:00
|
|
|
|
if(!rpc.SetVAB(VAN::Position, VF_V2F, position_data))return(false);
|
2023-09-20 21:53:30 +08:00
|
|
|
|
|
|
|
|
|
for(uint i=0;i<DRAW_OBJECT_COUNT;i++)
|
|
|
|
|
{
|
|
|
|
|
render_obj[i].r=rpc.Create(render_obj[i].mi,pipeline);
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
2023-09-20 21:53:30 +08:00
|
|
|
|
if(!render_obj[i].r)
|
|
|
|
|
return(false);
|
|
|
|
|
|
2024-03-26 01:33:17 +08:00
|
|
|
|
render_root.CreateSubNode(rotate(deg2rad(double(360/DRAW_OBJECT_COUNT*i)),Vector3f(0,0,1)),render_obj[i].r);
|
2023-09-20 21:53:30 +08:00
|
|
|
|
}
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
2023-09-20 21:53:30 +08:00
|
|
|
|
render_root.RefreshMatrix();
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
2023-09-20 21:53:30 +08:00
|
|
|
|
render_list->Expend(&render_root);
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
~TestApp()
|
|
|
|
|
{
|
|
|
|
|
SAFE_CLEAR(render_list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
render_list=new RenderList(device);
|
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2023-09-22 01:08:32 +08:00
|
|
|
|
if(!InitVBOAndRenderList())
|
2023-05-22 15:29:07 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
BuildCommandBuffer(render_list);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-06 23:24:57 +08:00
|
|
|
|
void Resize(uint w,uint h)override
|
2023-05-22 15:29:07 +08:00
|
|
|
|
{
|
|
|
|
|
VulkanApplicationFramework::Resize(w,h);
|
|
|
|
|
|
|
|
|
|
BuildCommandBuffer(render_list);
|
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|