ULRE/example/Vulkan/fourth_triangle.cpp

144 lines
3.2 KiB
C++
Raw Normal View History

2023-05-22 15:29:07 +08:00
// fourth_triangle
// 该范例主要演示使用材质实例传递颜色参数绘制三角形
#include"VulkanAppFramework.h"
#include<hgl/math/Math.h>
#include<hgl/filesystem/FileSystem.h>
#include<hgl/graph/VKRenderablePrimitiveCreater.h>
2023-06-02 20:45:19 +08:00
#include<hgl/graph/mtl/2d/Material2DCreateConfig.h>
2023-05-22 15:29:07 +08:00
#include<hgl/graph/RenderList.h>
using namespace hgl;
using namespace hgl::graph;
constexpr uint32_t SCREEN_WIDTH=1024;
constexpr uint32_t SCREEN_HEIGHT=1024;
constexpr uint32_t VERTEX_COUNT=3;
constexpr float position_data[VERTEX_COUNT*2]=
{
0.0, 0.0,
-0.1, 0.9,
0.1, 0.9
};
2023-06-05 21:39:12 +08:00
constexpr float color_data[6][4]=
2023-05-22 15:29:07 +08:00
{
{1,0,0,1},
2023-06-05 21:39:12 +08:00
{1,1,0,1},
2023-05-22 15:29:07 +08:00
{0,1,0,1},
2023-06-05 21:39:12 +08:00
{0,1,1,1},
{0,0,1,1},
{1,0,1,1},
2023-05-22 15:29:07 +08:00
};
class TestApp:public VulkanApplicationFramework
{
private:
SceneNode render_root;
RenderList * render_list =nullptr;
2023-08-18 18:39:25 +08:00
MaterialInstance * material_instance[6]{};
Renderable * render_obj[6] {};
2023-05-22 15:29:07 +08:00
Pipeline * pipeline =nullptr;
private:
bool InitMaterial()
{
{
2023-06-02 20:45:19 +08:00
mtl::Material2DCreateConfig cfg(device->GetDeviceAttribute(),"PureColor2D");
2023-05-22 15:29:07 +08:00
cfg.coordinate_system=CoordinateSystem2D::NDC;
cfg.local_to_world=true;
AutoDelete<mtl::MaterialCreateInfo> mci=mtl::CreatePureColor2D(&cfg);
for(uint i=0;i<6;i++)
2023-06-05 21:39:12 +08:00
{
material_instance[i]=db->CreateMaterialInstance(mci);
2023-05-22 15:29:07 +08:00
2023-06-05 21:39:12 +08:00
if(!material_instance[i])
return(false);
material_instance[i]->WriteMIData(color_data[i],sizeof(float)*4); //设置MaterialInstance的数据
}
2023-06-05 21:39:12 +08:00
}
2023-05-22 15:29:07 +08:00
// pipeline=db->CreatePipeline(material_instance,sc_render_target,OS_TEXT("res/pipeline/solid2d"));
2023-06-05 21:39:12 +08:00
// pipeline=CreatePipeline(material_instance,InlinePipeline::Solid2D,Prim::Triangles); //等同上一行为Framework重载默认使用swapchain的render target
2023-05-22 15:29:07 +08:00
return pipeline;
}
bool InitVBO()
{
2023-06-05 21:39:12 +08:00
//RenderablePrimitiveCreater rpc(db,VERTEX_COUNT);
2023-05-22 15:29:07 +08:00
2023-06-05 21:39:12 +08:00
//if(!rpc.SetVBO(VAN::Position, VF_V2F, position_data))return(false);
//
//render_obj=rpc.Create(material_instance,pipeline);
2023-05-22 15:29:07 +08:00
2023-06-05 21:39:12 +08:00
//if(!render_obj)
// return(false);
//
//for(uint i=0;i<6;i++)
//{
// render_root.CreateSubNode(rotate(deg2rad(60*i),Vector3f(0,0,1)),render_obj);
//}
2023-05-22 15:29:07 +08:00
2023-06-05 21:39:12 +08:00
//render_root.RefreshMatrix();
2023-05-22 15:29:07 +08:00
2023-06-05 21:39:12 +08:00
//render_list->Expend(&render_root);
2023-05-22 15:29:07 +08:00
return(true);
}
public:
~TestApp()
{
SAFE_CLEAR(render_list);
}
bool Init()
{
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
return(false);
render_list=new RenderList(device);
if(!InitMaterial())
return(false);
if(!InitVBO())
return(false);
BuildCommandBuffer(render_list);
return(true);
}
void Resize(int w,int h)override
{
VulkanApplicationFramework::Resize(w,h);
BuildCommandBuffer(render_list);
}
};//class TestApp:public VulkanApplicationFramework
int main(int,char **)
{
TestApp app;
if(!app.Init())
return(-1);
while(app.Run());
return 0;
}