ULRE/example/Basic/draw_triangle_in_NDC.cpp

167 lines
3.9 KiB
C++
Raw Permalink Normal View History

2023-09-27 11:36:39 +08:00
// 该范例主要演示使用NDC坐标系直接绘制一个渐变色的三角形
2019-05-06 12:00:03 +08:00
#include"VulkanAppFramework.h"
#include<hgl/math/Math.h>
#include<hgl/math/HalfFloat.h>
2019-05-17 19:22:13 +08:00
#include<hgl/filesystem/FileSystem.h>
2021-05-24 20:44:20 +08:00
#include<hgl/graph/SceneInfo.h>
#include<hgl/graph/VKVertexInputConfig.h>
#include<hgl/graph/VKRenderablePrimitiveCreater.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
2019-04-28 18:18:56 +08:00
class TestApp:public VulkanApplicationFramework
2019-04-09 00:22:26 +08:00
{
private:
2019-05-04 16:31:28 +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;
Renderable * 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(device->GetDeviceAttribute(),"VertexColor2d",Prim::Triangles);
cfg.coordinate_system=CoordinateSystem2D::NDC;
cfg.local_to_world=false;
AutoDelete<mtl::MaterialCreateInfo> mci=mtl::CreateVertexColor2D(&cfg);
material_instance=db->CreateMaterialInstance(mci,&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,Prim::Triangles); //等同上一行为Framework重载默认使用swapchain的render target
return pipeline;
2019-04-12 16:39:22 +08:00
}
bool InitVBO()
{
2023-10-12 10:39:43 +08:00
RenderablePrimitiveCreater rpc(db,"Triangle",VERTEX_COUNT);
#ifdef USE_HALF_FLOAT_POSITION
Float32toFloat16(position_data_hf,position_data_float,VERTEX_COUNT*2);
#endif//USE_HALF_FLOAT_POSITION
2024-04-24 01:44:01 +08:00
if(!rpc.SetVAB(VAN::Position, PositionFormat, position_data))return(false);
if(!rpc.SetVAB(VAN::Color, ColorFormat, color_data ))return(false);
2020-09-21 19:31:34 +08:00
render_obj=rpc.Create(material_instance,pipeline);
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:
2019-04-20 16:12:22 +08:00
bool Init()
2019-04-28 18:18:56 +08:00
{
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false);
InitVIL();
if(!InitAutoMaterial())
return(false);
if(!InitPipeline())
2019-04-28 18:18:56 +08:00
return(false);
2019-04-20 02:28:57 +08:00
if(!InitVBO())
return(false);
2019-04-20 16:12:22 +08:00
if(!BuildCommandBuffer(render_obj))
2020-10-24 19:14:31 +08:00
return(false);
2019-04-28 18:18:56 +08:00
return(true);
}
void Resize(uint w,uint h)override
{
VulkanApplicationFramework::Resize(w,h);
2019-07-17 04:49:49 +08:00
BuildCommandBuffer(render_obj);
}
2019-04-28 18:18:56 +08:00
};//class TestApp:public VulkanApplicationFramework
int main(int,char **)
{
TestApp app;
if(!app.Init())
return(-1);
2019-04-20 16:12:22 +08:00
while(app.Run());
2019-04-09 00:22:26 +08:00
return 0;
}