From f70be8e42c2a605a9cbddb34d342dd0dca9067fc Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Fri, 24 Jun 2022 21:36:24 +0800 Subject: [PATCH] moved to inline_geometry namespace they are CreateRenderable....functions. --- example/Vulkan/Atmosphere.cpp | 2 +- example/Vulkan/Cubemap.cpp | 8 +- example/Vulkan/DeferredRender.cpp | 16 +- example/Vulkan/EquirectangularMap.cpp | 2 +- example/Vulkan/Geometry3D.cpp | 8 +- example/Vulkan/InlineGeometryScene.cpp | 14 +- example/Vulkan/LoadStaticMesh.cpp | 4 +- example/Vulkan/OffscreenRender.cpp | 12 +- example/Vulkan/RayPicking.cpp | 4 +- example/Vulkan/SceneTree.cpp | 6 +- example/Vulkan/SkyColor.cpp | 2 +- example/Vulkan/TerrainSimple.cpp | 4 +- inc/hgl/graph/InlineGeometry.h | 395 ++--- src/SceneGraph/InlineGeometry.cpp | 1959 ++++++++++++------------ 14 files changed, 1229 insertions(+), 1207 deletions(-) diff --git a/example/Vulkan/Atmosphere.cpp b/example/Vulkan/Atmosphere.cpp index f15eef05..8cd6a408 100644 --- a/example/Vulkan/Atmosphere.cpp +++ b/example/Vulkan/Atmosphere.cpp @@ -81,7 +81,7 @@ private: bool InitScene() { - ro_sphere=CreateRenderableSphere(db,material_instance->GetVAB(),128); + ro_sphere=inline_geometry::CreateSphere(db,material_instance->GetVAB(),128); render_root.CreateSubNode(scale(100),db->CreateRenderable(ro_sphere,material_instance,pipeline_solid)); diff --git a/example/Vulkan/Cubemap.cpp b/example/Vulkan/Cubemap.cpp index e82addd6..40238cb7 100644 --- a/example/Vulkan/Cubemap.cpp +++ b/example/Vulkan/Cubemap.cpp @@ -121,22 +121,24 @@ private: void CreateRenderObject() { + using namespace inline_geometry; + { struct AxisCreateInfo aci; aci.size=GetCameraInfo().zfar; - ro_axis=CreateRenderableAxis(db,axis_mi->GetVAB(),&aci); + ro_axis=CreateAxis(db,axis_mi->GetVAB(),&aci); } { struct CubeCreateInfo cci; - ro_cube=CreateRenderableCube(db,sky_mi->GetVAB(),&cci); + ro_cube=CreateCube(db,sky_mi->GetVAB(),&cci); } { - ro_sphere=CreateRenderableSphere(db,envmap_mi->GetVAB(),64); + ro_sphere=CreateSphere(db,envmap_mi->GetVAB(),64); } } diff --git a/example/Vulkan/DeferredRender.cpp b/example/Vulkan/DeferredRender.cpp index e077ea11..f4adc5e6 100644 --- a/example/Vulkan/DeferredRender.cpp +++ b/example/Vulkan/DeferredRender.cpp @@ -273,18 +273,20 @@ private: void CreateRenderObject(const VAB *vab) { + using namespace inline_geometry; + { struct PlaneCreateInfo pci; - ro_plane=CreateRenderablePlane(db,vab,&pci); + ro_plane=CreatePlane(db,vab,&pci); } { struct CubeCreateInfo cci; - ro_cube=CreateRenderableCube(db,vab,&cci); + ro_cube=CreateCube(db,vab,&cci); } { - ro_sphere=CreateRenderableSphere(db,vab,64); + ro_sphere=CreateSphere(db,vab,64); } { @@ -299,7 +301,7 @@ private: tci.uv_scale.x=4; tci.uv_scale.y=1; - ro_torus=CreateRenderableTorus(db,vab,&tci); + ro_torus=CreateTorus(db,vab,&tci); } { @@ -309,7 +311,7 @@ private: cci.radius=10; cci.numberSlices=32; - ro_cylinder=CreateRenderableCylinder(db,vab,&cci); + ro_cylinder=CreateCylinder(db,vab,&cci); } { @@ -320,13 +322,13 @@ private: cci.numberSlices=128; cci.numberStacks=32; - ro_cone=CreateRenderableCone(db,vab,&cci); + ro_cone=CreateCone(db,vab,&cci); } } bool InitCompositionRenderable() { - ro_gbc_plane=CreateRenderableGBufferComposition(db,sp_composition.material_instance->GetVAB()); + ro_gbc_plane=inline_geometry::CreateGBufferCompositionRectangle(db,sp_composition.material_instance->GetVAB()); if(!ro_gbc_plane)return(false); ro_gbc_plane_ri=db->CreateRenderable(ro_gbc_plane,sp_composition.material_instance,sp_composition.pipeline_fan); diff --git a/example/Vulkan/EquirectangularMap.cpp b/example/Vulkan/EquirectangularMap.cpp index 7a4d0cd7..9a1a0f52 100644 --- a/example/Vulkan/EquirectangularMap.cpp +++ b/example/Vulkan/EquirectangularMap.cpp @@ -64,7 +64,7 @@ private: void CreateRenderObject() { - ro_sphere=CreateRenderableSphere(db,envmap_mi->GetVAB(),128); + ro_sphere=inline_geometry::CreateSphere(db,envmap_mi->GetVAB(),128); } bool InitUBO() diff --git a/example/Vulkan/Geometry3D.cpp b/example/Vulkan/Geometry3D.cpp index 1e7d526b..388a3f0c 100644 --- a/example/Vulkan/Geometry3D.cpp +++ b/example/Vulkan/Geometry3D.cpp @@ -58,6 +58,8 @@ private: void CreateRenderObject() { + using namespace inline_geometry; + struct PlaneGridCreateInfo pgci; pgci.coord[0]=Vector3f(-100,-100,0); @@ -76,16 +78,16 @@ private: const VAB *vab=material_instance->GetVAB(); - ro_plane_grid[0]=CreateRenderablePlaneGrid(db,vab,&pgci); + ro_plane_grid[0]=CreatePlaneGrid(db,vab,&pgci); pgci.color.Set(0,0.5,0,1); pgci.side_color.Set(0,1,0,1); - ro_plane_grid[1]=CreateRenderablePlaneGrid(db,vab,&pgci); + ro_plane_grid[1]=CreatePlaneGrid(db,vab,&pgci); pgci.color.Set(0,0,0.5,1); pgci.side_color.Set(0,0,1,1); - ro_plane_grid[2]=CreateRenderablePlaneGrid(db,vab,&pgci); + ro_plane_grid[2]=CreatePlaneGrid(db,vab,&pgci); } bool InitScene() diff --git a/example/Vulkan/InlineGeometryScene.cpp b/example/Vulkan/InlineGeometryScene.cpp index bf85d559..d14b7bd7 100644 --- a/example/Vulkan/InlineGeometryScene.cpp +++ b/example/Vulkan/InlineGeometryScene.cpp @@ -147,12 +147,14 @@ private: void CreateRenderObject() { + using namespace inline_geometry; + { struct AxisCreateInfo aci; aci.size=200; - ro_axis=CreateRenderableAxis(db,axis_mi->GetVAB(),&aci); + ro_axis=CreateAxis(db,axis_mi->GetVAB(),&aci); } const VAB *vab=material_instance->GetVAB(); @@ -164,11 +166,11 @@ private: cci.tex_coord=true; cci.color_type=CubeCreateInfo::ColorType::SameColor; cci.color[0]=Vector4f(1,1,1,1); - ro_cube=CreateRenderableCube(db,vab,&cci); + ro_cube=CreateCube(db,vab,&cci); } { - ro_sphere=CreateRenderableSphere(db,vab,64); + ro_sphere=CreateSphere(db,vab,64); } { @@ -183,7 +185,7 @@ private: tci.uv_scale.x=4; tci.uv_scale.y=1; - ro_torus=CreateRenderableTorus(db,vab,&tci); + ro_torus=CreateTorus(db,vab,&tci); } { @@ -193,7 +195,7 @@ private: cci.radius=10; cci.numberSlices=32; - ro_cylinder=CreateRenderableCylinder(db,vab,&cci); + ro_cylinder=CreateCylinder(db,vab,&cci); } { @@ -204,7 +206,7 @@ private: cci.numberSlices=128; cci.numberStacks=32; - ro_cone=CreateRenderableCone(db,vab,&cci); + ro_cone=CreateCone(db,vab,&cci); } } diff --git a/example/Vulkan/LoadStaticMesh.cpp b/example/Vulkan/LoadStaticMesh.cpp index 9cd81643..fe663685 100644 --- a/example/Vulkan/LoadStaticMesh.cpp +++ b/example/Vulkan/LoadStaticMesh.cpp @@ -188,7 +188,7 @@ private: aci.size.Set(1000,1000,1000); - axis_renderable=CreateRenderableAxis(db,mp_line.material,&aci); + axis_renderable=CreateAxis(db,mp_line.material,&aci); axis_renderable_instance=CreateRenderable(mp_line,axis_renderable); } @@ -198,7 +198,7 @@ private: cci.has_color=true; cci.color.Set(1,1,1,1); - bbox_renderable=CreateRenderableBoundingBox(db,mp_line.material,&cci); + bbox_renderable=CreateBoundingBox(db,mp_line.material,&cci); bbox_renderable_instance=CreateRenderable(mp_line,bbox_renderable); } } diff --git a/example/Vulkan/OffscreenRender.cpp b/example/Vulkan/OffscreenRender.cpp index 85e699d1..db9bc412 100644 --- a/example/Vulkan/OffscreenRender.cpp +++ b/example/Vulkan/OffscreenRender.cpp @@ -96,6 +96,8 @@ public: return(false); { + using namespace inline_geometry; + CircleCreateInfo cci; cci.center=Vector2f(OFFSCREEN_SIZE*0.5,OFFSCREEN_SIZE*0.5); @@ -105,7 +107,7 @@ public: cci.center_color=Vector4f(1,1,1,1); cci.border_color=Vector4f(1,1,1,0); - Primitive *primitive=CreateRenderableCircle(db,os.material_instance->GetVAB(),&cci); + Primitive *primitive=CreateCircle(db,os.material_instance->GetVAB(),&cci); if(!primitive)return(false); os.renderable=db->CreateRenderable(primitive,os.material_instance,os.pipeline); @@ -136,12 +138,14 @@ public: if(!cube.material_instance->BindSampler(DescriptorSetsType::Value,"tex",os.render_taget->GetColorTexture(),cube.sampler)) return(false); - { + { + using namespace inline_geometry; + CubeCreateInfo cci; cci.tex_coord=true; - Primitive *primitive=CreateRenderableCube(db,cube.material_instance->GetVAB(),&cci); + Primitive *primitive=CreateCube(db,cube.material_instance->GetVAB(),&cci); if(!primitive)return(false); cube.renderable=db->CreateRenderable(primitive,cube.material_instance,cube.pipeline); @@ -159,7 +163,7 @@ public: bool Init() { - if(!CameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT)) + //if(!CameraAppFramework::Init(SCREEN_WIDTH,SCREEN_HEIGHT)) return(false); SetClearColor(COLOR::MozillaCharcoal); diff --git a/example/Vulkan/RayPicking.cpp b/example/Vulkan/RayPicking.cpp index 1aaaca8c..f09ef1a6 100644 --- a/example/Vulkan/RayPicking.cpp +++ b/example/Vulkan/RayPicking.cpp @@ -78,6 +78,8 @@ private: bool CreateRenderObject() { + using namespace inline_geometry; + { struct PlaneGridCreateInfo pgci; @@ -97,7 +99,7 @@ private: const VAB *vab=material_instance->GetVAB(); - ro_plane_grid=CreateRenderablePlaneGrid(db,vab,&pgci); + ro_plane_grid=CreatePlaneGrid(db,vab,&pgci); } { diff --git a/example/Vulkan/SceneTree.cpp b/example/Vulkan/SceneTree.cpp index 1442735f..ce009fa0 100644 --- a/example/Vulkan/SceneTree.cpp +++ b/example/Vulkan/SceneTree.cpp @@ -36,7 +36,7 @@ private: GPUBuffer * ubo_color =nullptr; GPUBuffer * ubo_sun =nullptr; - Primitive * renderable_object =nullptr; + Primitive * primitive =nullptr; Pipeline * pipeline =nullptr; @@ -67,7 +67,7 @@ private: void CreateRenderObject() { - renderable_object=CreateRenderableSphere(db,material_instance->GetVAB(),40); + primitive=inline_geometry::CreateSphere(db,material_instance->GetVAB(),40); } bool InitUBO() @@ -112,7 +112,7 @@ private: uint count; float size; - Renderable *ri=db->CreateRenderable(renderable_object,material_instance,pipeline); + Renderable *ri=db->CreateRenderable(primitive,material_instance,pipeline); for(uint i=0;i<360;i++) { diff --git a/example/Vulkan/SkyColor.cpp b/example/Vulkan/SkyColor.cpp index 89484438..3a3d3eac 100644 --- a/example/Vulkan/SkyColor.cpp +++ b/example/Vulkan/SkyColor.cpp @@ -90,7 +90,7 @@ private: { const VAB *vab=material_instance->GetVAB(); - ro_skyphere=CreateRenderableDome(db,vab,64); + ro_skyphere=inline_geometry::CreateDome(db,vab,64); } bool InitScene() diff --git a/example/Vulkan/TerrainSimple.cpp b/example/Vulkan/TerrainSimple.cpp index 695ea42b..5581762e 100644 --- a/example/Vulkan/TerrainSimple.cpp +++ b/example/Vulkan/TerrainSimple.cpp @@ -77,6 +77,8 @@ private: void CreateRenderObject() { + using namespace inline_geometry; + struct PlaneGridCreateInfo pgci; pgci.coord[0]=Vector3f(-100,-100,0); @@ -95,7 +97,7 @@ private: const VAB *vab=material_instance->GetVAB(); - renderable=CreateRenderablePlaneGrid(db,vab,&pgci); + renderable=CreatePlaneGrid(db,vab,&pgci); } bool InitScene() diff --git a/inc/hgl/graph/InlineGeometry.h b/inc/hgl/graph/InlineGeometry.h index 11b9c5c9..3bc6aa05 100644 --- a/inc/hgl/graph/InlineGeometry.h +++ b/inc/hgl/graph/InlineGeometry.h @@ -10,227 +10,230 @@ namespace hgl { namespace graph { - /** - * 矩形创建信息(扇形/三角形条) - */ - struct RectangleCreateInfo + namespace inline_geometry { - RectScope2f scope; - };//struct RectangleCreateInfo - - Primitive *CreateRenderableRectangle(RenderResource *db,const VAB *vab,const RectangleCreateInfo *rci); - - /** - * 创建延迟渲染用全屏平面 - */ - Primitive *CreateRenderableGBufferComposition(RenderResource *db,const VAB *vab); - - /** - * 圆角矩形创建信息(扇形/线圈) - */ - struct RoundRectangleCreateInfo:public RectangleCreateInfo - { - float radius; ///<圆角半径 - uint32_t round_per; ///<圆角精度 - };//struct RoundRectangleCreateInfo:public RectangleCreateInfo - - Primitive *CreateRenderableRoundRectangle(RenderResource *db,const VAB *vab,const RoundRectangleCreateInfo *rci); - - /** - * 圆形创建信息 - */ - struct CircleCreateInfo - { - Vector2f center; ///<圆心坐标 - Vector2f radius; ///<半径 - uint field_count=8; ///<分段次数 - - bool has_color =false; - - Vector4f center_color; ///<圆心颜色 - Vector4f border_color; ///<边缘颜色 - };//struct CircleCreateInfo - - /** - * 创建一个2D圆形(扇形/线圈) - */ - Primitive *CreateRenderableCircle(RenderResource *db,const VAB *vab,const CircleCreateInfo *cci); - - /** - * 平面网格创建信息 - */ - struct PlaneGridCreateInfo - { - Vector3f coord[4]; - Vector2u step; - - Vector2u side_step; //到边界的步数 - - Color4f color; //一般线条颜色 - Color4f side_color; //边界线条颜色 - };//struct PlaneGridCreateInfo - - /** - * 创建一个平面网格(线条) - */ - Primitive *CreateRenderablePlaneGrid(RenderResource *db,const VAB *vab,const PlaneGridCreateInfo *pgci); - - struct PlaneCreateInfo - { - Vector2f tile; - - public: - - PlaneCreateInfo() + /** + * 矩形创建信息(扇形/三角形条) + */ + struct RectangleCreateInfo { - tile.x=1.0f; - tile.y=1.0f; - } - };//struct PlaneCreateInfo + RectScope2f scope; + };//struct RectangleCreateInfo - /** - * 创建一个平面(三角形) - */ - Primitive *CreateRenderablePlane(RenderResource *db,const VAB *vab,const PlaneCreateInfo *pci); + Primitive *CreateRectangle(RenderResource *db,const VAB *vab,const RectangleCreateInfo *rci); - struct CubeCreateInfo - { - bool normal; - bool tangent; - bool tex_coord; + /** + * 创建延迟渲染用全屏平面 + */ + Primitive *CreateGBufferCompositionRectangle(RenderResource *db,const VAB *vab); - enum class ColorType + /** + * 圆角矩形创建信息(扇形/线圈) + */ + struct RoundRectangleCreateInfo:public RectangleCreateInfo { - NoColor=0, ///<没有颜色 - SameColor, ///<一个颜色 - FaceColor, ///<每个面一个颜色(请写入6个颜色值) - VertexColor, ///<每个顶点一个颜色(请写入24个颜色值) + float radius; ///<圆角半径 + uint32_t round_per; ///<圆角精度 + };//struct RoundRectangleCreateInfo:public RectangleCreateInfo - ENUM_CLASS_RANGE(NoColor,VertexColor) - }; + Primitive *CreateRoundRectangle(RenderResource *db,const VAB *vab,const RoundRectangleCreateInfo *rci); - ColorType color_type; - Vector4f color[24]; - - public: - - CubeCreateInfo() + /** + * 圆形创建信息 + */ + struct CircleCreateInfo { - normal=false; - tangent=false; - tex_coord=false; + Vector2f center; ///<圆心坐标 + Vector2f radius; ///<半径 + uint field_count=8; ///<分段次数 - color_type=ColorType::NoColor; - } - };//struct CubeCreateInfo + bool has_color =false; - /** - * 创建一个立方体(三角形) - */ - Primitive *CreateRenderableCube(RenderResource *db,const VAB *vab,const CubeCreateInfo *cci); + Vector4f center_color; ///<圆心颜色 + Vector4f border_color; ///<边缘颜色 + };//struct CircleCreateInfo - struct BoundingBoxCreateInfo - { - bool normal; + /** + * 创建一个2D圆形(扇形/线圈) + */ + Primitive *CreateCircle(RenderResource *db,const VAB *vab,const CircleCreateInfo *cci); - enum class ColorType + /** + * 平面网格创建信息 + */ + struct PlaneGridCreateInfo { - NoColor=0, ///<没有颜色 - SameColor, ///<一个颜色 - VertexColor, ///<每个顶点一个颜色(请写入8个颜色值) + Vector3f coord[4]; + Vector2u step; - ENUM_CLASS_RANGE(NoColor,VertexColor) - }; + Vector2u side_step; //到边界的步数 - ColorType color_type; - Vector4f color[8]; + Color4f color; //一般线条颜色 + Color4f side_color; //边界线条颜色 + };//struct PlaneGridCreateInfo - public: + /** + * 创建一个平面网格(线条) + */ + Primitive *CreatePlaneGrid(RenderResource *db,const VAB *vab,const PlaneGridCreateInfo *pgci); - BoundingBoxCreateInfo() + struct PlaneCreateInfo { - normal=false; + Vector2f tile; - color_type=ColorType::NoColor; - } - };//struct BoundingBoxCreateInfo + public: - /** - * 创建一个绑定盒(线条) - */ - Primitive *CreateRenderableBoundingBox(RenderResource *db,const VAB *vab,const BoundingBoxCreateInfo *cci); + PlaneCreateInfo() + { + tile.x=1.0f; + tile.y=1.0f; + } + };//struct PlaneCreateInfo - /** - * 创建一个球心坐标为0,0,0,半径为1的球体(三角形) - */ - Primitive *CreateRenderableSphere(RenderResource *db,const VAB *vab,const uint numberSlices); + /** + * 创建一个平面(三角形) + */ + Primitive *CreatePlane(RenderResource *db,const VAB *vab,const PlaneCreateInfo *pci); - /** - * 创建一个穹顶(三角形) - */ - Primitive *CreateRenderableDome(RenderResource *db,const VAB *vab, const uint numberSlices); - - struct TorusCreateInfo - { - float innerRadius, - outerRadius; - - uint numberSlices, - numberStacks; - - Vector2f uv_scale={1.0,1.0}; - };//struct TorusCreateInfo - - /** - * 创建一个圆环(三角形) - */ - Primitive *CreateRenderableTorus(RenderResource *db,const VAB *vab,const TorusCreateInfo *tci); - - struct CylinderCreateInfo - { - float halfExtend, //高度 - radius; //半径 - uint numberSlices; - };//struct CylinderCreateInfo - - /** - * 创建一个圆柱(三角形) - */ - Primitive *CreateRenderableCylinder(RenderResource *db,const VAB *vab,const CylinderCreateInfo *cci); - - struct ConeCreateInfo - { - float halfExtend, //高度 - radius; //半径 - uint numberSlices, //圆切分精度 - numberStacks; //柱高层数 - };//struct ConeCreateInfo - - /** - * 创建一个圆锥(三角形) - */ - Primitive *CreateRenderableCone(RenderResource *db,const VAB *vab,const ConeCreateInfo *cci); - - struct AxisCreateInfo - { - float size; - Color4f color[3]; - - public: - - AxisCreateInfo() + struct CubeCreateInfo { - 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 + bool normal; + bool tangent; + bool tex_coord; - /** - * 创建一个坐标线(线条) - */ - Primitive *CreateRenderableAxis(RenderResource *db,const VAB *vab,const AxisCreateInfo *aci); + enum class ColorType + { + NoColor=0, ///<没有颜色 + SameColor, ///<一个颜色 + FaceColor, ///<每个面一个颜色(请写入6个颜色值) + VertexColor, ///<每个顶点一个颜色(请写入24个颜色值) + + ENUM_CLASS_RANGE(NoColor,VertexColor) + }; + + ColorType color_type; + Vector4f color[24]; + + public: + + CubeCreateInfo() + { + normal=false; + tangent=false; + tex_coord=false; + + color_type=ColorType::NoColor; + } + };//struct CubeCreateInfo + + /** + * 创建一个立方体(三角形) + */ + Primitive *CreateCube(RenderResource *db,const VAB *vab,const CubeCreateInfo *cci); + + struct BoundingBoxCreateInfo + { + bool normal; + + enum class ColorType + { + NoColor=0, ///<没有颜色 + SameColor, ///<一个颜色 + VertexColor, ///<每个顶点一个颜色(请写入8个颜色值) + + ENUM_CLASS_RANGE(NoColor,VertexColor) + }; + + ColorType color_type; + Vector4f color[8]; + + public: + + BoundingBoxCreateInfo() + { + normal=false; + + color_type=ColorType::NoColor; + } + };//struct BoundingBoxCreateInfo + + /** + * 创建一个绑定盒(线条) + */ + Primitive *CreateBoundingBox(RenderResource *db,const VAB *vab,const BoundingBoxCreateInfo *cci); + + /** + * 创建一个球心坐标为0,0,0,半径为1的球体(三角形) + */ + Primitive *CreateSphere(RenderResource *db,const VAB *vab,const uint numberSlices); + + /** + * 创建一个穹顶(三角形) + */ + Primitive *CreateDome(RenderResource *db,const VAB *vab, const uint numberSlices); + + struct TorusCreateInfo + { + float innerRadius, + outerRadius; + + uint numberSlices, + numberStacks; + + Vector2f uv_scale={1.0,1.0}; + };//struct TorusCreateInfo + + /** + * 创建一个圆环(三角形) + */ + Primitive *CreateTorus(RenderResource *db,const VAB *vab,const TorusCreateInfo *tci); + + struct CylinderCreateInfo + { + float halfExtend, //高度 + radius; //半径 + uint numberSlices; + };//struct CylinderCreateInfo + + /** + * 创建一个圆柱(三角形) + */ + Primitive *CreateCylinder(RenderResource *db,const VAB *vab,const CylinderCreateInfo *cci); + + struct ConeCreateInfo + { + float halfExtend, //高度 + radius; //半径 + uint numberSlices, //圆切分精度 + numberStacks; //柱高层数 + };//struct ConeCreateInfo + + /** + * 创建一个圆锥(三角形) + */ + Primitive *CreateCone(RenderResource *db,const VAB *vab,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(RenderResource *db,const VAB *vab,const AxisCreateInfo *aci); + }//namespace inline_geometry }//namespace graph };//namespace hgl #endif//HGL_GRAPH_INLINE_GEOMETRY_INCLUDE diff --git a/src/SceneGraph/InlineGeometry.cpp b/src/SceneGraph/InlineGeometry.cpp index 5e429be6..c0902aa6 100644 --- a/src/SceneGraph/InlineGeometry.cpp +++ b/src/SceneGraph/InlineGeometry.cpp @@ -12,847 +12,821 @@ namespace hgl { namespace graph { - Primitive *CreateRenderableRectangle(RenderResource *db,const VAB *vab,const RectangleCreateInfo *rci) + namespace inline_geometry { - PrimitiveCreater rc(db,vab); - - if(!rc.Init(4)) - return(nullptr); - - AutoDelete vertex=rc.CreateVADA(VAN::Position); - - if(!vertex) - return(nullptr); - - vertex->WriteRectFan(rci->scope); - - return rc.Finish(); - } - - Primitive *CreateRenderableGBufferComposition(RenderResource *db,const VAB *vab) - { - RectangleCreateInfo rci; - - rci.scope.Set(-1,-1,2,2); - - return CreateRenderableRectangle(db,vab,&rci); - } - - Primitive *CreateRenderableRoundRectangle(RenderResource *db,const VAB *vab,const RoundRectangleCreateInfo *rci) - { - PrimitiveCreater rc(db,vab); - - if(rci->radius==0||rci->round_per<=1) //这是要画矩形 + Primitive *CreateRectangle(RenderResource *db,const VAB *vab,const RectangleCreateInfo *rci) { + PrimitiveCreater rc(db,vab); + if(!rc.Init(4)) return(nullptr); - + AutoDelete vertex=rc.CreateVADA(VAN::Position); + if(!vertex) + return(nullptr); + vertex->WriteRectFan(rci->scope); + + return rc.Finish(); } - else + + Primitive *CreateGBufferCompositionRectangle(RenderResource *db,const VAB *vab) { - float radius=rci->radius; + RectangleCreateInfo rci; - if(radius>rci->scope.GetWidth()/2.0f)radius=rci->scope.GetWidth()/2.0f; - if(radius>rci->scope.GetHeight()/2.0f)radius=rci->scope.GetHeight()/2.0f; + rci.scope.Set(-1,-1,2,2); - if(!rc.Init(rci->round_per*4)) - return(nullptr); + return CreateRectangle(db,vab,&rci); + } + + Primitive *CreateRoundRectangle(RenderResource *db,const VAB *vab,const RoundRectangleCreateInfo *rci) + { + PrimitiveCreater rc(db,vab); + + if(rci->radius==0||rci->round_per<=1) //这是要画矩形 + { + if(!rc.Init(4)) + return(nullptr); + + AutoDelete vertex=rc.CreateVADA(VAN::Position); + + vertex->WriteRectFan(rci->scope); + } + else + { + float radius=rci->radius; + + if(radius>rci->scope.GetWidth()/2.0f)radius=rci->scope.GetWidth()/2.0f; + if(radius>rci->scope.GetHeight()/2.0f)radius=rci->scope.GetHeight()/2.0f; + + if(!rc.Init(rci->round_per*4)) + return(nullptr); - AutoDelete vertex=rc.CreateVADA(VAN::Position); + AutoDelete vertex=rc.CreateVADA(VAN::Position); - Vector2f *coord=new Vector2f[rci->round_per]; + Vector2f *coord=new Vector2f[rci->round_per]; - float l=rci->scope.GetLeft(), - r=rci->scope.GetRight(), - t=rci->scope.GetTop(), - b=rci->scope.GetBottom(); + float l=rci->scope.GetLeft(), + r=rci->scope.GetRight(), + t=rci->scope.GetTop(), + b=rci->scope.GetBottom(); - for(uint i=0;iround_per;i++) - { - float ang=float(i)/float(rci->round_per-1)*90.0f; + for(uint i=0;iround_per;i++) + { + float ang=float(i)/float(rci->round_per-1)*90.0f; - float x=sin(deg2rad(ang))*radius; - float y=cos(deg2rad(ang))*radius; + float x=sin(deg2rad(ang))*radius; + float y=cos(deg2rad(ang))*radius; - coord[i].x=x; - coord[i].y=y; + coord[i].x=x; + coord[i].y=y; - //右上角 - vertex->Write(r-radius+x, - t+radius-y); + //右上角 + vertex->Write(r-radius+x, + t+radius-y); + } + + //右下角 + for(uint i=0;iround_per;i++) + { + vertex->Write(r-radius+coord[rci->round_per-1-i].x, + b-radius+coord[rci->round_per-1-i].y); + } + + //左下角 + for(uint i=0;iround_per;i++) + { + vertex->Write(l+radius-coord[i].x, + b-radius+coord[i].y); + } + + //左上角 + for(uint i=0;iround_per;i++) + { + vertex->Write(l+radius-coord[rci->round_per-1-i].x, + t+radius-coord[rci->round_per-1-i].y); + } + + delete[] coord; } - //右下角 - for(uint i=0;iround_per;i++) - { - vertex->Write(r-radius+coord[rci->round_per-1-i].x, - b-radius+coord[rci->round_per-1-i].y); - } - - //左下角 - for(uint i=0;iround_per;i++) - { - vertex->Write(l+radius-coord[i].x, - b-radius+coord[i].y); - } - - //左上角 - for(uint i=0;iround_per;i++) - { - vertex->Write(l+radius-coord[rci->round_per-1-i].x, - t+radius-coord[rci->round_per-1-i].y); - } - - delete[] coord; + return rc.Finish(); } - return rc.Finish(); - } - - Primitive *CreateRenderableCircle(RenderResource *db,const VAB *vab,const CircleCreateInfo *cci) - { - PrimitiveCreater rc(db,vab); - - uint edge; - - if(cci->has_color) + Primitive *CreateCircle(RenderResource *db,const VAB *vab,const CircleCreateInfo *cci) { - edge=cci->field_count+1; - if(!rc.Init(cci->field_count+2))return(nullptr); - } - else - { - edge=cci->field_count; - if(!rc.Init(cci->field_count))return(nullptr); - } + PrimitiveCreater rc(db,vab); - AutoDelete vertex=rc.CreateVADA(VAN::Position); - AutoDelete color=rc.CreateVADA(VAN::Color); + uint edge; - if(!vertex) - return(nullptr); - - if(cci->has_color) - { - if(!color) - return(nullptr); - - vertex->Write(cci->center); - color->Write(cci->center_color); - } - - for(uint i=0;ifield_count)*360.0f; - - float x=cci->center.x+sin(deg2rad(ang))*cci->radius.x; - float y=cci->center.y+cos(deg2rad(ang))*cci->radius.y; - - vertex->Write(x,y); - if(cci->has_color) - color->Write(cci->border_color); + { + edge=cci->field_count+1; + if(!rc.Init(cci->field_count+2))return(nullptr); + } + else + { + edge=cci->field_count; + if(!rc.Init(cci->field_count))return(nullptr); + } + + AutoDelete vertex=rc.CreateVADA(VAN::Position); + AutoDelete color=rc.CreateVADA(VAN::Color); + + if(!vertex) + return(nullptr); + + if(cci->has_color) + { + if(!color) + return(nullptr); + + vertex->Write(cci->center); + color->Write(cci->center_color); + } + + for(uint i=0;ifield_count)*360.0f; + + float x=cci->center.x+sin(deg2rad(ang))*cci->radius.x; + float y=cci->center.y+cos(deg2rad(ang))*cci->radius.y; + + vertex->Write(x,y); + + if(cci->has_color) + color->Write(cci->border_color); + } + + return rc.Finish(); } - return rc.Finish(); - } - - Primitive *CreateRenderablePlaneGrid(RenderResource *db,const VAB *vab,const PlaneGridCreateInfo *pgci) - { - PrimitiveCreater rc(db,vab); - - if(!rc.Init(((pgci->step.x+1)+(pgci->step.y+1))*2)) - return(nullptr); - - AutoDelete vertex=rc.CreateVADA(VAN::Position); - for(uint row=0;row<=pgci->step.x;row++) + Primitive *CreatePlaneGrid(RenderResource *db,const VAB *vab,const PlaneGridCreateInfo *pgci) { - float pos=float(row)/float(pgci->step.x); + PrimitiveCreater rc(db,vab); - vertex->WriteLine( to(pgci->coord[0],pgci->coord[1],pos), - to(pgci->coord[3],pgci->coord[2],pos)); - } + if(!rc.Init(((pgci->step.x+1)+(pgci->step.y+1))*2)) + return(nullptr); - for(uint col=0;col<=pgci->step.y;col++) - { - float pos=float(col)/float(pgci->step.y); - - vertex->WriteLine(to(pgci->coord[1],pgci->coord[2],pos), - to(pgci->coord[0],pgci->coord[3],pos)); - } - - AutoDelete color=rc.CreateVADA(VAN::Color); - if(color) - { + AutoDelete vertex=rc.CreateVADA(VAN::Position); for(uint row=0;row<=pgci->step.x;row++) { - if((row%pgci->side_step.x)==0) - color->RepeatWrite(pgci->side_color,2); - else - color->RepeatWrite(pgci->color,2); + float pos=float(row)/float(pgci->step.x); + + vertex->WriteLine( to(pgci->coord[0],pgci->coord[1],pos), + to(pgci->coord[3],pgci->coord[2],pos)); } for(uint col=0;col<=pgci->step.y;col++) { - if((col%pgci->side_step.y)==0) - color->RepeatWrite(pgci->side_color,2); - else - color->RepeatWrite(pgci->color,2); + float pos=float(col)/float(pgci->step.y); + + vertex->WriteLine(to(pgci->coord[1],pgci->coord[2],pos), + to(pgci->coord[0],pgci->coord[3],pos)); } - } - - return rc.Finish(); - } - - Primitive *CreateRenderablePlane(RenderResource *db,const VAB *vab,const PlaneCreateInfo *pci) - { - const float xy_vertices [] = { -0.5f,-0.5f,0.0f, +0.5f,-0.5f,0.0f, +0.5f,+0.5f,0.0f, -0.5f,+0.5f,0.0f }; - float xy_tex_coord[] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f }; - const Vector3f xy_normal(0.0f,0.0f,1.0f); - const Vector3f xy_tangent(1.0f,0.0f,0.0f); - - PrimitiveCreater rc(db,vab); - - if(!rc.Init(4)) - return(nullptr); - - rc.WriteVAD(VAN::Position,xy_vertices,sizeof(xy_vertices)); - - { - AutoDelete normal=rc.CreateVADA(VAN::Normal); - - if(normal)normal->RepeatWrite(xy_normal,4); - } - - { - AutoDelete tangent=rc.CreateVADA(VAN::Tangent); - - tangent->RepeatWrite(xy_tangent,4); - } - - { - AutoDelete tex_coord=rc.CreateVADA(VAN::TexCoord); - - if(tex_coord) - { - xy_tex_coord[2]=xy_tex_coord[4]=pci->tile.x; - xy_tex_coord[5]=xy_tex_coord[7]=pci->tile.y; - - tex_coord->Write(xy_tex_coord); - } - } - - return rc.Finish(); - } - - Primitive *CreateRenderableCube(RenderResource *db,const VAB *vab,const CubeCreateInfo *cci) - { - /** - * 4 5 - * *------------* Z Y - * /| /| | Y | Z - * 0/ | 1/ | | / | / - * *--+---------* | | / | / - * | | | | | / | / - * | 7| | 6| |/ |/ - * | *---------+--* *-----------X *-----------X - * | / | / - * |/ 2|/ my Cubemap - * 3*------------* - * - * 注:cubemap纹理坐标系依然遵循OpenGL时代定下的坐标系,所以这里的position虽然使用vulkan坐标系,但在shader中当做cubemap纹理坐标使用时,需要在shader中转换为opengl坐标系(交换yz即可) - */ - - constexpr float positions[]={ -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, +0.5f, +0.5f, -0.5f, +0.5f, +0.5f, -0.5f, -0.5f, - -0.5f, +0.5f, -0.5f, -0.5f, +0.5f, +0.5f, +0.5f, +0.5f, +0.5f, +0.5f, +0.5f, -0.5f, - -0.5f, -0.5f, -0.5f, -0.5f, +0.5f, -0.5f, +0.5f, +0.5f, -0.5f, +0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, +0.5f, -0.5f, +0.5f, +0.5f, +0.5f, +0.5f, +0.5f, +0.5f, -0.5f, +0.5f, - -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, +0.5f, -0.5f, +0.5f, +0.5f, -0.5f, +0.5f, -0.5f, - +0.5f, -0.5f, -0.5f, +0.5f, -0.5f, +0.5f, +0.5f, +0.5f, +0.5f, +0.5f, +0.5f, -0.5f}; - - constexpr float normals[]={ +0.0f, +1.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f, +1.0f, +0.0f, - +0.0f, -1.0f, +0.0f, +0.0f, -1.0f, +0.0f, +0.0f, -1.0f, +0.0f, +0.0f, -1.0f, +0.0f, - +0.0f, -0.0f, -1.0f, +0.0f, -0.0f, -1.0f, +0.0f, -0.0f, -1.0f, +0.0f, -0.0f, -1.0f, - +0.0f, -0.0f, +1.0f, +0.0f, -0.0f, +1.0f, +0.0f, -0.0f, +1.0f, +0.0f, -0.0f, +1.0f, - -1.0f, -0.0f, +0.0f, -1.0f, -0.0f, +0.0f, -1.0f, -0.0f, +0.0f, -1.0f, -0.0f, +0.0f, - +1.0f, -0.0f, +0.0f, +1.0f, -0.0f, +0.0f, +1.0f, -0.0f, +0.0f, +1.0f, -0.0f, +0.0f}; - - constexpr float tangents[] = { +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, - +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, - -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, - +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, - 0.0f, 0.0f,+1.0f, 0.0f, 0.0f,+1.0f, 0.0f, 0.0f,+1.0f, 0.0f, 0.0f,+1.0f, - 0.0f, 0.0f,-1.0f, 0.0f, 0.0f,-1.0f, 0.0f, 0.0f,-1.0f, 0.0f, 0.0f,-1.0f}; - - constexpr float tex_coords[] ={ 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, - 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, - 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, - 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f}; - // The associated indices. - constexpr uint16 indices[]={ 0, 2, 1, 0, 3, 2, - 4, 5, 6, 4, 6, 7, - 8, 9, 10, 8, 10, 11, - 12, 15, 14, 12, 14, 13, - 16, 17, 18, 16, 18, 19, - 20, 23, 22, 20, 22, 21}; - - PrimitiveCreater rc(db,vab); - - if(!rc.Init(24)) - return(nullptr); - - rc.WriteVAD(VAN::Position,positions,sizeof(positions)); - - if(cci->normal) - rc.WriteVAD(VAN::Normal,normals,sizeof(normals)); - - if(cci->tangent) - rc.WriteVAD(VAN::Tangent,tangents,sizeof(tangents)); - - if(cci->tex_coord) - rc.WriteVAD(VAN::TexCoord,tex_coords,sizeof(tex_coords)); - - if(cci->color_type!=CubeCreateInfo::ColorType::NoColor) - { - ENUM_CLASS_RANGE_ERROR_RETURN_NULLPTR(cci->color_type); AutoDelete color=rc.CreateVADA(VAN::Color); - if(color) { - if(cci->color_type==CubeCreateInfo::ColorType::SameColor) - color->RepeatWrite(cci->color[0],24); - else - if(cci->color_type==CubeCreateInfo::ColorType::FaceColor) + for(uint row=0;row<=pgci->step.x;row++) { - for(uint face=0;face<6;face++) - color->RepeatWrite(cci->color[face],4); - } - else - if(cci->color_type==CubeCreateInfo::ColorType::VertexColor) - color->Write(cci->color,24); - else - return(nullptr); - } - } - - rc.CreateIBO16(6*2*3,indices); - return rc.Finish(); - } - - template void CreateSphereIndices(T *tp,uint numberParallels,const uint numberSlices) - { - for (uint i = 0; i < numberParallels; i++) - { - for (uint j = 0; j < numberSlices; j++) - { - *tp= i * (numberSlices + 1) + j; ++tp; - *tp=(i + 1) * (numberSlices + 1) + (j + 1); ++tp; - *tp=(i + 1) * (numberSlices + 1) + j; ++tp; - - *tp= i * (numberSlices + 1) + j; ++tp; - *tp= i * (numberSlices + 1) + (j + 1); ++tp; - *tp=(i + 1) * (numberSlices + 1) + (j + 1); ++tp; - } - } - } - - namespace - { - constexpr uint GLUS_VERTICES_FACTOR =4; - constexpr uint GLUS_VERTICES_DIVISOR=4; - constexpr uint GLUS_MAX_VERTICES =1048576; - constexpr uint GLUS_MAX_INDICES =GLUS_MAX_VERTICES*GLUS_VERTICES_FACTOR; - - void glusQuaternionRotateRyf(float quaternion[4], const float angle) - { - float halfAngleRadian = deg2rad(angle) * 0.5f; - - quaternion[0] = 0.0f; - quaternion[1] = sin(halfAngleRadian); - quaternion[2] = 0.0f; - quaternion[3] = cos(halfAngleRadian); - } - - void glusQuaternionRotateRzf(float quaternion[4], const float angle) - { - float halfAngleRadian = deg2rad(angle) * 0.5f; - - quaternion[0] = 0.0f; - quaternion[1] = 0.0f; - quaternion[2] = sin(halfAngleRadian); - quaternion[3] = cos(halfAngleRadian); - } - - void glusQuaternionGetMatrix4x4f(float matrix[16], const float quaternion[4]) - { - float x = quaternion[0]; - float y = quaternion[1]; - float z = quaternion[2]; - float w = quaternion[3]; - - matrix[0] = 1.0f - 2.0f * y * y - 2.0f * z * z; - matrix[1] = 2.0f * x * y + 2.0f * w * z; - matrix[2] = 2.0f * x * z - 2.0f * w * y; - matrix[3] = 0.0f; - - matrix[4] = 2.0f * x * y - 2.0f * w * z; - matrix[5] = 1.0f - 2.0f * x * x - 2.0f * z * z; - matrix[6] = 2.0f * y * z + 2.0f * w * x; - matrix[7] = 0.0f; - - matrix[8] = 2.0f * x * z + 2.0f * w * y; - matrix[9] = 2.0f * y * z - 2.0f * w * x; - matrix[10] = 1.0f - 2.0f * x * x - 2.0f * y * y; - matrix[11] = 0.0f; - - matrix[12] = 0.0f; - matrix[13] = 0.0f; - matrix[14] = 0.0f; - matrix[15] = 1.0f; - } - - void glusMatrix4x4MultiplyVector3f(float result[3], const float matrix[16], const float vector[3]) - { - int i; - - float temp[3]; - - for (i = 0; i < 3; i++) - { - temp[i] = matrix[i] * vector[0] + matrix[4 + i] * vector[1] + matrix[8 + i] * vector[2]; - } - - for (i = 0; i < 3; i++) - { - result[i] = temp[i]; - } - } - }//namespace - - /** - * 创建一个球体的可渲染数据,球心为0,0,0,半径为1 - * @param numberSlices 切片数 - * @return 可渲染数据 - */ - Primitive *CreateRenderableSphere(RenderResource *db,const VAB *vab,const uint numberSlices) - { - PrimitiveCreater rc(db,vab); - - uint numberParallels = (numberSlices+1) / 2; - uint numberVertices = (numberParallels + 1) * (numberSlices + 1); - uint numberIndices = numberParallels * numberSlices * 6; - - const double angleStep = double(2.0f * HGL_PI) / ((double) numberSlices); - - // used later to help us calculating tangents vectors - float helpVector[3] = { 1.0f, 0.0f, 0.0f }; - float helpQuaternion[4]; - float helpMatrix[16]; - float tex_x; - - if(!rc.Init(numberVertices)) - return(nullptr); - - AutoDelete vertex=rc.CreateVADA(VAN::Position); - AutoDelete normal=rc.CreateVADA(VAN::Normal); - AutoDelete tangent=rc.CreateVADA(VAN::Tangent); - AutoDelete tex_coord=rc.CreateVADA(VAN::TexCoord); - - float *vp=vertex->Get(); - float *np=normal?normal->Get():nullptr; - float *tp=tangent?tangent->Get():nullptr; - float *tcp=tex_coord?tex_coord->Get():nullptr; - - for (uint i = 0; i < numberParallels + 1; i++) - { - for (uint j = 0; j < numberSlices + 1; j++) - { - float x = sin(angleStep * (double) i) * sin(angleStep * (double) j); - float y = sin(angleStep * (double) i) * cos(angleStep * (double) j); - float z = cos(angleStep * (double) i); - - *vp=x;++vp; - *vp=y;++vp; - *vp=z;++vp; - - if(np) - { - *np=+x;++np; - *np=-y;++np; - *np=+z;++np; + if((row%pgci->side_step.x)==0) + color->RepeatWrite(pgci->side_color,2); + else + color->RepeatWrite(pgci->color,2); } - if(tcp) + for(uint col=0;col<=pgci->step.y;col++) { - tex_x=(float) j / (float) numberSlices; - - *tcp=tex_x;++tcp; - *tcp=1.0f - (float) i / (float) numberParallels;++tcp; - - if(tp) - { - // use quaternion to get the tangent vector - glusQuaternionRotateRyf(helpQuaternion, 360.0f * tex_x); - glusQuaternionGetMatrix4x4f(helpMatrix, helpQuaternion); - - glusMatrix4x4MultiplyVector3f(tp, helpMatrix, helpVector); - tp+=3; - } + if((col%pgci->side_step.y)==0) + color->RepeatWrite(pgci->side_color,2); + else + color->RepeatWrite(pgci->color,2); } } + + return rc.Finish(); } - if(numberVertices<=0xffff) - CreateSphereIndices(rc.CreateIBO16(numberIndices),numberParallels,numberSlices); - else - CreateSphereIndices(rc.CreateIBO32(numberIndices),numberParallels,numberSlices); + Primitive *CreatePlane(RenderResource *db,const VAB *vab,const PlaneCreateInfo *pci) + { + const float xy_vertices [] = { -0.5f,-0.5f,0.0f, +0.5f,-0.5f,0.0f, +0.5f,+0.5f,0.0f, -0.5f,+0.5f,0.0f }; + float xy_tex_coord[] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f }; + const Vector3f xy_normal(0.0f,0.0f,1.0f); + const Vector3f xy_tangent(1.0f,0.0f,0.0f); - return rc.Finish(); - } + PrimitiveCreater rc(db,vab); - Primitive *CreateRenderableDome(RenderResource *db,const VAB *vab,const uint numberSlices) - { - PrimitiveCreater rc(db,vab); - - uint i, j; - - uint numberParallels = numberSlices / 4; - uint numberVertices = (numberParallels + 1) * (numberSlices + 1); - uint numberIndices = numberParallels * numberSlices * 6; - - float angleStep = (2.0f * HGL_PI) / ((float) numberSlices); - - // used later to help us calculating tangents vectors - float helpVector[3] = { 1.0f, 0.0f, 0.0f }; - float helpQuaternion[4]; - float helpMatrix[16]; - float tex_x; - - if (numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) - return nullptr; - - if(!rc.Init(numberVertices)) - return(nullptr); - - AutoDelete vertex=rc.CreateVADA(VAN::Position); - AutoDelete normal=rc.CreateVADA(VAN::Normal); - AutoDelete tangent=rc.CreateVADA(VAN::Tangent); - AutoDelete tex_coord=rc.CreateVADA(VAN::TexCoord); + if(!rc.Init(4)) + return(nullptr); - float *vp=vertex->Get(); - float *np=normal?normal->Get():nullptr; - float *tp=tangent?tangent->Get():nullptr; - float *tcp=tex_coord?tex_coord->Get():nullptr; + rc.WriteVAD(VAN::Position,xy_vertices,sizeof(xy_vertices)); - for (i = 0; i < numberParallels + 1; i++) - { - for (j = 0; j < numberSlices + 1; j++) { - uint vertexIndex = (i * (numberSlices + 1) + j) * 4; - uint normalIndex = (i * (numberSlices + 1) + j) * 3; - uint tangentIndex = (i * (numberSlices + 1) + j) * 3; - uint texCoordsIndex = (i * (numberSlices + 1) + j) * 2; - - float x = sin(angleStep * (double) i) * sin(angleStep * (double) j); - float y = sin(angleStep * (double) i) * cos(angleStep * (double) j); - float z = cos(angleStep * (double) i); + AutoDelete normal=rc.CreateVADA(VAN::Normal); - *vp=x;++vp; - *vp=y;++vp; - *vp=z;++vp; + if(normal)normal->RepeatWrite(xy_normal,4); + } - if(np) + { + AutoDelete tangent=rc.CreateVADA(VAN::Tangent); + + tangent->RepeatWrite(xy_tangent,4); + } + + { + AutoDelete tex_coord=rc.CreateVADA(VAN::TexCoord); + + if(tex_coord) { - *np=+x;++np; - *np=-y;++np; - *np=+z;++np; + xy_tex_coord[2]=xy_tex_coord[4]=pci->tile.x; + xy_tex_coord[5]=xy_tex_coord[7]=pci->tile.y; + + tex_coord->Write(xy_tex_coord); + } + } + + return rc.Finish(); + } + + Primitive *CreateCube(RenderResource *db,const VAB *vab,const CubeCreateInfo *cci) + { + /** + * 4 5 + * *------------* Z Y + * /| /| | Y | Z + * 0/ | 1/ | | / | / + * *--+---------* | | / | / + * | | | | | / | / + * | 7| | 6| |/ |/ + * | *---------+--* *-----------X *-----------X + * | / | / + * |/ 2|/ my Cubemap + * 3*------------* + * + * 注:cubemap纹理坐标系依然遵循OpenGL时代定下的坐标系,所以这里的position虽然使用vulkan坐标系,但在shader中当做cubemap纹理坐标使用时,需要在shader中转换为opengl坐标系(交换yz即可) + */ + + constexpr float positions[]={ -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, +0.5f, +0.5f, -0.5f, +0.5f, +0.5f, -0.5f, -0.5f, + -0.5f, +0.5f, -0.5f, -0.5f, +0.5f, +0.5f, +0.5f, +0.5f, +0.5f, +0.5f, +0.5f, -0.5f, + -0.5f, -0.5f, -0.5f, -0.5f, +0.5f, -0.5f, +0.5f, +0.5f, -0.5f, +0.5f, -0.5f, -0.5f, + -0.5f, -0.5f, +0.5f, -0.5f, +0.5f, +0.5f, +0.5f, +0.5f, +0.5f, +0.5f, -0.5f, +0.5f, + -0.5f, -0.5f, -0.5f, -0.5f, -0.5f, +0.5f, -0.5f, +0.5f, +0.5f, -0.5f, +0.5f, -0.5f, + +0.5f, -0.5f, -0.5f, +0.5f, -0.5f, +0.5f, +0.5f, +0.5f, +0.5f, +0.5f, +0.5f, -0.5f}; + + constexpr float normals[]={ +0.0f, +1.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f, +1.0f, +0.0f, +0.0f, +1.0f, +0.0f, + +0.0f, -1.0f, +0.0f, +0.0f, -1.0f, +0.0f, +0.0f, -1.0f, +0.0f, +0.0f, -1.0f, +0.0f, + +0.0f, -0.0f, -1.0f, +0.0f, -0.0f, -1.0f, +0.0f, -0.0f, -1.0f, +0.0f, -0.0f, -1.0f, + +0.0f, -0.0f, +1.0f, +0.0f, -0.0f, +1.0f, +0.0f, -0.0f, +1.0f, +0.0f, -0.0f, +1.0f, + -1.0f, -0.0f, +0.0f, -1.0f, -0.0f, +0.0f, -1.0f, -0.0f, +0.0f, -1.0f, -0.0f, +0.0f, + +1.0f, -0.0f, +0.0f, +1.0f, -0.0f, +0.0f, +1.0f, -0.0f, +0.0f, +1.0f, -0.0f, +0.0f}; + + constexpr float tangents[] = { +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, + +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, + -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, + +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, +1.0f, 0.0f, 0.0f, + 0.0f, 0.0f,+1.0f, 0.0f, 0.0f,+1.0f, 0.0f, 0.0f,+1.0f, 0.0f, 0.0f,+1.0f, + 0.0f, 0.0f,-1.0f, 0.0f, 0.0f,-1.0f, 0.0f, 0.0f,-1.0f, 0.0f, 0.0f,-1.0f}; + + constexpr float tex_coords[] ={ 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, + 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, + 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f}; + // The associated indices. + constexpr uint16 indices[]={ 0, 2, 1, 0, 3, 2, + 4, 5, 6, 4, 6, 7, + 8, 9, 10, 8, 10, 11, + 12, 15, 14, 12, 14, 13, + 16, 17, 18, 16, 18, 19, + 20, 23, 22, 20, 22, 21}; + + PrimitiveCreater rc(db,vab); + + if(!rc.Init(24)) + return(nullptr); + + rc.WriteVAD(VAN::Position,positions,sizeof(positions)); + + if(cci->normal) + rc.WriteVAD(VAN::Normal,normals,sizeof(normals)); + + if(cci->tangent) + rc.WriteVAD(VAN::Tangent,tangents,sizeof(tangents)); + + if(cci->tex_coord) + rc.WriteVAD(VAN::TexCoord,tex_coords,sizeof(tex_coords)); + + if(cci->color_type!=CubeCreateInfo::ColorType::NoColor) + { + ENUM_CLASS_RANGE_ERROR_RETURN_NULLPTR(cci->color_type); + + AutoDelete color=rc.CreateVADA(VAN::Color); + + if(color) + { + if(cci->color_type==CubeCreateInfo::ColorType::SameColor) + color->RepeatWrite(cci->color[0],24); + else + if(cci->color_type==CubeCreateInfo::ColorType::FaceColor) + { + for(uint face=0;face<6;face++) + color->RepeatWrite(cci->color[face],4); + } + else + if(cci->color_type==CubeCreateInfo::ColorType::VertexColor) + color->Write(cci->color,24); + else + return(nullptr); + } + } + + rc.CreateIBO16(6*2*3,indices); + return rc.Finish(); + } + + template void CreateSphereIndices(T *tp,uint numberParallels,const uint numberSlices) + { + for (uint i = 0; i < numberParallels; i++) + { + for (uint j = 0; j < numberSlices; j++) + { + *tp= i * (numberSlices + 1) + j; ++tp; + *tp=(i + 1) * (numberSlices + 1) + (j + 1); ++tp; + *tp=(i + 1) * (numberSlices + 1) + j; ++tp; + + *tp= i * (numberSlices + 1) + j; ++tp; + *tp= i * (numberSlices + 1) + (j + 1); ++tp; + *tp=(i + 1) * (numberSlices + 1) + (j + 1); ++tp; + } + } + } + + namespace + { + constexpr uint GLUS_VERTICES_FACTOR =4; + constexpr uint GLUS_VERTICES_DIVISOR=4; + constexpr uint GLUS_MAX_VERTICES =1048576; + constexpr uint GLUS_MAX_INDICES =GLUS_MAX_VERTICES*GLUS_VERTICES_FACTOR; + + void glusQuaternionRotateRyf(float quaternion[4], const float angle) + { + float halfAngleRadian = deg2rad(angle) * 0.5f; + + quaternion[0] = 0.0f; + quaternion[1] = sin(halfAngleRadian); + quaternion[2] = 0.0f; + quaternion[3] = cos(halfAngleRadian); + } + + void glusQuaternionRotateRzf(float quaternion[4], const float angle) + { + float halfAngleRadian = deg2rad(angle) * 0.5f; + + quaternion[0] = 0.0f; + quaternion[1] = 0.0f; + quaternion[2] = sin(halfAngleRadian); + quaternion[3] = cos(halfAngleRadian); + } + + void glusQuaternionGetMatrix4x4f(float matrix[16], const float quaternion[4]) + { + float x = quaternion[0]; + float y = quaternion[1]; + float z = quaternion[2]; + float w = quaternion[3]; + + matrix[0] = 1.0f - 2.0f * y * y - 2.0f * z * z; + matrix[1] = 2.0f * x * y + 2.0f * w * z; + matrix[2] = 2.0f * x * z - 2.0f * w * y; + matrix[3] = 0.0f; + + matrix[4] = 2.0f * x * y - 2.0f * w * z; + matrix[5] = 1.0f - 2.0f * x * x - 2.0f * z * z; + matrix[6] = 2.0f * y * z + 2.0f * w * x; + matrix[7] = 0.0f; + + matrix[8] = 2.0f * x * z + 2.0f * w * y; + matrix[9] = 2.0f * y * z - 2.0f * w * x; + matrix[10] = 1.0f - 2.0f * x * x - 2.0f * y * y; + matrix[11] = 0.0f; + + matrix[12] = 0.0f; + matrix[13] = 0.0f; + matrix[14] = 0.0f; + matrix[15] = 1.0f; + } + + void glusMatrix4x4MultiplyVector3f(float result[3], const float matrix[16], const float vector[3]) + { + int i; + + float temp[3]; + + for (i = 0; i < 3; i++) + { + temp[i] = matrix[i] * vector[0] + matrix[4 + i] * vector[1] + matrix[8 + i] * vector[2]; } - if(tcp) - { - tex_x=(float) j / (float) numberSlices; + for (i = 0; i < 3; i++) + { + result[i] = temp[i]; + } + } + }//namespace - *tcp=tex_x;++tcp; - *tcp=1.0f - (float) i / (float) numberParallels;++tcp; + /** + * 创建一个球体的可渲染数据,球心为0,0,0,半径为1 + * @param numberSlices 切片数 + * @return 可渲染数据 + */ + Primitive *CreateSphere(RenderResource *db,const VAB *vab,const uint numberSlices) + { + PrimitiveCreater rc(db,vab); - if(tp) + uint numberParallels = (numberSlices+1) / 2; + uint numberVertices = (numberParallels + 1) * (numberSlices + 1); + uint numberIndices = numberParallels * numberSlices * 6; + + const double angleStep = double(2.0f * HGL_PI) / ((double) numberSlices); + + // used later to help us calculating tangents vectors + float helpVector[3] = { 1.0f, 0.0f, 0.0f }; + float helpQuaternion[4]; + float helpMatrix[16]; + float tex_x; + + if(!rc.Init(numberVertices)) + return(nullptr); + + AutoDelete vertex=rc.CreateVADA(VAN::Position); + AutoDelete normal=rc.CreateVADA(VAN::Normal); + AutoDelete tangent=rc.CreateVADA(VAN::Tangent); + AutoDelete tex_coord=rc.CreateVADA(VAN::TexCoord); + + float *vp=vertex->Get(); + float *np=normal?normal->Get():nullptr; + float *tp=tangent?tangent->Get():nullptr; + float *tcp=tex_coord?tex_coord->Get():nullptr; + + for (uint i = 0; i < numberParallels + 1; i++) + { + for (uint j = 0; j < numberSlices + 1; j++) + { + float x = sin(angleStep * (double) i) * sin(angleStep * (double) j); + float y = sin(angleStep * (double) i) * cos(angleStep * (double) j); + float z = cos(angleStep * (double) i); + + *vp=x;++vp; + *vp=y;++vp; + *vp=z;++vp; + + if(np) { - // use quaternion to get the tangent vector - glusQuaternionRotateRyf(helpQuaternion, 360.0f * tex_x); - glusQuaternionGetMatrix4x4f(helpMatrix, helpQuaternion); + *np=+x;++np; + *np=-y;++np; + *np=+z;++np; + } - glusMatrix4x4MultiplyVector3f(tp, helpMatrix, helpVector); - tp+=3; + if(tcp) + { + tex_x=(float) j / (float) numberSlices; + + *tcp=tex_x;++tcp; + *tcp=1.0f - (float) i / (float) numberParallels;++tcp; + + if(tp) + { + // use quaternion to get the tangent vector + glusQuaternionRotateRyf(helpQuaternion, 360.0f * tex_x); + glusQuaternionGetMatrix4x4f(helpMatrix, helpQuaternion); + + glusMatrix4x4MultiplyVector3f(tp, helpMatrix, helpVector); + tp+=3; + } } } } + + if(numberVertices<=0xffff) + CreateSphereIndices(rc.CreateIBO16(numberIndices),numberParallels,numberSlices); + else + CreateSphereIndices(rc.CreateIBO32(numberIndices),numberParallels,numberSlices); + + return rc.Finish(); } - if(numberVertices<=0xffff) - CreateSphereIndices(rc.CreateIBO16(numberIndices),numberParallels,numberSlices); - else - CreateSphereIndices(rc.CreateIBO32(numberIndices),numberParallels,numberSlices); - - return rc.Finish(); - } - - namespace - { - template - void CreateTorusIndices(T *tp,uint numberSlices,uint numberStacks) + Primitive *CreateDome(RenderResource *db,const VAB *vab,const uint numberSlices) { - // loop counters - uint sideCount, faceCount; + PrimitiveCreater rc(db,vab); - // used to generate the indices - uint v0, v1, v2, v3; + uint i, j; - for (sideCount = 0; sideCount < numberSlices; ++sideCount) + uint numberParallels = numberSlices / 4; + uint numberVertices = (numberParallels + 1) * (numberSlices + 1); + uint numberIndices = numberParallels * numberSlices * 6; + + float angleStep = (2.0f * HGL_PI) / ((float) numberSlices); + + // used later to help us calculating tangents vectors + float helpVector[3] = { 1.0f, 0.0f, 0.0f }; + float helpQuaternion[4]; + float helpMatrix[16]; + float tex_x; + + if (numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) + return nullptr; + + if(!rc.Init(numberVertices)) + return(nullptr); + + AutoDelete vertex=rc.CreateVADA(VAN::Position); + AutoDelete normal=rc.CreateVADA(VAN::Normal); + AutoDelete tangent=rc.CreateVADA(VAN::Tangent); + AutoDelete tex_coord=rc.CreateVADA(VAN::TexCoord); + + float *vp=vertex->Get(); + float *np=normal?normal->Get():nullptr; + float *tp=tangent?tangent->Get():nullptr; + float *tcp=tex_coord?tex_coord->Get():nullptr; + + for (i = 0; i < numberParallels + 1; i++) { - for (faceCount = 0; faceCount < numberStacks; ++faceCount) + for (j = 0; j < numberSlices + 1; j++) { - // get the number of the vertices for a face of the torus. They must be < numVertices - v0 = ((sideCount * (numberStacks + 1)) + faceCount); - v1 = (((sideCount + 1) * (numberStacks + 1)) + faceCount); - v2 = (((sideCount + 1) * (numberStacks + 1)) + (faceCount + 1)); - v3 = ((sideCount * (numberStacks + 1)) + (faceCount + 1)); + uint vertexIndex = (i * (numberSlices + 1) + j) * 4; + uint normalIndex = (i * (numberSlices + 1) + j) * 3; + uint tangentIndex = (i * (numberSlices + 1) + j) * 3; + uint texCoordsIndex = (i * (numberSlices + 1) + j) * 2; + + float x = sin(angleStep * (double) i) * sin(angleStep * (double) j); + float y = sin(angleStep * (double) i) * cos(angleStep * (double) j); + float z = cos(angleStep * (double) i); - // first triangle of the face, counter clock wise winding - *tp = v0; ++tp; - *tp = v2; ++tp; - *tp = v1; ++tp; + *vp=x;++vp; + *vp=y;++vp; + *vp=z;++vp; - // second triangle of the face, counter clock wise winding - *tp = v0; ++tp; - *tp = v3; ++tp; - *tp = v2; ++tp; + if(np) + { + *np=+x;++np; + *np=-y;++np; + *np=+z;++np; + } + + if(tcp) + { + tex_x=(float) j / (float) numberSlices; + + *tcp=tex_x;++tcp; + *tcp=1.0f - (float) i / (float) numberParallels;++tcp; + + if(tp) + { + // use quaternion to get the tangent vector + glusQuaternionRotateRyf(helpQuaternion, 360.0f * tex_x); + glusQuaternionGetMatrix4x4f(helpMatrix, helpQuaternion); + + glusMatrix4x4MultiplyVector3f(tp, helpMatrix, helpVector); + tp+=3; + } + } } } + + if(numberVertices<=0xffff) + CreateSphereIndices(rc.CreateIBO16(numberIndices),numberParallels,numberSlices); + else + CreateSphereIndices(rc.CreateIBO32(numberIndices),numberParallels,numberSlices); + + return rc.Finish(); } - }//namespace - Primitive *CreateRenderableTorus(RenderResource *db,const VAB *vab,const TorusCreateInfo *tci) - { - PrimitiveCreater rc(db,vab); - - // s, t = parametric values of the equations, in the range [0,1] - float s = 0; - float t = 0; - - // sIncr, tIncr are increment values aplied to s and t on each loop iteration to generate the torus - float sIncr; - float tIncr; - - // to store precomputed sin and cos values - float cos2PIs, sin2PIs, cos2PIt, sin2PIt; - - uint sideCount,faceCount; - - uint numberVertices; - uint numberIndices; - - // used later to help us calculating tangents vectors - float helpVector[3] = { 0.0f, 1.0f, 0.0f }; - float helpQuaternion[4]; - float helpMatrix[16]; - - float torusRadius = (tci->outerRadius - tci->innerRadius) / 2.0f; - float centerRadius = tci->outerRadius - torusRadius; - - numberVertices = (tci->numberStacks + 1) * (tci->numberSlices + 1); - numberIndices = tci->numberStacks * tci->numberSlices * 2 * 3; // 2 triangles per face * 3 indices per triangle - - if (tci->numberSlices < 3 || tci->numberStacks < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) - return(nullptr); - - sIncr = 1.0f / (float) tci->numberSlices; - tIncr = 1.0f / (float) tci->numberStacks; - - if(!rc.Init(numberVertices)) - return(nullptr); - - AutoDelete vertex=rc.CreateVADA(VAN::Position); - AutoDelete normal=rc.CreateVADA(VAN::Normal); - AutoDelete tangent=rc.CreateVADA(VAN::Tangent); - AutoDelete tex_coord=rc.CreateVADA(VAN::TexCoord); - - float *vp=vertex->Get(); - float *np=normal?normal->Get():nullptr; - float *tp=tangent?tangent->Get():nullptr; - float *tcp=tex_coord?tex_coord->Get():nullptr; - - // generate vertices and its attributes - for (sideCount = 0; sideCount <= tci->numberSlices; ++sideCount, s += sIncr) + namespace { - // precompute some values - cos2PIs = cos(2.0f * HGL_PI * s); - sin2PIs = sin(2.0f * HGL_PI * s); + template + void CreateTorusIndices(T *tp,uint numberSlices,uint numberStacks) + { + // loop counters + uint sideCount, faceCount; - t = 0.0f; - for (faceCount = 0; faceCount <= tci->numberStacks; ++faceCount, t += tIncr) + // used to generate the indices + uint v0, v1, v2, v3; + + for (sideCount = 0; sideCount < numberSlices; ++sideCount) + { + for (faceCount = 0; faceCount < numberStacks; ++faceCount) + { + // get the number of the vertices for a face of the torus. They must be < numVertices + v0 = ((sideCount * (numberStacks + 1)) + faceCount); + v1 = (((sideCount + 1) * (numberStacks + 1)) + faceCount); + v2 = (((sideCount + 1) * (numberStacks + 1)) + (faceCount + 1)); + v3 = ((sideCount * (numberStacks + 1)) + (faceCount + 1)); + + // first triangle of the face, counter clock wise winding + *tp = v0; ++tp; + *tp = v2; ++tp; + *tp = v1; ++tp; + + // second triangle of the face, counter clock wise winding + *tp = v0; ++tp; + *tp = v3; ++tp; + *tp = v2; ++tp; + } + } + } + }//namespace + + Primitive *CreateTorus(RenderResource *db,const VAB *vab,const TorusCreateInfo *tci) + { + PrimitiveCreater rc(db,vab); + + // s, t = parametric values of the equations, in the range [0,1] + float s = 0; + float t = 0; + + // sIncr, tIncr are increment values aplied to s and t on each loop iteration to generate the torus + float sIncr; + float tIncr; + + // to store precomputed sin and cos values + float cos2PIs, sin2PIs, cos2PIt, sin2PIt; + + uint sideCount,faceCount; + + uint numberVertices; + uint numberIndices; + + // used later to help us calculating tangents vectors + float helpVector[3] = { 0.0f, 1.0f, 0.0f }; + float helpQuaternion[4]; + float helpMatrix[16]; + + float torusRadius = (tci->outerRadius - tci->innerRadius) / 2.0f; + float centerRadius = tci->outerRadius - torusRadius; + + numberVertices = (tci->numberStacks + 1) * (tci->numberSlices + 1); + numberIndices = tci->numberStacks * tci->numberSlices * 2 * 3; // 2 triangles per face * 3 indices per triangle + + if (tci->numberSlices < 3 || tci->numberStacks < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) + return(nullptr); + + sIncr = 1.0f / (float) tci->numberSlices; + tIncr = 1.0f / (float) tci->numberStacks; + + if(!rc.Init(numberVertices)) + return(nullptr); + + AutoDelete vertex=rc.CreateVADA(VAN::Position); + AutoDelete normal=rc.CreateVADA(VAN::Normal); + AutoDelete tangent=rc.CreateVADA(VAN::Tangent); + AutoDelete tex_coord=rc.CreateVADA(VAN::TexCoord); + + float *vp=vertex->Get(); + float *np=normal?normal->Get():nullptr; + float *tp=tangent?tangent->Get():nullptr; + float *tcp=tex_coord?tex_coord->Get():nullptr; + + // generate vertices and its attributes + for (sideCount = 0; sideCount <= tci->numberSlices; ++sideCount, s += sIncr) { // precompute some values - cos2PIt = cos(2.0f * HGL_PI * t); - sin2PIt = sin(2.0f * HGL_PI * t); + cos2PIs = cos(2.0f * HGL_PI * s); + sin2PIs = sin(2.0f * HGL_PI * s); - // generate vertex and stores it in the right position - *vp = (centerRadius + torusRadius * cos2PIt) * cos2PIs; ++vp; - *vp = torusRadius * sin2PIt; ++vp; - *vp = (centerRadius + torusRadius * cos2PIt) * sin2PIs; ++vp; - - if(np) + t = 0.0f; + for (faceCount = 0; faceCount <= tci->numberStacks; ++faceCount, t += tIncr) { - // generate normal and stores it in the right position - // NOTE: cos (2PIx) = cos (x) and sin (2PIx) = sin (x) so, we can use this formula - // normal = {cos(2PIs)cos(2PIt) , sin(2PIs)cos(2PIt) ,sin(2PIt)} - *np = +cos2PIs * cos2PIt; ++np; - *np = -sin2PIt; ++np; - *np = +sin2PIs * cos2PIt; ++np; - } + // precompute some values + cos2PIt = cos(2.0f * HGL_PI * t); + sin2PIt = sin(2.0f * HGL_PI * t); - if(tcp) - { - // generate texture coordinates and stores it in the right position - *tcp = s*tci->uv_scale.x; ++tcp; - *tcp = t*tci->uv_scale.y; ++tcp; - } + // generate vertex and stores it in the right position + *vp = (centerRadius + torusRadius * cos2PIt) * cos2PIs; ++vp; + *vp = torusRadius * sin2PIt; ++vp; + *vp = (centerRadius + torusRadius * cos2PIt) * sin2PIs; ++vp; - if(tp) - { - // use quaternion to get the tangent vector - glusQuaternionRotateRzf(helpQuaternion, 360.0f * s); - glusQuaternionGetMatrix4x4f(helpMatrix, helpQuaternion); + if(np) + { + // generate normal and stores it in the right position + // NOTE: cos (2PIx) = cos (x) and sin (2PIx) = sin (x) so, we can use this formula + // normal = {cos(2PIs)cos(2PIt) , sin(2PIs)cos(2PIt) ,sin(2PIt)} + *np = +cos2PIs * cos2PIt; ++np; + *np = -sin2PIt; ++np; + *np = +sin2PIs * cos2PIt; ++np; + } - glusMatrix4x4MultiplyVector3f(tp, helpMatrix, helpVector); - tp+=3; + if(tcp) + { + // generate texture coordinates and stores it in the right position + *tcp = s*tci->uv_scale.x; ++tcp; + *tcp = t*tci->uv_scale.y; ++tcp; + } + + if(tp) + { + // use quaternion to get the tangent vector + glusQuaternionRotateRzf(helpQuaternion, 360.0f * s); + glusQuaternionGetMatrix4x4f(helpMatrix, helpQuaternion); + + glusMatrix4x4MultiplyVector3f(tp, helpMatrix, helpVector); + tp+=3; + } } } + + if(numberVertices<=0xffff) + CreateTorusIndices(rc.CreateIBO16(numberIndices),tci->numberSlices,tci->numberStacks); + else + CreateTorusIndices(rc.CreateIBO32(numberIndices),tci->numberSlices,tci->numberStacks); + + return rc.Finish(); } - if(numberVertices<=0xffff) - CreateTorusIndices(rc.CreateIBO16(numberIndices),tci->numberSlices,tci->numberStacks); - else - CreateTorusIndices(rc.CreateIBO32(numberIndices),tci->numberSlices,tci->numberStacks); - - return rc.Finish(); - } - - namespace - { - template - void CreateCylinderIndices(T *tp,const uint numberSlices) + namespace { - uint i; - - T centerIndex = 0; - T indexCounter = 1; - - for (i = 0; i < numberSlices; i++) + template + void CreateCylinderIndices(T *tp,const uint numberSlices) { - *tp = centerIndex; ++tp; - *tp = indexCounter; ++tp; - *tp = indexCounter + 1; ++tp; + uint i; + T centerIndex = 0; + T indexCounter = 1; + + for (i = 0; i < numberSlices; i++) + { + *tp = centerIndex; ++tp; + *tp = indexCounter; ++tp; + *tp = indexCounter + 1; ++tp; + + indexCounter++; + } indexCounter++; - } - indexCounter++; - - // Top - centerIndex = indexCounter; - indexCounter++; - - for (i = 0; i < numberSlices; i++) - { - *tp = centerIndex; ++tp; - *tp = indexCounter + 1; ++tp; - *tp = indexCounter; ++tp; + // Top + centerIndex = indexCounter; indexCounter++; + + for (i = 0; i < numberSlices; i++) + { + *tp = centerIndex; ++tp; + *tp = indexCounter + 1; ++tp; + *tp = indexCounter; ++tp; + + indexCounter++; + } + indexCounter++; + + // Sides + for (i = 0; i < numberSlices; i++) + { + *tp = indexCounter; ++tp; + *tp = indexCounter + 1; ++tp; + *tp = indexCounter + 2; ++tp; + + *tp = indexCounter + 2; ++tp; + *tp = indexCounter + 1; ++tp; + *tp = indexCounter + 3; ++tp; + + indexCounter += 2; + } } - indexCounter++; + }//namespace - // Sides - for (i = 0; i < numberSlices; i++) - { - *tp = indexCounter; ++tp; - *tp = indexCounter + 1; ++tp; - *tp = indexCounter + 2; ++tp; - - *tp = indexCounter + 2; ++tp; - *tp = indexCounter + 1; ++tp; - *tp = indexCounter + 3; ++tp; - - indexCounter += 2; - } - } - }//namespace - - Primitive *CreateRenderableCylinder(RenderResource *db,const VAB *vab,const CylinderCreateInfo *cci) - { - uint numberIndices = cci->numberSlices * 3 * 2 + cci->numberSlices * 6; - - if(numberIndices<=0) - return(nullptr); - - PrimitiveCreater rc(db,vab); - - uint numberVertices = (cci->numberSlices + 2) * 2 + (cci->numberSlices + 1) * 2; - - if(!rc.Init(numberVertices)) - return(nullptr); - - float angleStep = (2.0f * HGL_PI) / ((float) cci->numberSlices); - - if (cci->numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) - return nullptr; - - AutoDelete vertex=rc.CreateVADA(VAN::Position); - AutoDelete normal=rc.CreateVADA(VAN::Normal); - AutoDelete tangent=rc.CreateVADA(VAN::Tangent); - AutoDelete tex_coord=rc.CreateVADA(VAN::TexCoord); - - float *vp=vertex->Get(); - float *np=normal?normal->Get():nullptr; - float *tp=tangent?tangent->Get():nullptr; - float *tcp=tex_coord?tex_coord->Get():nullptr; - - *vp = 0.0f; ++vp; - *vp = 0.0f; ++vp; - *vp = -cci->halfExtend; ++vp; - - if(np) + Primitive *CreateCylinder(RenderResource *db,const VAB *vab,const CylinderCreateInfo *cci) { - *np = 0.0f; ++np; - *np = 0.0f; ++np; - *np =-1.0f; ++np; - } + uint numberIndices = cci->numberSlices * 3 * 2 + cci->numberSlices * 6; - if(tp) - { - *tp = 0.0f; ++tp; - *tp = 1.0f; ++tp; - *tp = 0.0f; ++tp; - } + if(numberIndices<=0) + return(nullptr); - if(tcp) - { - *tcp = 0.0f; ++tcp; - *tcp = 0.0f; ++tcp; - } + PrimitiveCreater rc(db,vab); - for(uint i = 0; i < cci->numberSlices + 1; i++) - { - float currentAngle = angleStep * (float)i; + uint numberVertices = (cci->numberSlices + 2) * 2 + (cci->numberSlices + 1) * 2; - *vp = cos(currentAngle) * cci->radius; ++vp; - *vp = -sin(currentAngle) * cci->radius; ++vp; - *vp = -cci->halfExtend; ++vp; + if(!rc.Init(numberVertices)) + return(nullptr); + + float angleStep = (2.0f * HGL_PI) / ((float) cci->numberSlices); + + if (cci->numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) + return nullptr; + + AutoDelete vertex=rc.CreateVADA(VAN::Position); + AutoDelete normal=rc.CreateVADA(VAN::Normal); + AutoDelete tangent=rc.CreateVADA(VAN::Tangent); + AutoDelete tex_coord=rc.CreateVADA(VAN::TexCoord); + + float *vp=vertex->Get(); + float *np=normal?normal->Get():nullptr; + float *tp=tangent?tangent->Get():nullptr; + float *tcp=tex_coord?tex_coord->Get():nullptr; + + *vp = 0.0f; ++vp; + *vp = 0.0f; ++vp; + *vp = -cci->halfExtend; ++vp; if(np) { @@ -863,9 +837,9 @@ namespace hgl if(tp) { - *tp = sin(currentAngle); ++tp; - *tp = cos(currentAngle); ++tp; - *tp = 0.0f; ++tp; + *tp = 0.0f; ++tp; + *tp = 1.0f; ++tp; + *tp = 0.0f; ++tp; } if(tcp) @@ -873,39 +847,39 @@ namespace hgl *tcp = 0.0f; ++tcp; *tcp = 0.0f; ++tcp; } - } - *vp = 0.0f; ++vp; - *vp = 0.0f; ++vp; - *vp = cci->halfExtend; ++vp; + for(uint i = 0; i < cci->numberSlices + 1; i++) + { + float currentAngle = angleStep * (float)i; - if(np) - { - *np = 0.0f; ++np; - *np = 0.0f; ++np; - *np = 1.0f; ++np; - } + *vp = cos(currentAngle) * cci->radius; ++vp; + *vp = -sin(currentAngle) * cci->radius; ++vp; + *vp = -cci->halfExtend; ++vp; - if(tp) - { - *tp = 0.0f; ++tp; - *tp = -1.0f; ++tp; - *tp = 0.0f; ++tp; - } + if(np) + { + *np = 0.0f; ++np; + *np = 0.0f; ++np; + *np =-1.0f; ++np; + } - if(tcp) - { - *tcp = 1.0f; ++tcp; - *tcp = 1.0f; ++tcp; - } + if(tp) + { + *tp = sin(currentAngle); ++tp; + *tp = cos(currentAngle); ++tp; + *tp = 0.0f; ++tp; + } - for(uint i = 0; i < cci->numberSlices + 1; i++) - { - float currentAngle = angleStep * (float)i; + if(tcp) + { + *tcp = 0.0f; ++tcp; + *tcp = 0.0f; ++tcp; + } + } - *vp = cos(currentAngle) * cci->radius; ++vp; - *vp = -sin(currentAngle) * cci->radius; ++vp; - *vp = cci->halfExtend; ++vp; + *vp = 0.0f; ++vp; + *vp = 0.0f; ++vp; + *vp = cci->halfExtend; ++vp; if(np) { @@ -916,9 +890,9 @@ namespace hgl if(tp) { - *tp = -sin(currentAngle); ++tp; - *tp = -cos(currentAngle); ++tp; - *tp = 0.0f; ++tp; + *tp = 0.0f; ++tp; + *tp = -1.0f; ++tp; + *tp = 0.0f; ++tp; } if(tcp) @@ -926,25 +900,20 @@ namespace hgl *tcp = 1.0f; ++tcp; *tcp = 1.0f; ++tcp; } - } - for(uint i = 0; i < cci->numberSlices + 1; i++) - { - float currentAngle = angleStep * (float)i; - - float sign = -1.0f; - - for (uint j = 0; j < 2; j++) + for(uint i = 0; i < cci->numberSlices + 1; i++) { - *vp = cos(currentAngle) * cci->radius; ++vp; - *vp = -sin(currentAngle) * cci->radius; ++vp; - *vp = cci->halfExtend * sign; ++vp; + float currentAngle = angleStep * (float)i; + + *vp = cos(currentAngle) * cci->radius; ++vp; + *vp = -sin(currentAngle) * cci->radius; ++vp; + *vp = cci->halfExtend; ++vp; if(np) { - *np = cos(currentAngle); ++np; - *np = sin(currentAngle); ++np; - *np = 0.0f; ++np; + *np = 0.0f; ++np; + *np = 0.0f; ++np; + *np = 1.0f; ++np; } if(tp) @@ -956,125 +925,130 @@ namespace hgl if(tcp) { - *tcp = (float)i / (float)cci->numberSlices; ++tcp; - *tcp = (sign + 1.0f) / 2.0f; ++tcp; + *tcp = 1.0f; ++tcp; + *tcp = 1.0f; ++tcp; } - - sign = 1.0f; } + + for(uint i = 0; i < cci->numberSlices + 1; i++) + { + float currentAngle = angleStep * (float)i; + + float sign = -1.0f; + + for (uint j = 0; j < 2; j++) + { + *vp = cos(currentAngle) * cci->radius; ++vp; + *vp = -sin(currentAngle) * cci->radius; ++vp; + *vp = cci->halfExtend * sign; ++vp; + + if(np) + { + *np = cos(currentAngle); ++np; + *np = sin(currentAngle); ++np; + *np = 0.0f; ++np; + } + + if(tp) + { + *tp = -sin(currentAngle); ++tp; + *tp = -cos(currentAngle); ++tp; + *tp = 0.0f; ++tp; + } + + if(tcp) + { + *tcp = (float)i / (float)cci->numberSlices; ++tcp; + *tcp = (sign + 1.0f) / 2.0f; ++tcp; + } + + sign = 1.0f; + } + } + + if(numberVertices<=0xffff) + CreateCylinderIndices(rc.CreateIBO16(numberIndices),cci->numberSlices); + else + CreateCylinderIndices(rc.CreateIBO32(numberIndices),cci->numberSlices); + + return rc.Finish(); } - if(numberVertices<=0xffff) - CreateCylinderIndices(rc.CreateIBO16(numberIndices),cci->numberSlices); - else - CreateCylinderIndices(rc.CreateIBO32(numberIndices),cci->numberSlices); - - return rc.Finish(); - } - - namespace - { - template - void CreateConeIndices(T *tp,const uint numberSlices,const uint numberStacks) + namespace { - // Bottom - uint centerIndex = 0; - uint indexCounter = 1; - uint i,j; - - for (i = 0; i < numberSlices; i++) + template + void CreateConeIndices(T *tp,const uint numberSlices,const uint numberStacks) { - *tp = centerIndex; ++tp; - *tp = indexCounter; ++tp; - *tp = indexCounter + 1; ++tp; + // Bottom + uint centerIndex = 0; + uint indexCounter = 1; + uint i,j; - indexCounter++; - } - indexCounter++; - - // Sides - for (j = 0; j < numberStacks; j++) - { for (i = 0; i < numberSlices; i++) { - *tp = indexCounter; ++tp; - *tp = indexCounter + numberSlices + 1; ++tp; - *tp = indexCounter + 1; ++tp; - - *tp = indexCounter + 1; ++tp; - *tp = indexCounter + numberSlices + 1; ++tp; - *tp = indexCounter + numberSlices + 2; ++tp; + *tp = centerIndex; ++tp; + *tp = indexCounter; ++tp; + *tp = indexCounter + 1; ++tp; indexCounter++; } indexCounter++; + + // Sides + for (j = 0; j < numberStacks; j++) + { + for (i = 0; i < numberSlices; i++) + { + *tp = indexCounter; ++tp; + *tp = indexCounter + numberSlices + 1; ++tp; + *tp = indexCounter + 1; ++tp; + + *tp = indexCounter + 1; ++tp; + *tp = indexCounter + numberSlices + 1; ++tp; + *tp = indexCounter + numberSlices + 2; ++tp; + + indexCounter++; + } + indexCounter++; + } } - } - }//namespace + }//namespace - Primitive *CreateRenderableCone(RenderResource *db,const VAB *vab,const ConeCreateInfo *cci) - { - PrimitiveCreater rc(db,vab); - - uint i, j; - - uint numberVertices = (cci->numberSlices + 2) + (cci->numberSlices + 1) * (cci->numberStacks + 1); - - if(!rc.Init(numberVertices)) - return(nullptr); - - uint numberIndices = cci->numberSlices * 3 + cci->numberSlices * 6 * cci->numberStacks; - - float angleStep = (2.0f * HGL_PI) / ((float) cci->numberSlices); - - float h = 2.0f * cci->halfExtend; - float r = cci->radius; - float l = sqrtf(h*h + r*r); - - if (cci->numberSlices < 3 || cci->numberStacks < 1 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) - return nullptr; - - AutoDelete vertex=rc.CreateVADA(VAN::Position); - AutoDelete normal=rc.CreateVADA(VAN::Normal); - AutoDelete tangent=rc.CreateVADA(VAN::Tangent); - AutoDelete tex_coord=rc.CreateVADA(VAN::TexCoord); - - float *vp=vertex->Get(); - float *np=normal?normal->Get():nullptr; - float *tp=tangent?tangent->Get():nullptr; - float *tcp=tex_coord?tex_coord->Get():nullptr; - - *vp = 0.0f; ++vp; - *vp = 0.0f; ++vp; - *vp = -cci->halfExtend; ++vp; - - if(np) + Primitive *CreateCone(RenderResource *db,const VAB *vab,const ConeCreateInfo *cci) { - *np = 0.0f;++np; - *np = 0.0f;++np; - *np =-1.0f;++np; - } + PrimitiveCreater rc(db,vab); - if(tp) - { - *tp = 0.0f; ++tp; - *tp = 1.0f; ++tp; - *tp = 0.0f; ++tp; - } + uint i, j; - if(tcp) - { - *tcp = 0.0f; ++tcp; - *tcp = 0.0f; ++tcp; - } + uint numberVertices = (cci->numberSlices + 2) + (cci->numberSlices + 1) * (cci->numberStacks + 1); - for (i = 0; i < cci->numberSlices + 1; i++) - { - float currentAngle = angleStep * (float)i; + if(!rc.Init(numberVertices)) + return(nullptr); - *vp = cos(currentAngle) * cci->radius; ++vp; - *vp = -sin(currentAngle) * cci->radius; ++vp; - *vp = -cci->halfExtend; ++vp; + uint numberIndices = cci->numberSlices * 3 + cci->numberSlices * 6 * cci->numberStacks; + + float angleStep = (2.0f * HGL_PI) / ((float) cci->numberSlices); + + float h = 2.0f * cci->halfExtend; + float r = cci->radius; + float l = sqrtf(h*h + r*r); + + if (cci->numberSlices < 3 || cci->numberStacks < 1 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) + return nullptr; + + AutoDelete vertex=rc.CreateVADA(VAN::Position); + AutoDelete normal=rc.CreateVADA(VAN::Normal); + AutoDelete tangent=rc.CreateVADA(VAN::Tangent); + AutoDelete tex_coord=rc.CreateVADA(VAN::TexCoord); + + float *vp=vertex->Get(); + float *np=normal?normal->Get():nullptr; + float *tp=tangent?tangent->Get():nullptr; + float *tcp=tex_coord?tex_coord->Get():nullptr; + + *vp = 0.0f; ++vp; + *vp = 0.0f; ++vp; + *vp = -cci->halfExtend; ++vp; if(np) { @@ -1085,9 +1059,9 @@ namespace hgl if(tp) { - *tp = sin(currentAngle); ++tp; - *tp = cos(currentAngle); ++tp; - *tp = 0.0f; ++tp; + *tp = 0.0f; ++tp; + *tp = 1.0f; ++tp; + *tp = 0.0f; ++tp; } if(tcp) @@ -1095,129 +1069,158 @@ namespace hgl *tcp = 0.0f; ++tcp; *tcp = 0.0f; ++tcp; } - } - - for (j = 0; j < cci->numberStacks + 1; j++) - { - float level = (float)j / (float)cci->numberStacks; for (i = 0; i < cci->numberSlices + 1; i++) { float currentAngle = angleStep * (float)i; - *vp = cos(currentAngle) * cci->radius * (1.0f - level); ++vp; - *vp = -sin(currentAngle) * cci->radius * (1.0f - level); ++vp; - *vp = -cci->halfExtend + 2.0f * cci->halfExtend * level; ++vp; + *vp = cos(currentAngle) * cci->radius; ++vp; + *vp = -sin(currentAngle) * cci->radius; ++vp; + *vp = -cci->halfExtend; ++vp; if(np) { - *np = h / l * cos(currentAngle); ++np; - *np = h / l * sin(currentAngle); ++np; - *np = r / l; ++np; + *np = 0.0f;++np; + *np = 0.0f;++np; + *np =-1.0f;++np; } if(tp) { - *tp = -sin(currentAngle); ++tp; - *tp = -cos(currentAngle); ++tp; + *tp = sin(currentAngle); ++tp; + *tp = cos(currentAngle); ++tp; *tp = 0.0f; ++tp; } if(tcp) { - *tcp = (float)i / (float)cci->numberSlices; ++tcp; - *tcp = level; ++tcp; + *tcp = 0.0f; ++tcp; + *tcp = 0.0f; ++tcp; } } + + for (j = 0; j < cci->numberStacks + 1; j++) + { + float level = (float)j / (float)cci->numberStacks; + + for (i = 0; i < cci->numberSlices + 1; i++) + { + float currentAngle = angleStep * (float)i; + + *vp = cos(currentAngle) * cci->radius * (1.0f - level); ++vp; + *vp = -sin(currentAngle) * cci->radius * (1.0f - level); ++vp; + *vp = -cci->halfExtend + 2.0f * cci->halfExtend * level; ++vp; + + if(np) + { + *np = h / l * cos(currentAngle); ++np; + *np = h / l * sin(currentAngle); ++np; + *np = r / l; ++np; + } + + if(tp) + { + *tp = -sin(currentAngle); ++tp; + *tp = -cos(currentAngle); ++tp; + *tp = 0.0f; ++tp; + } + + if(tcp) + { + *tcp = (float)i / (float)cci->numberSlices; ++tcp; + *tcp = level; ++tcp; + } + } + } + + if(numberVertices<=0xffff) + CreateConeIndices(rc.CreateIBO16(numberIndices),cci->numberSlices,cci->numberStacks); + else + CreateConeIndices(rc.CreateIBO32(numberIndices),cci->numberSlices,cci->numberStacks); + + return rc.Finish(); } - if(numberVertices<=0xffff) - CreateConeIndices(rc.CreateIBO16(numberIndices),cci->numberSlices,cci->numberStacks); - else - CreateConeIndices(rc.CreateIBO32(numberIndices),cci->numberSlices,cci->numberStacks); - - return rc.Finish(); - } - - Primitive *CreateRenderableAxis(RenderResource *db,const VAB *vab,const AxisCreateInfo *aci) - { - if(!db||!vab||!aci)return(nullptr); - - PrimitiveCreater rc(db,vab); - - if(!rc.Init(6)) - return(nullptr); - - AutoDelete vertex=rc.CreateVADA(VAN::Position); - AutoDelete color=rc.CreateVADA(VAN::Color); - - if(!vertex||!color) - return(nullptr); - - const float s=aci->size; - - vertex->Write(0,0,0);color->Write(aci->color[0]); - vertex->Write(s,0,0);color->Write(aci->color[0]); - vertex->Write(0,0,0);color->Write(aci->color[1]); - vertex->Write(0,s,0);color->Write(aci->color[1]); - vertex->Write(0,0,0);color->Write(aci->color[2]); - vertex->Write(0,0,s);color->Write(aci->color[2]); - - return rc.Finish(); - } - - Primitive *CreateRenderableBoundingBox(RenderResource *db,const VAB *vab,const BoundingBoxCreateInfo *cci) - { - // Points of a cube. - /* 4 5 */ const float points[]={ -0.5,-0.5, 0.5, 0.5,-0.5,0.5, 0.5,-0.5,-0.5, -0.5,-0.5,-0.5, - /* *------------* */ -0.5, 0.5, 0.5, 0.5, 0.5,0.5, 0.5, 0.5,-0.5, -0.5, 0.5,-0.5}; - /* /| /| */ - /* 0/ | 1/ | */ - /* *--+---------* | */ - /* | | | | */ - /* | 7| | 6| */ - /* | *---------+--* */ - /* | / | / */ - /* |/ 2|/ */ - /* 3*------------* */ - - const uint16 indices[]= + Primitive *CreateAxis(RenderResource *db,const VAB *vab,const AxisCreateInfo *aci) { - 0,1, 1,2, 2,3, 3,0, - 4,5, 5,6, 6,7, 7,4, - 0,4, 1,5, 2,6, 3,7 - }; + if(!db||!vab||!aci)return(nullptr); - PrimitiveCreater rc(db,vab); + PrimitiveCreater rc(db,vab); - if(!rc.Init(8)) - return(nullptr); - - AutoDelete vertex=rc.CreateVADA(VAN::Position); - - if(!vertex)return(nullptr); - - rc.WriteVAD(VAN::Position,points,sizeof(points)); - - if(cci->color_type!=BoundingBoxCreateInfo::ColorType::NoColor) - { - ENUM_CLASS_RANGE_ERROR_RETURN_NULLPTR(cci->color_type); + if(!rc.Init(6)) + return(nullptr); + AutoDelete vertex=rc.CreateVADA(VAN::Position); AutoDelete color=rc.CreateVADA(VAN::Color); - if(color) - { - if(cci->color_type==BoundingBoxCreateInfo::ColorType::SameColor) - color->RepeatWrite(cci->color[0],8); - else - if(cci->color_type==BoundingBoxCreateInfo::ColorType::VertexColor) - color->Write(cci->color,8); - } + if(!vertex||!color) + return(nullptr); + + const float s=aci->size; + + vertex->Write(0,0,0);color->Write(aci->color[0]); + vertex->Write(s,0,0);color->Write(aci->color[0]); + vertex->Write(0,0,0);color->Write(aci->color[1]); + vertex->Write(0,s,0);color->Write(aci->color[1]); + vertex->Write(0,0,0);color->Write(aci->color[2]); + vertex->Write(0,0,s);color->Write(aci->color[2]); + + return rc.Finish(); } - rc.CreateIBO16(24,indices); + Primitive *CreateBoundingBox(RenderResource *db,const VAB *vab,const BoundingBoxCreateInfo *cci) + { + // Points of a cube. + /* 4 5 */ const float points[]={ -0.5,-0.5, 0.5, 0.5,-0.5,0.5, 0.5,-0.5,-0.5, -0.5,-0.5,-0.5, + /* *------------* */ -0.5, 0.5, 0.5, 0.5, 0.5,0.5, 0.5, 0.5,-0.5, -0.5, 0.5,-0.5}; + /* /| /| */ + /* 0/ | 1/ | */ + /* *--+---------* | */ + /* | | | | */ + /* | 7| | 6| */ + /* | *---------+--* */ + /* | / | / */ + /* |/ 2|/ */ + /* 3*------------* */ - return rc.Finish(); - } + const uint16 indices[]= + { + 0,1, 1,2, 2,3, 3,0, + 4,5, 5,6, 6,7, 7,4, + 0,4, 1,5, 2,6, 3,7 + }; + + PrimitiveCreater rc(db,vab); + + if(!rc.Init(8)) + return(nullptr); + + AutoDelete vertex=rc.CreateVADA(VAN::Position); + + if(!vertex)return(nullptr); + + rc.WriteVAD(VAN::Position,points,sizeof(points)); + + if(cci->color_type!=BoundingBoxCreateInfo::ColorType::NoColor) + { + ENUM_CLASS_RANGE_ERROR_RETURN_NULLPTR(cci->color_type); + + AutoDelete color=rc.CreateVADA(VAN::Color); + + if(color) + { + if(cci->color_type==BoundingBoxCreateInfo::ColorType::SameColor) + color->RepeatWrite(cci->color[0],8); + else + if(cci->color_type==BoundingBoxCreateInfo::ColorType::VertexColor) + color->Write(cci->color,8); + } + } + + rc.CreateIBO16(24,indices); + + return rc.Finish(); + } + }//namespace inline_geometry }//namespace graph }//namespace hgl