2023-09-27 11:36:39 +08:00
|
|
|
|
// 该范例主要演示使用RenderList系统绘制多个三角形,并利用RenderList进行排序以及自动合并进行Instance渲染
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
2025-03-13 23:41:51 +08:00
|
|
|
|
#include<hgl/WorkManager.h>
|
2022-06-24 16:27:43 +08:00
|
|
|
|
#include<hgl/math/Math.h>
|
2024-05-25 22:47:26 +08:00
|
|
|
|
#include<hgl/graph/PrimitiveCreater.h>
|
2023-08-18 18:39:25 +08:00
|
|
|
|
#include<hgl/graph/VKVertexInputConfig.h>
|
2023-10-07 20:59:44 +08:00
|
|
|
|
#include<hgl/graph/mtl/Material2DCreateConfig.h>
|
2025-06-14 23:42:50 +08:00
|
|
|
|
#include<hgl/component/StaticMeshComponent.h>
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t VERTEX_COUNT=3;
|
|
|
|
|
|
2024-05-25 22:47:26 +08:00
|
|
|
|
constexpr uint32_t TRIANGLE_NUMBER=12;
|
|
|
|
|
|
2023-05-07 00:42:06 +08:00
|
|
|
|
constexpr float position_data[VERTEX_COUNT*2]=
|
2022-06-24 16:27:43 +08:00
|
|
|
|
{
|
2023-05-07 00:42:06 +08:00
|
|
|
|
0.0, 0.0,
|
|
|
|
|
-0.1, 0.9,
|
|
|
|
|
0.1, 0.9
|
2022-06-24 16:27:43 +08:00
|
|
|
|
};
|
|
|
|
|
|
2023-08-18 18:39:25 +08:00
|
|
|
|
constexpr uint8 color_data[VERTEX_COUNT][4]=
|
2024-04-23 03:32:25 +08:00
|
|
|
|
{
|
|
|
|
|
{255,0,0,255},
|
2023-08-18 18:39:25 +08:00
|
|
|
|
{0,255,0,255},
|
|
|
|
|
{0,0,255,255}
|
2022-06-24 16:27:43 +08:00
|
|
|
|
};
|
|
|
|
|
|
2025-03-13 23:41:51 +08:00
|
|
|
|
class TestApp:public WorkObject
|
2022-06-24 16:27:43 +08:00
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
MaterialInstance * material_instance =nullptr;
|
2025-05-18 23:42:39 +08:00
|
|
|
|
Mesh * render_obj =nullptr;
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
Pipeline * pipeline =nullptr;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
|
|
|
|
{
|
2023-05-06 22:19:50 +08:00
|
|
|
|
{
|
2025-05-18 23:42:39 +08:00
|
|
|
|
mtl::Material2DCreateConfig cfg(PrimitiveType::Triangles,
|
|
|
|
|
CoordinateSystem2D::NDC,
|
|
|
|
|
mtl::WithLocalToWorld::With);
|
2023-05-06 22:19:50 +08:00
|
|
|
|
|
2023-08-18 18:39:25 +08:00
|
|
|
|
VILConfig vil_config;
|
|
|
|
|
|
|
|
|
|
vil_config.Add(VAN::Color,VF_V4UN8);
|
|
|
|
|
|
2025-05-18 23:42:39 +08:00
|
|
|
|
material_instance=CreateMaterialInstance(mtl::inline_material::VertexColor2D,&cfg,&vil_config);
|
2023-05-06 22:19:50 +08:00
|
|
|
|
}
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
if(!material_instance)
|
|
|
|
|
return(false);
|
2023-08-18 18:39:25 +08:00
|
|
|
|
|
2022-06-24 16:27:43 +08:00
|
|
|
|
// pipeline=db->CreatePipeline(material_instance,sc_render_target,OS_TEXT("res/pipeline/solid2d"));
|
2025-06-12 00:00:14 +08:00
|
|
|
|
pipeline=CreatePipeline(material_instance,InlinePipeline::Solid2D); //等同上一行,为Framework重载,默认使用swapchain的render target
|
2023-08-18 18:39:25 +08:00
|
|
|
|
|
2022-06-24 16:27:43 +08:00
|
|
|
|
return pipeline;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool InitVBO()
|
|
|
|
|
{
|
2025-05-18 02:19:14 +08:00
|
|
|
|
render_obj=CreateMesh("Triangle",VERTEX_COUNT,material_instance,pipeline,
|
2025-03-13 23:41:51 +08:00
|
|
|
|
{
|
|
|
|
|
{VAN::Position, VF_V2F, position_data},
|
|
|
|
|
{VAN::Color, VF_V4UN8, color_data }
|
|
|
|
|
});
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
2023-02-13 11:48:53 +08:00
|
|
|
|
if(!render_obj)
|
|
|
|
|
return(false);
|
2024-05-25 22:47:26 +08:00
|
|
|
|
|
|
|
|
|
double rad;
|
|
|
|
|
Matrix4f mat;
|
2025-06-04 01:09:05 +08:00
|
|
|
|
|
|
|
|
|
SceneNode *scene_root=GetSceneRoot(); ///<取得场景根节点
|
2023-05-07 00:42:06 +08:00
|
|
|
|
|
2024-05-25 22:47:26 +08:00
|
|
|
|
for(uint i=0;i<TRIANGLE_NUMBER;i++)
|
|
|
|
|
{
|
2025-06-14 23:42:50 +08:00
|
|
|
|
rad=deg2rad<double>((360.0f/double(TRIANGLE_NUMBER))*i); //这里一定要加<double>或<float>,否则结果用int保存会出现问题
|
2024-05-25 22:47:26 +08:00
|
|
|
|
mat=rotate(rad,Vector3f(0,0,1));
|
|
|
|
|
|
2025-06-14 23:42:50 +08:00
|
|
|
|
CreateComponent<StaticMeshComponent>(mat,scene_root,render_obj);
|
2024-05-25 22:47:26 +08:00
|
|
|
|
}
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2025-05-22 01:11:30 +08:00
|
|
|
|
using WorkObject::WorkObject;
|
|
|
|
|
|
|
|
|
|
bool Init() override
|
2022-06-24 16:27:43 +08:00
|
|
|
|
{
|
2025-06-04 01:09:05 +08:00
|
|
|
|
GetRenderer()->SetClearColor(Color4f(0.2f,0.2f,0.2f,1.0f));
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
2025-05-22 01:11:30 +08:00
|
|
|
|
return(false);
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
if(!InitVBO())
|
2025-05-22 01:11:30 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
2022-06-24 16:27:43 +08:00
|
|
|
|
}
|
2025-03-13 23:41:51 +08:00
|
|
|
|
};//class TestApp:public WorkObject
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
2025-03-13 23:41:51 +08:00
|
|
|
|
int os_main(int,os_char **)
|
2022-06-24 16:27:43 +08:00
|
|
|
|
{
|
2025-03-13 23:41:51 +08:00
|
|
|
|
return RunFramework<TestApp>(OS_TEXT("AutoInstance"),1024,1024);
|
2022-06-24 16:27:43 +08:00
|
|
|
|
}
|