diff --git a/example/Basic/CMakeLists.txt b/example/Basic/CMakeLists.txt index cdd141e0..b1c07df0 100644 --- a/example/Basic/CMakeLists.txt +++ b/example/Basic/CMakeLists.txt @@ -16,3 +16,5 @@ CreateProject(02_auto_instance auto_instance.cpp) CreateProject(03_auto_merge_material_instance auto_merge_material_instance.cpp) CreateProject(04_Billboard BillboardTest.cpp) + +CreateProject(05_RenderBoundBox RenderBoundBox.cpp) diff --git a/example/Basic/RenderBoundBox.cpp b/example/Basic/RenderBoundBox.cpp new file mode 100644 index 00000000..eaf3665c --- /dev/null +++ b/example/Basic/RenderBoundBox.cpp @@ -0,0 +1,310 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace hgl; +using namespace hgl::graph; + +constexpr const COLOR TestColor[]= +{ + COLOR::MozillaCharcoal, + COLOR::MozillaSand, + + COLOR::BlenderAxisRed, + COLOR::BlenderAxisGreen, + COLOR::BlenderAxisBlue, + + COLOR::BananaYellow, +}; + +constexpr const size_t COLOR_COUNT=sizeof(TestColor)/sizeof(COLOR); + +class TestApp:public WorkObject +{ +private: + + + + struct MaterialData + { + Material * material = nullptr; + const VIL * vil = nullptr; + + Pipeline * pipeline = nullptr; + MaterialInstance * mi[COLOR_COUNT]{}; + }; + + MaterialData solid; + MaterialData wire; + + VertexDataManager * mesh_vdm=nullptr; + + struct RenderMesh + { + Primitive *prim; + Mesh *mesh; + MeshComponentData *data; + ComponentDataPtr cdp; + + public: + + ~RenderMesh() + { + cdp.unref(); + delete mesh; + delete prim; + } + }; + + RenderMesh *rm_plane=nullptr, + *rm_sphere=nullptr, + *rm_cone=nullptr, + *rm_cylinder=nullptr, + *rm_torus=nullptr, + *rm_box=nullptr; + +private: + + bool InitMaterialInstance(MaterialData *md) + { + if(!md) + return(false); + + Color4f color; + + for(size_t i=0;imi[i]=CreateMaterialInstance(md->material,nullptr,&color); + + if(!md->mi[i]) + return(false); + } + + md->vil=md->material->GetDefaultVIL(); + + if(!md->vil) + return(false); + + md->pipeline=CreatePipeline(md->material,InlinePipeline::Solid3D); + + return md->pipeline; + } + + bool InitSolidMDP() + { + mtl::Material3DCreateConfig cfg(PrimitiveType::Triangles); + + mtl::MaterialCreateInfo *mci=mtl::CreateGizmo3D(GetDevAttr(),&cfg); + + if(!mci) + return(false); + + solid.material=CreateMaterial("Gizmo3D",mci); + + return InitMaterialInstance(&solid); + } + + bool InitWireMDP() + { + mtl::Material3DCreateConfig cfg(PrimitiveType::Lines); + + mtl::MaterialCreateInfo *mci=mtl::CreatePureColor3D(GetDevAttr(),&cfg); + + if(!mci) + return(false); + + wire.material=CreateMaterial("PureColorLine3D",mci); + + return InitMaterialInstance(&wire); + } + + bool InitVDM() + { + mesh_vdm=CreateVDM(solid.vil,HGL_SIZE_1MB); + + if(!mesh_vdm) + return(false); + + return true; + } + + RenderMesh *CreateRenderMesh(Primitive *prim,MaterialData *md,const int color) + { + if(!prim) + return(nullptr); + + Mesh *mesh=graph::CreateMesh(prim,md->mi[color],md->pipeline); + + if(!mesh) + return nullptr; + + RenderMesh *rm=new RenderMesh; + + rm->prim=prim; + rm->mesh=mesh; + rm->data=new MeshComponentData(mesh); + rm->cdp =rm->data; + + return rm; + } + + bool CreateGeometryMesh() + { + using namespace inline_geometry; + + PrimitiveCreater *prim_creater=new PrimitiveCreater(mesh_vdm); + + if(!prim_creater) + return(false); + + rm_plane=CreateRenderMesh(CreatePlaneSqaure(prim_creater),&solid,0); + + //Sphere + rm_sphere=CreateRenderMesh(CreateSphere(prim_creater,16),&solid,1); + + //Cone + { + struct ConeCreateInfo cci; + + cci.radius =1; //圆锥半径 + cci.halfExtend =1; //圆锤一半高度 + cci.numberSlices=16; //圆锥底部分割数 + cci.numberStacks=8; //圆锥高度分割数 + + rm_cone=CreateRenderMesh(CreateCone(prim_creater,&cci),&solid,2); + } + + //Cyliner + { + struct CylinderCreateInfo cci; + + cci.halfExtend =1.25; //圆柱一半高度 + cci.numberSlices=16; //圆柱底部分割数 + cci.radius =1.25f; //圆柱半径 + + rm_cylinder=CreateRenderMesh(CreateCylinder(prim_creater,&cci),&solid,3); + } + + //Torus + { + struct TorusCreateInfo tci; + + tci.innerRadius=0.975; + tci.outerRadius=1.0; + tci.numberSlices=64; + tci.numberStacks=8; + + rm_torus=CreateRenderMesh(CreateTorus(prim_creater,&tci),&solid,4); + } + + delete prim_creater; + return(true); + } + + bool CreateBoundingBoxMesh() + { + using namespace inline_geometry; + + auto pc=GetPrimitiveCreater(wire.material); + + inline_geometry::BoundingBoxCreateInfo bbci; + + rm_box=CreateRenderMesh(CreateBoundingBox(pc,&bbci),&wire,5); + + return rm_box; + } + + bool InitScene() + { + CreateComponentInfo cci(GetSceneRoot()); + + { + cci.mat=scale(10,10,1); + + CreateComponent(&cci,rm_plane->cdp); + } + + { + //cci.mat=rotate(90,AxisVector::Y); + cci.mat=Identity4f; + auto *mc=CreateComponent(&cci,rm_torus->cdp); + mc->SetOverrideMaterial(solid.mi[1]); + } + + return(true); + } + + bool InitBoundingBoxScene() + { + CreateComponentInfo cci(GetSceneRoot()); + + CreateComponent(&cci,rm_box->cdp); + + CameraControl *camera_control=GetCameraControl(); + + camera_control->SetPosition(Vector3f(8,8,8)); + camera_control->SetTarget(Vector3f(0,0,0)); + + return(true); + } + +public: + + using WorkObject::WorkObject; + + ~TestApp() + { + SAFE_CLEAR(rm_box) + + SAFE_CLEAR(rm_torus) + SAFE_CLEAR(rm_cylinder) + SAFE_CLEAR(rm_cone) + SAFE_CLEAR(rm_sphere) + SAFE_CLEAR(rm_plane) + + SAFE_CLEAR(mesh_vdm) + } + + bool Init() override + { + if(!InitSolidMDP()) + return(false); + + if(!InitWireMDP()) + return(false); + + if(!InitVDM()) + return(false); + + if(!CreateGeometryMesh()) + return(false); + + if(!CreateBoundingBoxMesh()) + return(false); + + if(!InitScene()) + return(false); + + if(!InitBoundingBoxScene()) + return(false); + + return(true); + } +};//class TestApp:public CameraAppFramework + +int os_main(int,os_char **) +{ + return RunFramework(OS_TEXT("Render Bounding Box"),1280,720); +} diff --git a/inc/hgl/graph/InlineGeometry.h b/inc/hgl/graph/InlineGeometry.h index 4ea60832..810ed7b3 100644 --- a/inc/hgl/graph/InlineGeometry.h +++ b/inc/hgl/graph/InlineGeometry.h @@ -1,5 +1,4 @@ -#ifndef HGL_GRAPH_INLINE_GEOMETRY_INCLUDE -#define HGL_GRAPH_INLINE_GEOMETRY_INCLUDE +#pragma once #include #include @@ -7,235 +6,232 @@ #include #include #include -namespace hgl + +namespace hgl::graph { - namespace graph + class PrimitiveCreater; + + namespace inline_geometry { - class PrimitiveCreater; - - namespace inline_geometry + /** + * 矩形创建信息(扇形/三角形条) + */ + struct RectangleCreateInfo { - /** - * 矩形创建信息(扇形/三角形条) - */ - struct RectangleCreateInfo - { - RectScope2f scope; - };//struct RectangleCreateInfo + RectScope2f scope; + };//struct RectangleCreateInfo - Primitive *CreateRectangle(PrimitiveCreater *pc,const RectangleCreateInfo *rci); + Primitive *CreateRectangle(PrimitiveCreater *pc,const RectangleCreateInfo *rci); - /** - * 创建延迟渲染用全屏平面 - */ - Primitive *CreateGBufferCompositionRectangle(PrimitiveCreater *pc); + /** + * 创建延迟渲染用全屏平面 + */ + Primitive *CreateGBufferCompositionRectangle(PrimitiveCreater *pc); - /** - * 圆角矩形创建信息(扇形/线圈) - */ - struct RoundRectangleCreateInfo:public RectangleCreateInfo - { - float radius; ///<圆角半径 - uint32_t round_per; ///<圆角精度 - };//struct RoundRectangleCreateInfo:public RectangleCreateInfo + /** + * 圆角矩形创建信息(扇形/线圈) + */ + struct RoundRectangleCreateInfo:public RectangleCreateInfo + { + float radius; ///<圆角半径 + uint32_t round_per; ///<圆角精度 + };//struct RoundRectangleCreateInfo:public RectangleCreateInfo - Primitive *CreateRoundRectangle(PrimitiveCreater *pc,const RoundRectangleCreateInfo *rci); + Primitive *CreateRoundRectangle(PrimitiveCreater *pc,const RoundRectangleCreateInfo *rci); - /** - * 圆形创建信息 - */ - struct CircleCreateInfo - { - Vector2f center; ///<圆心坐标 - Vector2f radius; ///<半径 - uint field_count=8; ///<分段数量 + /** + * 圆形创建信息 + */ + struct CircleCreateInfo + { + Vector2f center; ///<圆心坐标 + Vector2f radius; ///<半径 + uint field_count=8; ///<分段数量 - bool has_center; ///<是否有圆心点 + bool has_center; ///<是否有圆心点 - Vector4f center_color; ///<圆心颜色 - Vector4f border_color; ///<边缘颜色 - };//struct CircleCreateInfo + Vector4f center_color; ///<圆心颜色 + Vector4f border_color; ///<边缘颜色 + };//struct CircleCreateInfo - /** - * 创建一个2D圆形(扇形/线圈) - */ - Primitive *CreateCircle2D(PrimitiveCreater *pc,const CircleCreateInfo *cci); + /** + * 创建一个2D圆形(扇形/线圈) + */ + Primitive *CreateCircle2D(PrimitiveCreater *pc,const CircleCreateInfo *cci); - /** - * 创建一个3D圆形(扇形/圆形,XY,Z永远为0) - */ - Primitive *CreateCircle3D(PrimitiveCreater *pc,const CircleCreateInfo *cci); + /** + * 创建一个3D圆形(扇形/圆形,XY,Z永远为0) + */ + Primitive *CreateCircle3D(PrimitiveCreater *pc,const CircleCreateInfo *cci); - /** - * 创建一个使用三角形绘制的3D圆形(扇形/圆形,XY,Z永远为0) - */ - Primitive *CreateCircle3DByIndexTriangles(PrimitiveCreater *pc,const CircleCreateInfo *cci); + /** + * 创建一个使用三角形绘制的3D圆形(扇形/圆形,XY,Z永远为0) + */ + Primitive *CreateCircle3DByIndexTriangles(PrimitiveCreater *pc,const CircleCreateInfo *cci); - /** - * 平面网格创建信息
- * 会创建一个在XY平面上居中的网格,单个格子尺寸为1。 - */ - struct PlaneGridCreateInfo + /** + * 平面网格创建信息
+ * 会创建一个在XY平面上居中的网格,单个格子尺寸为1。 + */ + struct PlaneGridCreateInfo + { + Size2u grid_size; ///<格子数量 + + Size2u sub_count; ///<细分格子数量 + + uint8 lum; ///<一般线条亮度 + uint8 sub_lum; ///<细分及边界线条亮度 + };//struct PlaneGridCreateInfo + + /** + * 创建一个平面网格(线条) + */ + Primitive *CreatePlaneGrid2D(PrimitiveCreater *pc,const PlaneGridCreateInfo *pgci); //创建一个平面网格(线条) + + Primitive *CreatePlaneGrid3D(PrimitiveCreater *pc,const PlaneGridCreateInfo *pgci); + + /** + * 创建一个平面正方形(三角形) + */ + Primitive *CreatePlaneSqaure(PrimitiveCreater *pc); + + struct CubeCreateInfo + { + bool normal; + bool tangent; + bool tex_coord; + + enum class ColorType { - Size2u grid_size; ///<格子数量 + NoColor=0, ///<没有颜色 + SameColor, ///<一个颜色 + FaceColor, ///<每个面一个颜色(请写入6个颜色值) + VertexColor, ///<每个顶点一个颜色(请写入24个颜色值) - Size2u sub_count; ///<细分格子数量 + ENUM_CLASS_RANGE(NoColor,VertexColor) + }; - uint8 lum; ///<一般线条亮度 - uint8 sub_lum; ///<细分及边界线条亮度 - };//struct PlaneGridCreateInfo + ColorType color_type; + Vector4f color[24]; - /** - * 创建一个平面网格(线条) - */ - Primitive *CreatePlaneGrid2D(PrimitiveCreater *pc,const PlaneGridCreateInfo *pgci); //创建一个平面网格(线条) + public: - Primitive *CreatePlaneGrid3D(PrimitiveCreater *pc,const PlaneGridCreateInfo *pgci); - - /** - * 创建一个平面正方形(三角形) - */ - Primitive *CreatePlaneSqaure(PrimitiveCreater *pc); - - struct CubeCreateInfo + CubeCreateInfo() { - bool normal; - bool tangent; - bool tex_coord; + normal=false; + tangent=false; + tex_coord=false; - enum class ColorType - { - NoColor=0, ///<没有颜色 - SameColor, ///<一个颜色 - FaceColor, ///<每个面一个颜色(请写入6个颜色值) - VertexColor, ///<每个顶点一个颜色(请写入24个颜色值) + color_type=ColorType::NoColor; + } + };//struct CubeCreateInfo - ENUM_CLASS_RANGE(NoColor,VertexColor) - }; + /** + * 创建一个立方体(三角形) + */ + Primitive *CreateCube(PrimitiveCreater *pc,const CubeCreateInfo *cci); - ColorType color_type; - Vector4f color[24]; + struct BoundingBoxCreateInfo + { + bool normal; - public: - - CubeCreateInfo() - { - normal=false; - tangent=false; - tex_coord=false; - - color_type=ColorType::NoColor; - } - };//struct CubeCreateInfo - - /** - * 创建一个立方体(三角形) - */ - Primitive *CreateCube(PrimitiveCreater *pc,const CubeCreateInfo *cci); - - struct BoundingBoxCreateInfo + enum class ColorType { - bool normal; + NoColor=0, ///<没有颜色 + SameColor, ///<一个颜色 + VertexColor, ///<每个顶点一个颜色(请写入8个颜色值) - enum class ColorType - { - NoColor=0, ///<没有颜色 - SameColor, ///<一个颜色 - VertexColor, ///<每个顶点一个颜色(请写入8个颜色值) + ENUM_CLASS_RANGE(NoColor,VertexColor) + }; - ENUM_CLASS_RANGE(NoColor,VertexColor) - }; + ColorType color_type; + Vector4f color[8]; - ColorType color_type; - Vector4f color[8]; + public: - public: - - BoundingBoxCreateInfo(bool n=false,ColorType ct=ColorType::NoColor) - { - normal=n; - - color_type=ct; - } - };//struct BoundingBoxCreateInfo - - /** - * 创建一个绑定盒(线条) - */ - Primitive *CreateBoundingBox(PrimitiveCreater *pc,const BoundingBoxCreateInfo *cci); - - /** - * 创建一个球心坐标为0,0,0,半径为1的球体(三角形) - */ - Primitive *CreateSphere(PrimitiveCreater *,const uint numberSlices); - - /** - * 创建一个穹顶(三角形) - */ - Primitive *CreateDome(PrimitiveCreater *pc, const uint numberSlices); - - struct TorusCreateInfo + BoundingBoxCreateInfo(bool n=false,ColorType ct=ColorType::NoColor) { - float innerRadius, - outerRadius; + normal=n; - uint numberSlices, - numberStacks; + color_type=ct; + } + };//struct BoundingBoxCreateInfo - Vector2f uv_scale={1.0,1.0}; - };//struct TorusCreateInfo + /** + * 创建一个绑定盒(线条) + */ + Primitive *CreateBoundingBox(PrimitiveCreater *pc,const BoundingBoxCreateInfo *cci); - /** - * 创建一个圆环(三角形) - */ - Primitive *CreateTorus(PrimitiveCreater *pc,const TorusCreateInfo *tci); + /** + * 创建一个球心坐标为0,0,0,半径为1的球体(三角形) + */ + Primitive *CreateSphere(PrimitiveCreater *,const uint numberSlices); - struct CylinderCreateInfo + /** + * 创建一个穹顶(三角形) + */ + Primitive *CreateDome(PrimitiveCreater *pc, const uint numberSlices); + + struct TorusCreateInfo + { + float innerRadius, + outerRadius; + + uint numberSlices, + numberStacks; + + Vector2f uv_scale={1.0,1.0}; + };//struct TorusCreateInfo + + /** + * 创建一个圆环(三角形) + */ + Primitive *CreateTorus(PrimitiveCreater *pc,const TorusCreateInfo *tci); + + struct CylinderCreateInfo + { + float halfExtend, //高度 + radius; //半径 + uint numberSlices; + };//struct CylinderCreateInfo + + /** + * 创建一个圆柱(三角形) + */ + Primitive *CreateCylinder(PrimitiveCreater *,const CylinderCreateInfo *cci); + + struct ConeCreateInfo + { + float halfExtend, //高度 + radius; //半径 + uint numberSlices, //圆切分精度 + numberStacks; //柱高层数 + };//struct ConeCreateInfo + + /** + * 创建一个圆锥(三角形) + */ + Primitive *CreateCone(PrimitiveCreater *,const ConeCreateInfo *cci); + + struct AxisCreateInfo + { + float size; + Color4f color[3]; + + public: + + AxisCreateInfo() { - float halfExtend, //高度 - radius; //半径 - uint numberSlices; - };//struct CylinderCreateInfo + size=1.0f; + color[0].Set(1,0,0,1); + color[1].Set(0,1,0,1); + color[2].Set(0,0,1,1); + } + };//struct AxisCreateInfo - /** - * 创建一个圆柱(三角形) - */ - Primitive *CreateCylinder(PrimitiveCreater *,const CylinderCreateInfo *cci); - - struct ConeCreateInfo - { - float halfExtend, //高度 - radius; //半径 - uint numberSlices, //圆切分精度 - numberStacks; //柱高层数 - };//struct ConeCreateInfo - - /** - * 创建一个圆锥(三角形) - */ - Primitive *CreateCone(PrimitiveCreater *,const ConeCreateInfo *cci); - - struct AxisCreateInfo - { - float size; - Color4f color[3]; - - public: - - AxisCreateInfo() - { - size=1.0f; - color[0].Set(1,0,0,1); - color[1].Set(0,1,0,1); - color[2].Set(0,0,1,1); - } - };//struct AxisCreateInfo - - /** - * 创建一个坐标线(线条) - */ - Primitive *CreateAxis(PrimitiveCreater *pc,const AxisCreateInfo *aci); - }//namespace inline_geometry - }//namespace graph -};//namespace hgl -#endif//HGL_GRAPH_INLINE_GEOMETRY_INCLUDE + /** + * 创建一个坐标线(线条) + */ + Primitive *CreateAxis(PrimitiveCreater *pc,const AxisCreateInfo *aci); + }//namespace inline_geometry +}//namespace hgl::graph