2023-09-20 15:55:14 +08:00
|
|
|
|
// AutoMergeMaterialInstance
|
|
|
|
|
// 该范例主要演示使用一个材质下的不同材质实例传递颜色参数绘制三角形,并依赖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-06-02 20:45:19 +08:00
|
|
|
|
#include<hgl/graph/mtl/2d/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
|
|
|
|
|
|
|
|
|
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-06-02 20:45:19 +08:00
|
|
|
|
mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"PureColor2D");
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
|
|
|
|
cfg.coordinate_system=CoordinateSystem2D::NDC;
|
|
|
|
|
cfg.local_to_world=true;
|
|
|
|
|
|
|
|
|
|
AutoDelete<mtl::MaterialCreateInfo> mci=mtl::CreatePureColor2D(&cfg);
|
|
|
|
|
|
2023-09-20 21:53:30 +08:00
|
|
|
|
material=db->CreateMaterial(mci);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
render_obj[i].mi->WriteMIData(color,sizeof(Color4f)); //设置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-09-20 21:53:30 +08:00
|
|
|
|
RenderablePrimitiveCreater rpc(db,VERTEX_COUNT);
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
2023-09-20 21:53:30 +08:00
|
|
|
|
if(!rpc.SetVBO(VAN::Position, VF_V2F, position_data))return(false);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
render_root.CreateSubNode(rotate(deg2rad(360/DRAW_OBJECT_COUNT*i),Vector3f(0,0,1)),render_obj[i].r);
|
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resize(int w,int h)override
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|