Changed the vertex and index buffer types to a class.

This commit is contained in:
leo 2015-04-30 22:35:40 +00:00
parent 20945362e5
commit 170b7fc775
4 changed files with 73 additions and 42 deletions

View file

@ -303,89 +303,89 @@ bool lcContext::SaveRenderToTextureImage(const QString& FileName, int Width, int
lcVertexBuffer lcContext::CreateVertexBuffer(int Size, const void* Data) lcVertexBuffer lcContext::CreateVertexBuffer(int Size, const void* Data)
{ {
lcVertexBuffer VertexBuffer;
if (GL_HasVertexBufferObject()) if (GL_HasVertexBufferObject())
{ {
lcVertexBuffer VertexBuffer; glGenBuffers(1, &VertexBuffer.Object);
glBindBuffer(GL_ARRAY_BUFFER_ARB, VertexBuffer.Object);
glGenBuffers(1, &VertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER_ARB, VertexBuffer);
glBufferData(GL_ARRAY_BUFFER_ARB, Size, Data, GL_STATIC_DRAW_ARB); glBufferData(GL_ARRAY_BUFFER_ARB, Size, Data, GL_STATIC_DRAW_ARB);
glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); // context remove glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); // context remove
mVertexBufferObject = 0; mVertexBufferObject = 0;
return VertexBuffer;
} }
else else
{ {
void* VertexBuffer = malloc(Size); VertexBuffer.Pointer = malloc(Size);
memcpy(VertexBuffer, Data, Size); memcpy(VertexBuffer.Pointer, Data, Size);
return (lcVertexBuffer)VertexBuffer;
} }
return VertexBuffer;
} }
void lcContext::DestroyVertexBuffer(lcVertexBuffer& VertexBuffer) void lcContext::DestroyVertexBuffer(lcVertexBuffer& VertexBuffer)
{ {
if (!VertexBuffer) if (!VertexBuffer.IsValid())
return; return;
if (GL_HasVertexBufferObject()) if (GL_HasVertexBufferObject())
{ {
if (mVertexBufferObject == VertexBuffer) if (mVertexBufferObject == VertexBuffer.Object)
{ {
glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); glBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
mVertexBufferObject = 0; mVertexBufferObject = 0;
} }
glDeleteBuffers(1, &VertexBuffer); glDeleteBuffers(1, &VertexBuffer.Object);
} }
else else
{ {
free((void*)VertexBuffer); free(VertexBuffer.Pointer);
} }
VertexBuffer.Pointer = NULL;
} }
lcIndexBuffer lcContext::CreateIndexBuffer(int Size, const void* Data) lcIndexBuffer lcContext::CreateIndexBuffer(int Size, const void* Data)
{ {
lcIndexBuffer IndexBuffer;
if (GL_HasVertexBufferObject()) if (GL_HasVertexBufferObject())
{ {
lcIndexBuffer IndexBuffer; glGenBuffers(1, &IndexBuffer.Object);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, IndexBuffer.Object);
glGenBuffers(1, &IndexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, IndexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB, Size, Data, GL_STATIC_DRAW_ARB); glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB, Size, Data, GL_STATIC_DRAW_ARB);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); // context remove glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); // context remove
mIndexBufferObject = 0; mIndexBufferObject = 0;
return IndexBuffer;
} }
else else
{ {
void* IndexBuffer = malloc(Size); IndexBuffer.Pointer = malloc(Size);
memcpy(IndexBuffer, Data, Size); memcpy(IndexBuffer.Pointer, Data, Size);
return (lcIndexBuffer)IndexBuffer;
} }
return IndexBuffer;
} }
void lcContext::DestroyIndexBuffer(lcIndexBuffer& IndexBuffer) void lcContext::DestroyIndexBuffer(lcIndexBuffer& IndexBuffer)
{ {
if (!IndexBuffer) if (!IndexBuffer.IsValid())
return; return;
if (GL_HasVertexBufferObject()) if (GL_HasVertexBufferObject())
{ {
if (mIndexBufferObject == IndexBuffer) if (mIndexBufferObject == IndexBuffer.Object)
{ {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
mIndexBufferObject = 0; mIndexBufferObject = 0;
} }
glDeleteBuffers(1, &IndexBuffer); glDeleteBuffers(1, &IndexBuffer.Object);
} }
else else
{ {
free((void*)IndexBuffer); free(IndexBuffer.Pointer);
} }
} }
@ -411,7 +411,7 @@ void lcContext::SetVertexBuffer(lcVertexBuffer VertexBuffer)
{ {
if (GL_HasVertexBufferObject()) if (GL_HasVertexBufferObject())
{ {
GLuint VertexBufferObject = VertexBuffer; GLuint VertexBufferObject = VertexBuffer.Object;
mVertexBufferPointer = NULL; mVertexBufferPointer = NULL;
if (VertexBufferObject != mVertexBufferObject) if (VertexBufferObject != mVertexBufferObject)
@ -423,7 +423,7 @@ void lcContext::SetVertexBuffer(lcVertexBuffer VertexBuffer)
} }
else else
{ {
mVertexBufferPointer = (char*)VertexBuffer; mVertexBufferPointer = (char*)VertexBuffer.Pointer;
mVertexBufferOffset = (char*)~0; mVertexBufferOffset = (char*)~0;
} }
} }
@ -497,7 +497,7 @@ void lcContext::SetIndexBuffer(lcIndexBuffer IndexBuffer)
{ {
if (GL_HasVertexBufferObject()) if (GL_HasVertexBufferObject())
{ {
GLuint IndexBufferObject = IndexBuffer; GLuint IndexBufferObject = IndexBuffer.Object;
mIndexBufferPointer = NULL; mIndexBufferPointer = NULL;
if (IndexBufferObject != mIndexBufferObject) if (IndexBufferObject != mIndexBufferObject)
@ -508,7 +508,7 @@ void lcContext::SetIndexBuffer(lcIndexBuffer IndexBuffer)
} }
else else
{ {
mIndexBufferPointer = (char*)IndexBuffer; mIndexBufferPointer = (char*)IndexBuffer.Pointer;
} }
} }
@ -529,10 +529,10 @@ void lcContext::BindMesh(lcMesh* Mesh)
if (Mesh->mVertexCacheOffset != -1) if (Mesh->mVertexCacheOffset != -1)
{ {
GLuint VertexBufferObject = Library->mVertexBuffer; GLuint VertexBufferObject = Library->mVertexBuffer.Object;
mVertexBufferPointer = (char*)Mesh->mVertexCacheOffset; mVertexBufferPointer = (char*)(quintptr)Mesh->mVertexCacheOffset;
GLuint IndexBufferObject = Library->mIndexBuffer; GLuint IndexBufferObject = Library->mIndexBuffer.Object;
mIndexBufferPointer = (char*)Mesh->mIndexCacheOffset; mIndexBufferPointer = (char*)(quintptr)Mesh->mIndexCacheOffset;
if (VertexBufferObject != mVertexBufferObject) if (VertexBufferObject != mVertexBufferObject)
{ {

View file

@ -18,11 +18,45 @@ public:
lcArray<lcObject*> mInterfaceObjects; lcArray<lcObject*> mInterfaceObjects;
}; };
typedef quintptr lcVertexBuffer; class lcVertexBuffer
typedef quintptr lcIndexBuffer; {
public:
lcVertexBuffer()
: Pointer(NULL)
{
}
#define LC_INVALID_VERTEX_BUFFER 0 bool IsValid() const
#define LC_INVALID_INDEX_BUFFER 0 {
return Pointer != NULL;
}
union
{
GLuint Object;
void* Pointer;
};
};
class lcIndexBuffer
{
public:
lcIndexBuffer()
: Pointer(NULL)
{
}
bool IsValid() const
{
return Pointer != NULL;
}
union
{
GLuint Object;
void* Pointer;
};
};
class lcContext class lcContext
{ {

View file

@ -33,8 +33,6 @@ lcPiecesLibrary::lcPiecesLibrary()
mCacheFileName[0] = 0; mCacheFileName[0] = 0;
mSaveCache = false; mSaveCache = false;
mBuffersDirty = false; mBuffersDirty = false;
mVertexBuffer = LC_INVALID_VERTEX_BUFFER;
mIndexBuffer = LC_INVALID_INDEX_BUFFER;
} }
lcPiecesLibrary::~lcPiecesLibrary() lcPiecesLibrary::~lcPiecesLibrary()

View file

@ -20,7 +20,6 @@ View::View(lcModel* Model)
{ {
mModel = Model; mModel = Model;
mCamera = NULL; mCamera = NULL;
mGridBuffer = LC_INVALID_VERTEX_BUFFER;
memset(mGridSettings, 0, sizeof(mGridSettings)); memset(mGridSettings, 0, sizeof(mGridSettings));
mDragState = LC_DRAGSTATE_NONE; mDragState = LC_DRAGSTATE_NONE;
@ -1256,7 +1255,7 @@ void View::DrawGrid()
MaxY = 2; MaxY = 2;
} }
if (mGridBuffer == LC_INVALID_VERTEX_BUFFER || MinX != mGridSettings[0] || MinY != mGridSettings[1] || MaxX != mGridSettings[2] || MaxY != mGridSettings[3] || if (!mGridBuffer.IsValid() || MinX != mGridSettings[0] || MinY != mGridSettings[1] || MaxX != mGridSettings[2] || MaxY != mGridSettings[3] ||
Spacing != mGridSettings[4] || (Preferences.mDrawGridStuds ? 1 : 0) != mGridSettings[5] || (Preferences.mDrawGridLines ? 1 : 0) != mGridSettings[6]) Spacing != mGridSettings[4] || (Preferences.mDrawGridStuds ? 1 : 0) != mGridSettings[5] || (Preferences.mDrawGridLines ? 1 : 0) != mGridSettings[6])
{ {
int VertexBufferSize = 0; int VertexBufferSize = 0;