History

Simpler rendering.

Andrzej Kapolka committed 12 years ago

Commit e05884d1 parent 4f5448f4 files · +108 −123Browse files

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

Unified · Split

interface/src/MetavoxelSystem.cpp+57−78
@@ -90,8 +90,8 @@ public: · 89 unmodified lines
9090
9191 SpannerRenderVisitor::SpannerRenderVisitor() :
9292 SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(),
93− QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannerMaskAttribute(),
94− QVector<AttributePointer>(), QVector<AttributePointer>(), Application::getInstance()->getMetavoxels()->getLOD(),
93+ QVector<AttributePointer>(), QVector<AttributePointer>(), QVector<AttributePointer>(),
94+ Application::getInstance()->getMetavoxels()->getLOD(),
9595 encodeOrder(Application::getInstance()->getViewFrustum()->getDirection())) {
9696 }
9797
@@ -239,53 +239,22 @@ void MetavoxelSystemClient::sendDatagram(const QByteArray& data) { · 141 unmodified lines
239239 Application::getInstance()->getBandwidthMeter()->outputStream(BandwidthMeter::METAVOXELS).updateValue(data.size());
240240 }
241241
242−PointBuffer::PointBuffer(const QVector<BufferPointVectorPair>& levelPoints) :
243− _levelPoints(levelPoints) {
242+PointBuffer::PointBuffer(const BufferPointVector& points) :
243+ _points(points) {
244244 }
245245
246−void PointBuffer::render(int level) {
246+void PointBuffer::render() {
247247 // initalize buffer, etc. on first render
248248 if (!_buffer.isCreated()) {
249− _offsets.append(0);
250− _lastLeafCount = 0;
251− int totalPoints = 0;
252− foreach (const BufferPointVectorPair& pair, _levelPoints) {
253− _offsets.append(totalPoints += ((_lastLeafCount += pair.first.size()) + pair.second.size()));
254− }
255249 _buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
256250 _buffer.create();
257251 _buffer.bind();
258− _buffer.allocate(totalPoints * sizeof(BufferPoint));
259− int offset = 0;
260− for (int i = 0; i < _levelPoints.size(); i++) {
261− // write the internal nodes from the current level
262− const BufferPointVector& internal = _levelPoints.at(i).second;
263− int length = internal.size() * sizeof(BufferPoint);
264− _buffer.write(offset, internal.constData(), length);
265− offset += length;
266−
267− // and the leaves from the top down
268− for (int j = 0; j <= i; j++) {
269− const BufferPointVector& leaves = _levelPoints.at(j).first;
270− length = leaves.size() * sizeof(BufferPoint);
271− _buffer.write(offset, leaves.constData(), length);
272− offset += length;
273− }
274− }
275− _levelPoints.clear();
252+ _pointCount = _points.size();
253+ _buffer.allocate(_points.constData(), _pointCount * sizeof(BufferPoint));
254+ _points.clear();
276255 _buffer.release();
277256 }
278− int first, count;
279− int nextLevel = level + 1;
280− if (nextLevel >= _offsets.size()) {
281− first = _offsets.last() - _lastLeafCount;
282− count = _lastLeafCount;
283−
284− } else {
285− first = _offsets.at(level);
286− count = _offsets.at(nextLevel) - first;
287− }
288− if (count == 0) {
257+ if (_pointCount == 0) {
289258 return;
290259 }
291260 _buffer.bind();
@@ -295,7 +264,7 @@ void PointBuffer::render(int level) { · 3 unmodified lines
295264 glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(BufferPoint), &point->color);
296265 glNormalPointer(GL_BYTE, sizeof(BufferPoint), &point->normal);
297266
298− glDrawArrays(GL_POINTS, first, count);
267+ glDrawArrays(GL_POINTS, 0, _pointCount);
299268
300269 _buffer.release();
301270 }
@@ -304,23 +273,18 @@ PointBufferAttribute::PointBufferAttribute() : · 2 unmodified lines
304273 InlineAttribute<PointBufferPointer>("pointBuffer") {
305274 }
306275
307−MetavoxelNode* PointBufferAttribute::createMetavoxelNode(const AttributeValue& value, const MetavoxelNode* original) const {
308− return new MetavoxelNode(value, original);
309−}
310−
311276 bool PointBufferAttribute::merge(void*& parent, void* children[], bool postRead) const {
312− for (int i = 0; i < MERGE_COUNT; i++) {
313− if (decodeInline<PointBufferPointer>(children[i])) {
277+ PointBufferPointer firstChild = decodeInline<PointBufferPointer>(children[0]);
278+ for (int i = 1; i < MERGE_COUNT; i++) {
279+ if (firstChild != decodeInline<PointBufferPointer>(children[i])) {
280+ *(PointBufferPointer*)&parent = _defaultValue;
314281 return false;
315282 }
316283 }
284+ *(PointBufferPointer*)&parent = firstChild;
317285 return true;
318286 }
319287
320−AttributeValue PointBufferAttribute::inherit(const AttributeValue& parentValue) const {
321− return AttributeValue(parentValue.getAttribute());
322−}
323−
324288 void PointMetavoxelRendererImplementation::init() {
325289 if (!_program.isLinked()) {
326290 _program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/metavoxel_point.vert");
@@ -340,12 +304,14 @@ public: · 13 unmodified lines
340304
341305 PointAugmentVisitor(const MetavoxelLOD& lod);
342306
307+ virtual void prepare(MetavoxelData* data);
343308 virtual int visit(MetavoxelInfo& info);
344309 virtual bool postVisit(MetavoxelInfo& info);
345310
346311 private:
347312
348− QVector<BufferPointVectorPair> _depthPoints;
313+ BufferPointVector _points;
314+ float _pointLeafSize;
349315 };
350316
351317 PointAugmentVisitor::PointAugmentVisitor(const MetavoxelLOD& lod) :
@@ -356,48 +322,61 @@ PointAugmentVisitor::PointAugmentVisitor(const MetavoxelLOD& lod) : · 4 unmodified lines
356322
357323 const int ALPHA_RENDER_THRESHOLD = 0;
358324
325+void PointAugmentVisitor::prepare(MetavoxelData* data) {
326+ MetavoxelVisitor::prepare(data);
327+ const float MAX_POINT_LEAF_SIZE = 64.0f;
328+ _pointLeafSize = qMin(data->getSize(), MAX_POINT_LEAF_SIZE);
329+}
330+
359331 int PointAugmentVisitor::visit(MetavoxelInfo& info) {
360− if (_depth >= _depthPoints.size()) {
361− _depthPoints.resize(_depth + 1);
332+ if (!info.isLeaf) {
333+ return (info.size > _pointLeafSize) ? DEFAULT_ORDER : (DEFAULT_ORDER | ALL_NODES_REST);
362334 }
363335 QRgb color = info.inputValues.at(0).getInlineValue<QRgb>();
364336 quint8 alpha = qAlpha(color);
365− if (alpha <= ALPHA_RENDER_THRESHOLD) {
366− return info.isLeaf ? STOP_RECURSION : (DEFAULT_ORDER | ALL_NODES);
337+ if (alpha > ALPHA_RENDER_THRESHOLD) {
338+ QRgb normal = info.inputValues.at(1).getInlineValue<QRgb>();
339+ BufferPoint point = { glm::vec4(info.minimum + glm::vec3(info.size, info.size, info.size) * 0.5f, info.size),
340+ { quint8(qRed(color)), quint8(qGreen(color)), quint8(qBlue(color)) },
341+ { quint8(qRed(normal)), quint8(qGreen(normal)), quint8(qBlue(normal)) } };
342+ _points.append(point);
367343 }
368− QRgb normal = info.inputValues.at(1).getInlineValue<QRgb>();
369− BufferPoint point = { glm::vec4(info.minimum + glm::vec3(info.size, info.size, info.size) * 0.5f, info.size),
370− { quint8(qRed(color)), quint8(qGreen(color)), quint8(qBlue(color)) },
371− { quint8(qRed(normal)), quint8(qGreen(normal)), quint8(qBlue(normal)) } };
372− if (info.isLeaf) {
373− _depthPoints[_depth].first.append(point);
374− return STOP_RECURSION;
344+ if (info.size >= _pointLeafSize) {
345+ BufferPointVector swapPoints;
346+ _points.swap(swapPoints);
347+ info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(PointBufferPointer(
348+ new PointBuffer(swapPoints))));
375349 }
376− _depthPoints[_depth].second.append(point);
377− return DEFAULT_ORDER | ALL_NODES;
350+ return STOP_RECURSION;
378351 }
379352
380353 bool PointAugmentVisitor::postVisit(MetavoxelInfo& info) {
381− if (_depth != 0) {
354+ if (info.size != _pointLeafSize) {
382355 return false;
383356 }
384− int lastDepth = _depthPoints.size();
385− QVector<BufferPointVectorPair> levelPoints(lastDepth - _depth);
386− for (int i = 0; i < levelPoints.size(); i++) {
387− BufferPointVectorPair& levelPair = levelPoints[i];
388− BufferPointVectorPair& depthPair = _depthPoints[_depth + i];
389− levelPair.first.swap(depthPair.first);
390− levelPair.second.swap(depthPair.second);
391− }
357+ BufferPointVector swapPoints;
358+ _points.swap(swapPoints);
392359 info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(PointBufferPointer(
393− new PointBuffer(levelPoints))));
360+ new PointBuffer(swapPoints))));
394361 return true;
395362 }
396363
397364 void PointMetavoxelRendererImplementation::augment(MetavoxelData& data, const MetavoxelData& previous,
398365 MetavoxelInfo& info, const MetavoxelLOD& lod) {
366+ // copy the previous buffers
367+ MetavoxelData expandedPrevious = previous;
368+ while (expandedPrevious.getSize() < data.getSize()) {
369+ expandedPrevious.expand();
370+ }
371+ const AttributePointer& pointBufferAttribute = Application::getInstance()->getMetavoxels()->getPointBufferAttribute();
372+ MetavoxelNode* root = expandedPrevious.getRoot(pointBufferAttribute);
373+ if (root) {
374+ data.setRoot(pointBufferAttribute, root);
375+ root->incrementReferenceCount();
376+ }
377+
399378 PointAugmentVisitor visitor(lod);
400− data.guideToDifferent(previous, visitor, &info);
379+ data.guideToDifferent(expandedPrevious, visitor);
401380 }
402381
403382 class PointRenderVisitor : public MetavoxelVisitor {
@@ -421,7 +400,7 @@ PointRenderVisitor::PointRenderVisitor(const MetavoxelLOD& lod) : · 17 unmodified lines
421400 int PointRenderVisitor::visit(MetavoxelInfo& info) {
422401 PointBufferPointer buffer = info.inputValues.at(0).getInlineValue<PointBufferPointer>();
423402 if (buffer) {
424− buffer->render(1000);
403+ buffer->render();
425404 }
426405 return info.isLeaf ? STOP_RECURSION : _order;
427406 }
@@ -450,7 +429,7 @@ void PointMetavoxelRendererImplementation::render(MetavoxelData& data, Metavoxel · 22 unmodified lines
450429 glDisable(GL_BLEND);
451430
452431 PointRenderVisitor visitor(lod);
453− data.guide(visitor, &info);
432+ data.guide(visitor);
454433
455434 glEnable(GL_BLEND);
456435