新的BufferObject
This commit is contained in:
parent
095951acd5
commit
6e4e99f56b
@ -1,8 +1,7 @@
|
|||||||
#ifndef HGL_GRAPH_BUFFER_OBJECT_INCLUDE
|
#ifndef HGL_GRAPH_BUFFER_OBJECT_INCLUDE
|
||||||
#define HGL_GRAPH_BUFFER_OBJECT_INCLUDE
|
#define HGL_GRAPH_BUFFER_OBJECT_INCLUDE
|
||||||
|
|
||||||
#include<GLEWCore/glew.h>
|
#include<hgl/graph/BufferData.h>
|
||||||
#include<hgl/type/DataType.h>
|
|
||||||
namespace hgl
|
namespace hgl
|
||||||
{
|
{
|
||||||
namespace graph
|
namespace graph
|
||||||
@ -13,59 +12,116 @@ namespace hgl
|
|||||||
*/
|
*/
|
||||||
class BufferObject
|
class BufferObject
|
||||||
{
|
{
|
||||||
|
protected:
|
||||||
|
|
||||||
GLuint buffer_index; ///<缓冲区索引
|
GLuint buffer_index; ///<缓冲区索引
|
||||||
GLenum buffer_type; ///<缓冲区类型(GL_ARRAY_BUFFER,GL_ELEMENT_ARRAY_BUFFER等)
|
GLenum buffer_type; ///<缓冲区类型(GL_ARRAY_BUFFER,GL_ELEMENT_ARRAY_BUFFER等)
|
||||||
GLenum user_pattern; ///<数据存储区使用模式(GL_STATIC_DRAW,GL_DYNAMIC_DRAW等)
|
GLenum user_pattern; ///<数据存储区使用模式(GL_STATIC_DRAW,GL_DYNAMIC_DRAW等)
|
||||||
|
|
||||||
GLsizeiptr total_bytes; ///<数据总字节数
|
GLsizeiptr buffer_bytes;
|
||||||
void *local_data,*local_data_end; ///<本地数据内存指针
|
const BufferData *buffer_data;
|
||||||
bool self_alloc; ///<是否自身分配
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
BufferObject(GLuint index,GLenum type,GLenum up)
|
friend BufferObject *CreateBuffer(GLenum type,GLenum user_pattern,BufferData *buf);
|
||||||
|
|
||||||
|
BufferObject(GLuint index,GLenum type)
|
||||||
{
|
{
|
||||||
buffer_index=index;
|
buffer_index=index;
|
||||||
buffer_type=type;
|
buffer_type =type;
|
||||||
user_pattern=up;
|
|
||||||
|
user_pattern=0;
|
||||||
|
buffer_bytes=0;
|
||||||
|
buffer_data=nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferObject(GLuint index,GLenum type,GLenum up,void *data,GLsizeiptr size,bool data_self_alloc)
|
public:
|
||||||
{
|
|
||||||
buffer_index=index;
|
|
||||||
buffer_type=type;
|
|
||||||
user_pattern=up;
|
|
||||||
|
|
||||||
total_bytes=size;
|
virtual ~BufferObject();
|
||||||
|
|
||||||
local_data=data;
|
public:
|
||||||
local_data_end=((char *)data)+size;
|
|
||||||
|
|
||||||
self_alloc=data_self_alloc;
|
GLuint GetBufferIndex ()const {return buffer_index;} ///<取得OpenGL缓冲区
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~BufferObject()
|
|
||||||
{
|
|
||||||
if(self_alloc)
|
|
||||||
if(local_data)delete[] local_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
GLuint GetBuffer ()const {return buffer_index;} ///<取得OpenGL缓冲区
|
|
||||||
GLenum GetBufferType ()const {return buffer_type;} ///<取得缓冲区类型
|
GLenum GetBufferType ()const {return buffer_type;} ///<取得缓冲区类型
|
||||||
GLenum GetUserPattern ()const {return user_pattern;} ///<取得缓冲区使用方法
|
GLenum GetUserPattern ()const {return user_pattern;} ///<取得缓冲区使用方法
|
||||||
GLsizeiptr GetTotalBytes ()const {return total_bytes;} ///<取得数据总字节数
|
|
||||||
|
|
||||||
void * GetData () {return data;} ///<取得数据指针
|
const BufferData *GetBufferData ()const {return buffer_data;} ///<取得缓冲数区(这里返回const是为了不想让只有BufferObject的模块可以修改数据)
|
||||||
void * GetData (const uint pos) {return ((char *)data)+data_bytes*pos;} ///<取得数据指针
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
virtual bool Submit (const void *,GLsizeiptr,GLenum user_pattern)=0; ///<提交数据
|
||||||
|
bool Submit (const BufferData *buf_data,GLenum user_pattern) ///<提交数据
|
||||||
|
{
|
||||||
|
if(!buf_data)return(false);
|
||||||
|
buffer_data=buf_data;
|
||||||
|
|
||||||
|
const void * data=buf_data->GetData();
|
||||||
|
const GLsizeiptr size=buf_data->GetTotalBytes();
|
||||||
|
|
||||||
|
if(!data||size<=0)return(false);
|
||||||
|
|
||||||
|
return Submit(data,size,user_pattern);
|
||||||
|
}
|
||||||
|
virtual bool Change (const void *,GLsizeiptr,GLsizeiptr)=0; ///<修改数据
|
||||||
};//class BufferObject
|
};//class BufferObject
|
||||||
|
|
||||||
BufferObject *CreateBuffer(GLenum type)
|
/**
|
||||||
|
* 创建一个缓冲区
|
||||||
|
* @param type 缓冲区类型(GL_ARRAY_BUFFER,GL_ELEMENT_ARRAY_BUFFER等)
|
||||||
|
* @param user_pattern 缓冲区数据用法(GL_STATIC_DRAW,GL_DYNAMIC_DRAW等)
|
||||||
|
* @param buf 数据缓冲区
|
||||||
|
*/
|
||||||
|
BufferObject *CreateBuffer(GLenum type,GLenum user_pattern=0,BufferData *buf=nullptr);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个数据对象
|
||||||
|
* @param buf_type 缓冲区类型(GL_ARRAY_BUFFER,GL_ELEMENT_ARRAY_BUFFER等)
|
||||||
|
* @param user_pattern 数据存储区使用模式(GL_STATIC_DRAW,GL_DYNAMIC_DRAW等)
|
||||||
|
* @param total_bytes 数据总字节数
|
||||||
|
*/
|
||||||
|
inline BufferObject *CreateBuffer( const GLenum &buf_type,
|
||||||
|
const GLenum &user_pattern,
|
||||||
|
const GLsizeiptr &total_bytes)
|
||||||
{
|
{
|
||||||
隐藏BufferObject创建方法,并且必须在外部创建好buffer index再创建buffer object。
|
if(total_bytes<=0)return(nullptr);
|
||||||
并将buffer object本身与内存镜像数据部分分离,这样保证buffer object的单纯性,
|
|
||||||
其它无需操作数据的模块,也没有可直接操作数据的API可用。
|
BufferObject *buf=CreateBuffer(buf_type);
|
||||||
|
|
||||||
|
if(!buf)
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
|
if(buf->Create(data,total_bytes))
|
||||||
|
return buf;
|
||||||
|
|
||||||
|
delete buf;
|
||||||
|
return(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个数据对象
|
||||||
|
* @param buf_type 缓冲区类型(GL_ARRAY_BUFFER,GL_ELEMENT_ARRAY_BUFFER等)
|
||||||
|
* @param user_pattern 数据存储区使用模式(GL_STATIC_DRAW,GL_DYNAMIC_DRAW等)
|
||||||
|
* @param total_bytes 数据总字节数
|
||||||
|
* @param data 数据指针
|
||||||
|
*/
|
||||||
|
inline BufferObject *CreateBuffer( const GLenum &buf_type,
|
||||||
|
const GLenum &user_pattern,
|
||||||
|
const GLsizeiptr &total_bytes,void *data)
|
||||||
|
{
|
||||||
|
if(total_bytes<=0)return(nullptr);
|
||||||
|
if(!data)return(nullptr);
|
||||||
|
|
||||||
|
BufferObject *buf=CreateBuffer(buf_type);
|
||||||
|
|
||||||
|
if(!buf)
|
||||||
|
return(nullptr);
|
||||||
|
|
||||||
|
if(buf->Submit(data,total_bytes,user_pattern))
|
||||||
|
return buf;
|
||||||
|
|
||||||
|
delete buf;
|
||||||
|
return(nullptr);
|
||||||
|
}
|
||||||
}//namespace graph
|
}//namespace graph
|
||||||
}//namespace hgl
|
}//namespace hgl
|
||||||
#endif//HGL_GRAPH_BUFFER_OBJECT_INCLUDE
|
#endif//HGL_GRAPH_BUFFER_OBJECT_INCLUDE
|
||||||
|
Loading…
x
Reference in New Issue
Block a user