ULRE/example/Basic/draw_triangle_in_NDC.cpp

157 lines
3.7 KiB
C++
Raw Normal View History

2023-09-27 11:36:39 +08:00
// 该范例主要演示使用NDC坐标系直接绘制一个渐变色的三角形
2019-05-06 12:00:03 +08:00
#include<hgl/WorkManager.h>
#include<hgl/math/HalfFloat.h>
#include<hgl/graph/VKVertexInputConfig.h>
#include<hgl/graph/PrimitiveCreater.h>
#include<hgl/graph/mtl/Material2DCreateConfig.h>
2019-04-15 22:19:09 +08:00
using namespace hgl;
using namespace hgl::graph;
2019-04-09 00:22:26 +08:00
2020-06-09 19:40:08 +08:00
constexpr uint32_t SCREEN_WIDTH=1280;
constexpr uint32_t SCREEN_HEIGHT=720;
constexpr uint32_t VERTEX_COUNT=3;
constexpr float position_data_float[VERTEX_COUNT*2]=
{
0.0, -0.5,
-0.5, 0.5,
0.5, 0.5
};
#define USE_HALF_FLOAT_POSITION
#ifdef USE_HALF_FLOAT_POSITION
constexpr VkFormat PositionFormat=VF_V2HF;
half_float position_data_hf[VERTEX_COUNT*2];
#define position_data position_data_hf
#else
constexpr VkFormat PositionFormat=VF_V2F;
#define position_data position_data_float
#endif//USE_HALF_FLOAT_POSITION
#define USE_UNORM8_COLOR
#ifdef USE_UNORM8_COLOR
constexpr uint8 color_data[VERTEX_COUNT*4]=
{
255,0,0,255,
0,255,0,255,
0,0,255,255
};
constexpr VkFormat ColorFormat=VF_V4UN8;
#else
constexpr float color_data[VERTEX_COUNT*4]=
{
1,0,0,1,
0,1,0,1,
0,0,1,1
};
2019-04-20 17:18:02 +08:00
constexpr VkFormat ColorFormat=VF_V4F;
#endif//USE_UNORM8_COLOR
class TestApp:public WorkObject
2019-04-09 00:22:26 +08:00
{
private:
2019-05-04 16:31:28 +08:00
Color4f clear_color=Color4f(0.2f,0.2f,0.2f,1.0f);
#if defined(USE_HALF_FLOAT_POSITION)||defined(USE_UNORM8_COLOR)
VILConfig vil_config;
#endif
2021-06-16 20:29:25 +08:00
MaterialInstance * material_instance =nullptr;
2025-05-18 02:03:16 +08:00
Mesh * render_obj =nullptr;
2020-10-21 12:47:06 +08:00
Pipeline * pipeline =nullptr;
2019-04-09 00:22:26 +08:00
2019-04-28 18:18:56 +08:00
private:
2019-04-12 16:39:22 +08:00
void InitVIL()
2019-04-12 16:39:22 +08:00
{
#ifdef USE_HALF_FLOAT_POSITION
vil_config.Add(VAN::Position,PositionFormat);
#endif//USE_HALF_FLOAT_POSITION
#ifdef USE_UNORM8_COLOR
vil_config.Add(VAN::Color,ColorFormat);
#endif//USE_HALF_FLOAT_POSITION
}
bool InitAutoMaterial()
{
mtl::Material2DCreateConfig cfg(PrimitiveType::Triangles,
CoordinateSystem2D::NDC,
mtl::WithLocalToWorld::Without);
material_instance=CreateMaterialInstance(mtl::inline_material::VertexColor2D,&cfg,&vil_config);
return material_instance;
}
bool InitPipeline()
{
2021-09-22 17:22:17 +08:00
// pipeline=db->CreatePipeline(material_instance,sc_render_target,OS_TEXT("res/pipeline/solid2d"));
pipeline=CreatePipeline(material_instance,InlinePipeline::Solid2D,PrimitiveType::Triangles); //等同上一行为Framework重载默认使用swapchain的render target
2021-09-22 17:22:17 +08:00
return pipeline;
2019-04-12 16:39:22 +08:00
}
bool InitVBO()
{
#ifdef USE_HALF_FLOAT_POSITION
Float32toFloat16(position_data_hf,position_data_float,VERTEX_COUNT*2);
#endif//USE_HALF_FLOAT_POSITION
render_obj=CreateMesh("Triangle",VERTEX_COUNT,material_instance,pipeline,
{
{VAN::Position,PositionFormat,position_data},
{VAN::Color, ColorFormat, color_data}
});
2022-10-28 17:57:09 +08:00
return(render_obj);
2019-04-28 18:18:56 +08:00
}
2019-04-18 21:02:42 +08:00
2019-04-28 18:18:56 +08:00
public:
using WorkObject::WorkObject;
bool Init() override
2019-04-28 18:18:56 +08:00
{
InitVIL();
if(!InitAutoMaterial())
return(false);
if(!InitPipeline())
return(false);
2019-04-20 02:28:57 +08:00
if(!InitVBO())
return(false);
return(true);
2019-04-28 18:18:56 +08:00
}
void Tick(double)override{}
void Render(double delta_time,graph::RenderCmdBuffer *cmd)
{
cmd->SetClearColor(0,clear_color);
2019-07-17 04:49:49 +08:00
cmd->BeginRenderPass();
cmd->Render(render_obj);
cmd->EndRenderPass();
}
};//class TestApp:public WorkObject
2019-04-28 18:18:56 +08:00
int os_main(int,os_char **)
2019-04-28 18:18:56 +08:00
{
2025-02-01 16:32:08 +08:00
return RunFramework<TestApp>(OS_TEXT("Draw triangle in NDC space"));
2019-04-09 00:22:26 +08:00
}