Skip to content

Commit 3b2bd03

Browse files
committed
Adding option to interleave VBO
1 parent 687508b commit 3b2bd03

File tree

3 files changed

+173
-10
lines changed

3 files changed

+173
-10
lines changed

src/ogl/VBO_Vertex.cxx

Lines changed: 154 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,30 @@ GLuint VBO_Vertex::actVerts = 0;
2323
GLuint VBO_Vertex::actNorms = 0;
2424
GLuint VBO_Vertex::actTxcds = 0;
2525
GLuint VBO_Vertex::actColrs = 0;
26+
GLuint VBO_Vertex::actIntrs = 0;
2627
GLuint VBO_Vertex::actBounded = 0;
2728

2829
VBO_Vertex::VBO_Vertex(
2930
bool handleTexture_,
3031
bool handleNormal_,
31-
bool handleColor_) :
32+
bool handleColor_,
33+
bool interlaced_) :
3234
VBO_Handler(),
3335
handleTexture(handleTexture_), handleNormal(handleNormal_),
3436
handleColor(handleColor_), verts(0), txcds(0), norms(0), colrs(0),
35-
vbosReady(false)
37+
vbosReady(false), interlaced(interlaced_)
3638
{
39+
interlaced = false;
40+
interlacedSize = 3;
41+
strideTexture = interlacedSize;
42+
if (handleTexture)
43+
interlacedSize += 2;
44+
strideNormal = interlacedSize;
45+
if (handleNormal)
46+
interlacedSize += 3;
47+
strideColor = interlacedSize;
48+
if (handleColor)
49+
interlacedSize += 4;
3750
}
3851

3952
void VBO_Vertex::resize()
@@ -51,6 +64,8 @@ void VBO_Vertex::resize()
5164
hostedNormals.resize(3 * vboSize);
5265
if (!hostedColors.empty())
5366
hostedColors.resize(4 * vboSize);
67+
if (!hostedInterlaced.empty())
68+
hostedInterlaced.resize(interlacedSize * vboSize);
5469
if (!vbosReady)
5570
// Don't do anything on GL Context memory if not ready
5671
return;
@@ -65,6 +80,9 @@ void VBO_Vertex::init()
6580
return;
6681
// GL Context is ready
6782
// Bind all buffers
83+
if (interlaced)
84+
glGenBuffers(1, &intrs);
85+
else
6886
{
6987
glGenBuffers(1, &verts);
7088
if (handleTexture)
@@ -76,6 +94,15 @@ void VBO_Vertex::init()
7694
}
7795

7896
// Copy in memory buffer to GPU
97+
if (interlaced)
98+
{
99+
bindBuffer(intrs);
100+
glBufferData(GL_ARRAY_BUFFER,
101+
vboSize * interlacedSize * sizeof(GLfloat),
102+
hostedInterlaced.data(),
103+
GL_DYNAMIC_DRAW);
104+
}
105+
else
79106
{
80107
bindBuffer(verts);
81108
glBufferData(GL_ARRAY_BUFFER,
@@ -115,6 +142,9 @@ void VBO_Vertex::destroy()
115142
if (!vboSize)
116143
return;
117144
// drop all buffers
145+
if (interlaced)
146+
deleteBuffer(intrs);
147+
else
118148
{
119149
deleteBuffer(verts);
120150
if (handleTexture)
@@ -140,6 +170,20 @@ std::string VBO_Vertex::vboName()
140170

141171
void VBO_Vertex::vertexData(int index, int size, const GLfloat vertices[])
142172
{
173+
if (interlaced)
174+
{
175+
if (hostedInterlaced.empty())
176+
hostedInterlaced.resize(interlacedSize * vboSize);
177+
auto dataP = &hostedInterlaced[index * interlacedSize];
178+
auto vertP = vertices;
179+
for (int i = 0; i < size; i++)
180+
{
181+
memcpy(dataP, vertP, sizeof(GLfloat[3]));
182+
dataP += interlacedSize;
183+
vertP += 3;
184+
}
185+
}
186+
else
143187
{
144188
if (hostedVertices.empty())
145189
hostedVertices.resize(3 * vboSize);
@@ -149,6 +193,15 @@ void VBO_Vertex::vertexData(int index, int size, const GLfloat vertices[])
149193
}
150194
if (!vbosReady)
151195
return;
196+
if (interlaced)
197+
{
198+
bindBuffer(intrs);
199+
glBufferSubData(GL_ARRAY_BUFFER,
200+
index * interlacedSize * sizeof(GLfloat),
201+
size * interlacedSize * sizeof(GLfloat),
202+
&hostedInterlaced[index * interlacedSize]);
203+
}
204+
else
152205
{
153206
bindBuffer(verts);
154207
glBufferSubData(GL_ARRAY_BUFFER,
@@ -162,6 +215,21 @@ void VBO_Vertex::textureData(int index, int size, const GLfloat textures[])
162215
{
163216
if (!handleTexture)
164217
return;
218+
if (interlaced)
219+
{
220+
if (hostedInterlaced.empty())
221+
hostedInterlaced.resize(interlacedSize * vboSize);
222+
auto dataP = &hostedInterlaced[index * interlacedSize];
223+
auto textP = textures;
224+
dataP += strideTexture;
225+
for (int i = 0; i < size; i++)
226+
{
227+
memcpy(dataP, textP, sizeof(GLfloat[2]));
228+
dataP += interlacedSize;
229+
textP += 2;
230+
}
231+
}
232+
else
165233
{
166234
if (hostedTextures.empty())
167235
hostedTextures.resize(2 * vboSize);
@@ -171,6 +239,15 @@ void VBO_Vertex::textureData(int index, int size, const GLfloat textures[])
171239
}
172240
if (!vbosReady)
173241
return;
242+
if (interlaced)
243+
{
244+
bindBuffer(intrs);
245+
glBufferSubData(GL_ARRAY_BUFFER,
246+
index * interlacedSize * sizeof(GLfloat),
247+
size * interlacedSize * sizeof(GLfloat),
248+
&hostedInterlaced[index * interlacedSize]);
249+
}
250+
else
174251
{
175252
bindBuffer(txcds);
176253
glBufferSubData(GL_ARRAY_BUFFER,
@@ -184,6 +261,21 @@ void VBO_Vertex::normalData(int index, int size, const GLfloat normals[])
184261
{
185262
if (!handleNormal)
186263
return;
264+
if (interlaced)
265+
{
266+
if (hostedInterlaced.empty())
267+
hostedInterlaced.resize(interlacedSize * vboSize);
268+
auto dataP = &hostedInterlaced[index * interlacedSize];
269+
auto normP = normals;
270+
dataP += strideNormal;
271+
for (int i = 0; i < size; i++)
272+
{
273+
memcpy(dataP, normP, sizeof(GLfloat[3]));
274+
dataP += interlacedSize;
275+
normP += 3;
276+
}
277+
}
278+
else
187279
{
188280
if (hostedNormals.empty())
189281
hostedNormals.resize(3 * vboSize);
@@ -193,6 +285,15 @@ void VBO_Vertex::normalData(int index, int size, const GLfloat normals[])
193285
}
194286
if (!vbosReady)
195287
return;
288+
if (interlaced)
289+
{
290+
bindBuffer(intrs);
291+
glBufferSubData(GL_ARRAY_BUFFER,
292+
index * interlacedSize * sizeof(GLfloat),
293+
size * interlacedSize * sizeof(GLfloat),
294+
&hostedInterlaced[index * interlacedSize]);
295+
}
296+
else
196297
{
197298
bindBuffer(norms);
198299
glBufferSubData(GL_ARRAY_BUFFER,
@@ -206,6 +307,21 @@ void VBO_Vertex::colorData(int index, int size, const GLfloat colors[])
206307
{
207308
if (!handleColor)
208309
return;
310+
if (interlaced)
311+
{
312+
if (hostedInterlaced.empty())
313+
hostedInterlaced.resize(interlacedSize * vboSize);
314+
auto dataP = &hostedInterlaced[index * interlacedSize];
315+
auto coloP = colors;
316+
dataP += strideColor;
317+
for (int i = 0; i < size; i++)
318+
{
319+
memcpy(dataP, coloP, sizeof(GLfloat[4]));
320+
dataP += interlacedSize;
321+
coloP += 4;
322+
}
323+
}
324+
else
209325
{
210326
if (hostedColors.empty())
211327
hostedColors.resize(4 * vboSize);
@@ -215,6 +331,15 @@ void VBO_Vertex::colorData(int index, int size, const GLfloat colors[])
215331
}
216332
if (!vbosReady)
217333
return;
334+
if (interlaced)
335+
{
336+
bindBuffer(intrs);
337+
glBufferSubData(GL_ARRAY_BUFFER,
338+
index * interlacedSize * sizeof(GLfloat),
339+
size * interlacedSize * sizeof(GLfloat),
340+
&hostedInterlaced[index * interlacedSize]);
341+
}
342+
else
218343
{
219344
bindBuffer(colrs);
220345
glBufferSubData(GL_ARRAY_BUFFER,
@@ -256,6 +381,24 @@ void VBO_Vertex::enableVertexOnly()
256381

257382
void VBO_Vertex::enableVertex()
258383
{
384+
if (interlaced)
385+
{
386+
if (actIntrs == intrs)
387+
return;
388+
actIntrs = intrs;
389+
bindBuffer(intrs);
390+
glVertexPointer(3, GL_FLOAT, interlacedSize * sizeof(GLfloat), 0);
391+
glTexCoordPointer(2, GL_FLOAT,
392+
interlacedSize * sizeof(GLfloat),
393+
(void *)(strideTexture * sizeof(GLfloat)));
394+
glNormalPointer(GL_FLOAT,
395+
interlacedSize * sizeof(GLfloat),
396+
(void *)(strideNormal * sizeof(GLfloat)));
397+
glColorPointer(4, GL_FLOAT,
398+
interlacedSize * sizeof(GLfloat),
399+
(void *)(strideColor * sizeof(GLfloat)));
400+
}
401+
else
259402
{
260403
if (actVerts == verts)
261404
return;
@@ -282,6 +425,7 @@ void VBO_Vertex::enableTextures()
282425
if (actTxcds == txcds)
283426
return;
284427
actTxcds = txcds;
428+
if (!interlaced)
285429
{
286430
bindBuffer(txcds);
287431
glTexCoordPointer(2, GL_FLOAT, 0, 0);
@@ -305,6 +449,7 @@ void VBO_Vertex::enableNormals()
305449
if (actNorms == norms)
306450
return;
307451
actNorms = norms;
452+
if (!interlaced)
308453
{
309454
bindBuffer(norms);
310455
glNormalPointer(GL_FLOAT, 0, 0);
@@ -328,6 +473,7 @@ void VBO_Vertex::enableColors()
328473
if (actColrs == colrs)
329474
return;
330475
actColrs = colrs;
476+
if (!interlaced)
331477
{
332478
bindBuffer(colrs);
333479
glColorPointer(4, GL_FLOAT, 0, 0);
@@ -350,6 +496,12 @@ void VBO_Vertex::deleteBuffer(GLuint buffer)
350496
// tell that this buffer is not more bounded
351497
if (actBounded == buffer)
352498
actBounded = 0;
499+
if (interlaced)
500+
{
501+
if (actIntrs == buffer)
502+
actIntrs = 0;
503+
}
504+
else
353505
{
354506
if (actVerts == buffer)
355507
actVerts = 0;

src/ogl/VBO_Vertex.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class VBO_Vertex: public VBO_Handler
3434
VBO_Vertex(
3535
bool handleTexture,
3636
bool handleNormal,
37-
bool handleColor);
37+
bool handleColor,
38+
bool interlaced);
3839
~VBO_Vertex() = default;
3940

4041
// This should be called when there is not enough space on the current
@@ -76,6 +77,7 @@ class VBO_Vertex: public VBO_Handler
7677
static GLuint actTxcds;
7778
static GLuint actNorms;
7879
static GLuint actColrs;
80+
static GLuint actIntrs;
7981

8082
// Last bounded buffer
8183
static GLuint actBounded;
@@ -91,6 +93,7 @@ class VBO_Vertex: public VBO_Handler
9193
GLuint txcds;
9294
GLuint norms;
9395
GLuint colrs;
96+
GLuint intrs;
9497

9598
// Bind vertex Buffer
9699
void enableVertex();
@@ -112,8 +115,15 @@ class VBO_Vertex: public VBO_Handler
112115
std::vector<GLfloat> hostedTextures;
113116
std::vector<GLfloat> hostedNormals;
114117
std::vector<GLfloat> hostedColors;
118+
std::vector<GLfloat> hostedInterlaced;
115119

116120
bool vbosReady;
121+
122+
bool interlaced;
123+
int interlacedSize;
124+
int strideTexture;
125+
int strideNormal;
126+
int strideColor;
117127
};
118128

119129
// Local Variables: ***

src/ogl/Vertex_Chunk.cxx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Vertex_Chunk::Vertex_Chunk(Component comp, unsigned int size)
3737
, indexSize(size)
3838
, myComp(comp)
3939
{
40+
const bool interlaced = true;
4041
if (!size)
4142
{
4243
// Do nothing if size 0
@@ -55,25 +56,25 @@ Vertex_Chunk::Vertex_Chunk(Component comp, unsigned int size)
5556
switch (comp)
5657
{
5758
case Component::V:
58-
myVBO = new VBO_Vertex(false, false, false);
59+
myVBO = new VBO_Vertex(false, false, false, interlaced);
5960
break;
6061
case Component::VC:
61-
myVBO = new VBO_Vertex(false, false, true);
62+
myVBO = new VBO_Vertex(false, false, true, interlaced);
6263
break;
6364
case Component::VN:
64-
myVBO = new VBO_Vertex(false, true, false);
65+
myVBO = new VBO_Vertex(false, true, false, interlaced);
6566
break;
6667
case Component::VT:
67-
myVBO = new VBO_Vertex(true, false, false);
68+
myVBO = new VBO_Vertex(true, false, false, interlaced);
6869
break;
6970
case Component::VTC:
70-
myVBO = new VBO_Vertex(true, false, true);
71+
myVBO = new VBO_Vertex(true, false, true, interlaced);
7172
break;
7273
case Component::VTN:
73-
myVBO = new VBO_Vertex(true, true, false);
74+
myVBO = new VBO_Vertex(true, true, false, interlaced);
7475
break;
7576
case Component::VTNC:
76-
myVBO = new VBO_Vertex(true, true, true);
77+
myVBO = new VBO_Vertex(true, true, true, true);
7778
break;
7879
default:
7980
break;

0 commit comments

Comments
 (0)