Fixed draw_triangle_in_NDC.cpp,can run.

This commit is contained in:
hyzboy 2025-02-01 15:40:35 +08:00
parent 0d67993326
commit 3807d9fcbf
3 changed files with 34 additions and 38 deletions

View File

@ -1,6 +1,6 @@
// 该范例主要演示使用NDC坐标系直接绘制一个渐变色的三角形 // 该范例主要演示使用NDC坐标系直接绘制一个渐变色的三角形
#include"VulkanAppFramework.h" #include<hgl/WorkManager.h>
#include<hgl/math/HalfFloat.h> #include<hgl/math/HalfFloat.h>
#include<hgl/graph/VKVertexInputConfig.h> #include<hgl/graph/VKVertexInputConfig.h>
#include<hgl/graph/PrimitiveCreater.h> #include<hgl/graph/PrimitiveCreater.h>
@ -39,7 +39,8 @@ constexpr VkFormat PositionFormat=VF_V2F;
#ifdef USE_UNORM8_COLOR #ifdef USE_UNORM8_COLOR
constexpr uint8 color_data[VERTEX_COUNT*4]= constexpr uint8 color_data[VERTEX_COUNT*4]=
{ 255,0,0,255, {
255,0,0,255,
0,255,0,255, 0,255,0,255,
0,0,255,255 0,0,255,255
}; };
@ -47,7 +48,8 @@ constexpr uint8 color_data[VERTEX_COUNT*4]=
constexpr VkFormat ColorFormat=VF_V4UN8; constexpr VkFormat ColorFormat=VF_V4UN8;
#else #else
constexpr float color_data[VERTEX_COUNT*4]= constexpr float color_data[VERTEX_COUNT*4]=
{ 1,0,0,1, {
1,0,0,1,
0,1,0,1, 0,1,0,1,
0,0,1,1 0,0,1,1
}; };
@ -55,10 +57,12 @@ constexpr float color_data[VERTEX_COUNT*4]=
constexpr VkFormat ColorFormat=VF_V4F; constexpr VkFormat ColorFormat=VF_V4F;
#endif//USE_UNORM8_COLOR #endif//USE_UNORM8_COLOR
class TestApp:public VulkanApplicationFramework class TestApp:public WorkObject
{ {
private: private:
Color4f clear_color=Color4f(0.2f,0.2f,0.2f,1.0f);
#if defined(USE_HALF_FLOAT_POSITION)||defined(USE_UNORM8_COLOR) #if defined(USE_HALF_FLOAT_POSITION)||defined(USE_UNORM8_COLOR)
VILConfig vil_config; VILConfig vil_config;
#endif #endif
@ -83,7 +87,7 @@ private:
bool InitAutoMaterial() bool InitAutoMaterial()
{ {
mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"VertexColor2d",Prim::Triangles); mtl::Material2DCreateConfig cfg(GetDeviceAttribute(),"VertexColor2d",Prim::Triangles);
cfg.coordinate_system=CoordinateSystem2D::NDC; cfg.coordinate_system=CoordinateSystem2D::NDC;
cfg.local_to_world=false; cfg.local_to_world=false;
@ -105,54 +109,47 @@ private:
bool InitVBO() bool InitVBO()
{ {
PrimitiveCreater rpc(device,material_instance->GetVIL());
rpc.Init("Triangle",VERTEX_COUNT);
#ifdef USE_HALF_FLOAT_POSITION #ifdef USE_HALF_FLOAT_POSITION
Float32toFloat16(position_data_hf,position_data_float,VERTEX_COUNT*2); Float32toFloat16(position_data_hf,position_data_float,VERTEX_COUNT*2);
#endif//USE_HALF_FLOAT_POSITION #endif//USE_HALF_FLOAT_POSITION
if(!rpc.WriteVAB(VAN::Position, PositionFormat, position_data))return(false); render_obj=CreateRenderable("Triangle",VERTEX_COUNT,material_instance,pipeline,
if(!rpc.WriteVAB(VAN::Color, ColorFormat, color_data ))return(false); {
{VAN::Position,PositionFormat,position_data},
render_obj=db->CreateRenderable(&rpc,material_instance,pipeline); {VAN::Color, ColorFormat, color_data}
});
return(render_obj); return(render_obj);
} }
public: public:
bool Init(uint w,uint h) TestApp(RenderFramework *rf):WorkObject(rf,rf->GetSwapchainRenderTarget())
{ {
if(!VulkanApplicationFramework::Init(w,h))
return(false);
InitVIL(); InitVIL();
if(!InitAutoMaterial()) if(!InitAutoMaterial())
return(false); return;
if(!InitPipeline()) if(!InitPipeline())
return(false); return;
if(!InitVBO()) if(!InitVBO())
return(false); return;
if(!BuildCommandBuffer(render_obj))
return(false);
return(true);
} }
void Resize(uint w,uint h)override void Tick(double)override{}
void Render(double delta_time,graph::RenderCmdBuffer *cmd)
{ {
VulkanApplicationFramework::Resize(w,h); cmd->SetClearColor(0,clear_color);
BuildCommandBuffer(render_obj); cmd->BeginRenderPass();
cmd->Render(render_obj);
cmd->EndRenderPass();
} }
};//class TestApp:public VulkanApplicationFramework };//class TestApp:public WorkObject
int main(int,char **) int os_main(int,os_char **)
{ {
return RunApp<TestApp>(SCREEN_WIDTH,SCREEN_HEIGHT); return RunFramework<TestApp>(OS_TEXT("RenderFramework Test"));
} }

View File

@ -1,5 +1,4 @@
// 该范例主要演示使用NDC坐标系直接绘制一个渐变色的三角形 #include<hgl/WorkManager.h>
#include<hgl/WorkManager.h>
#include<hgl/graph/VKVertexInputConfig.h> #include<hgl/graph/VKVertexInputConfig.h>
#include<hgl/graph/VKRenderResource.h> #include<hgl/graph/VKRenderResource.h>
#include<hgl/graph/mtl/Material2DCreateConfig.h> #include<hgl/graph/mtl/Material2DCreateConfig.h>
@ -21,7 +20,8 @@ constexpr float position_data[VERTEX_COUNT*2]=
}; };
constexpr float color_data[VERTEX_COUNT*4]= constexpr float color_data[VERTEX_COUNT*4]=
{ 1,0,0,1, {
1,0,0,1,
0,1,0,1, 0,1,0,1,
0,0,1,1 0,0,1,1
}; };

View File

@ -10,7 +10,7 @@
#include<hgl/graph/mtl/ShaderBuffer.h> #include<hgl/graph/mtl/ShaderBuffer.h>
#include<hgl/graph/VKSamplerType.h> #include<hgl/graph/VKSamplerType.h>
namespace hgl{namespace graph namespace hgl::graph
{ {
struct GPUDeviceAttribute; struct GPUDeviceAttribute;
struct UBODescriptor; struct UBODescriptor;
@ -92,5 +92,4 @@ namespace hgl{namespace graph
bool CreateShader(); bool CreateShader();
};//class MaterialCreateInfo };//class MaterialCreateInfo
}//namespace mtl }//namespace mtl
}//namespace graph }//namespace hgl::graph
}//namespace hgl