-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvertexBufferObject.cpp
executable file
·91 lines (77 loc) · 2.25 KB
/
vertexBufferObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "common_header.h"
#include "vertexBufferObject.h"
CVertexBufferObject::CVertexBufferObject()
{
bDataUploaded = false;
}
// Creates vertex buffer object.
// a_iSize - initial size of buffer
void CVertexBufferObject::CreateVBO(int a_iSize)
{
glGenBuffers(1, &uiBuffer);
data.reserve(a_iSize);
iSize = a_iSize;
}
// Releases VBO and frees all memory.
void CVertexBufferObject::DeleteVBO()
{
glDeleteBuffers(1, &uiBuffer);
bDataUploaded = false;
data.clear();
}
// Maps whole buffer data to memory and returns pointer to data.
// iUsageHint - GL_READ_ONLY, GL_WRITE_ONLY...
void* CVertexBufferObject::MapBufferToMemory(int iUsageHint)
{
if(!bDataUploaded)return NULL;
void* ptrRes = glMapBuffer(iBufferType, iUsageHint);
return ptrRes;
}
// Maps specified part of buffer to memory.
// iUsageHint - GL_READ_ONLY, GL_WRITE_ONLY...
// uiOffset - data offset(from where should data be mapped).
// uiLength - length of data
void* CVertexBufferObject::MapSubBufferToMemory(int iUsageHint, uint uiOffset, uint uiLength)
{
if(!bDataUploaded)return NULL;
void* ptrRes = glMapBufferRange(iBufferType, uiOffset, uiLength, iUsageHint);
return ptrRes;
}
// Unmaps previously mapped buffer.
void CVertexBufferObject::UnmapBuffer()
{
glUnmapBuffer(iBufferType);
}
// Binds this VBO.
// a_iBufferType - buffer type (GL_ARRAY_BUFFER, ...)
void CVertexBufferObject::BindVBO(int a_iBufferType)
{
iBufferType = a_iBufferType;
glBindBuffer(iBufferType, uiBuffer);
}
// Sends data to GPU.
// iUsageHint - GL_STATIC_DRAW, GL_DYNAMIC_DRAW...
void CVertexBufferObject::UploadDataToGPU(int iDrawingHint)
{
glBufferData(iBufferType, data.size(), &data[0], iDrawingHint);
bDataUploaded = true;
data.clear();
}
// Adds arbitrary data to VBO.
// ptrData - pointer to arbitrary data
// uiDataSize - data size in bytes
void CVertexBufferObject::AddData(void* ptrData, uint uiDataSize)
{
data.insert(data.end(), (unsigned char*)ptrData, (unsigned char*)ptrData+uiDataSize);
}
// Returns data pointer (only before uploading).
void* CVertexBufferObject::GetDataPointer()
{
if(bDataUploaded)return NULL;
return (void*)&(data[0]);
}
// Returns VBO ID.
uint CVertexBufferObject::GetBufferID()
{
return uiBuffer;
}