History

Batch voxel drawing, too.

Andrzej Kapolka committed 11 years ago

Commit 1e294e01 parent da857142 files · +307 −278Browse files

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

Unified · Split

interface/src/MetavoxelSystem.cpp+244−220
@@ -56,12 +56,50 @@ MetavoxelSystem::~MetavoxelSystem() { · 55 unmodified lines
5656
5757 void MetavoxelSystem::init() {
5858 MetavoxelClientManager::init();
59− DefaultMetavoxelRendererImplementation::init();
6059
6160 _voxelBufferAttribute = AttributeRegistry::getInstance()->registerAttribute(
6261 new BufferDataAttribute("voxelBuffer"));
6362 _voxelBufferAttribute->setLODThresholdMultiplier(
6463 AttributeRegistry::getInstance()->getVoxelColorAttribute()->getLODThresholdMultiplier());
64+
65+ _baseHeightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
66+ "shaders/metavoxel_heightfield_base.vert");
67+ _baseHeightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
68+ "shaders/metavoxel_heightfield_base.frag");
69+ _baseHeightfieldProgram.link();
70+
71+ _baseHeightfieldProgram.bind();
72+ _baseHeightfieldProgram.setUniformValue("heightMap", 0);
73+ _baseHeightfieldProgram.setUniformValue("diffuseMap", 1);
74+ _baseHeightScaleLocation = _baseHeightfieldProgram.uniformLocation("heightScale");
75+ _baseColorScaleLocation = _baseHeightfieldProgram.uniformLocation("colorScale");
76+ _baseHeightfieldProgram.release();
77+
78+ loadSplatProgram("heightfield", _splatHeightfieldProgram, _splatHeightfieldLocations);
79+
80+ _heightfieldCursorProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
81+ "shaders/metavoxel_heightfield_cursor.vert");
82+ _heightfieldCursorProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
83+ "shaders/metavoxel_cursor.frag");
84+ _heightfieldCursorProgram.link();
85+
86+ _heightfieldCursorProgram.bind();
87+ _heightfieldCursorProgram.setUniformValue("heightMap", 0);
88+ _heightfieldCursorProgram.release();
89+
90+ _baseVoxelProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
91+ "shaders/metavoxel_voxel_base.vert");
92+ _baseVoxelProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
93+ "shaders/metavoxel_voxel_base.frag");
94+ _baseVoxelProgram.link();
95+
96+ loadSplatProgram("voxel", _splatVoxelProgram, _splatVoxelLocations);
97+
98+ _voxelCursorProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
99+ "shaders/metavoxel_voxel_cursor.vert");
100+ _voxelCursorProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
101+ "shaders/metavoxel_cursor.frag");
102+ _voxelCursorProgram.link();
65103 }
66104
67105 MetavoxelLOD MetavoxelSystem::getLOD() {
@@ -175,7 +213,7 @@ void MetavoxelSystem::render() { · 107 unmodified lines
175213
176214 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
177215
178− DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().bind();
216+ _baseHeightfieldProgram.bind();
179217
180218 foreach (const HeightfieldBaseBatch& batch, _heightfieldBaseBatches) {
181219 glPushMatrix();
@@ -193,10 +231,8 @@ void MetavoxelSystem::render() { · 11 unmodified lines
193231
194232 glBindTexture(GL_TEXTURE_2D, batch.heightTextureID);
195233
196− DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().setUniform(
197− DefaultMetavoxelRendererImplementation::getBaseHeightScaleLocation(), batch.heightScale);
198− DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().setUniform(
199− DefaultMetavoxelRendererImplementation::getBaseColorScaleLocation(), batch.colorScale);
234+ _baseHeightfieldProgram.setUniform(_baseHeightScaleLocation, batch.heightScale);
235+ _baseHeightfieldProgram.setUniform(_baseColorScaleLocation, batch.colorScale);
200236
201237 glActiveTexture(GL_TEXTURE1);
202238 glBindTexture(GL_TEXTURE_2D, batch.colorTextureID);
@@ -208,12 +244,15 @@ void MetavoxelSystem::render() { · 5 unmodified lines
208244 glActiveTexture(GL_TEXTURE0);
209245 glBindTexture(GL_TEXTURE_2D, 0);
210246
247+ batch.vertexBuffer->release();
248+ batch.indexBuffer->release();
249+
211250 glPopMatrix();
212251 }
213252
214253 Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, false);
215254
216− DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().release();
255+ _baseHeightfieldProgram.release();
217256
218257 glDisable(GL_ALPHA_TEST);
219258 glEnable(GL_BLEND);
@@ -224,10 +263,8 @@ void MetavoxelSystem::render() { · 4 unmodified lines
224263 glEnable(GL_POLYGON_OFFSET_FILL);
225264 glPolygonOffset(-1.0f, -1.0f);
226265
227− DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().bind();
228− const DefaultMetavoxelRendererImplementation::SplatLocations& locations =
229− DefaultMetavoxelRendererImplementation::getSplatHeightfieldLocations();
230−
266+ _splatHeightfieldProgram.bind();
267+
231268 foreach (const HeightfieldSplatBatch& batch, _heightfieldSplatBatches) {
232269 glPushMatrix();
233270 glTranslatef(batch.translation.x, batch.translation.y, batch.translation.z);
@@ -244,26 +281,22 @@ void MetavoxelSystem::render() { · 10 unmodified lines
244281
245282 glBindTexture(GL_TEXTURE_2D, batch.heightTextureID);
246283
247− DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue(
248− locations.heightScale, batch.heightScale.x, batch.heightScale.y);
249− DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniform(
250− locations.textureScale, batch.textureScale);
251− DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniform(locations.splatTextureOffset,
252− batch.splatTextureOffset);
284+ _splatHeightfieldProgram.setUniformValue(_splatHeightfieldLocations.heightScale,
285+ batch.heightScale.x, batch.heightScale.y);
286+ _splatHeightfieldProgram.setUniform(_splatHeightfieldLocations.textureScale, batch.textureScale);
287+ _splatHeightfieldProgram.setUniform(_splatHeightfieldLocations.splatTextureOffset, batch.splatTextureOffset);
253288
254289 const float QUARTER_STEP = 0.25f * EIGHT_BIT_MAXIMUM_RECIPROCAL;
255− DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniform(
256− locations.splatTextureScalesS, batch.splatTextureScalesS);
257− DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniform(
258− locations.splatTextureScalesT, batch.splatTextureScalesT);
259− DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue(
260− locations.textureValueMinima,
290+ _splatHeightfieldProgram.setUniform(_splatHeightfieldLocations.splatTextureScalesS, batch.splatTextureScalesS);
291+ _splatHeightfieldProgram.setUniform(_splatHeightfieldLocations.splatTextureScalesT, batch.splatTextureScalesT);
292+ _splatHeightfieldProgram.setUniformValue(
293+ _splatHeightfieldLocations.textureValueMinima,
261294 (batch.materialIndex + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP,
262295 (batch.materialIndex + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP,
263296 (batch.materialIndex + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP,
264297 (batch.materialIndex + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP);
265− DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue(
266− locations.textureValueMaxima,
298+ _splatHeightfieldProgram.setUniformValue(
299+ _splatHeightfieldLocations.textureValueMaxima,
267300 (batch.materialIndex + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP,
268301 (batch.materialIndex + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP,
269302 (batch.materialIndex + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP,
@@ -290,10 +323,13 @@ void MetavoxelSystem::render() { · 20 unmodified lines
290323 glActiveTexture(GL_TEXTURE0);
291324 glBindTexture(GL_TEXTURE_2D, 0);
292325
326+ batch.vertexBuffer->release();
327+ batch.indexBuffer->release();
328+
293329 glPopMatrix();
294330 }
295331
296− DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().release();
332+ _splatHeightfieldProgram.release();
297333
298334 glDisable(GL_POLYGON_OFFSET_FILL);
299335 glDepthMask(true);
@@ -310,6 +346,121 @@ void MetavoxelSystem::render() { · 10 unmodified lines
310346 _heightfieldBaseBatches.clear();
311347 }
312348
349+ if (!_voxelBaseBatches.isEmpty()) {
350+ Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, true);
351+
352+ glEnableClientState(GL_VERTEX_ARRAY);
353+ glDisable(GL_BLEND);
354+ glEnable(GL_CULL_FACE);
355+ glEnable(GL_ALPHA_TEST);
356+ glAlphaFunc(GL_EQUAL, 0.0f);
357+
358+ glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
359+
360+ glEnableClientState(GL_COLOR_ARRAY);
361+ glEnableClientState(GL_NORMAL_ARRAY);
362+
363+ _baseVoxelProgram.bind();
364+
365+ foreach (const VoxelBaseBatch& batch, _voxelBaseBatches) {
366+ batch.vertexBuffer->bind();
367+ batch.indexBuffer->bind();
368+
369+ VoxelPoint* point = 0;
370+ glVertexPointer(3, GL_FLOAT, sizeof(VoxelPoint), &point->vertex);
371+ glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(VoxelPoint), &point->color);
372+ glNormalPointer(GL_BYTE, sizeof(VoxelPoint), &point->normal);
373+
374+ glDrawRangeElements(GL_QUADS, 0, batch.vertexCount - 1, batch.indexCount, GL_UNSIGNED_INT, 0);
375+
376+ batch.vertexBuffer->release();
377+ batch.indexBuffer->release();
378+ }
379+
380+ _baseVoxelProgram.release();
381+
382+ glDisable(GL_ALPHA_TEST);
383+ glEnable(GL_BLEND);
384+
385+ Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, false);
386+
387+ if (!_voxelSplatBatches.isEmpty()) {
388+ glDepthFunc(GL_LEQUAL);
389+ glDepthMask(false);
390+ glEnable(GL_POLYGON_OFFSET_FILL);
391+ glPolygonOffset(-1.0f, -1.0f);
392+
393+ _splatVoxelProgram.bind();
394+
395+ _splatVoxelProgram.enableAttributeArray(_splatVoxelLocations.materials);
396+ _splatVoxelProgram.enableAttributeArray(_splatVoxelLocations.materialWeights);
397+
398+ foreach (const VoxelSplatBatch& batch, _voxelSplatBatches) {
399+ batch.vertexBuffer->bind();
400+ batch.indexBuffer->bind();
401+
402+ VoxelPoint* point = 0;
403+ glVertexPointer(3, GL_FLOAT, sizeof(VoxelPoint), &point->vertex);
404+ glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(VoxelPoint), &point->color);
405+ glNormalPointer(GL_BYTE, sizeof(VoxelPoint), &point->normal);
406+
407+ _splatVoxelProgram.setAttributeBuffer(_splatVoxelLocations.materials,
408+ GL_UNSIGNED_BYTE, (qint64)&point->materials, SPLAT_COUNT, sizeof(VoxelPoint));
409+ _splatVoxelProgram.setAttributeBuffer(_splatVoxelLocations.materialWeights,
410+ GL_UNSIGNED_BYTE, (qint64)&point->materialWeights, SPLAT_COUNT, sizeof(VoxelPoint));
411+
412+ const float QUARTER_STEP = 0.25f * EIGHT_BIT_MAXIMUM_RECIPROCAL;
413+ _splatVoxelProgram.setUniform(_splatVoxelLocations.splatTextureScalesS, batch.splatTextureScalesS);
414+ _splatVoxelProgram.setUniform(_splatVoxelLocations.splatTextureScalesT, batch.splatTextureScalesT);
415+ _splatVoxelProgram.setUniformValue(
416+ _splatVoxelLocations.textureValueMinima,
417+ (batch.materialIndex + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP,
418+ (batch.materialIndex + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP,
419+ (batch.materialIndex + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP,
420+ (batch.materialIndex + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP);
421+ _splatVoxelProgram.setUniformValue(
422+ _splatVoxelLocations.textureValueMaxima,
423+ (batch.materialIndex + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP,
424+ (batch.materialIndex + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP,
425+ (batch.materialIndex + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP,
426+ (batch.materialIndex + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP);
427+
428+ for (int i = 0; i < SPLAT_COUNT; i++) {
429+ glActiveTexture(GL_TEXTURE0 + SPLAT_TEXTURE_UNITS[i]);
430+ glBindTexture(GL_TEXTURE_2D, batch.splatTextureIDs[i]);
431+ }
432+
433+ glDrawRangeElements(GL_QUADS, 0, batch.vertexCount - 1, batch.indexCount, GL_UNSIGNED_INT, 0);
434+
435+ for (int i = 0; i < SPLAT_COUNT; i++) {
436+ glActiveTexture(GL_TEXTURE0 + SPLAT_TEXTURE_UNITS[i]);
437+ glBindTexture(GL_TEXTURE_2D, 0);
438+ }
439+
440+ glActiveTexture(GL_TEXTURE0);
441+
442+ batch.vertexBuffer->release();
443+ batch.indexBuffer->release();
444+ }
445+
446+ glDisable(GL_POLYGON_OFFSET_FILL);
447+ glDepthMask(true);
448+ glDepthFunc(GL_LESS);
449+
450+ _splatVoxelProgram.disableAttributeArray(_splatVoxelLocations.materials);
451+ _splatVoxelProgram.disableAttributeArray(_splatVoxelLocations.materialWeights);
452+
453+ _voxelSplatBatches.clear();
454+ }
455+
456+ glDisableClientState(GL_VERTEX_ARRAY);
457+ glDisableClientState(GL_COLOR_ARRAY);
458+ glDisableClientState(GL_NORMAL_ARRAY);
459+ glDisable(GL_CULL_FACE);
460+
461+ _voxelBaseBatches.clear();
462+ }
463+
313464 // give external parties a chance to join in
314465 emit rendering();
315466 }
@@ -440,7 +591,7 @@ void MetavoxelSystem::renderHeightfieldCursor(const glm::vec3& position, float r · 124 unmodified lines
440591 glEnableClientState(GL_VERTEX_ARRAY);
441592 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
442593
443− DefaultMetavoxelRendererImplementation::getHeightfieldCursorProgram().bind();
594+ _heightfieldCursorProgram.bind();
444595
445596 glActiveTexture(GL_TEXTURE4);
446597 float scale = 1.0f / radius;
@@ -456,7 +607,7 @@ void MetavoxelSystem::renderHeightfieldCursor(const glm::vec3& position, float r · 9 unmodified lines
456607 SpannerCursorRenderVisitor visitor(Box(position - extents, position + extents));
457608 guide(visitor);
458609
459− DefaultMetavoxelRendererImplementation::getHeightfieldCursorProgram().release();
610+ _heightfieldCursorProgram.release();
460611
461612 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
462613 glDisableClientState(GL_VERTEX_ARRAY);
@@ -504,7 +655,7 @@ void MetavoxelSystem::renderVoxelCursor(const glm::vec3& position, float radius) · 41 unmodified lines
504655
505656 glEnableClientState(GL_VERTEX_ARRAY);
506657
507− DefaultMetavoxelRendererImplementation::getVoxelCursorProgram().bind();
658+ _voxelCursorProgram.bind();
508659
509660 glActiveTexture(GL_TEXTURE4);
510661 float scale = 1.0f / radius;
@@ -521,16 +672,16 @@ void MetavoxelSystem::renderVoxelCursor(const glm::vec3& position, float radius) · 10 unmodified lines
521672 BufferCursorRenderVisitor voxelVisitor(Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute(), bounds);
522673 guideToAugmented(voxelVisitor);
523674
524− DefaultMetavoxelRendererImplementation::getVoxelCursorProgram().release();
675+ _voxelCursorProgram.release();
525676
526677 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
527678
528− DefaultMetavoxelRendererImplementation::getHeightfieldCursorProgram().bind();
679+ _heightfieldCursorProgram.bind();
529680
530681 SpannerCursorRenderVisitor spannerVisitor(bounds);
531682 guide(spannerVisitor);
532683
533− DefaultMetavoxelRendererImplementation::getHeightfieldCursorProgram().release();
684+ _heightfieldCursorProgram.release();
534685
535686 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
536687
@@ -612,6 +763,29 @@ void MetavoxelSystem::guideToAugmented(MetavoxelVisitor& visitor, bool render) { · 75 unmodified lines
612763 }
613764 }
614765
766+void MetavoxelSystem::loadSplatProgram(const char* type, ProgramObject& program, SplatLocations& locations) {
767+ program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
768+ "shaders/metavoxel_" + type + "_splat.vert");
769+ program.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
770+ "shaders/metavoxel_" + type + "_splat.frag");
771+ program.link();
772+
773+ program.bind();
774+ program.setUniformValue("heightMap", 0);
775+ program.setUniformValue("textureMap", 1);
776+ program.setUniformValueArray("diffuseMaps", SPLAT_TEXTURE_UNITS, SPLAT_COUNT);
777+ locations.heightScale = program.uniformLocation("heightScale");
778+ locations.textureScale = program.uniformLocation("textureScale");
779+ locations.splatTextureOffset = program.uniformLocation("splatTextureOffset");
780+ locations.splatTextureScalesS = program.uniformLocation("splatTextureScalesS");
781+ locations.splatTextureScalesT = program.uniformLocation("splatTextureScalesT");
782+ locations.textureValueMinima = program.uniformLocation("textureValueMinima");
783+ locations.textureValueMaxima = program.uniformLocation("textureValueMaxima");
784+ locations.materials = program.attributeLocation("materials");
785+ locations.materialWeights = program.attributeLocation("materialWeights");
786+ program.release();
787+}
788+
615789 Throttle::Throttle() :
616790 _limit(INT_MAX),
617791 _total(0) {
@@ -946,10 +1120,12 @@ void VoxelBuffer::render(bool cursor) { · 328 unmodified lines
9461120 _vertexBuffer.create();
9471121 _vertexBuffer.bind();
9481122 _vertexBuffer.allocate(_vertices.constData(), _vertices.size() * sizeof(VoxelPoint));
1123+ _vertexBuffer.release();
9491124
9501125 _indexBuffer.create();
9511126 _indexBuffer.bind();
9521127 _indexBuffer.allocate(_indices.constData(), _indices.size() * sizeof(int));
1128+ _indexBuffer.release();
9531129
9541130 if (!_materials.isEmpty()) {
9551131 _networkTextures.resize(_materials.size());
@@ -961,101 +1137,62 @@ void VoxelBuffer::render(bool cursor) { · 5 unmodified lines
9611137 }
9621138 }
9631139 }
964− } else {
1140+ }
1141+
1142+ if (cursor) {
9651143 _vertexBuffer.bind();
9661144 _indexBuffer.bind();
1145+
1146+ VoxelPoint* point = 0;
1147+ glVertexPointer(3, GL_FLOAT, sizeof(VoxelPoint), &point->vertex);
1148+ glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(VoxelPoint), &point->color);
1149+ glNormalPointer(GL_BYTE, sizeof(VoxelPoint), &point->normal);
1150+
1151+ glDrawRangeElements(GL_QUADS, 0, _vertexCount - 1, _indexCount, GL_UNSIGNED_INT, 0);
1152+
1153+ _vertexBuffer.release();
1154+ _indexBuffer.release();
1155+ return;
9671156 }
9681157
969− VoxelPoint* point = 0;
970− glVertexPointer(3, GL_FLOAT, sizeof(VoxelPoint), &point->vertex);
971− glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(VoxelPoint), &point->color);
972− glNormalPointer(GL_BYTE, sizeof(VoxelPoint), &point->normal);
1158+ VoxelBaseBatch baseBatch;
1159+ baseBatch.vertexBuffer = &_vertexBuffer;
1160+ baseBatch.indexBuffer = &_indexBuffer;
1161+ baseBatch.vertexCount = _vertexCount;
1162+ baseBatch.indexCount = _indexCount;
1163+ Application::getInstance()->getMetavoxels()->addVoxelBaseBatch(baseBatch);
9731164
974− glDrawRangeElements(GL_QUADS, 0, _vertexCount - 1, _indexCount, GL_UNSIGNED_INT, 0);
1165+ if (!_materials.isEmpty()) {
1166+ VoxelSplatBatch splatBatch;
1167+ splatBatch.vertexBuffer = &_vertexBuffer;
1168+ splatBatch.indexBuffer = &_indexBuffer;
1169+ splatBatch.vertexCount = _vertexCount;
1170+ splatBatch.indexCount = _indexCount;
9751171
976− if (!(_materials.isEmpty() || cursor)) {
977− Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, false);
978−
979− glDepthFunc(GL_LEQUAL);
980− glDepthMask(false);
981− glEnable(GL_BLEND);
982− glDisable(GL_ALPHA_TEST);
983− glEnable(GL_POLYGON_OFFSET_FILL);
984− glPolygonOffset(-1.0f, -1.0f);
985−
986− DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().bind();
987− const DefaultMetavoxelRendererImplementation::SplatLocations& locations =
988− DefaultMetavoxelRendererImplementation::getSplatVoxelLocations();
989−
990− DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setAttributeBuffer(locations.materials,
991− GL_UNSIGNED_BYTE, (qint64)&point->materials, SPLAT_COUNT, sizeof(VoxelPoint));
992− DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().enableAttributeArray(locations.materials);
993−
994− DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setAttributeBuffer(locations.materialWeights,
995− GL_UNSIGNED_BYTE, (qint64)&point->materialWeights, SPLAT_COUNT, sizeof(VoxelPoint));
996− DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().enableAttributeArray(locations.materialWeights);
997−
9981172 for (int i = 0; i < _materials.size(); i += SPLAT_COUNT) {
999− QVector4D scalesS, scalesT;
1000−
10011173 for (int j = 0; j < SPLAT_COUNT; j++) {
1002− glActiveTexture(GL_TEXTURE0 + SPLAT_TEXTURE_UNITS[j]);
10031174 int index = i + j;
10041175 if (index < _networkTextures.size()) {
10051176 const NetworkTexturePointer& texture = _networkTextures.at(index);
10061177 if (texture) {
10071178 MaterialObject* material = static_cast<MaterialObject*>(_materials.at(index).data());
1008− scalesS[j] = 1.0f / material->getScaleS();
1009− scalesT[j] = 1.0f / material->getScaleT();
1010− glBindTexture(GL_TEXTURE_2D, texture->getID());
1179+ splatBatch.splatTextureScalesS[j] = 1.0f / material->getScaleS();
1180+ splatBatch.splatTextureScalesT[j] = 1.0f / material->getScaleT();
1181+ splatBatch.splatTextureIDs[j] = texture->getID();
1182+
10111183 } else {
1012− glBindTexture(GL_TEXTURE_2D, 0);
1184+ splatBatch.splatTextureIDs[j] = 0;
10131185 }
10141186 } else {
1015− glBindTexture(GL_TEXTURE_2D, 0);
1187+ splatBatch.splatTextureIDs[j] = 0;
10161188 }
10171189 }
1018− const float QUARTER_STEP = 0.25f * EIGHT_BIT_MAXIMUM_RECIPROCAL;
1019− DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setUniformValue(
1020− locations.splatTextureScalesS, scalesS);
1021− DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setUniformValue(
1022− locations.splatTextureScalesT, scalesT);
1023− DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setUniformValue(
1024− locations.textureValueMinima,
1025− (i + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, (i + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP,
1026− (i + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, (i + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP);
1027− DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setUniformValue(
1028− locations.textureValueMaxima,
1029− (i + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, (i + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP,
1030− (i + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, (i + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP);
1031− glDrawRangeElements(GL_QUADS, 0, _vertexCount - 1, _indexCount, GL_UNSIGNED_INT, 0);
1032− }
1033−
1034− for (int i = 0; i < SPLAT_COUNT; i++) {
1035− glActiveTexture(GL_TEXTURE0 + SPLAT_TEXTURE_UNITS[i]);
1036− glBindTexture(GL_TEXTURE_2D, 0);
1190+ splatBatch.materialIndex = i;
1191+ Application::getInstance()->getMetavoxels()->addVoxelSplatBatch(splatBatch);
10371192 }
1038−
1039− glActiveTexture(GL_TEXTURE0);
1040−
1041− glDisable(GL_POLYGON_OFFSET_FILL);
1042− glEnable(GL_ALPHA_TEST);
1043− glDisable(GL_BLEND);
1044− glDepthMask(true);
1045− glDepthFunc(GL_LESS);
1046−
1047− Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, true);
1048−
1049− DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().disableAttributeArray(locations.materials);
1050− DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().disableAttributeArray(locations.materialWeights);
1051−
1052− DefaultMetavoxelRendererImplementation::getBaseVoxelProgram().bind();
10531193 }
10541194
1055− _vertexBuffer.release();
1056− _indexBuffer.release();
1057−
1058− if (_hermiteCount > 0 && Menu::getInstance()->isOptionChecked(MenuOption::DisplayHermiteData) && !cursor) {
1195+ if (_hermiteCount > 0 && Menu::getInstance()->isOptionChecked(MenuOption::DisplayHermiteData)) {
10591196 if (!_hermiteBuffer.isCreated()) {
10601197 _hermiteBuffer.create();
10611198 _hermiteBuffer.bind();
@@ -1066,9 +1203,6 @@ void VoxelBuffer::render(bool cursor) { · 4 unmodified lines
10661203 _hermiteBuffer.bind();
10671204 }
10681205
1069− glDisableClientState(GL_COLOR_ARRAY);
1070− glDisableClientState(GL_NORMAL_ARRAY);
1071−
10721206 glVertexPointer(3, GL_FLOAT, 0, 0);
10731207
10741208 Application::getInstance()->getDeferredLightingEffect()->getSimpleProgram().bind();
@@ -1080,10 +1214,7 @@ void VoxelBuffer::render(bool cursor) { · 5 unmodified lines
10801214
10811215 glDrawArrays(GL_LINES, 0, _hermiteCount);
10821216
1083− DefaultMetavoxelRendererImplementation::getBaseVoxelProgram().bind();
1084−
1085− glEnableClientState(GL_COLOR_ARRAY);
1086− glEnableClientState(GL_NORMAL_ARRAY);
1217+ Application::getInstance()->getDeferredLightingEffect()->getSimpleProgram().release();
10871218
10881219 _hermiteBuffer.release();
10891220 }
@@ -1107,49 +1238,6 @@ AttributeValue BufferDataAttribute::inherit(const AttributeValue& parentValue) c · 17 unmodified lines
11071238 return AttributeValue(parentValue.getAttribute());
11081239 }
11091240
1110−void DefaultMetavoxelRendererImplementation::init() {
1111− if (!_baseHeightfieldProgram.isLinked()) {
1112− _baseHeightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
1113− "shaders/metavoxel_heightfield_base.vert");
1114− _baseHeightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
1115− "shaders/metavoxel_heightfield_base.frag");
1116− _baseHeightfieldProgram.link();
1117−
1118− _baseHeightfieldProgram.bind();
1119− _baseHeightfieldProgram.setUniformValue("heightMap", 0);
1120− _baseHeightfieldProgram.setUniformValue("diffuseMap", 1);
1121− _baseHeightScaleLocation = _baseHeightfieldProgram.uniformLocation("heightScale");
1122− _baseColorScaleLocation = _baseHeightfieldProgram.uniformLocation("colorScale");
1123− _baseHeightfieldProgram.release();
1124−
1125− loadSplatProgram("heightfield", _splatHeightfieldProgram, _splatHeightfieldLocations);
1126−
1127− _heightfieldCursorProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
1128− "shaders/metavoxel_heightfield_cursor.vert");
1129− _heightfieldCursorProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
1130− "shaders/metavoxel_cursor.frag");
1131− _heightfieldCursorProgram.link();
1132−
1133− _heightfieldCursorProgram.bind();
1134− _heightfieldCursorProgram.setUniformValue("heightMap", 0);
1135− _heightfieldCursorProgram.release();
1136−
1137− _baseVoxelProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
1138− "shaders/metavoxel_voxel_base.vert");
1139− _baseVoxelProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
1140− "shaders/metavoxel_voxel_base.frag");
1141− _baseVoxelProgram.link();
1142−
1143− loadSplatProgram("voxel", _splatVoxelProgram, _splatVoxelLocations);
1144−
1145− _voxelCursorProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
1146− "shaders/metavoxel_voxel_cursor.vert");
1147− _voxelCursorProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
1148− "shaders/metavoxel_cursor.frag");
1149− _voxelCursorProgram.link();
1150− }
1151−}
1152−
11531241 DefaultMetavoxelRendererImplementation::DefaultMetavoxelRendererImplementation() {
11541242 }
11551243
@@ -1872,76 +1960,12 @@ void DefaultMetavoxelRendererImplementation::render(MetavoxelData& data, Metavox · 716 unmodified lines
18721960 SpannerRenderVisitor spannerRenderVisitor(lod);
18731961 data.guide(spannerRenderVisitor);
18741962 }
1875−
1876− Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, true);
1877−
1878− glEnableClientState(GL_VERTEX_ARRAY);
1879− glDisable(GL_BLEND);
1880− glEnable(GL_CULL_FACE);
1881− glEnable(GL_ALPHA_TEST);
1882− glAlphaFunc(GL_EQUAL, 0.0f);
1883−
1884− glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
1885−
18861963 if (Menu::getInstance()->isOptionChecked(MenuOption::RenderDualContourSurfaces)) {
1887− glEnableClientState(GL_COLOR_ARRAY);
1888− glEnableClientState(GL_NORMAL_ARRAY);
1889−
1890− _baseVoxelProgram.bind();
1891−
18921964 BufferRenderVisitor voxelRenderVisitor(Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute());
18931965 data.guide(voxelRenderVisitor);
1894−
1895− _baseVoxelProgram.release();
1896−
1897− glDisableClientState(GL_COLOR_ARRAY);
1898− glDisableClientState(GL_NORMAL_ARRAY);
18991966 }
1900−
1901− glDisable(GL_ALPHA_TEST);
1902− glDisable(GL_CULL_FACE);
1903− glEnable(GL_BLEND);
1904−
1905− glDisableClientState(GL_VERTEX_ARRAY);
1906−
1907− Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, false);
1908−}
1909−
1910−void DefaultMetavoxelRendererImplementation::loadSplatProgram(const char* type,
1911− ProgramObject& program, SplatLocations& locations) {
1912− program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
1913− "shaders/metavoxel_" + type + "_splat.vert");
1914− program.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
1915− "shaders/metavoxel_" + type + "_splat.frag");
1916− program.link();
1917−
1918− program.bind();
1919− program.setUniformValue("heightMap", 0);
1920− program.setUniformValue("textureMap", 1);
1921− program.setUniformValueArray("diffuseMaps", SPLAT_TEXTURE_UNITS, SPLAT_COUNT);
1922− locations.heightScale = program.uniformLocation("heightScale");
1923− locations.textureScale = program.uniformLocation("textureScale");
1924− locations.splatTextureOffset = program.uniformLocation("splatTextureOffset");
1925− locations.splatTextureScalesS = program.uniformLocation("splatTextureScalesS");
1926− locations.splatTextureScalesT = program.uniformLocation("splatTextureScalesT");
1927− locations.textureValueMinima = program.uniformLocation("textureValueMinima");
1928− locations.textureValueMaxima = program.uniformLocation("textureValueMaxima");
1929− locations.materials = program.attributeLocation("materials");
1930− locations.materialWeights = program.attributeLocation("materialWeights");
1931− program.release();
19321967 }
19331968
1934−ProgramObject DefaultMetavoxelRendererImplementation::_baseHeightfieldProgram;
1935−int DefaultMetavoxelRendererImplementation::_baseHeightScaleLocation;
1936−int DefaultMetavoxelRendererImplementation::_baseColorScaleLocation;
1937−ProgramObject DefaultMetavoxelRendererImplementation::_splatHeightfieldProgram;
1938−DefaultMetavoxelRendererImplementation::SplatLocations DefaultMetavoxelRendererImplementation::_splatHeightfieldLocations;
1939−ProgramObject DefaultMetavoxelRendererImplementation::_heightfieldCursorProgram;
1940−ProgramObject DefaultMetavoxelRendererImplementation::_baseVoxelProgram;
1941−ProgramObject DefaultMetavoxelRendererImplementation::_splatVoxelProgram;
1942−DefaultMetavoxelRendererImplementation::SplatLocations DefaultMetavoxelRendererImplementation::_splatVoxelLocations;
1943−ProgramObject DefaultMetavoxelRendererImplementation::_voxelCursorProgram;
1944−
19451969 SphereRenderer::SphereRenderer() {
19461970 }
19471971