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
90 90
91SpannerRenderVisitor::SpannerRenderVisitor() :91SpannerRenderVisitor::SpannerRenderVisitor() :
92 SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(),92 SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(),
93 QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannerMaskAttribute(),93 QVector<AttributePointer>(), QVector<AttributePointer>(), QVector<AttributePointer>(),
94 QVector<AttributePointer>(), QVector<AttributePointer>(), Application::getInstance()->getMetavoxels()->getLOD(),94 Application::getInstance()->getMetavoxels()->getLOD(),
95 encodeOrder(Application::getInstance()->getViewFrustum()->getDirection())) {95 encodeOrder(Application::getInstance()->getViewFrustum()->getDirection())) {
96}96}
97 97
@@ -239,53 +239,22 @@ void MetavoxelSystemClient::sendDatagram(const QByteArray& data) { · 141 unmodified lines
239 Application::getInstance()->getBandwidthMeter()->outputStream(BandwidthMeter::METAVOXELS).updateValue(data.size());239 Application::getInstance()->getBandwidthMeter()->outputStream(BandwidthMeter::METAVOXELS).updateValue(data.size());
240}240}
241 241
242PointBuffer::PointBuffer(const QVector<BufferPointVectorPair>& levelPoints) :242PointBuffer::PointBuffer(const BufferPointVector& points) :
243 _levelPoints(levelPoints) {243 _points(points) {
244}244}
245 245
246void PointBuffer::render(int level) {246void PointBuffer::render() {
247 // initalize buffer, etc. on first render247 // initalize buffer, etc. on first render
248 if (!_buffer.isCreated()) {248 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 }
255 _buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);249 _buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
256 _buffer.create();250 _buffer.create();
257 _buffer.bind();251 _buffer.bind();
258 _buffer.allocate(totalPoints * sizeof(BufferPoint));252 _pointCount = _points.size();
259 int offset = 0;253 _buffer.allocate(_points.constData(), _pointCount * sizeof(BufferPoint));
260 for (int i = 0; i < _levelPoints.size(); i++) {254 _points.clear();
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();
276 _buffer.release();255 _buffer.release();
277 }256 }
278 int first, count;257 if (_pointCount == 0) {
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) {
289 return;258 return;
290 }259 }
291 _buffer.bind();260 _buffer.bind();
@@ -295,7 +264,7 @@ void PointBuffer::render(int level) { · 3 unmodified lines
295 glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(BufferPoint), &point->color);264 glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(BufferPoint), &point->color);
296 glNormalPointer(GL_BYTE, sizeof(BufferPoint), &point->normal);265 glNormalPointer(GL_BYTE, sizeof(BufferPoint), &point->normal);
297 266
298 glDrawArrays(GL_POINTS, first, count);267 glDrawArrays(GL_POINTS, 0, _pointCount);
299 268
300 _buffer.release();269 _buffer.release();
301}270}
@@ -304,23 +273,18 @@ PointBufferAttribute::PointBufferAttribute() : · 2 unmodified lines
304 InlineAttribute<PointBufferPointer>("pointBuffer") {273 InlineAttribute<PointBufferPointer>("pointBuffer") {
305}274}
306 275
307MetavoxelNode* PointBufferAttribute::createMetavoxelNode(const AttributeValue& value, const MetavoxelNode* original) const {
308 return new MetavoxelNode(value, original);
309}
310
311bool PointBufferAttribute::merge(void*& parent, void* children[], bool postRead) const {276bool PointBufferAttribute::merge(void*& parent, void* children[], bool postRead) const {
312 for (int i = 0; i < MERGE_COUNT; i++) {277 PointBufferPointer firstChild = decodeInline<PointBufferPointer>(children[0]);
313 if (decodeInline<PointBufferPointer>(children[i])) {278 for (int i = 1; i < MERGE_COUNT; i++) {
279 if (firstChild != decodeInline<PointBufferPointer>(children[i])) {
280 *(PointBufferPointer*)&parent = _defaultValue;
314 return false;281 return false;
315 }282 }
316 }283 }
284 *(PointBufferPointer*)&parent = firstChild;
317 return true;285 return true;
318}286}
319 287
320AttributeValue PointBufferAttribute::inherit(const AttributeValue& parentValue) const {
321 return AttributeValue(parentValue.getAttribute());
322}
323
324void PointMetavoxelRendererImplementation::init() {288void PointMetavoxelRendererImplementation::init() {
325 if (!_program.isLinked()) {289 if (!_program.isLinked()) {
326 _program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/metavoxel_point.vert");290 _program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/metavoxel_point.vert");
@@ -340,12 +304,14 @@ public: · 13 unmodified lines
340 304
341 PointAugmentVisitor(const MetavoxelLOD& lod);305 PointAugmentVisitor(const MetavoxelLOD& lod);
342 306
307 virtual void prepare(MetavoxelData* data);
343 virtual int visit(MetavoxelInfo& info);308 virtual int visit(MetavoxelInfo& info);
344 virtual bool postVisit(MetavoxelInfo& info);309 virtual bool postVisit(MetavoxelInfo& info);
345 310
346private:311private:
347 312
348 QVector<BufferPointVectorPair> _depthPoints;313 BufferPointVector _points;
314 float _pointLeafSize;
349};315};
350 316
351PointAugmentVisitor::PointAugmentVisitor(const MetavoxelLOD& lod) :317PointAugmentVisitor::PointAugmentVisitor(const MetavoxelLOD& lod) :
@@ -356,48 +322,61 @@ PointAugmentVisitor::PointAugmentVisitor(const MetavoxelLOD& lod) : · 4 unmodified lines
356 322
357const int ALPHA_RENDER_THRESHOLD = 0;323const int ALPHA_RENDER_THRESHOLD = 0;
358 324
325void 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
359int PointAugmentVisitor::visit(MetavoxelInfo& info) {331int PointAugmentVisitor::visit(MetavoxelInfo& info) {
360 if (_depth >= _depthPoints.size()) {332 if (!info.isLeaf) {
361 _depthPoints.resize(_depth + 1);333 return (info.size > _pointLeafSize) ? DEFAULT_ORDER : (DEFAULT_ORDER | ALL_NODES_REST);
362 }334 }
363 QRgb color = info.inputValues.at(0).getInlineValue<QRgb>();335 QRgb color = info.inputValues.at(0).getInlineValue<QRgb>();
364 quint8 alpha = qAlpha(color);336 quint8 alpha = qAlpha(color);
365 if (alpha <= ALPHA_RENDER_THRESHOLD) {337 if (alpha > ALPHA_RENDER_THRESHOLD) {
366 return info.isLeaf ? STOP_RECURSION : (DEFAULT_ORDER | ALL_NODES);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);
367 }343 }
368 QRgb normal = info.inputValues.at(1).getInlineValue<QRgb>();344 if (info.size >= _pointLeafSize) {
369 BufferPoint point = { glm::vec4(info.minimum + glm::vec3(info.size, info.size, info.size) * 0.5f, info.size),345 BufferPointVector swapPoints;
370 { quint8(qRed(color)), quint8(qGreen(color)), quint8(qBlue(color)) }, 346 _points.swap(swapPoints);
371 { quint8(qRed(normal)), quint8(qGreen(normal)), quint8(qBlue(normal)) } };347 info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(PointBufferPointer(
372 if (info.isLeaf) {348 new PointBuffer(swapPoints))));
373 _depthPoints[_depth].first.append(point);
374 return STOP_RECURSION;
375 }349 }
376 _depthPoints[_depth].second.append(point);350 return STOP_RECURSION;
377 return DEFAULT_ORDER | ALL_NODES;
378}351}
379 352
380bool PointAugmentVisitor::postVisit(MetavoxelInfo& info) {353bool PointAugmentVisitor::postVisit(MetavoxelInfo& info) {
381 if (_depth != 0) {354 if (info.size != _pointLeafSize) {
382 return false;355 return false;
383 }356 }
384 int lastDepth = _depthPoints.size();357 BufferPointVector swapPoints;
385 QVector<BufferPointVectorPair> levelPoints(lastDepth - _depth);358 _points.swap(swapPoints);
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 }
392 info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(PointBufferPointer(359 info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(PointBufferPointer(
393 new PointBuffer(levelPoints))));360 new PointBuffer(swapPoints))));
394 return true;361 return true;
395}362}
396 363
397void PointMetavoxelRendererImplementation::augment(MetavoxelData& data, const MetavoxelData& previous,364void PointMetavoxelRendererImplementation::augment(MetavoxelData& data, const MetavoxelData& previous,
398 MetavoxelInfo& info, const MetavoxelLOD& lod) {365 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
399 PointAugmentVisitor visitor(lod);378 PointAugmentVisitor visitor(lod);
400 data.guideToDifferent(previous, visitor, &info);379 data.guideToDifferent(expandedPrevious, visitor);
401}380}
402 381
403class PointRenderVisitor : public MetavoxelVisitor {382class PointRenderVisitor : public MetavoxelVisitor {
@@ -421,7 +400,7 @@ PointRenderVisitor::PointRenderVisitor(const MetavoxelLOD& lod) : · 17 unmodified lines
421int PointRenderVisitor::visit(MetavoxelInfo& info) {400int PointRenderVisitor::visit(MetavoxelInfo& info) {
422 PointBufferPointer buffer = info.inputValues.at(0).getInlineValue<PointBufferPointer>();401 PointBufferPointer buffer = info.inputValues.at(0).getInlineValue<PointBufferPointer>();
423 if (buffer) {402 if (buffer) {
424 buffer->render(1000);403 buffer->render();
425 }404 }
426 return info.isLeaf ? STOP_RECURSION : _order;405 return info.isLeaf ? STOP_RECURSION : _order;
427}406}
@@ -450,7 +429,7 @@ void PointMetavoxelRendererImplementation::render(MetavoxelData& data, Metavoxel · 22 unmodified lines
450 glDisable(GL_BLEND);429 glDisable(GL_BLEND);
451 430
452 PointRenderVisitor visitor(lod);431 PointRenderVisitor visitor(lod);
453 data.guide(visitor, &info);432 data.guide(visitor);
454 433
455 glEnable(GL_BLEND);434 glEnable(GL_BLEND);
456 435