2022-06-24 16:27:43 +08:00
|
|
|
|
// third_triangle
|
|
|
|
|
// 该范例主要演示使用2D坐系统直接绘制一个渐变色的三角形
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
|
|
|
|
#include"VulkanAppFramework.h"
|
|
|
|
|
#include<hgl/math/Math.h>
|
|
|
|
|
#include<hgl/filesystem/FileSystem.h>
|
2022-06-24 16:27:43 +08:00
|
|
|
|
#include<hgl/graph/SceneInfo.h>
|
2023-03-22 18:15:45 +08:00
|
|
|
|
#include<hgl/graph/VKRenderablePrimitiveCreater.h>
|
2023-03-22 15:58:59 +08:00
|
|
|
|
#include<hgl/graph/mtl/2d/VertexColor2D.h>
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t SCREEN_WIDTH=1280;
|
|
|
|
|
constexpr uint32_t SCREEN_HEIGHT=720;
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t VERTEX_COUNT=3;
|
|
|
|
|
|
2023-03-22 17:56:36 +08:00
|
|
|
|
static float position_data[VERTEX_COUNT][2]=
|
2020-12-02 20:27:36 +08:00
|
|
|
|
{
|
2023-03-22 17:56:36 +08:00
|
|
|
|
{0.5, 0.25},
|
|
|
|
|
{0.75, 0.75},
|
|
|
|
|
{0.25, 0.75}
|
2020-12-02 20:27:36 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constexpr float color_data[VERTEX_COUNT][4]=
|
2022-10-14 17:52:35 +08:00
|
|
|
|
{
|
|
|
|
|
{1,0,0,1},
|
2020-12-02 20:27:36 +08:00
|
|
|
|
{0,1,0,1},
|
|
|
|
|
{0,0,1,1}
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-22 17:56:36 +08:00
|
|
|
|
#define USE_ZERO2ONE_COORD //使用左上角0,0右下角1,1的坐标系
|
|
|
|
|
|
2020-12-02 20:27:36 +08:00
|
|
|
|
class TestApp:public VulkanApplicationFramework
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
2021-06-16 21:03:52 +08:00
|
|
|
|
MaterialInstance * material_instance =nullptr;
|
2022-06-24 21:17:28 +08:00
|
|
|
|
Renderable * render_obj =nullptr;
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
|
|
|
|
Pipeline * pipeline =nullptr;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
bool InitMaterial()
|
2023-03-22 17:56:36 +08:00
|
|
|
|
{
|
|
|
|
|
#ifdef USE_ZERO2ONE_COORD
|
|
|
|
|
MaterialCreateInfo *mci=mtl::CreateVertexColor2D(CoordinateSystem2D::ZeroToOne);
|
|
|
|
|
#else
|
2023-03-22 15:58:59 +08:00
|
|
|
|
MaterialCreateInfo *mci=mtl::CreateVertexColor2D(CoordinateSystem2D::Ortho);
|
2023-03-22 17:56:36 +08:00
|
|
|
|
#endif//USE_ZERO2ONE_COORD
|
2023-03-22 15:58:59 +08:00
|
|
|
|
|
|
|
|
|
//material_instance=db->CreateMaterialInstance(OS_TEXT("res/material/VertexColor2D"));
|
|
|
|
|
|
|
|
|
|
material_instance=db->CreateMaterialInstance(mci);
|
|
|
|
|
|
|
|
|
|
delete mci;
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
|
|
|
|
if(!material_instance)
|
|
|
|
|
return(false);
|
2022-03-09 20:55:09 +08:00
|
|
|
|
|
2023-03-22 02:41:19 +08:00
|
|
|
|
db->BindGlobalDescriptor(material_instance);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
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
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
2021-09-22 17:22:17 +08:00
|
|
|
|
return pipeline;
|
2020-12-02 20:27:36 +08:00
|
|
|
|
}
|
2022-06-24 16:27:43 +08:00
|
|
|
|
|
2020-12-02 20:27:36 +08:00
|
|
|
|
bool InitVBO()
|
|
|
|
|
{
|
2023-03-22 18:15:45 +08:00
|
|
|
|
RenderablePrimitiveCreater rpc(db,VERTEX_COUNT);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
2023-03-22 17:56:36 +08:00
|
|
|
|
#ifndef USE_ZERO2ONE_COORD //使用ortho坐标系
|
|
|
|
|
|
|
|
|
|
for(uint i=0;i<VERTEX_COUNT;i++)
|
|
|
|
|
{
|
|
|
|
|
position_data[i][0]*=SCREEN_WIDTH;
|
|
|
|
|
position_data[i][1]*=SCREEN_HEIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif//USE_ZERO2ONE_COORD
|
|
|
|
|
|
2023-03-22 18:15:45 +08:00
|
|
|
|
if(!rpc.SetVBO(VAN::Position, VF_V2F, position_data))return(false);
|
|
|
|
|
if(!rpc.SetVBO(VAN::Color, VF_V4F, color_data ))return(false);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
2023-03-22 18:15:45 +08:00
|
|
|
|
render_obj=rpc.Create(material_instance,pipeline);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
bool Init()
|
|
|
|
|
{
|
|
|
|
|
if(!VulkanApplicationFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitMaterial())
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
if(!InitVBO())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
if(!BuildCommandBuffer(render_obj))
|
2022-06-24 16:27:43 +08:00
|
|
|
|
return(false);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Resize(int w,int h)override
|
|
|
|
|
{
|
2022-03-09 20:55:09 +08:00
|
|
|
|
VulkanApplicationFramework::Resize(w,h);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
|
2022-06-24 21:17:28 +08:00
|
|
|
|
BuildCommandBuffer(render_obj);
|
2020-12-02 20:27:36 +08:00
|
|
|
|
}
|
|
|
|
|
};//class TestApp:public VulkanApplicationFramework
|
|
|
|
|
|
|
|
|
|
int main(int,char **)
|
|
|
|
|
{
|
|
|
|
|
TestApp app;
|
|
|
|
|
|
|
|
|
|
if(!app.Init())
|
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
|
|
while(app.Run());
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|