VertexBuffer相关代码修正至可编译(未做使用测试)

This commit is contained in:
2018-11-30 19:26:08 +08:00
parent 03900a66b0
commit 6394ad8b74
27 changed files with 1880 additions and 58602 deletions

View File

@@ -0,0 +1,41 @@
#ifndef HGL_GRAPH_PIXEL_COMPENT_INCLUDE
#define HGL_GRAPH_PIXEL_COMPENT_INCLUDE
#include<GLEWCore/glew.h>
namespace hgl
{
namespace graph
{
/**
* 顶点象素成份枚举
*/
enum PixelCompoment
{
HGL_PC_NONE=0,
//单通道
HGL_PC_ALPHA,
HGL_PC_DEPTH,
HGL_PC_LUMINANCE,
//2通道
HGL_PC_LUMINANCE_ALPHA,
//3通道
HGL_PC_RGB,
//4通道
HGL_PC_RGBA,
HGL_PC_RGB_SPECULAR,
HGL_PC_RGB_DEPTH,
HGL_PC_NORMAL_HEIGHT,
//4通道 2+2模式
HGL_PC_RGB_NORMAL,
HGL_PC_NORMAL_TANGENT,
HGL_PC_END
};//enum PixelCompoment
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_PIXEL_COMPENT_INCLUDE

118
inc/hgl/graph/VertexArray.h Normal file
View File

@@ -0,0 +1,118 @@
#ifndef HGL_GRAPH_VERTEX_ARRAY_INCLUDE
#define HGL_GRAPH_VERTEX_ARRAY_INCLUDE
#include<hgl/type/List.h>
#include<hgl/graph/VertexBuffer.h>
#include<hgl/graph/PixelCompoment.h>
#include<hgl/math/Math.h>
namespace hgl
{
namespace graph
{
/**
* 顶点阵列数据
*/
class VertexArray
{
protected:
uint primitive; ///<绘制的图元类型
ObjectList<VertexBufferBase> vertex_buffer_list; ///<顶点数据缓冲区
AABB aabb; ///<AABB绑定盒
OBB obb; ///<OBB绑定盒
int vertex_compoment; ///<顶点属性格式
PixelCompoment color_compoment; ///<颜色属性格式
VertexBufferBase *element_buffer;
VertexBufferBase *vertex_buffer;
VertexBufferBase *color_buffer;
private:
bool _SetVertexBuffer (VertexBufferBase *); ///<真实设置顶点缓冲区数据
bool _SetElementBuffer (); ///<真实设置索引缓冲区数据
public:
VertexArray(uint prim,uint max_vertex_attrib);
~VertexArray()=default;
uint GetPrimitive ()const{return primitive;} ///<取得要绘制的图元类型
public: //通用顶点缓冲区设置
int AddVertexAttribBuffer (VertexBufferBase *); ///<设置顶点缓冲区数据
VertexBufferBase * GetVertexAttribBuffer (int index){return vertex_buffer_list[index];} ///<取得顶点缓冲区数据
bool ClearVertexAttribBuffer (int index){return vertex_buffer_list.Delete(index);} ///<清除顶点缓冲区数据
void ClearVertexAttribBuffers (){vertex_buffer_list.Clear();} ///<清除所有顶点缓冲区数据
public: //索引缓冲区设置
bool SetElementBuffer(VertexBufferBase *vb)
{
if(!vb)return(false);
element_buffer=vb;
return(true);
}
public: //顶点格式相关
bool SetVertexBuffer(VertexBufferBase *vb)
{
if(!vb)return(false);
vertex_compoment=vb->GetComponent();
return(AddVertexAttribBuffer(vb)>=0);
}
int GetVertexCompoment()const{return vertex_compoment;} ///<取得顶点数据成分数量
public: //颜色格式相关
PixelCompoment GetColorCompoment()const{return color_compoment;} ///<取得顶点颜色格式
bool SetColor(VertexBufferBase *vb,PixelCompoment cf)
{
if(!vb)return(false);
if(cf<=HGL_PC_NONE||cf>=HGL_PC_END)return(false);
color_compoment=cf;
return(AddVertexAttribBuffer(vb)>=0);
}
public: //绑定盒相关
void SetBoundingBox (const Vector3f &min_v,Vector3f &max_v)
{
aabb.minPoint=POINT_VEC(min_v);
aabb.maxPoint=POINT_VEC(max_v);
obb.SetFrom(aabb);
}
const AABB & GetAABB ()const{return aabb;} ///<取得AABB绑定盒
const OBB & GetOBB ()const{return obb;} ///<取得OBB绑定盒
const Vector3f GetCenter ()const ///<取得中心点
{
return POINT_TO_FLOAT3(obb.CenterPoint());
}
void GetBoundingBox (Vector3f &min_v,Vector3f &max_v) ///<取得最小顶点和最大顶点
{
min_v=POINT_TO_FLOAT3(aabb.minPoint);
max_v=POINT_TO_FLOAT3(aabb.maxPoint);
}
public:
int GetDrawCount (); ///<取得可绘制的数据总数量
};//class VertexArray
}//namespace graph
}//namespace hgl
#endif//HGL_GRAPH_VERTEX_ARRAY_INCLUDE

View File

@@ -0,0 +1,924 @@
#ifndef HGL_VERTEX_BUFFER_OBJECT_INCLUDE
#define HGL_VERTEX_BUFFER_OBJECT_INCLUDE
#include<hgl/type/Color3f.h>
#include<hgl/type/Color4f.h>
#include<hgl/type/RectScope.h>
// #include<hgl/type/BaseString.h>
#include<hgl/graph/VertexBufferBase.h>
//#include<hgl/LogInfo.h>
#include<GLEWCore/glew.h>
namespace hgl
{
namespace graph
{
class VertexBufferControl;
/**
* 顶点属性数据实际模板
*/
template<typename T,int C> class VertexBuffer:public VertexBufferBase
{
protected:
T *mem_type; ///<符合当前类型的地址
T *access; ///<当前访问地址
T *start; ///<访问起始地址
public:
VertexBuffer(int _size,const T *_data=nullptr,uint level=GL_STATIC_DRAW):VertexBufferBase(level,C*_size*sizeof(T))
{
dc_num=C;
count=_size;
mem_type=(T *)GetData();
access=0;
start=0;
if(_data)
memcpy(mem_type,_data,bytes);
}
virtual ~VertexBuffer()=default;
int GetDataBytes()const
{
return sizeof(T);
}
void SetCount(int _count)
{
count=_count;
SetDataSize(_count*C*sizeof(T));
mem_type=(T *)GetData();
access=0;
start=0;
}
/**
* 取得数据区地址
* @param offset 从第几个数据开始访问
* @return 访问地址
*/
T *Get(int offset=0)
{
if(!mem_type||offset>=count)
{
// LOG_HINT(OS_TEXT("VertexBuffer::Get() out,offset:")+OSString(offset));
return(nullptr);
}
return mem_type+offset*C;
}
/**
* 开始访问数据区
* @param offset 从第几个数据开始访问
* @return 访问地址
*/
void *Begin(int offset=0)
{
if(access)
{
// LOG_HINT(OS_TEXT("VertexBuffer::Begin() access!=0,offset:")+OSString(offset));
return(nullptr);
}
access=Get(offset);
if(access)
start=access;
return access;
}
/**
* 结束访问
*/
void End()
{
ChangeVertexBuffer( ((char *)start )-((char *)mem_type),
((char *)access)-((char *)start),
start);
access=nullptr;
start=nullptr;
}
/**
* 写入指定数量的数据
* @param vp 数据指针
* @param number 数据数量
*/
bool WriteData(const T *vp,const int number)
{
if(!this->access||this->access+C*number>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer::Write(const T *,number) out,number:")+OSString(number));
return(false);
}
memcpy(access,vp,C*number*sizeof(T));
access+=C*number;
return(true);
}
};//class VertexBuffer
/**
* 一元数据缓冲区
*/
template<typename T> class VertexBuffer1:public VertexBuffer<T,1>
{
public:
using VertexBuffer<T,1>::VertexBuffer;
virtual ~VertexBuffer1()=default;
uint GetDataType()const;
bool Write(const T v1)
{
if(!this->access||this->access+1>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer1::Write(const T) out"));
return(false);
}
*this->access++=v1;
return(true);
}
/**
* 将一个值重复多次写入缓冲区
* @param v 值
* @param count 写入数量
*/
bool Write(const T v,const int count)
{
if(!this->access||this->access+count>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer1::Write(const T,")+OSString(count)+OS_TEXT(") out"));
return(false);
}
hgl_set(this->access,v,count);
this->access+=count;
return(true);
}
};//class VertexBuffer1
/**
* 二元数据缓冲区
*/
template<typename T> class VertexBuffer2:public VertexBuffer<T,2>
{
public:
using VertexBuffer<T,2>::VertexBuffer;
virtual ~VertexBuffer2()=default;
uint GetDataType()const;
bool Write(const T v1,const T v2)
{
if(!this->access||this->access+2>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer2::Write(const T ,const T) out"));
return(false);
}
*this->access++=v1;
*this->access++=v2;
return(true);
}
bool Write(const T *v)
{
if(!this->access||this->access+2>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer2::Write(T *) out"));
return(false);
}
*this->access++=*v++;
*this->access++=*v;
return(true);
}
bool Write(const Vector2f &v)
{
if(!this->access||this->access+2>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer2::Write(vec2 &) out"));
return(false);
}
*this->access++=v.x;
*this->access++=v.y;
return(true);
}
/**
* 将一个值重复多次写入缓冲区
* @param v 值
* @param count 写入数量
*/
bool Write(const Vector2f &v,const int count)
{
if(!this->access||this->access+(count<<1)>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer1::Write(const Vector2f &,")+OSString(count)+OS_TEXT(") out"));
return(false);
}
for(int i=0;i<count;i++)
{
*this->access++=v.x;
*this->access++=v.y;
}
return(true);
}
bool WriteLine(const T start_x,const T start_y,const T end_x,const T end_y)
{
if(!this->access||this->access+4>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer2::WriteLine(T,T,T,T) out"));
return(false);
}
*this->access++=start_x;
*this->access++=start_y;
*this->access++=end_x;
*this->access++=end_y;
return(true);
}
bool WriteLine(const Vector2f &start,const Vector2f &end)
{
if(!this->access||this->access+4>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer2::WriteLine(vec2,vec2) out"));
return(false);
}
*this->access++=start.x;
*this->access++=start.y;
*this->access++=end.x;
*this->access++=end.y;
return(true);
}
/**
* 写入2D三角形
*/
bool WriteTriangle(const Vector2f &v1,const Vector2f &v2,const Vector2f &v3)
{
if(!this->access||this->access+6>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer2::WriteTriangle(vec2,vec2,vec2) out"));
return(false);
}
*this->access++=v1.x;
*this->access++=v1.y;
*this->access++=v2.x;
*this->access++=v2.y;
*this->access++=v3.x;
*this->access++=v3.y;
return(true);
}
/**
* 写入2D三角形
*/
bool WriteTriangle(const Vector2f *v)
{
if(!this->access||this->access+6>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer2::WriteTriangle(vec2 *) out"));
return(false);
}
*this->access++=v->x;
*this->access++=v->y;
++v;
*this->access++=v->x;
*this->access++=v->y;
++v;
*this->access++=v->x;
*this->access++=v->y;
return(true);
}
/**
* 写入2D四边形坐标数据
*/
bool WriteQuad(const Vector2f &lt,const Vector2f &rt,const Vector2f &rb,const Vector2f &lb)
{
if(WriteTriangle(lt,lb,rb))
if(WriteTriangle(lt,rb,rt))
return(true);
// LOG_HINT(OS_TEXT("VertexBuffer2::WriteQuad(vec2 &,vec2 &,vec2 &,vec2 &) error"));
return(false);
}
/**
* 写入2D矩形两个三角形坐标数据
*/
bool WriteRect(const T left,const T top,const T width,const T height)
{
const Vector2f lt(left ,top);
const Vector2f rt(left+width,top);
const Vector2f rb(left+width,top+height);
const Vector2f lb(left ,top+height);
return WriteQuad(lt,rt,rb,lb);
}
};//class VertexBuffer2
/**
* 三元数据缓冲区
*/
template<typename T> class VertexBuffer3:public VertexBuffer<T,3>
{
public:
using VertexBuffer<T,3>::VertexBuffer;
virtual ~VertexBuffer3()=default;
uint GetDataType()const;
/**
* 计算绑定盒
* @param min_vertex 最小值坐标
* @param max_vertex 最大值坐标
*/
template<typename V>
void GetBoundingBox(V &min_vertex,V &max_vertex)
{
T *p=this->mem_type;
//先以corner为最小值,length为最大值求取最小最大值
min_vertex.x=*p++;
min_vertex.y=*p++;
min_vertex.z=*p++;
max_vertex=min_vertex;
for(int i=1;i<this->count;i++)
{
if(*p<min_vertex.x)min_vertex.x=*p;
if(*p>max_vertex.x)max_vertex.x=*p;
++p;
if(*p<min_vertex.y)min_vertex.y=*p;
if(*p>max_vertex.y)max_vertex.y=*p;
++p;
if(*p<min_vertex.z)min_vertex.z=*p;
if(*p>max_vertex.z)max_vertex.z=*p;
++p;
}
}
bool Write(const T v1,const T v2,const T v3)
{
if(!this->access||this->access+3>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer3::Write(T,T,T) out"));
return(false);
}
*this->access++=v1;
*this->access++=v2;
*this->access++=v3;
return(true);
}
bool Write(const T *v)
{
if(!this->access||this->access+3>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer3::Write(T *) out"));
return(false);
}
*this->access++=*v++;
*this->access++=*v++;
*this->access++=*v;
return(true);
}
bool Write(const Vector3f &v)
{
if(!this->access||this->access+3>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer3::Write(vec3 &) out"));
return(false);
}
*this->access++=v.x;
*this->access++=v.y;
*this->access++=v.z;
return(true);
}
bool Write(const Vector4f &v)
{
if(!this->access||this->access+3>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer3::Write(vec4 &) out"));
return(false);
}
*this->access++=v.x;
*this->access++=v.y;
*this->access++=v.z;
return(true);
}
/**
* 将一个值重复多次写入缓冲区
* @param v 值
* @param count 写入数量
*/
bool Write(const Vector3f &v,const int count)
{
if(!this->access||this->access+(count*3)>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer3::Write(const Vector3f,")+OSString(count)+OS_TEXT(") out"));
return(false);
}
for(int i=0;i<count;i++)
{
*this->access++=v.x;
*this->access++=v.y;
*this->access++=v.z;
}
return(true);
}
bool Write(const Color3f &v)
{
if(!this->access||this->access+3>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer3::Write(color3f &) out"));
return(false);
}
*this->access++=v.r;
*this->access++=v.g;
*this->access++=v.b;
return(true);
}
bool WriteLine(const T start_x,const T start_y,const T start_z,const T end_x,const T end_y,const T end_z)
{
if(!this->access||this->access+6>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer3::WriteLine(T,T,T,T,T,T) out"));
return(false);
}
*this->access++=start_x;
*this->access++=start_y;
*this->access++=start_z;
*this->access++=end_x;
*this->access++=end_y;
*this->access++=end_z;
return(true);
}
bool WriteLine(const Vector3f &start,const Vector3f &end)
{
if(!this->access||this->access+6>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer3::WriteLine(vec3,vec3) out"));
return(false);
}
*this->access++=start.x;
*this->access++=start.y;
*this->access++=start.z;
*this->access++=end.x;
*this->access++=end.y;
*this->access++=end.z;
return(true);
}
/**
* 写入3D三角形
*/
bool WriteTriangle(const Vector3f &v1,const Vector3f &v2,const Vector3f &v3)
{
if(!this->access||this->access+9>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer3::WriteTriangle(vec3,vec3,vec3) out"));
return(false);
}
*this->access++=v1.x;
*this->access++=v1.y;
*this->access++=v1.z;
*this->access++=v2.x;
*this->access++=v2.y;
*this->access++=v2.z;
*this->access++=v3.x;
*this->access++=v3.y;
*this->access++=v3.z;
return(true);
}
/**
* 写入3D三角形
*/
bool WriteTriangle(const Vector3f *v)
{
if(!this->access||this->access+9>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer3::WriteTriangle(vec3 *) out"));
return(false);
}
*this->access++=v->x;
*this->access++=v->y;
*this->access++=v->z;
++v;
*this->access++=v->x;
*this->access++=v->y;
*this->access++=v->z;
++v;
*this->access++=v->x;
*this->access++=v->y;
*this->access++=v->z;
return(true);
}
/**
* 写入3D四边形坐标数据
*/
bool WriteQuad(const Vector3f &lt,const Vector3f &rt,const Vector3f &rb,const Vector3f &lb)
{
if(WriteTriangle(lt,lb,rb))
if(WriteTriangle(lt,rb,rt))
return(true);
// LOG_HINT(OS_TEXT("VertexBuffer3::WriteQuad(vec3 &,vec3 &,vec3 &,vec3 &) error"));
return(false);
}
};//class VertexBuffer3
/**
* 四元数据缓冲区
*/
template<typename T> class VertexBuffer4:public VertexBuffer<T,4>
{
public:
using VertexBuffer<T,4>::VertexBuffer;
virtual ~VertexBuffer4()=default;
uint GetDataType()const;
/**
* 计算绑定盒
* @param min_vertex 最小值坐标
* @param max_vertex 最大值坐标
*/
template<typename V>
void GetBoundingBox(V &min_vertex,V &max_vertex)
{
T *p=this->mem_type;
//先以corner为最小值,length为最大值求取最小最大值
min_vertex.x=*p++;
min_vertex.y=*p++;
min_vertex.z=*p++;
max_vertex=min_vertex;
for(int i=1;i<this->count;i++)
{
if(*p<min_vertex.x)min_vertex.x=*p;
if(*p>max_vertex.x)max_vertex.x=*p;
++p;
if(*p<min_vertex.y)min_vertex.y=*p;
if(*p>max_vertex.y)max_vertex.y=*p;
++p;
if(*p<min_vertex.z)min_vertex.z=*p;
if(*p>max_vertex.z)max_vertex.z=*p;
++p;
}
}
bool Write(const T v1,const T v2,const T v3,const T v4)
{
if(!this->access||this->access+4>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer4::Write(T,T,T,T) out"));
return(false);
}
*this->access++=v1;
*this->access++=v2;
*this->access++=v3;
*this->access++=v4;
return(true);
}
bool Write(const T *v)
{
if(!this->access||this->access+4>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer4::Write(T *) out"));
return(false);
}
*this->access++=*v++;
*this->access++=*v++;
*this->access++=*v++;
*this->access++=*v;
return(true);
}
bool Write(const Vector4f &v)
{
if(!this->access||this->access+4>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer4::Write(color4 &) out"));
return(false);
}
*this->access++=v.x;
*this->access++=v.y;
*this->access++=v.z;
*this->access++=v.w;
return(true);
}
bool Write(const Color4f &v)
{
if(!this->access||this->access+4>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer4::Write(color4 &) out"));
return(false);
}
*this->access++=v.r;
*this->access++=v.g;
*this->access++=v.b;
*this->access++=v.a;
return(true);
}
/**
* 将一个值重复多次写入缓冲区
* @param v 值
* @param count 写入数量
*/
bool Write(const Vector4f &v,const int count)
{
if(!this->access||this->access+(count<<2)>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer4::Write(const Vector4f,")+OSString(count)+OS_TEXT(") out"));
return(false);
}
for(int i=0;i<count;i++)
{
*this->access++=v.x;
*this->access++=v.y;
*this->access++=v.z;
*this->access++=v.w;
}
return(true);
}
bool WriteLine(const T start_x,const T start_y,const T start_z,const T end_x,const T end_y,const T end_z)
{
if(!this->access||this->access+8>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer4::WriteLine(T,T,T,T,T,T) out"));
return(false);
}
*this->access++=start_x;
*this->access++=start_y;
*this->access++=start_z;
*this->access++=1.0f;
*this->access++=end_x;
*this->access++=end_y;
*this->access++=end_z;
*this->access++=1.0f;
return(true);
}
bool WriteLine(const Vector3f &start,const Vector3f &end)
{
if(!this->access||this->access+8>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer4::WriteLine(vec3,vec3) out"));
return(false);
}
*this->access++=start.x;
*this->access++=start.y;
*this->access++=start.z;
*this->access++=1.0f;
*this->access++=end.x;
*this->access++=end.y;
*this->access++=end.z;
*this->access++=1.0f;
return(true);
}
/**
* 写入3D三角形
*/
bool WriteTriangle(const Vector3f &v1,const Vector3f &v2,const Vector3f &v3)
{
if(!this->access||this->access+12>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer4::WriteTriangle(vec3,vec3,vec3) out"));
return(false);
}
*this->access++=v1.x;
*this->access++=v1.y;
*this->access++=v1.z;
*this->access++=1.0f;
*this->access++=v2.x;
*this->access++=v2.y;
*this->access++=v2.z;
*this->access++=1.0f;
*this->access++=v3.x;
*this->access++=v3.y;
*this->access++=v3.z;
*this->access++=1.0f;
return(true);
}
/**
* 写入3D三角形
*/
bool WriteTriangle(const Vector3f *v)
{
if(!this->access||this->access+12>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer4::WriteTriangle(vec3 *) out"));
return(false);
}
*this->access++=v->x;
*this->access++=v->y;
*this->access++=v->z;
*this->access++=1.0f;
++v;
*this->access++=v->x;
*this->access++=v->y;
*this->access++=v->z;
*this->access++=1.0f;
++v;
*this->access++=v->x;
*this->access++=v->y;
*this->access++=v->z;
*this->access++=1.0f;
return(true);
}
/**
* 写入2D矩形,注:这个函数会依次写入Left,Top,Width,Height四个值
*/
template<typename V>
bool WriteRectangle2D(const RectScope2<V> &rect)
{
if(!this->access||this->access+4>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer4::WriteRectangle2D(RectScope2 ) out"));
return(false);
}
*this->access++=rect.Left;
*this->access++=rect.Top;
*this->access++=rect.Width;
*this->access++=rect.Height;
return(true);
}
/**
* 写入2D矩形,注:这个函数会依次写入Left,Top,Width,Height四个值
*/
template<typename V>
bool WriteRectangle2D(const RectScope2<V> *rect,const int count)
{
if(!this->access||this->access+(4*count)>this->mem_end)
{
// LOG_HINT(OS_TEXT("VertexBuffer4::WriteRectangle2D(RectScope2 *,count) out"));
return(false);
}
for(int i=0;i<count;i++)
{
*this->access++=rect->Left;
*this->access++=rect->Top;
*this->access++=rect->Width;
*this->access++=rect->Height;
++rect;
}
return(true);
}
};//class VertexBuffer4
//缓冲区具体数据类型定义
typedef VertexBuffer1<int8 > VB1i8 ,VB1b; template<> inline uint VertexBuffer1<int8 >::GetDataType()const{return GL_BYTE; }
typedef VertexBuffer1<int16 > VB1i16 ,VB1s; template<> inline uint VertexBuffer1<int16 >::GetDataType()const{return GL_SHORT; }
typedef VertexBuffer1<int32 > VB1i32 ,VB1i; template<> inline uint VertexBuffer1<int32 >::GetDataType()const{return GL_INT; }
typedef VertexBuffer1<uint8 > VB1u8 ,VB1ub; template<> inline uint VertexBuffer1<uint8 >::GetDataType()const{return GL_UNSIGNED_BYTE; }
typedef VertexBuffer1<uint16> VB1u16 ,VB1us; template<> inline uint VertexBuffer1<uint16 >::GetDataType()const{return GL_UNSIGNED_SHORT;}
typedef VertexBuffer1<uint32> VB1u32 ,VB1ui; template<> inline uint VertexBuffer1<uint32 >::GetDataType()const{return GL_UNSIGNED_INT; }
typedef VertexBuffer1<float > VB1f; template<> inline uint VertexBuffer1<float >::GetDataType()const{return GL_FLOAT; }
typedef VertexBuffer1<double> VB1d; template<> inline uint VertexBuffer1<double >::GetDataType()const{return GL_DOUBLE; }
typedef VertexBuffer2<int8 > VB2i8 ,VB2b; template<> inline uint VertexBuffer2<int8 >::GetDataType()const{return GL_BYTE; }
typedef VertexBuffer2<int16 > VB2i16 ,VB2s; template<> inline uint VertexBuffer2<int16 >::GetDataType()const{return GL_SHORT; }
typedef VertexBuffer2<int32 > VB2i32 ,VB2i; template<> inline uint VertexBuffer2<int32 >::GetDataType()const{return GL_INT; }
typedef VertexBuffer2<uint8 > VB2u8 ,VB2ub; template<> inline uint VertexBuffer2<uint8 >::GetDataType()const{return GL_UNSIGNED_BYTE; }
typedef VertexBuffer2<uint16> VB2u16 ,VB2us; template<> inline uint VertexBuffer2<uint16 >::GetDataType()const{return GL_UNSIGNED_SHORT;}
typedef VertexBuffer2<uint32> VB2u32 ,VB2ui; template<> inline uint VertexBuffer2<uint32 >::GetDataType()const{return GL_UNSIGNED_INT; }
typedef VertexBuffer2<float > VB2f; template<> inline uint VertexBuffer2<float >::GetDataType()const{return GL_FLOAT; }
typedef VertexBuffer2<double> VB2d; template<> inline uint VertexBuffer2<double >::GetDataType()const{return GL_DOUBLE; }
typedef VertexBuffer3<int8 > VB3i8 ,VB3b; template<> inline uint VertexBuffer3<int8 >::GetDataType()const{return GL_BYTE; }
typedef VertexBuffer3<int16 > VB3i16 ,VB3s; template<> inline uint VertexBuffer3<int16 >::GetDataType()const{return GL_SHORT; }
typedef VertexBuffer3<int32 > VB3i32 ,VB3i; template<> inline uint VertexBuffer3<int32 >::GetDataType()const{return GL_INT; }
typedef VertexBuffer3<uint8 > VB3u8 ,VB3ub; template<> inline uint VertexBuffer3<uint8 >::GetDataType()const{return GL_UNSIGNED_BYTE; }
typedef VertexBuffer3<uint16> VB3u16 ,VB3us; template<> inline uint VertexBuffer3<uint16 >::GetDataType()const{return GL_UNSIGNED_SHORT;}
typedef VertexBuffer3<uint32> VB3u32 ,VB3ui; template<> inline uint VertexBuffer3<uint32 >::GetDataType()const{return GL_UNSIGNED_INT; }
typedef VertexBuffer3<float > VB3f; template<> inline uint VertexBuffer3<float >::GetDataType()const{return GL_FLOAT; }
typedef VertexBuffer3<double> VB3d; template<> inline uint VertexBuffer3<double >::GetDataType()const{return GL_DOUBLE; }
typedef VertexBuffer4<int8 > VB4i8 ,VB4b; template<> inline uint VertexBuffer4<int8 >::GetDataType()const{return GL_BYTE; }
typedef VertexBuffer4<int16 > VB4i16 ,VB4s; template<> inline uint VertexBuffer4<int16 >::GetDataType()const{return GL_SHORT; }
typedef VertexBuffer4<int32 > VB4i32 ,VB4i; template<> inline uint VertexBuffer4<int32 >::GetDataType()const{return GL_INT; }
typedef VertexBuffer4<uint8 > VB4u8 ,VB4ub; template<> inline uint VertexBuffer4<uint8 >::GetDataType()const{return GL_UNSIGNED_BYTE; }
typedef VertexBuffer4<uint16> VB4u16 ,VB4us; template<> inline uint VertexBuffer4<uint16 >::GetDataType()const{return GL_UNSIGNED_SHORT;}
typedef VertexBuffer4<uint32> VB4u32 ,VB4ui; template<> inline uint VertexBuffer4<uint32 >::GetDataType()const{return GL_UNSIGNED_INT; }
typedef VertexBuffer4<float > VB4f; template<> inline uint VertexBuffer4<float >::GetDataType()const{return GL_FLOAT; }
typedef VertexBuffer4<double> VB4d; template<> inline uint VertexBuffer4<double >::GetDataType()const{return GL_DOUBLE; }
}//namespace graph
}//namespace hgl
#endif//HGL_VERTEX_BUFFER_OBJECT_INCLUDE

View File

@@ -0,0 +1,57 @@
#ifndef HGL_VERTEX_BUFFER_BASE_INCLUDE
#define HGL_VERTEX_BUFFER_BASE_INCLUDE
#include<hgl/type/DataType.h>
namespace hgl
{
namespace graph
{
class VertexBufferControl;
class VertexBufferBase
{
void *mem_data; ///<内存中的数据
protected:
int dc_num; ///<每个数据成员数(比如二维坐标为2、三维坐标为3)
int count; ///<数据个数
int bytes; ///<字节数
void *mem_end; ///<内存数据区访问结束地址
protected:
uint data_level; ///<数据级别
VertexBufferControl *vbc; ///<顶点缓冲区控制器
protected:
void SetDataSize(int size);
public:
VertexBufferBase(uint level,uint size);
virtual ~VertexBufferBase();
virtual uint GetDataType()const=0; ///<取得数据类型
virtual int GetDataBytes()const=0; ///<取得每数据字节数
int GetComponent()const { return dc_num; } ///<取数缓冲区元数据数量
int GetCount()const { return count; } ///<取得数据数量
int GetStride()const { return dc_num*GetDataBytes();} ///<取得每一组数据字节数
void * GetData()const { return mem_data;} ///<取得数据指针
int GetBytes()const { return bytes; } ///<取得数据字节数
public: //以下函数在各渲染器内部实现
bool CreateVertexBuffer(uint type);
void ChangeVertexBuffer(int,int,void *);
//void BindVertexBuffer();
int GetBufferIndex()const; ///<取得缓冲区索引
void CloseVertexBuffer();
};//class VertexBufferBase
}//namespace graph
}//namespace hgl
#endif//HGL_VERTEX_BUFFER_BASE_INCLUDE

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,136 +0,0 @@
#ifndef HGL_RENDER_DEVICE_INCLUDE
#define HGL_RENDER_DEVICE_INCLUDE
#include<hgl/type/_Object.h>
#include<hgl/type/BaseString.h>
#include<hgl/type/List.h>
namespace hgl
{
/**
* 显示模式数据结构
*/
struct VideoMode
{
int width; ///<宽
int height; ///<高
int bit; ///<色彩位数
int freq; ///<刷新率
int red; ///<红色位数
int green; ///<绿色位数
int blue; ///<蓝色位数
};//struct VideoMode
/**
* 显示屏数据结构
*/
struct Display
{
UTF8String name; ///<显示屏名称
int width,height; ///<显示屏尺寸(单位:毫米)
int x,y; ///<多显示屏时的排列位置
public:
virtual const VideoMode *GetCurVideoMode()const=0;
virtual const ObjectList<VideoMode> &GetVideoModeList()const=0;
};
struct WindowSetup
{
UTF8String Name; ///<窗口标题
// OSString IconFilename; ///<图标文件名称
// OSString CursorFilename; ///<光标文件名称
bool Edge =true; ///<是否显示边框
bool SysMenu =true; ///<是否显示系统菜单
bool Right =false; ///<窗口是否使用靠右风格
bool Resize =false; ///<窗口大小是否可调整
bool Minimize =false; ///<窗口是否可以最小化
bool Maximize =false; ///<窗口是否可以最大化
bool TopMost =false; ///<永远在最上面
bool AppTaskBar =true; ///<程序项在任务栏显示
};
/**
* 渲染设备
*/
struct RenderSetup
{
uint alpha; ///<Alpha缓冲区位深度,默认8位
uint depth; ///<Depth缓冲区位深度,默认24
uint stencil; ///<Stencil缓冲区位深度,默认8,不使用请写0
bool no_use_stencil; ///<不使用stencil缓冲区
struct
{
uint red; ///<Accum缓冲区红色位深度,默认0
uint green; ///<Accum缓冲区绿色位深度,默认0
uint blue; ///<Accum缓冲区蓝色位深度,默认0
uint alpha; ///<Accum缓冲区Alpha位深度,默认0
}accum;
uint msaa; ///<多重采样级别(全屏抗矩齿级别)
struct
{
struct
{
bool nicest; ///<高质量贴图压缩,默认为真
}compress;
bool rect; ///<是否启用矩形贴图,默认为否
bool npot; ///<是否启用非2次幂贴图默认为否
float lod_bias; ///<默认纹理LOD Bias(默认0)
float max_anistropy; ///<纹理最大各向异性过滤值比例(使用0.0-1.0默认1)
}texture;
struct
{
bool debug=true;
bool core=true;
bool es=false;
bool egl=false;
uint major=3;
uint minor=3;
}opengl;
};
class RenderWindow;
/**
* 渲染设备基础类<br/>
* 该类是程序与操作系统或其它系统库的访问交接模块
*/
class RenderDevice:public _Object
{
public:
RenderDevice()=default;
virtual ~RenderDevice()=default;
virtual const bool Init()=0; ///<初始化渲染设备
virtual const void Close()=0; ///<关闭渲染设备
virtual const UTF8String GetName()=0; ///<取得设备名称
virtual const void GetDisplayList(List<Display *> &)=0; ///<取得显示屏列表
virtual const Display * GetDefaultDisplay()=0; ///<取得默认显示屏
public:
virtual RenderWindow *Create(int,int,const WindowSetup *,const RenderSetup *)=0; ///<创建一个窗口渲染设备
virtual RenderWindow *Create(const Display *,const VideoMode *,const RenderSetup *)=0; ///<创建一个全屏渲染设备
};//class RenderDevice
RenderDevice *CreateRenderDeviceGLFW(); ///<创建一个基于GLFW的渲染设备
}//namespace hgl
#endif//HGL_RENDER_DEVICE_INCLUDE

View File

@@ -1,25 +0,0 @@
#ifndef HGL_RENDER_DRIVER_INCLUDE
#define HGL_RENDER_DRIVER_INCLUDE
#include<hgl/render/RenderStatus.h>
namespace hgl
{
/**
* 渲染驱动
* 用于对真实渲染API的交接管理
*/
class RenderDriver
{
private:
RenderStatus current_status;
public:
virtual void SetCurStatus(const RenderStatus &)=0;
virtual void ClearColorBuffer()=0;
virtual void ClearDepthBuffer()=0;
};//class RenderDriver
}//namespace hgl
#endif//HGL_RENDER_DRIVER_INCLUDE

View File

@@ -1,47 +0,0 @@
#ifndef HGL_RENDER_STATUS_INCLUDE
#define HGL_RENDER_STATUS_INCLUDE
#include<hgl/type/Color4f.h>
#include<hgl/CompOperator.h>
namespace hgl
{
enum DEPTH_TEST_FUNC
{
DEPTH_TEST_NEVER=0,
DEPTH_TEST_LESS,
DEPTH_TEST_EQUAL,
DEPTH_TEST_LEQUAL,
DEPTH_TEST_GREATER,
DEPTH_TEST_NOTEQUAL,
DEPTH_TEST_GEQUAL,
DEPTH_TEST_ALWAYS
};//
struct DepthStatus
{
float near_depth =0,
far_depth =1;
bool depth_mask =true;
float clear_depth =far_depth;
DEPTH_TEST_FUNC depth_func =DEPTH_TEST_LESS;
bool depth_test;
public:
CompOperatorMemcmp(struct DepthStatus &);
};//
/**
* 渲染状态
*/
struct RenderStatus
{
bool color_mask[4];
Color4f clear_color;
DepthStatus depth;
};//class RenderStatus
}//namespace hgl
#endif//HGL_RENDER_STATUS_INCLUDE

View File

@@ -1,51 +0,0 @@
#ifndef HGL_RENDER_WINDOW_INCLUDE
#define HGL_RENDER_WINDOW_INCLUDE
#include<hgl/type/Vertex2.h>
#include<hgl/type/BaseString.h>
namespace hgl
{
/**
* 渲染窗口基类
*/
class RenderWindow
{
protected:
UTF8String caption;
bool full_screen;
int width,height;
public:
const uint GetWidth()const{return width;}
const uint GetHeight()const{return height;}
const bool GetFullScreen()const{return full_screen;}
public: //方法
RenderWindow()=default; ///<本类构造函数
virtual ~RenderWindow()=default; ///<本类析构函数
virtual void ToMin()=0; ///<窗口最小化
virtual void ToMax()=0; ///<窗口最大化
virtual void Show()=0; ///<显示窗口
virtual void Hide()=0; ///<隐藏窗口
virtual const UTF8String &GetCaption()const{return caption;}
virtual void SetCaption(const UTF8String &)=0;
public: //被实际操作系统接口层所调用的函数,在不了解的情况下请不要使用
virtual void SetSize(int w,int h)=0; ///<设置窗口大小
virtual void MakeToCurrent()=0; ///<切换到当前
virtual void SwapBuffer()=0; ///<交换缓冲区
virtual void WaitEvent(const double &time_out=0)=0; ///<等待下一个事件
virtual void PollEvent()=0; ///<轮询事件
virtual bool IsOpen()=0; ///<是否依然存在
};//class RenderWindow
}//namespace hgl
#endif//HGL_RENDER_WINDOW_INCLUDE

View File

@@ -1,195 +0,0 @@
#ifndef HGL_SHADER_INCLUDE
#define HGL_SHADER_INCLUDE
#include<hgl/type/BaseString.h>
#include<hgl/type/Map.h>
#include<hgl/math/Math.h>
// #include<hgl/graph/UBO.h>
// #include<hgl/graph/SSBO.h>
namespace hgl
{
constexpr uint HGL_MAX_SHADER_NAME_LENGTH=128; ///<最大Shader名称长度
/**
* 着色程序类型枚举
*/
enum ShaderType ///着色程序类型
{
stVertex=0, ///<顶点着色程序
stTessControl, ///<镶嵌控制着色程序(需OpenGL 4.0或ARB_tessellation_shader)
stTessEval, ///<镶嵌评估着色程序(需OpenGL 4.0或ARB_tessellation_shader)
stGeometry, ///<几何着色程序
stFragment, ///<片断着色程序
stCompute, ///<计算着色程序(需OpenGL 4.3或ARB_compute_shader)
stEnd
};//enum ShaderType
extern const char ShaderTypeName[ShaderType::stEnd][32]; ///<着色程序名称
/**
* 着色器程序
*/
class Shader
{
uint program;
uint shader_index[ShaderType::stEnd];
private:
bool Link(); ///<连接使用当前着色程序
protected:
Map<UTF8String,int> attrib_location;
Map<UTF8String,int> uniform_location;
// Map<UTF8String,int> uniform_block_index;
// MapObject<UTF8String,UBO> uniform_block_object;
//
// Map<UTF8String,int> ssbo_index;
// MapObject<UTF8String,SSBO> ssbo_object;
int _GetAttribLocation(const char *); ///<取得指定属性地址
int _GetUniformLocation(const char *); ///<取得一个变量的地址
// int _GetUniformBlockIndex(const char *); ///<取得一个只读数据块的地址索引
// int _GetShaderStorageIndex(const char *); ///<取得一个数据存储区的地址索引
public:
Shader(){program=0;hgl_zero(shader_index,ShaderType::stEnd);}
~Shader(){Clear();}
void Clear(); ///<清除着色程序
bool AddShader (const int shader_type,const char *); ///<增加一个着色程序
bool AddVertexShader (const char *code){return AddShader(ShaderType::stVertex,code);} ///<增加一个顶点着色程序
bool AddGeometryShader (const char *code){return AddShader(ShaderType::stGeometry,code);} ///<增加一个几何着色程序
bool AddFragmentShader (const char *code){return AddShader(ShaderType::stFragment,code);} ///<增加一个片断着色程序
bool AddComputeShader (const char *code){return AddShader(ShaderType::stFragment,code);} ///<增加一个计算着色程序
bool AddTessShader (const char *tess_control_shader,const char *tess_evaluation_shader) ///<增加一个镶嵌着色程序
{
if(!AddShader(ShaderType::stTessControl,tess_control_shader ))return(false);
if(!AddShader(ShaderType::stTessEval, tess_evaluation_shader ))return(false);
return(true);
}
bool Build(); ///<构建当前添加的着色程序
bool Use(); ///<使用当前着色程序
int GetAttribLocation(const char *); ///<取得指定属性地址
int GetUniformLocation(const char *); ///<取得一个变量的地址
// int GetUniformBlockIndex(const char *); ///<取得一个只读数据块索引
// int GetShaderStorageIndex(const char *); ///<取得一个数据存储区索引
//bool SetAttrib1f(int,float);
//bool GetAttrib1f(int,float &);
public: //设置一致变量用函数
bool SetUniform1f(int,float);
bool SetUniform2f(int,float,float);
bool SetUniform3f(int,float,float,float);
bool SetUniform4f(int,float,float,float,float);
bool SetUniform1i(int,int);
bool SetUniform2i(int,int,int);
bool SetUniform3i(int,int,int,int);
bool SetUniform4i(int,int,int,int,int);
bool SetUniform1ui(int,unsigned int);
bool SetUniform2ui(int,unsigned int,unsigned int);
bool SetUniform3ui(int,unsigned int,unsigned int,unsigned int);
bool SetUniform4ui(int,unsigned int,unsigned int,unsigned int,unsigned int);
bool SetUniform1fv(int,const float *);
bool SetUniform2fv(int,const float *);
bool SetUniform3fv(int,const float *);
bool SetUniform4fv(int,const float *);
bool SetUniform2fv(int index,const Vector2f &v){return SetUniform2fv(index,(const float *)&v);}
bool SetUniform3fv(int index,const Vector3f &v){return SetUniform3fv(index,(const float *)&v);}
bool SetUniform4fv(int index,const Vector4f &v){return SetUniform4fv(index,(const float *)&v);}
bool SetUniform1iv(int,const int *);
bool SetUniform2iv(int,const int *);
bool SetUniform3iv(int,const int *);
bool SetUniform4iv(int,const int *);
bool SetUniform1uiv(int,const unsigned int *);
bool SetUniform2uiv(int,const unsigned int *);
bool SetUniform3uiv(int,const unsigned int *);
bool SetUniform4uiv(int,const unsigned int *);
bool SetUniformMatrix2fv(int,const float *);
bool SetUniformMatrix3fv(int,const float *);
bool SetUniformMatrix4fv(int,const float *);
bool SetUniformMatrix2x3fv(int,const float *);
bool SetUniformMatrix3x2fv(int,const float *);
bool SetUniformMatrix2x4fv(int,const float *);
bool SetUniformMatrix4x2fv(int,const float *);
bool SetUniformMatrix3x4fv(int,const float *);
bool SetUniformMatrix4x3fv(int,const float *);
public:
bool SetUniform1f(const char *,float);
bool SetUniform2f(const char *,float,float);
bool SetUniform3f(const char *,float,float,float);
bool SetUniform4f(const char *,float,float,float,float);
bool SetUniform1i(const char *,int);
bool SetUniform2i(const char *,int,int);
bool SetUniform3i(const char *,int,int,int);
bool SetUniform4i(const char *,int,int,int,int);
bool SetUniform1ui(const char *,unsigned int);
bool SetUniform2ui(const char *,unsigned int,unsigned int);
bool SetUniform3ui(const char *,unsigned int,unsigned int,unsigned int);
bool SetUniform4ui(const char *,unsigned int,unsigned int,unsigned int,unsigned int);
bool SetUniform1fv(const char *,const float *);
bool SetUniform2fv(const char *,const float *);
bool SetUniform3fv(const char *,const float *);
bool SetUniform4fv(const char *,const float *);
bool SetUniform2fv(const char *name,const Vector2f &v){return SetUniform2fv(name,(const float *)&v);}
bool SetUniform3fv(const char *name,const Vector3f &v){return SetUniform3fv(name,(const float *)&v);}
bool SetUniform4fv(const char *name,const Vector4f &v){return SetUniform4fv(name,(const float *)&v);}
bool SetUniform1iv(const char *,const int *);
bool SetUniform2iv(const char *,const int *);
bool SetUniform3iv(const char *,const int *);
bool SetUniform4iv(const char *,const int *);
bool SetUniform1uiv(const char *,const unsigned int *);
bool SetUniform2uiv(const char *,const unsigned int *);
bool SetUniform3uiv(const char *,const unsigned int *);
bool SetUniform4uiv(const char *,const unsigned int *);
bool SetUniformMatrix2fv(const char *,const float *);
bool SetUniformMatrix3fv(const char *,const float *);
bool SetUniformMatrix4fv(const char *,const float *);
bool SetUniformMatrix3fv(const char *name,const Matrix3f &m){return SetUniformMatrix3fv(name,(const float *)&m);}
bool SetUniformMatrix4fv(const char *name,const Matrix4f &m){return SetUniformMatrix4fv(name,(const float *)&m);}
bool SetUniformMatrix2x3fv(const char *,const float *);
bool SetUniformMatrix3x2fv(const char *,const float *);
bool SetUniformMatrix2x4fv(const char *,const float *);
bool SetUniformMatrix4x2fv(const char *,const float *);
bool SetUniformMatrix3x4fv(const char *,const float *);
bool SetUniformMatrix4x3fv(const char *,const float *);
// public: //Uniform Block
//
// UBO *GetUniformBlock(const char *,uint=HGL_DYNAMIC_DRAW);
// SSBO *GetShaderStorage(const char *,uint=HGL_DYNAMIC_DRAW);
};//class Shader
}//namespace hgl
#endif//HGL_SHADER_INCLUDE

175
inc/hgl/type/FixedArray.h Normal file
View File

@@ -0,0 +1,175 @@
#ifndef HGL_TYPE_FIXED_ARRAY_INCLUDE
#define HGL_TYPE_FIXED_ARRAY_INCLUDE
#include<hgl/TypeFunc.h>
namespace hgl
{
/**
* 固定阵列数据模板
*/
template<typename T> class FixedArray
{
protected:
uint max_count;
T default_value;
T *items;
uint item_count;
public:
FixedArray()
{
max_count=0;
items=nullptr;
item_count=0;
}
virtual ~FixedArray()
{
ClearAll();
SAFE_FREE(items);
}
virtual void SetDefaultValue(const T &value)
{
default_value=value;
}
virtual bool SetMaxCount(const uint size)
{
if(size<=0)return(false);
if(!items)
{
max_count=size;
items=(T *)hgl_malloc(max_count*sizeof(T));
T *p=items;
for(uint i=0;i<=max_count;i++)
*p++=default_value;
}
else
{
if(size==max_count)return(true);
items=(T *)hgl_realloc(items,size*sizeof(T));
if(size>max_count)
{
T *p=items+max_count;
for(uint i=max_count;i<size;i++)
*p++=default_value;
}
max_count=size;
}
return(true);
}
const uint GetCount()const{return item_count;}
const uint GetMaxCount()const{return max_count;}
T operator[](uint n)
{
if(!items||n>=max_count)return default_value;
return items[n];
}
const T operator[](uint n)const
{
if(!items||n>=max_count)return default_value;
return items[n];
}
virtual bool Set(uint n,T &data)
{
if(!items||n>max_count)return(false);
if(data==default_value)return(false);
if(items[n]!=default_value)
{
if(items[n]==data)
return(true);
return(false);
}
items[n]=data;
++item_count;
return(true);
}
virtual bool Clear(uint n)
{
if(!items||n>max_count)return(false);
if(items[n]==default_value)return(true);
items[n]=default_value;
--item_count;
return(true);
}
virtual void ClearAll()
{
if(!items)return;
for(uint i=0;i<max_count;i++)
items[i]=default_value;
item_count=0;
}
};//class FixedArray
/**
* 固定对象数据阵列模板
*/
template<typename T> class ObjectFixedArray:public FixedArray<T *>
{
public:
ObjectFixedArray():FixedArray<T *>()
{
this->default_value=nullptr;
}
~ObjectFixedArray() override
{
ClearAll();
}
bool Clear(uint n) override
{
if(!this->items||n>this->max_count)return(false);
if(!this->items[n])return(true);
delete this->items[n];
this->items[n]=nullptr;
--this->item_count;
return(true);
}
void ClearAll() override
{
if(!this->items)
return;
for(uint i=0;i<this->max_count;i++)
{
if(this->items[i])delete this->items[i];
this->items[i]=nullptr;
}
this->item_count=0;
}
};//class ObjectFixedArray
}//namespace hgl
#endif//HGL_TYPE_FIXED_ARRAY_INCLUDE

View File

@@ -1,7 +1,6 @@
#ifndef HGL_LIST_CPP
#define HGL_LIST_CPP
#include<string.h>
//--------------------------------------------------------------------------------------------------
// 代码中的部分memcpy可替换为memmove,但这样会引起CodeGuard/Valgrind之类的内存调试器报错
//--------------------------------------------------------------------------------------------------

View File

@@ -3,6 +3,7 @@
#include<stdlib.h>
#include<initializer_list>
#include<hgl/type/DataType.h>
namespace hgl
{

View File

@@ -0,0 +1,80 @@
#ifndef HGL_RECT_SCOPE_CPP
#define HGL_RECT_SCOPE_CPP
#include<hgl/type/RectScope.h>
namespace hgl
{
/**
* 本类构造函数
*/
template<typename T>
RectScope2<T>::RectScope2()
{
Left=0;
Top=0;
Width=0;
Height=0;
}
/**
* 本类构造函数
* @param l 矩形最左边的坐标值
* @param t 矩形最上边的坐标值
* @param w 矩形的宽度
* @param h 矩形的高度
*/
template<typename T>
RectScope2<T>::RectScope2(T l,T t,T w,T h)
{
Left=l;
Top=t;
Width=w;
Height=h;
}
template<typename T> template<typename N>
RectScope2<T>::RectScope2(const RectScope2<N> &rs)
{
Left =rs.Left;
Top =rs.Top;
Width =rs.Width;
Height =rs.Height;
}
/**
* 设置数据
* @param l 矩形最左边的坐标值
* @param t 矩形最上边的坐标值
* @param w 矩形的宽度
* @param h 矩形的高度
*/
template<typename T>
void RectScope2<T>::Set(T l,T t,T w,T h)
{
Left=l;
Top=t;
Width=w;
Height=h;
}
/**
* 求坐标点x,y,z是否在这个矩形内
* @param x X坐标
* @param y Y坐标
* @return 点x,y,z是否在矩形范围内
*/
template<typename T>
bool RectScope2<T>::PointIn(T x,T y) const
{
T pos;
pos=x-Left;
if(pos<0||pos>=Width)return(false);
pos=y-Top;
if(pos<0||pos>=Height)return(false);
return(true);
}
}//namespace hgl
#endif//HGL_RECT_SCOPE_CPP

127
inc/hgl/type/RectScope.h Normal file
View File

@@ -0,0 +1,127 @@
#ifndef HGL_RECTSCOPE_INCLUDE
#define HGL_RECTSCOPE_INCLUDE
#include<hgl/math/Math.h>
namespace hgl
{
/**
* 这个类用于描述和处理一个矩形范围
*/
template<typename T> class RectScope2 ///矩形范围类
{
public:
T Left; ///<矩形左边所在的坐标
T Top; ///<矩形上边所在的坐标
T Width; ///<矩形的宽度
T Height; ///<矩形的高度
public:
T GetBottom()const{return Height+Top;}
T GetRight()const{return Width+Left;}
void SetBottom(T v){Top=v-Height;}
void SetRight(T v){Left=v-Width;}
T GetCenterX()const{return Left+(Width/2);}
T GetCenterY()const{return Top+(Height/2);}
public:
RectScope2();
RectScope2(T,T,T,T);
template<typename N> RectScope2(const RectScope2<N> &);
void Clear()
{
Left=0;
Top=0;
Width=0;
Height=0;
}
void Set(T,T,T,T);
bool PointIn(T,T)const;
bool PointIn(const Vector2f &v)const{return PointIn(v.x,v.y);}
bool PointIn(const Vector3f &v)const{return PointIn(v.x,v.y);}
template<typename N>
void operator = (const RectScope2<N> &rs)
{
Left =rs.Left;
Top =rs.Top;
Width =rs.Width;
Height =rs.Height;
}
template<typename N>
bool operator == (const RectScope2<N> &rs) const
{
if((Left ==rs.Left )
&&(Top ==rs.Top )
&&(Width ==rs.Width )
&&(Height ==rs.Height ))return(true);
else
return(false);
}
template<typename N>
bool operator != (const RectScope2<N> &rs) const
{
return(!operator==(rs));
}
template<typename N>
void operator += (const RectScope2<N> &rs)
{
const T r=hgl_max(GetRight(),rs.GetRight());
const T b=hgl_max(GetBottom(),rs.GetBottom());
Left=hgl_min(Left,rs.Left);
Top =hgl_max(Top,rs.Top);
Width=r-Left;
Height=b-Top;
}
template<typename N>
RectScope2<T> operator + (const N &v) const
{
return RectScope2<T>(Left+v[0],Top+v[1],Width,Height);
}
template<typename N>
RectScope2<T> operator - (const N &v) const
{
return RectScope2<T>(Left+v[0],Top+v[1],Width,Height);
}
template<typename N>
RectScope2<T> &operator += (const N &v)
{
Left+=v[0];
Top+=v[1];
return(*this);
}
template<typename N>
RectScope2<T> &operator -= (const N &v)
{
Left-=v[0];
Top-=v[1];
return(*this);
}
};//class RectScope2
typedef RectScope2<double> RectScope2d;
typedef RectScope2<float> RectScope2f;
typedef RectScope2<int> RectScope2i;
typedef RectScope2<uint> RectScope2ui;
typedef RectScope2<short> RectScope2s;
typedef RectScope2<ushort> RectScope2us;
}//namespace hgl
#include<hgl/type/RectScope.cpp>
#endif//HGL_RECTSCOPE_INCLUDE