ULRE/example/Basic/auto_instance.cpp

114 lines
3.0 KiB
C++
Raw Normal View History

2023-09-27 11:36:39 +08:00
// 该范例主要演示使用RenderList系统绘制多个三角形并利用RenderList进行排序以及自动合并进行Instance渲染
2022-06-24 16:27:43 +08:00
#include<hgl/WorkManager.h>
2022-06-24 16:27:43 +08:00
#include<hgl/math/Math.h>
#include<hgl/graph/PrimitiveCreater.h>
2023-08-18 18:39:25 +08:00
#include<hgl/graph/VKVertexInputConfig.h>
#include<hgl/graph/mtl/Material2DCreateConfig.h>
#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;
constexpr uint32_t TRIANGLE_NUMBER=12;
constexpr float position_data[VERTEX_COUNT*2]=
2022-06-24 16:27:43 +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]=
{
{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
};
class TestApp:public WorkObject
2022-06-24 16:27:43 +08:00
{
private:
MaterialInstance * material_instance =nullptr;
Mesh * render_obj =nullptr;
2022-06-24 16:27:43 +08:00
Pipeline * pipeline =nullptr;
private:
bool InitMaterial()
{
{
mtl::Material2DCreateConfig cfg(PrimitiveType::Triangles,
CoordinateSystem2D::NDC,
mtl::WithLocalToWorld::With);
2023-08-18 18:39:25 +08:00
VILConfig vil_config;
vil_config.Add(VAN::Color,VF_V4UN8);
material_instance=CreateMaterialInstance(mtl::inline_material::VertexColor2D,&cfg,&vil_config);
}
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"));
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()
{
render_obj=CreateMesh("Triangle",VERTEX_COUNT,material_instance,pipeline,
{
{VAN::Position, VF_V2F, position_data},
{VAN::Color, VF_V4UN8, color_data }
});
2022-06-24 16:27:43 +08:00
if(!render_obj)
return(false);
double rad;
Matrix4f mat;
SceneNode *scene_root=GetSceneRoot(); ///<取得场景根节点
for(uint i=0;i<TRIANGLE_NUMBER;i++)
{
rad=deg2rad<double>((360.0f/double(TRIANGLE_NUMBER))*i); //这里一定要加<double>或<float>否则结果用int保存会出现问题
mat=rotate(rad,Vector3f(0,0,1));
CreateComponent<StaticMeshComponent>(mat,scene_root,render_obj);
}
2022-06-24 16:27:43 +08:00
return(true);
}
public:
using WorkObject::WorkObject;
bool Init() override
2022-06-24 16:27:43 +08:00
{
GetRenderer()->SetClearColor(Color4f(0.2f,0.2f,0.2f,1.0f));
2022-06-24 16:27:43 +08:00
if(!InitMaterial())
return(false);
2022-06-24 16:27:43 +08:00
if(!InitVBO())
return(false);
return(true);
2022-06-24 16:27:43 +08:00
}
};//class TestApp:public WorkObject
2022-06-24 16:27:43 +08:00
int os_main(int,os_char **)
2022-06-24 16:27:43 +08:00
{
return RunFramework<TestApp>(OS_TEXT("AutoInstance"),1024,1024);
2022-06-24 16:27:43 +08:00
}