2023-09-27 11:36:39 +08:00
|
|
|
|
// 该范例主要演示使用NDC坐标系直接绘制一个渐变色的三角形
|
2019-05-06 12:00:03 +08:00
|
|
|
|
|
2025-02-01 15:40:35 +08:00
|
|
|
|
#include<hgl/WorkManager.h>
|
2023-02-21 18:35:08 +08:00
|
|
|
|
#include<hgl/math/HalfFloat.h>
|
2023-03-19 19:41:21 +08:00
|
|
|
|
#include<hgl/graph/VKVertexInputConfig.h>
|
2024-05-31 22:04:02 +08:00
|
|
|
|
#include<hgl/graph/PrimitiveCreater.h>
|
2023-10-07 20:59:44 +08:00
|
|
|
|
#include<hgl/graph/mtl/Material2DCreateConfig.h>
|
2019-04-15 22:19:09 +08:00
|
|
|
|
|
2019-04-10 01:13:31 +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;
|
2019-04-23 02:46:47 +08:00
|
|
|
|
|
2019-04-28 17:02:38 +08:00
|
|
|
|
constexpr uint32_t VERTEX_COUNT=3;
|
|
|
|
|
|
2023-02-21 18:35:08 +08:00
|
|
|
|
constexpr float position_data_float[VERTEX_COUNT*2]=
|
2019-04-23 02:46:47 +08:00
|
|
|
|
{
|
2023-02-21 18:35:08 +08:00
|
|
|
|
0.0, -0.5,
|
|
|
|
|
-0.5, 0.5,
|
|
|
|
|
0.5, 0.5
|
2019-04-28 17:02:38 +08:00
|
|
|
|
};
|
|
|
|
|
|
2023-02-21 18:35:08 +08:00
|
|
|
|
#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]=
|
2025-02-01 15:40:35 +08:00
|
|
|
|
{
|
|
|
|
|
255,0,0,255,
|
2023-02-21 18:35:08 +08:00
|
|
|
|
0,255,0,255,
|
|
|
|
|
0,0,255,255
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr VkFormat ColorFormat=VF_V4UN8;
|
|
|
|
|
#else
|
|
|
|
|
constexpr float color_data[VERTEX_COUNT*4]=
|
2025-02-01 15:40:35 +08:00
|
|
|
|
{
|
|
|
|
|
1,0,0,1,
|
2023-02-21 18:35:08 +08:00
|
|
|
|
0,1,0,1,
|
|
|
|
|
0,0,1,1
|
2019-04-23 02:46:47 +08:00
|
|
|
|
};
|
2019-04-20 17:18:02 +08:00
|
|
|
|
|
2023-02-21 18:35:08 +08:00
|
|
|
|
constexpr VkFormat ColorFormat=VF_V4F;
|
|
|
|
|
#endif//USE_UNORM8_COLOR
|
|
|
|
|
|
2025-02-01 15:40:35 +08:00
|
|
|
|
class TestApp:public WorkObject
|
2019-04-09 00:22:26 +08:00
|
|
|
|
{
|
2019-05-05 14:22:58 +08:00
|
|
|
|
private:
|
2019-05-04 16:31:28 +08:00
|
|
|
|
|
2025-02-01 15:40:35 +08:00
|
|
|
|
Color4f clear_color=Color4f(0.2f,0.2f,0.2f,1.0f);
|
|
|
|
|
|
2023-03-21 14:17:33 +08:00
|
|
|
|
#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;
|
2019-04-16 14:49:55 +08:00
|
|
|
|
|
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
|
|
|
|
|
2023-03-21 14:17:33 +08:00
|
|
|
|
void InitVIL()
|
2019-04-12 16:39:22 +08:00
|
|
|
|
{
|
2023-02-21 18:35:08 +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
|
2023-03-21 14:17:33 +08:00
|
|
|
|
}
|
2023-02-21 18:35:08 +08:00
|
|
|
|
|
2023-03-21 14:17:33 +08:00
|
|
|
|
bool InitAutoMaterial()
|
|
|
|
|
{
|
2025-05-18 23:42:39 +08:00
|
|
|
|
mtl::Material2DCreateConfig cfg(PrimitiveType::Triangles,
|
|
|
|
|
CoordinateSystem2D::NDC,
|
|
|
|
|
mtl::WithLocalToWorld::Without);
|
2023-05-08 09:50:45 +08:00
|
|
|
|
|
2025-05-18 23:42:39 +08:00
|
|
|
|
material_instance=CreateMaterialInstance(mtl::inline_material::VertexColor2D,&cfg,&vil_config);
|
2023-03-21 14:17:33 +08:00
|
|
|
|
|
|
|
|
|
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"));
|
2025-05-04 23:36:28 +08:00
|
|
|
|
pipeline=CreatePipeline(material_instance,InlinePipeline::Solid2D,PrimitiveType::Triangles); //等同上一行,为Framework重载,默认使用swapchain的render target
|
2021-09-22 17:22:17 +08:00
|
|
|
|
|
2023-03-21 14:17:33 +08:00
|
|
|
|
return pipeline;
|
2019-04-12 16:39:22 +08:00
|
|
|
|
}
|
2023-02-23 14:43:57 +08:00
|
|
|
|
|
2020-06-10 17:11:24 +08:00
|
|
|
|
bool InitVBO()
|
2019-05-05 14:22:58 +08:00
|
|
|
|
{
|
2023-02-21 18:35:08 +08:00
|
|
|
|
#ifdef USE_HALF_FLOAT_POSITION
|
|
|
|
|
Float32toFloat16(position_data_hf,position_data_float,VERTEX_COUNT*2);
|
|
|
|
|
#endif//USE_HALF_FLOAT_POSITION
|
|
|
|
|
|
2025-05-18 02:19:14 +08:00
|
|
|
|
render_obj=CreateMesh("Triangle",VERTEX_COUNT,material_instance,pipeline,
|
2025-02-01 15:40:35 +08:00
|
|
|
|
{
|
|
|
|
|
{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:
|
2025-05-22 01:11:30 +08:00
|
|
|
|
|
|
|
|
|
using WorkObject::WorkObject;
|
|
|
|
|
|
|
|
|
|
bool Init() override
|
2019-04-28 18:18:56 +08:00
|
|
|
|
{
|
2023-03-21 14:17:33 +08:00
|
|
|
|
InitVIL();
|
|
|
|
|
|
|
|
|
|
if(!InitAutoMaterial())
|
2025-05-22 01:11:30 +08:00
|
|
|
|
return(false);
|
2023-03-21 14:17:33 +08:00
|
|
|
|
|
|
|
|
|
if(!InitPipeline())
|
2025-05-22 01:11:30 +08:00
|
|
|
|
return(false);
|
2019-04-20 02:28:57 +08:00
|
|
|
|
|
2020-06-10 17:11:24 +08:00
|
|
|
|
if(!InitVBO())
|
2025-05-22 01:11:30 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
2019-04-28 18:18:56 +08:00
|
|
|
|
}
|
2019-06-13 23:12:11 +08:00
|
|
|
|
|
2025-02-01 15:40:35 +08:00
|
|
|
|
void Tick(double)override{}
|
|
|
|
|
|
|
|
|
|
void Render(double delta_time,graph::RenderCmdBuffer *cmd)
|
2019-06-13 23:12:11 +08:00
|
|
|
|
{
|
2025-02-01 15:40:35 +08:00
|
|
|
|
cmd->SetClearColor(0,clear_color);
|
2019-07-17 04:49:49 +08:00
|
|
|
|
|
2025-02-01 15:40:35 +08:00
|
|
|
|
cmd->BeginRenderPass();
|
|
|
|
|
cmd->Render(render_obj);
|
|
|
|
|
cmd->EndRenderPass();
|
2019-06-13 23:12:11 +08:00
|
|
|
|
}
|
2025-02-01 15:40:35 +08:00
|
|
|
|
};//class TestApp:public WorkObject
|
2019-04-28 18:18:56 +08:00
|
|
|
|
|
2025-02-01 15:40:35 +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
|
|
|
|
}
|