2023-09-27 11:36:39 +08:00
|
|
|
|
// 该范例主要演示使用一个材质下的不同材质实例传递颜色参数绘制三角形,并依赖RenderList中的自动合并功能,让同一材质下所有不同材质实例的对象一次渲染完成。
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
2025-03-14 00:17:54 +08:00
|
|
|
|
#include<hgl/WorkManager.h>
|
2023-05-22 15:29:07 +08:00
|
|
|
|
#include<hgl/math/Math.h>
|
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
2023-10-07 20:59:44 +08:00
|
|
|
|
#include<hgl/graph/mtl/Material2DCreateConfig.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 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;
|
2025-03-17 00:49:56 +08:00
|
|
|
|
constexpr double TRI_ROTATE_ANGLE=360.0f/DRAW_OBJECT_COUNT;
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
2023-10-10 10:57:29 +08:00
|
|
|
|
#define USE_MATERIAL_FILE true //是否使用材质文件
|
|
|
|
|
|
2025-03-14 00:17:54 +08:00
|
|
|
|
class TestApp:public WorkObject
|
2023-05-22 15:29:07 +08:00
|
|
|
|
{
|
2025-03-14 00:17:54 +08:00
|
|
|
|
Color4f clear_color =Color4f(0.2f,0.2f,0.2f,1.0f);
|
|
|
|
|
|
2023-05-22 15:29:07 +08:00
|
|
|
|
private:
|
|
|
|
|
|
2025-03-14 00:17:54 +08:00
|
|
|
|
AutoDelete<RenderList> render_list =nullptr;
|
|
|
|
|
|
2023-05-22 15:29:07 +08:00
|
|
|
|
SceneNode render_root;
|
|
|
|
|
|
2023-09-20 21:53:30 +08:00
|
|
|
|
Material * material =nullptr;
|
|
|
|
|
|
|
|
|
|
struct
|
|
|
|
|
{
|
|
|
|
|
MaterialInstance * mi;
|
2025-05-18 23:42:39 +08:00
|
|
|
|
Mesh * mesh;
|
2023-09-20 21:53:30 +08:00
|
|
|
|
}render_obj[DRAW_OBJECT_COUNT]{};
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
|
|
|
|
Pipeline * pipeline =nullptr;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
|
|
|
|
{
|
2025-05-18 23:42:39 +08:00
|
|
|
|
mtl::Material2DCreateConfig cfg(PrimitiveType::Triangles,CoordinateSystem2D::NDC,mtl::WithLocalToWorld::With);
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
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
|
|
|
|
|
2025-05-04 23:36:28 +08:00
|
|
|
|
pipeline=CreatePipeline(material,InlinePipeline::Solid2D,PrimitiveType::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
|
|
|
|
{
|
2025-03-14 00:17:54 +08:00
|
|
|
|
Primitive *prim=CreatePrimitive("Triangle",VERTEX_COUNT,material->GetDefaultVIL(),
|
|
|
|
|
{{VAN::Position, VF_V2F, position_data}});
|
2024-05-25 22:47:26 +08:00
|
|
|
|
|
|
|
|
|
if(!prim)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
db->Add(prim);
|
2024-10-06 14:51:38 +08:00
|
|
|
|
|
|
|
|
|
Matrix4f mat;
|
2023-09-20 21:53:30 +08:00
|
|
|
|
|
|
|
|
|
for(uint i=0;i<DRAW_OBJECT_COUNT;i++)
|
|
|
|
|
{
|
2025-05-18 23:42:39 +08:00
|
|
|
|
render_obj[i].mesh=db->CreateMesh(prim,render_obj[i].mi,pipeline);
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
2025-05-18 23:42:39 +08:00
|
|
|
|
if(!render_obj[i].mesh)
|
2023-09-20 21:53:30 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2025-03-17 00:49:56 +08:00
|
|
|
|
mat=rotate(deg2rad<double>(TRI_ROTATE_ANGLE*i),AxisVector::Z);
|
2024-10-06 14:51:38 +08:00
|
|
|
|
|
2025-05-18 23:42:39 +08:00
|
|
|
|
render_root.Add(new SceneNode(mat,render_obj[i].mesh));
|
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:
|
|
|
|
|
|
2025-03-14 00:17:54 +08:00
|
|
|
|
TestApp(RenderFramework *rf):WorkObject(rf,rf->GetSwapchainRenderTarget())
|
2023-05-22 15:29:07 +08:00
|
|
|
|
{
|
2025-03-14 00:17:54 +08:00
|
|
|
|
render_list=rf->CreateRenderList();
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
2025-03-14 00:17:54 +08:00
|
|
|
|
return;
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
2023-09-22 01:08:32 +08:00
|
|
|
|
if(!InitVBOAndRenderList())
|
2025-03-14 00:17:54 +08:00
|
|
|
|
return;
|
2023-05-22 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-03-14 00:17:54 +08:00
|
|
|
|
void Render(double delta_time,graph::RenderCmdBuffer *cmd)override
|
2023-05-22 15:29:07 +08:00
|
|
|
|
{
|
2025-03-14 00:17:54 +08:00
|
|
|
|
cmd->SetClearColor(0,clear_color);
|
2023-05-22 15:29:07 +08:00
|
|
|
|
|
2025-03-14 00:17:54 +08:00
|
|
|
|
cmd->BeginRenderPass();
|
|
|
|
|
render_list->Render(cmd);
|
|
|
|
|
cmd->EndRenderPass();
|
2023-05-22 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
2025-03-14 00:17:54 +08:00
|
|
|
|
int os_main(int,os_char **)
|
2023-05-22 15:29:07 +08:00
|
|
|
|
{
|
2025-03-14 00:17:54 +08:00
|
|
|
|
return RunFramework<TestApp>(OS_TEXT("AutoInstance"),1024,1024);
|
2023-05-22 15:29:07 +08:00
|
|
|
|
}
|