History

Fix for buffer crash.

Andrzej Kapolka committed 11 years ago

Commit 52641bd1 parent 27255fb2 files · +73 −69Browse files

Showing the changes to interface/src/MetavoxelSystem.cpp (1 of 2 files). Show all

Unified · Split

interface/src/MetavoxelSystem.cpp+64−62
@@ -223,8 +223,8 @@ void MetavoxelSystem::render() { · 222 unmodified lines
223223 glRotatef(glm::degrees(glm::angle(batch.rotation)), axis.x, axis.y, axis.z);
224224 glScalef(batch.scale.x, batch.scale.y, batch.scale.z);
225225
226− batch.vertexBuffer->bind();
227− batch.indexBuffer->bind();
226+ glBindBuffer(GL_ARRAY_BUFFER, batch.vertexBufferID);
227+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, batch.indexBufferID);
228228
229229 HeightfieldPoint* point = 0;
230230 glVertexPointer(3, GL_FLOAT, sizeof(HeightfieldPoint), &point->vertex);
@@ -245,8 +245,8 @@ void MetavoxelSystem::render() { · 14 unmodified lines
245245 glActiveTexture(GL_TEXTURE0);
246246 glBindTexture(GL_TEXTURE_2D, 0);
247247
248− batch.vertexBuffer->release();
249− batch.indexBuffer->release();
248+ glBindBuffer(GL_ARRAY_BUFFER, 0);
249+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
250250
251251 glPopMatrix();
252252 }
@@ -273,8 +273,8 @@ void MetavoxelSystem::render() { · 20 unmodified lines
273273 glRotatef(glm::degrees(glm::angle(batch.rotation)), axis.x, axis.y, axis.z);
274274 glScalef(batch.scale.x, batch.scale.y, batch.scale.z);
275275
276− batch.vertexBuffer->bind();
277− batch.indexBuffer->bind();
276+ glBindBuffer(GL_ARRAY_BUFFER, batch.vertexBufferID);
277+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, batch.indexBufferID);
278278
279279 HeightfieldPoint* point = 0;
280280 glVertexPointer(3, GL_FLOAT, sizeof(HeightfieldPoint), &point->vertex);
@@ -324,8 +324,8 @@ void MetavoxelSystem::render() { · 43 unmodified lines
324324 glActiveTexture(GL_TEXTURE0);
325325 glBindTexture(GL_TEXTURE_2D, 0);
326326
327− batch.vertexBuffer->release();
328− batch.indexBuffer->release();
327+ glBindBuffer(GL_ARRAY_BUFFER, 0);
328+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
329329
330330 glPopMatrix();
331331 }
@@ -368,8 +368,8 @@ void MetavoxelSystem::render() { · 36 unmodified lines
368368 glRotatef(glm::degrees(glm::angle(batch.rotation)), axis.x, axis.y, axis.z);
369369 glScalef(batch.scale.x, batch.scale.y, batch.scale.z);
370370
371− batch.vertexBuffer->bind();
372− batch.indexBuffer->bind();
371+ glBindBuffer(GL_ARRAY_BUFFER, batch.vertexBufferID);
372+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, batch.indexBufferID);
373373
374374 VoxelPoint* point = 0;
375375 glVertexPointer(3, GL_FLOAT, sizeof(VoxelPoint), &point->vertex);
@@ -378,8 +378,8 @@ void MetavoxelSystem::render() { · 2 unmodified lines
378378
379379 glDrawRangeElements(GL_QUADS, 0, batch.vertexCount - 1, batch.indexCount, GL_UNSIGNED_INT, 0);
380380
381− batch.vertexBuffer->release();
382− batch.indexBuffer->release();
381+ glBindBuffer(GL_ARRAY_BUFFER, 0);
382+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
383383
384384 glPopMatrix();
385385 }
@@ -409,8 +409,8 @@ void MetavoxelSystem::render() { · 23 unmodified lines
409409 glRotatef(glm::degrees(glm::angle(batch.rotation)), axis.x, axis.y, axis.z);
410410 glScalef(batch.scale.x, batch.scale.y, batch.scale.z);
411411
412− batch.vertexBuffer->bind();
413− batch.indexBuffer->bind();
412+ glBindBuffer(GL_ARRAY_BUFFER, batch.vertexBufferID);
413+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, batch.indexBufferID);
414414
415415 VoxelPoint* point = 0;
416416 glVertexPointer(3, GL_FLOAT, sizeof(VoxelPoint), &point->vertex);
@@ -453,8 +453,8 @@ void MetavoxelSystem::render() { · 36 unmodified lines
453453
454454 glActiveTexture(GL_TEXTURE0);
455455
456− batch.vertexBuffer->release();
457− batch.indexBuffer->release();
456+ glBindBuffer(GL_ARRAY_BUFFER, 0);
457+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
458458
459459 glPopMatrix();
460460 }
@@ -492,13 +492,13 @@ void MetavoxelSystem::render() { · 31 unmodified lines
492492 glRotatef(glm::degrees(glm::angle(batch.rotation)), axis.x, axis.y, axis.z);
493493 glScalef(batch.scale.x, batch.scale.y, batch.scale.z);
494494
495− batch.vertexBuffer->bind();
495+ glBindBuffer(GL_ARRAY_BUFFER, batch.vertexBufferID);
496496
497497 glVertexPointer(3, GL_FLOAT, 0, 0);
498498
499499 glDrawArrays(GL_LINES, 0, batch.vertexCount);
500500
501− batch.vertexBuffer->release();
501+ glBindBuffer(GL_ARRAY_BUFFER, 0);
502502
503503 glPopMatrix();
504504 }
@@ -547,6 +547,12 @@ void MetavoxelSystem::deleteTextures(int heightTextureID, int colorTextureID, in · 42 unmodified lines
547547 glDeleteTextures(1, (const GLuint*)&materialTextureID);
548548 }
549549
550+void MetavoxelSystem::deleteBuffers(int vertexBufferID, int indexBufferID, int hermiteBufferID) const {
551+ glDeleteBuffers(1, (const GLuint*)&vertexBufferID);
552+ glDeleteBuffers(1, (const GLuint*)&indexBufferID);
553+ glDeleteBuffers(1, (const GLuint*)&hermiteBufferID);
554+}
555+
550556 class SpannerRenderVisitor : public SpannerVisitor {
551557 public:
552558
@@ -649,8 +655,8 @@ void MetavoxelSystem::renderHeightfieldCursor(const glm::vec3& position, float r · 96 unmodified lines
649655 glRotatef(glm::degrees(glm::angle(batch.rotation)), axis.x, axis.y, axis.z);
650656 glScalef(batch.scale.x, batch.scale.y, batch.scale.z);
651657
652− batch.vertexBuffer->bind();
653− batch.indexBuffer->bind();
658+ glBindBuffer(GL_ARRAY_BUFFER, batch.vertexBufferID);
659+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, batch.indexBufferID);
654660
655661 HeightfieldPoint* point = 0;
656662 glVertexPointer(3, GL_FLOAT, sizeof(HeightfieldPoint), &point->vertex);
@@ -662,8 +668,8 @@ void MetavoxelSystem::renderHeightfieldCursor(const glm::vec3& position, float r · 5 unmodified lines
662668
663669 glBindTexture(GL_TEXTURE_2D, 0);
664670
665− batch.vertexBuffer->release();
666− batch.indexBuffer->release();
671+ glBindBuffer(GL_ARRAY_BUFFER, 0);
672+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
667673
668674 glPopMatrix();
669675 }
@@ -687,16 +693,16 @@ void MetavoxelSystem::renderHeightfieldCursor(const glm::vec3& position, float r · 17 unmodified lines
687693 glRotatef(glm::degrees(glm::angle(batch.rotation)), axis.x, axis.y, axis.z);
688694 glScalef(batch.scale.x, batch.scale.y, batch.scale.z);
689695
690− batch.vertexBuffer->bind();
691− batch.indexBuffer->bind();
696+ glBindBuffer(GL_ARRAY_BUFFER, batch.vertexBufferID);
697+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, batch.indexBufferID);
692698
693699 VoxelPoint* point = 0;
694700 glVertexPointer(3, GL_FLOAT, sizeof(VoxelPoint), &point->vertex);
695701
696702 glDrawRangeElements(GL_QUADS, 0, batch.vertexCount - 1, batch.indexCount, GL_UNSIGNED_INT, 0);
697703
698− batch.vertexBuffer->release();
699− batch.indexBuffer->release();
704+ glBindBuffer(GL_ARRAY_BUFFER, 0);
705+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
700706
701707 glPopMatrix();
702708 }
@@ -1033,10 +1039,17 @@ VoxelBuffer::VoxelBuffer(const QVector<VoxelPoint>& vertices, const QVector<int> · 330 unmodified lines
10331039 _vertexCount(vertices.size()),
10341040 _indexCount(indices.size()),
10351041 _hermiteCount(hermite.size()),
1036− _indexBuffer(QOpenGLBuffer::IndexBuffer),
1042+ _vertexBufferID(0),
1043+ _indexBufferID(0),
1044+ _hermiteBufferID(0),
10371045 _materials(materials) {
10381046 }
10391047
1048+VoxelBuffer::~VoxelBuffer() {
1049+ QMetaObject::invokeMethod(Application::getInstance()->getMetavoxels(), "deleteBuffers", Q_ARG(int, _vertexBufferID),
1050+ Q_ARG(int, _indexBufferID), Q_ARG(int, _hermiteBufferID));
1051+}
1052+
10401053 bool VoxelBuffer::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction,
10411054 float boundsDistance, float& distance) const {
10421055 float highest = _size - 1.0f;
@@ -1106,16 +1119,16 @@ bool VoxelBuffer::findRayIntersection(const glm::vec3& origin, const glm::vec3& · 63 unmodified lines
11061119 }
11071120
11081121 void VoxelBuffer::render(const glm::vec3& translation, const glm::quat& rotation, const glm::vec3& scale, bool cursor) {
1109− if (!_vertexBuffer.isCreated()) {
1110− _vertexBuffer.create();
1111− _vertexBuffer.bind();
1112− _vertexBuffer.allocate(_vertices.constData(), _vertices.size() * sizeof(VoxelPoint));
1113− _vertexBuffer.release();
1122+ if (_vertexBufferID == 0) {
1123+ glGenBuffers(1, &_vertexBufferID);
1124+ glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferID);
1125+ glBufferData(GL_ARRAY_BUFFER, _vertices.size() * sizeof(VoxelPoint), _vertices.constData(), GL_STATIC_DRAW);
1126+ glBindBuffer(GL_ARRAY_BUFFER, 0);
11141127
1115− _indexBuffer.create();
1116− _indexBuffer.bind();
1117− _indexBuffer.allocate(_indices.constData(), _indices.size() * sizeof(int));
1118− _indexBuffer.release();
1128+ glGenBuffers(1, &_indexBufferID);
1129+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferID);
1130+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, _indices.size() * sizeof(int), _indices.constData(), GL_STATIC_DRAW);
1131+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
11191132
11201133 if (!_materials.isEmpty()) {
11211134 _networkTextures.resize(_materials.size());
@@ -1134,8 +1147,8 @@ void VoxelBuffer::render(const glm::vec3& translation, const glm::quat& rotation · 12 unmodified lines
11341147 baseBatch.translation = translation;
11351148 baseBatch.rotation = rotation;
11361149 baseBatch.scale = scale;
1137− baseBatch.vertexBuffer = &_vertexBuffer;
1138− baseBatch.indexBuffer = &_indexBuffer;
1150+ baseBatch.vertexBufferID = _vertexBufferID;
1151+ baseBatch.indexBufferID = _indexBufferID;
11391152 baseBatch.vertexCount = _vertexCount;
11401153 baseBatch.indexCount = _indexCount;
11411154 Application::getInstance()->getMetavoxels()->addVoxelBaseBatch(baseBatch);
@@ -1145,8 +1158,8 @@ void VoxelBuffer::render(const glm::vec3& translation, const glm::quat& rotation · 3 unmodified lines
11451158 splatBatch.translation = translation;
11461159 splatBatch.rotation = rotation;
11471160 splatBatch.scale = scale;
1148− splatBatch.vertexBuffer = &_vertexBuffer;
1149− splatBatch.indexBuffer = &_indexBuffer;
1161+ splatBatch.vertexBufferID = _vertexBufferID;
1162+ splatBatch.indexBufferID = _indexBufferID;
11501163 splatBatch.vertexCount = _vertexCount;
11511164 splatBatch.indexCount = _indexCount;
11521165 splatBatch.splatTextureOffset = glm::vec3(
@@ -1178,18 +1191,18 @@ void VoxelBuffer::render(const glm::vec3& translation, const glm::quat& rotation · 25 unmodified lines
11781191 }
11791192
11801193 if (_hermiteCount > 0) {
1181− if (!_hermiteBuffer.isCreated()) {
1182− _hermiteBuffer.create();
1183− _hermiteBuffer.bind();
1184− _hermiteBuffer.allocate(_hermite.constData(), _hermite.size() * sizeof(glm::vec3));
1185− _hermiteBuffer.release();
1194+ if (_hermiteBufferID == 0) {
1195+ glGenBuffers(1, &_hermiteBufferID);
1196+ glBindBuffer(GL_ARRAY_BUFFER, _hermiteBufferID);
1197+ glBufferData(GL_ARRAY_BUFFER, _hermite.size() * sizeof(glm::vec3), _hermite.constData(), GL_STATIC_DRAW);
1198+ glBindBuffer(GL_ARRAY_BUFFER, 0);
11861199 _hermite.clear();
11871200 }
11881201 HermiteBatch hermiteBatch;
11891202 hermiteBatch.translation = translation;
11901203 hermiteBatch.rotation = rotation;
11911204 hermiteBatch.scale = scale;
1192− hermiteBatch.vertexBuffer = &_hermiteBuffer;
1205+ hermiteBatch.vertexBufferID = _hermiteBufferID;
11931206 hermiteBatch.vertexCount = _hermiteCount;
11941207 Application::getInstance()->getMetavoxels()->addHermiteBatch(hermiteBatch);
11951208 }
@@ -1741,29 +1754,19 @@ void HeightfieldNodeRenderer::render(const HeightfieldNodePointer& node, const g · 545 unmodified lines
17411754 const int NO_CORNERS = 0;
17421755 const int ALL_CORNERS = UPPER_LEFT_CORNER | UPPER_RIGHT_CORNER | LOWER_LEFT_CORNER | LOWER_RIGHT_CORNER;
17431756 int corners = NO_CORNERS;
1744− int stitchMinimumY = INT_MAX, stitchMaximumY = -1;
17451757 if (heightfieldHeight != 0.0f) {
17461758 corners |= UPPER_LEFT_CORNER;
1747− stitchMinimumY = qMin(stitchMinimumY, (int)heightfieldHeight);
1748− stitchMaximumY = qMax(stitchMaximumY, (int)heightfieldHeight);
17491759 }
17501760 if (nextHeightfieldHeightX != 0.0f && x != stackWidth) {
17511761 corners |= UPPER_RIGHT_CORNER;
1752− stitchMinimumY = qMin(stitchMinimumY, (int)nextHeightfieldHeightX);
1753− stitchMaximumY = qMax(stitchMaximumY, (int)nextHeightfieldHeightX);
17541762 }
17551763 if (nextHeightfieldHeightZ != 0.0f && z != stackHeight) {
17561764 corners |= LOWER_LEFT_CORNER;
1757− stitchMinimumY = qMin(stitchMinimumY, (int)nextHeightfieldHeightZ);
1758− stitchMaximumY = qMax(stitchMaximumY, (int)nextHeightfieldHeightZ);
17591765 }
17601766 if (nextHeightfieldHeightXZ != 0.0f && x != stackWidth && z != stackHeight) {
17611767 corners |= LOWER_RIGHT_CORNER;
1762− stitchMinimumY = qMin(stitchMinimumY, (int)nextHeightfieldHeightXZ);
1763− stitchMaximumY = qMax(stitchMaximumY, (int)nextHeightfieldHeightXZ);
17641768 }
17651769 bool stitchable = x != 0 && z != 0 && !(corners == NO_CORNERS || corners == ALL_CORNERS);
1766− bool stitched = false;
17671770 VoxelPoint cornerPoints[4];
17681771 int clampedX = qMax(x - 1, 0), clampedZ = qMax(z - 1, 0);
17691772 if (stitchable) {
@@ -2105,7 +2108,7 @@ void HeightfieldNodeRenderer::render(const HeightfieldNodePointer& node, const g · 335 unmodified lines
21052108 vertices.append(point);
21062109 }
21072110
2108− if (stitchable && !stitched && y >= stitchMinimumY && y <= stitchMaximumY) {
2111+ if (stitchable) {
21092112 int nextIndex = vertices.size();
21102113 const NormalIndex& previousIndexX = lastIndicesX.getClosest(y);
21112114 const NormalIndex& previousIndexZ = lastIndicesZ[x].getClosest(y);
@@ -2270,7 +2273,6 @@ void HeightfieldNodeRenderer::render(const HeightfieldNodePointer& node, const g · 158 unmodified lines
22702273 break;
22712274 }
22722275 }
2273− stitched = true;
22742276 }
22752277 }
22762278
@@ -2395,8 +2397,8 @@ void HeightfieldNodeRenderer::render(const HeightfieldNodePointer& node, const g · 118 unmodified lines
23952397 }
23962398
23972399 HeightfieldBaseLayerBatch baseBatch;
2398− baseBatch.vertexBuffer = &bufferPair.first;
2399− baseBatch.indexBuffer = &bufferPair.second;
2400+ baseBatch.vertexBufferID = bufferPair.first.bufferId();
2401+ baseBatch.indexBufferID = bufferPair.second.bufferId();
24002402 baseBatch.translation = translation;
24012403 baseBatch.rotation = rotation;
24022404 baseBatch.scale = scale;
@@ -2410,8 +2412,8 @@ void HeightfieldNodeRenderer::render(const HeightfieldNodePointer& node, const g · 7 unmodified lines
24102412
24112413 if (!(cursor || _networkTextures.isEmpty())) {
24122414 HeightfieldSplatBatch splatBatch;
2413− splatBatch.vertexBuffer = &bufferPair.first;
2414− splatBatch.indexBuffer = &bufferPair.second;
2415+ splatBatch.vertexBufferID = bufferPair.first.bufferId();
2416+ splatBatch.indexBufferID = bufferPair.second.bufferId();
24152417 splatBatch.translation = translation;
24162418 splatBatch.rotation = rotation;
24172419 splatBatch.scale = scale;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t