From 8ba37b8a9d47fae6f4b418ca7ec9658cac2b4940 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Mon, 24 Jun 2019 20:16:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=85=E7=BD=AE=E5=86=85=E4=BD=95=E4=BD=93?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9D=90=E6=A0=87=E8=BD=B4=E5=92=8C=E7=BB=91?= =?UTF-8?q?=E5=AE=9A=E7=9B=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/hgl/graph/InlineGeometry.h | 72 ++++++++++++-- src/SceneGraph/InlineGeometry.cpp | 151 +++++++++++++++++++++++++----- 2 files changed, 190 insertions(+), 33 deletions(-) diff --git a/inc/hgl/graph/InlineGeometry.h b/inc/hgl/graph/InlineGeometry.h index b2706e9f..a258120d 100644 --- a/inc/hgl/graph/InlineGeometry.h +++ b/inc/hgl/graph/InlineGeometry.h @@ -12,7 +12,7 @@ namespace hgl class SceneDB; /** - * 矩形创建信息 + * 矩形创建信息(扇形/三角形条) */ struct RectangleCreateInfo { @@ -22,7 +22,7 @@ namespace hgl vulkan::Renderable *CreateRenderableRectangle(SceneDB *db,vulkan::Material *mtl,const RectangleCreateInfo *rci); /** - * 圆角矩形创建信息 + * 圆角矩形创建信息(扇形) */ struct RoundRectangleCreateInfo:public RectangleCreateInfo { @@ -42,6 +42,9 @@ namespace hgl uint field_count; ///<分段次数 };//struct CircleCreateInfo + /** + * 创建一个2D圆形(扇形) + */ vulkan::Renderable *CreateRenderableCircle(SceneDB *db,vulkan::Material *mtl,const CircleCreateInfo *rci); /** @@ -58,6 +61,9 @@ namespace hgl Color4f side_color; //边界线条颜色 };//struct PlaneGridCreateInfo + /** + * 创建一个平面网格(线条) + */ vulkan::Renderable *CreateRenderablePlaneGrid(SceneDB *db,vulkan::Material *mtl,const PlaneGridCreateInfo *pgci); struct PlaneCreateInfo @@ -65,20 +71,39 @@ namespace hgl Vector2f tile; };//struct PlaneCreateInfo + /** + * 创建一个平面(三角形) + */ vulkan::Renderable *CreateRenderablePlane(SceneDB *db,vulkan::Material *mtl,const PlaneCreateInfo *pci); struct CubeCreateInfo { + Vector3f center; + Vector3f size; Vector2f tile; + + public: + + CubeCreateInfo() + { + center.Set(0,0,0); + center.Set(1,1,1); + tile.Set(1,1); + } };//struct CubeCreateInfo /** - * 创建一个中心坐标为0,0,0,长宽高为1的立方体 + * 创建一个立方体(三角形) */ vulkan::Renderable *CreateRenderableCube(SceneDB *db,vulkan::Material *mtl,const CubeCreateInfo *cci); /** - * 创建一个球心坐标为0,0,0,半径为1的球体 + * 创建一个绑定盒(线条) + */ + vulkan::Renderable *CreateRenderableBoundingBox(SceneDB *db,vulkan::Material *mtl,const CubeCreateInfo *cci); + + /** + * 创建一个球心坐标为0,0,0,半径为1的球体(三角形) */ vulkan::Renderable *CreateRenderableSphere(SceneDB *db,vulkan::Material *mtl,const uint numberSlices); @@ -89,7 +114,7 @@ namespace hgl };//struct DomeCreateInfo /** - * 创建一个穹顶 + * 创建一个穹顶(三角形) */ vulkan::Renderable *CreateRenderableDome(SceneDB *db,vulkan::Material *mtl, const DomeCreateInfo *); @@ -103,9 +128,9 @@ namespace hgl };//struct TorusCreateInfo /** - * 创建一个圆环 + * 创建一个圆环(三角形) */ - vulkan::Renderable *CreateRenderableTorus(SceneDB *db,vulkan::Material *mtl,TorusCreateInfo *tci); + vulkan::Renderable *CreateRenderableTorus(SceneDB *db,vulkan::Material *mtl,const TorusCreateInfo *tci); struct CylinderCreateInfo { @@ -114,7 +139,10 @@ namespace hgl uint numberSlices; };//struct CylinderCreateInfo - vulkan::Renderable *CreateRenderableCylinder(SceneDB *db,vulkan::Material *mtl,CylinderCreateInfo *cci); + /** + * 创建一个圆柱(三角形) + */ + vulkan::Renderable *CreateRenderableCylinder(SceneDB *db,vulkan::Material *mtl,const CylinderCreateInfo *cci); struct ConeCreateInfo { @@ -124,7 +152,33 @@ namespace hgl numberStacks; //柱高层数 };//struct ConeCreateInfo - vulkan::Renderable *CreateRenderableCone(SceneDB *db,vulkan::Material *mtl,ConeCreateInfo *cci); + /** + * 创建一个圆锥(三角形) + */ + vulkan::Renderable *CreateRenderableCone(SceneDB *db,vulkan::Material *mtl,const ConeCreateInfo *cci); + + struct AxisCreateInfo + { + Vector3f root; + Vector3f size; + Color4f color[3]; + + public: + + AxisCreateInfo() + { + root.Set(0,0,0); + size.Set(1,1,1); + color[0].Set(1,0,0,1); + color[1].Set(0,1,0,1); + color[2].Set(0,0,1,1); + } + };//struct AxisCreateInfo + + /** + * 创建一个坐标线(线条) + */ + vulkan::Renderable *CreateRenderableAxis(SceneDB *db,vulkan::Material *mtl,const AxisCreateInfo *aci); }//namespace graph };//namespace hgl #endif//HGL_GRAPH_INLINE_GEOMETRY_INCLUDE diff --git a/src/SceneGraph/InlineGeometry.cpp b/src/SceneGraph/InlineGeometry.cpp index d973c328..8671800d 100644 --- a/src/SceneGraph/InlineGeometry.cpp +++ b/src/SceneGraph/InlineGeometry.cpp @@ -74,11 +74,11 @@ namespace hgl void WriteTangent (const float *v){if(tangent )tangent ->BufferData(v);} void WriteTexCoord (const float *v){if(tex_coord )tex_coord ->BufferData(v);} - VERTEX_VB_FORMAT *GetVertex (){if(vertex )vertex ->Begin();return vertex;} - VB4f *GetColor (){if(color )color ->Begin();return color;} - VB3f *GetNormal (){if(normal )normal ->Begin();return normal;} - VB3f *GetTangent (){if(tangent )tangent ->Begin();return tangent;} - VB2f *GetTexCoord (){if(tex_coord )tex_coord ->Begin();return tex_coord;} + VERTEX_VB_FORMAT * GetVertex (){return vertex; } + VB4f * GetColor (){return color; } + VB3f * GetNormal (){return normal; } + VB3f * GetTangent (){return tangent; } + VB2f * GetTexCoord (){return tex_coord; } float *GetVertexPointer (){return vertex ?(float *)vertex ->Begin():nullptr;} float *GetColorPointer (){return color ?(float *)color ->Begin():nullptr;} @@ -116,10 +116,10 @@ namespace hgl Finish(vertex_binding,vertex); } - if(color)Finish(color_binding,color); - if(normal)Finish(normal_binding,normal); - if(tangent)Finish(tangent_binding,tangent); - if(tex_coord)Finish(texcoord_binding,tex_coord); + if(color )Finish(color_binding, color); + if(normal )Finish(normal_binding, normal); + if(tangent )Finish(tangent_binding, tangent); + if(tex_coord)Finish(texcoord_binding, tex_coord); if(ibo) { @@ -358,7 +358,7 @@ namespace hgl +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 }; - 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, + const 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 }; @@ -367,21 +367,45 @@ namespace hgl if(!gc.Init(24)) return(nullptr); - gc.WriteVertex(points); + if(cci->center ==Vector3f(0,0,0) + &&cci->size ==Vector3f(1,1,1)) + { + gc.WriteVertex(points); + } + else + { + const float *sp=points; + float *vp=gc.GetVertexPointer(); + + for(uint i=0;i<24;i++) + { + *vp=cci->center.x+(*sp)*cci->size.x; ++vp;++sp; + *vp=cci->center.y+(*sp)*cci->size.y; ++vp;++sp; + *vp=cci->center.z+(*sp)*cci->size.z; ++vp;++sp; + } + } + gc.WriteNormal(normals); gc.WriteTangent(tangents); - VB2f *tc_vb=gc.GetTexCoord(); - if(tc_vb) - { - float *tc=tex_coords; - for(uint i=0;i<24;i++) - { - (*tc)*=cci->tile.x;++tc; - (*tc)*=cci->tile.y;++tc; - } + float *tcp=gc.GetTexCoordPointer(); - tc_vb->BufferData(tex_coords); + if(tcp) + { + if(cci->tile.x==1&&cci->tile.y==1) + { + gc.WriteTexCoord(tex_coords); + } + else + { + const float *tc=tex_coords; + + for(uint i=0;i<24;i++) + { + *tcp=(*tc)*cci->tile.x;++tc;++tcp; + *tcp=(*tc)*cci->tile.y;++tc;++tcp; + } + } } gc.CreateIBO16(6*2*3,indices); @@ -668,7 +692,7 @@ namespace hgl } }//namespace - vulkan::Renderable *CreateRenderableTorus(SceneDB *db,vulkan::Material *mtl,TorusCreateInfo *tci) + vulkan::Renderable *CreateRenderableTorus(SceneDB *db,vulkan::Material *mtl,const TorusCreateInfo *tci) { GeometryCreater3D gc(db,mtl); @@ -819,7 +843,7 @@ namespace hgl } }//namespace - vulkan::Renderable *CreateRenderableCylinder(SceneDB *db,vulkan::Material *mtl,CylinderCreateInfo *cci) + vulkan::Renderable *CreateRenderableCylinder(SceneDB *db,vulkan::Material *mtl,const CylinderCreateInfo *cci) { uint numberIndices = cci->numberSlices * 3 * 2 + cci->numberSlices * 6; @@ -1033,7 +1057,7 @@ namespace hgl } }//namespace - vulkan::Renderable *CreateRenderableCone(SceneDB *db,vulkan::Material *mtl,ConeCreateInfo *cci) + vulkan::Renderable *CreateRenderableCone(SceneDB *db,vulkan::Material *mtl,const ConeCreateInfo *cci) { GeometryCreater3D gc(db,mtl); @@ -1154,5 +1178,84 @@ namespace hgl return gc.Finish(); } + + vulkan::Renderable *CreateRenderableAxis(SceneDB *db,vulkan::Material *mtl,const AxisCreateInfo *aci) + { + GeometryCreater3D gc(db,mtl); + + if(!gc.Init(6)) + return(false); + + VB3f *vertex=gc.GetVertex(); + VB4f *color=gc.GetColor(); + + if(!vertex||!color) + return(nullptr); + + vertex->Write(aci->root);color->Write(aci->color[0]); + vertex->Write(aci->root.x+aci->size[0],aci->root.y,aci->root.z);color->Write(aci->color[0]); + + vertex->Write(aci->root);color->Write(aci->color[1]); + vertex->Write(aci->root.x,aci->root.y+aci->size[1],aci->root.z);color->Write(aci->color[1]); + + vertex->Write(aci->root);color->Write(aci->color[2]); + vertex->Write(aci->root.x,aci->root.y,aci->root.z+aci->size[2]);color->Write(aci->color[2]); + + return gc.Finish(); + } + + vulkan::Renderable *CreateRenderableBoundingBox(SceneDB *db,vulkan::Material *mtl,const CubeCreateInfo *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[]= + { + 0,1, 1,2, 2,3, 3,0, + 4,5, 5,6, 6,7, 7,4, + 0,4, 1,5, 2,6, 3,7 + }; + + GeometryCreater3D gc(db,mtl); + + if(!gc.Init(8)) + return(false); + + VB3f *vertex=gc.GetVertex(); + + if(!vertex)return(nullptr); + + if(cci->center ==Vector3f(0,0,0) + &&cci->size ==Vector3f(1,1,1)) + { + gc.WriteVertex(points); + } + else + { + const float *sp=points; + float *vp=gc.GetVertexPointer(); + + for(uint i=0;i<8;i++) + { + *vp=cci->center.x+(*sp)*cci->size.x; ++vp;++sp; + *vp=cci->center.y+(*sp)*cci->size.y; ++vp;++sp; + *vp=cci->center.z+(*sp)*cci->size.z; ++vp;++sp; + } + } + + gc.CreateIBO16(24,indices); + + return gc.Finish(); + } }//namespace graph }//namespace hgl \ No newline at end of file