Showing the changes to interface/src/MetavoxelSystem.cpp (1 of 2 files). Show all
Unified · Split
| @@ -31,20 +31,9 @@ REGISTER_META_OBJECT(StaticModelRenderer) · 30 unmodified lines | |||
| 31 | 31 | ||
| 32 | static int bufferPointVectorMetaTypeId = qRegisterMetaType<BufferPointVector>(); | 32 | static int bufferPointVectorMetaTypeId = qRegisterMetaType<BufferPointVector>(); |
| 33 | 33 | ||
| 34 | ProgramObject MetavoxelSystem::_program; | ||
| 35 | int MetavoxelSystem::_pointScaleLocation; | ||
| 36 | |||
| 37 | void MetavoxelSystem::init() { | 34 | void MetavoxelSystem::init() { |
| 38 | MetavoxelClientManager::init(); | 35 | MetavoxelClientManager::init(); |
| 39 | 36 | PointMetavoxelRendererImplementation::init(); | |
| 40 | if (!_program.isLinked()) { | ||
| 41 | _program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/metavoxel_point.vert"); | ||
| 42 | _program.link(); | ||
| 43 | |||
| 44 | _program.bind(); | ||
| 45 | _pointScaleLocation = _program.uniformLocation("pointScale"); | ||
| 46 | _program.release(); | ||
| 47 | } | ||
| 48 | _pointBufferAttribute = AttributeRegistry::getInstance()->registerAttribute(new PointBufferAttribute()); | 37 | _pointBufferAttribute = AttributeRegistry::getInstance()->registerAttribute(new PointBufferAttribute()); |
| 49 | } | 38 | } |
| 50 | 39 | ||
| @@ -91,35 +80,6 @@ void MetavoxelSystem::simulate(float deltaTime) { · 40 unmodified lines | |||
| 91 | guide(spannerSimulateVisitor); | 80 | guide(spannerSimulateVisitor); |
| 92 | } | 81 | } |
| 93 | 82 | ||
| 94 | class PointBufferRenderVisitor : public MetavoxelVisitor { | ||
| 95 | public: | ||
| 96 | |||
| 97 | PointBufferRenderVisitor(); | ||
| 98 | |||
| 99 | virtual int visit(MetavoxelInfo& info); | ||
| 100 | |||
| 101 | private: | ||
| 102 | |||
| 103 | int _order; | ||
| 104 | }; | ||
| 105 | |||
| 106 | PointBufferRenderVisitor::PointBufferRenderVisitor() : | ||
| 107 | MetavoxelVisitor(QVector<AttributePointer>() << Application::getInstance()->getMetavoxels()->getPointBufferAttribute(), | ||
| 108 | QVector<AttributePointer>(), Application::getInstance()->getMetavoxels()->getLOD()), | ||
| 109 | _order(encodeOrder(Application::getInstance()->getViewFrustum()->getDirection())) { | ||
| 110 | } | ||
| 111 | |||
| 112 | int PointBufferRenderVisitor::visit(MetavoxelInfo& info) { | ||
| 113 | PointBufferPointer buffer = info.inputValues.at(0).getInlineValue<PointBufferPointer>(); | ||
| 114 | if (buffer) { | ||
| 115 | buffer->render(1000); | ||
| 116 | } | ||
| 117 | if (info.isLeaf) { | ||
| 118 | return STOP_RECURSION; | ||
| 119 | } | ||
| 120 | return _order; | ||
| 121 | } | ||
| 122 | |||
| 123 | class SpannerRenderVisitor : public SpannerVisitor { | 83 | class SpannerRenderVisitor : public SpannerVisitor { |
| 124 | public: | 84 | public: |
| 125 | 85 | ||
| @@ -165,64 +125,11 @@ int RenderVisitor::visit(MetavoxelInfo& info) { · 39 unmodified lines | |||
| 165 | void MetavoxelSystem::render() { | 125 | void MetavoxelSystem::render() { |
| 166 | RenderVisitor renderVisitor(getLOD()); | 126 | RenderVisitor renderVisitor(getLOD()); |
| 167 | guideToAugmented(renderVisitor); | 127 | guideToAugmented(renderVisitor); |
| 168 | |||
| 169 | int viewport[4]; | ||
| 170 | glGetIntegerv(GL_VIEWPORT, viewport); | ||
| 171 | const int VIEWPORT_WIDTH_INDEX = 2; | ||
| 172 | const int VIEWPORT_HEIGHT_INDEX = 3; | ||
| 173 | float viewportWidth = viewport[VIEWPORT_WIDTH_INDEX]; | ||
| 174 | float viewportHeight = viewport[VIEWPORT_HEIGHT_INDEX]; | ||
| 175 | float viewportDiagonal = sqrtf(viewportWidth*viewportWidth + viewportHeight*viewportHeight); | ||
| 176 | float worldDiagonal = glm::distance(Application::getInstance()->getViewFrustum()->getNearBottomLeft(), | ||
| 177 | Application::getInstance()->getViewFrustum()->getNearTopRight()); | ||
| 178 | |||
| 179 | _program.bind(); | ||
| 180 | _program.setUniformValue(_pointScaleLocation, viewportDiagonal * | ||
| 181 | Application::getInstance()->getViewFrustum()->getNearClip() / worldDiagonal); | ||
| 182 | |||
| 183 | glEnableClientState(GL_VERTEX_ARRAY); | ||
| 184 | glEnableClientState(GL_COLOR_ARRAY); | ||
| 185 | glEnableClientState(GL_NORMAL_ARRAY); | ||
| 186 | |||
| 187 | glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB); | ||
| 188 | |||
| 189 | glDisable(GL_BLEND); | ||
| 190 | |||
| 191 | //PointBufferRenderVisitor pointBufferRenderVisitor; | ||
| 192 | //guide(pointBufferRenderVisitor); | ||
| 193 | |||
| 194 | foreach (const SharedNodePointer& node, NodeList::getInstance()->getNodeHash()) { | ||
| 195 | if (node->getType() == NodeType::MetavoxelServer) { | ||
| 196 | QMutexLocker locker(&node->getMutex()); | ||
| 197 | MetavoxelSystemClient* client = static_cast<MetavoxelSystemClient*>(node->getLinkedData()); | ||
| 198 | if (client) { | ||
| 199 | client->render(); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | glEnable(GL_BLEND); | ||
| 205 | |||
| 206 | glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB); | ||
| 207 | |||
| 208 | glDisableClientState(GL_VERTEX_ARRAY); | ||
| 209 | glDisableClientState(GL_COLOR_ARRAY); | ||
| 210 | glDisableClientState(GL_NORMAL_ARRAY); | ||
| 211 | |||
| 212 | _program.release(); | ||
| 213 | 128 | ||
| 214 | SpannerRenderVisitor spannerRenderVisitor; | 129 | SpannerRenderVisitor spannerRenderVisitor; |
| 215 | guide(spannerRenderVisitor); | 130 | guide(spannerRenderVisitor); |
| 216 | } | 131 | } |
| 217 | 132 | ||
| 218 | void MetavoxelSystem::setClientPoints(const SharedNodePointer& node, const BufferPointVector& points) { | ||
| 219 | QMutexLocker locker(&node->getMutex()); | ||
| 220 | MetavoxelSystemClient* client = static_cast<MetavoxelSystemClient*>(node->getLinkedData()); | ||
| 221 | if (client) { | ||
| 222 | client->setPoints(points); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | MetavoxelClient* MetavoxelSystem::createClient(const SharedNodePointer& node) { | 133 | MetavoxelClient* MetavoxelSystem::createClient(const SharedNodePointer& node) { |
| 227 | return new MetavoxelSystemClient(node, _updater); | 134 | return new MetavoxelSystemClient(node, _updater); |
| 228 | } | 135 | } |
| @@ -240,35 +147,7 @@ void MetavoxelSystem::guideToAugmented(MetavoxelVisitor& visitor) { · 11 unmodified lines | |||
| 240 | } | 147 | } |
| 241 | 148 | ||
| 242 | MetavoxelSystemClient::MetavoxelSystemClient(const SharedNodePointer& node, MetavoxelUpdater* updater) : | 149 | MetavoxelSystemClient::MetavoxelSystemClient(const SharedNodePointer& node, MetavoxelUpdater* updater) : |
| 243 | MetavoxelClient(node, updater), | 150 | MetavoxelClient(node, updater) { |
| 244 | _pointCount(0) { | ||
| 245 | |||
| 246 | _buffer.setUsagePattern(QOpenGLBuffer::StaticDraw); | ||
| 247 | _buffer.create(); | ||
| 248 | } | ||
| 249 | |||
| 250 | void MetavoxelSystemClient::render() { | ||
| 251 | _buffer.bind(); | ||
| 252 | |||
| 253 | BufferPoint* point = 0; | ||
| 254 | glVertexPointer(4, GL_FLOAT, sizeof(BufferPoint), &point->vertex); | ||
| 255 | glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(BufferPoint), &point->color); | ||
| 256 | glNormalPointer(GL_BYTE, sizeof(BufferPoint), &point->normal); | ||
| 257 | |||
| 258 | glDrawArrays(GL_POINTS, 0, _pointCount); | ||
| 259 | |||
| 260 | _buffer.release(); | ||
| 261 | } | ||
| 262 | |||
| 263 | void MetavoxelSystemClient::setPoints(const BufferPointVector& points) { | ||
| 264 | qint64 now = QDateTime::currentMSecsSinceEpoch(); | ||
| 265 | _buffer.bind(); | ||
| 266 | _buffer.allocate(points.constData(), points.size() * sizeof(BufferPoint)); | ||
| 267 | _buffer.release(); | ||
| 268 | _pointCount = points.size(); | ||
| 269 | qDebug() << "upload" << (QDateTime::currentMSecsSinceEpoch() - now); | ||
| 270 | qDebug() << _pointCount; | ||
| 271 | |||
| 272 | } | 151 | } |
| 273 | 152 | ||
| 274 | void MetavoxelSystemClient::setAugmentedData(const MetavoxelData& data) { | 153 | void MetavoxelSystemClient::setAugmentedData(const MetavoxelData& data) { |
| @@ -288,166 +167,6 @@ int MetavoxelSystemClient::parseData(const QByteArray& packet) { · 13 unmodified lines | |||
| 288 | return packet.size(); | 167 | return packet.size(); |
| 289 | } | 168 | } |
| 290 | 169 | ||
| 291 | class BufferBuilder : public MetavoxelVisitor { | ||
| 292 | public: | ||
| 293 | |||
| 294 | BufferBuilder(const MetavoxelLOD& lod); | ||
| 295 | |||
| 296 | virtual int visit(MetavoxelInfo& info); | ||
| 297 | virtual bool postVisit(MetavoxelInfo& info); | ||
| 298 | |||
| 299 | private: | ||
| 300 | |||
| 301 | QVector<BufferPointVectorPair> _depthPoints; | ||
| 302 | }; | ||
| 303 | |||
| 304 | BufferBuilder::BufferBuilder(const MetavoxelLOD& lod) : | ||
| 305 | MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getColorAttribute() << | ||
| 306 | AttributeRegistry::getInstance()->getNormalAttribute() << | ||
| 307 | Application::getInstance()->getMetavoxels()->getPointBufferAttribute(), QVector<AttributePointer>() << | ||
| 308 | Application::getInstance()->getMetavoxels()->getPointBufferAttribute(), lod) { | ||
| 309 | } | ||
| 310 | |||
| 311 | const int ALPHA_RENDER_THRESHOLD = 0; | ||
| 312 | |||
| 313 | const int BUFFER_LEVELS = 5; | ||
| 314 | const int LAST_BUFFER_LEVEL = BUFFER_LEVELS - 1; | ||
| 315 | |||
| 316 | int BufferBuilder::visit(MetavoxelInfo& info) { | ||
| 317 | if (info.inputValues.at(2).getInlineValue<PointBufferPointer>()) { | ||
| 318 | info.outputValues[0] = AttributeValue(_outputs.at(0)); | ||
| 319 | } | ||
| 320 | if (_depth >= _depthPoints.size()) { | ||
| 321 | _depthPoints.resize(_depth + 1); | ||
| 322 | } | ||
| 323 | QRgb color = info.inputValues.at(0).getInlineValue<QRgb>(); | ||
| 324 | quint8 alpha = qAlpha(color); | ||
| 325 | if (alpha <= ALPHA_RENDER_THRESHOLD) { | ||
| 326 | return info.isLeaf ? STOP_RECURSION : DEFAULT_ORDER; | ||
| 327 | } | ||
| 328 | QRgb normal = info.inputValues.at(1).getInlineValue<QRgb>(); | ||
| 329 | BufferPoint point = { glm::vec4(info.minimum + glm::vec3(info.size, info.size, info.size) * 0.5f, info.size), | ||
| 330 | { quint8(qRed(color)), quint8(qGreen(color)), quint8(qBlue(color)) }, | ||
| 331 | { quint8(qRed(normal)), quint8(qGreen(normal)), quint8(qBlue(normal)) } }; | ||
| 332 | if (info.isLeaf) { | ||
| 333 | _depthPoints[_depth].first.append(point); | ||
| 334 | return STOP_RECURSION; | ||
| 335 | } | ||
| 336 | _depthPoints[_depth].second.append(point); | ||
| 337 | return DEFAULT_ORDER | ((_depth % BUFFER_LEVELS) == LAST_BUFFER_LEVEL ? 0 : ALL_NODES); | ||
| 338 | } | ||
| 339 | |||
| 340 | bool BufferBuilder::postVisit(MetavoxelInfo& info) { | ||
| 341 | if (_depth % BUFFER_LEVELS != 0) { | ||
| 342 | return false; | ||
| 343 | } | ||
| 344 | QVector<int> offsets; | ||
| 345 | offsets.append(0); | ||
| 346 | int leafCount = 0; | ||
| 347 | int totalPoints = 0; | ||
| 348 | int lastDepth = qMin(_depth + BUFFER_LEVELS, _depthPoints.size()); | ||
| 349 | for (int i = _depth; i < lastDepth; i++) { | ||
| 350 | const BufferPointVectorPair& pair = _depthPoints.at(i); | ||
| 351 | offsets.append(totalPoints += ((leafCount += pair.first.size()) + pair.second.size())); | ||
| 352 | } | ||
| 353 | QOpenGLBuffer buffer; | ||
| 354 | buffer.setUsagePattern(QOpenGLBuffer::StaticDraw); | ||
| 355 | buffer.create(); | ||
| 356 | buffer.bind(); | ||
| 357 | buffer.allocate(totalPoints * sizeof(BufferPoint)); | ||
| 358 | int offset = 0; | ||
| 359 | for (int i = _depth; i < lastDepth; i++) { | ||
| 360 | // write the internal nodes from the current level | ||
| 361 | BufferPointVector& internal = _depthPoints[i].second; | ||
| 362 | int length = internal.size() * sizeof(BufferPoint); | ||
| 363 | buffer.write(offset, internal.constData(), length); | ||
| 364 | offset += length; | ||
| 365 | internal.clear(); | ||
| 366 | |||
| 367 | // and the leaves from the top down | ||
| 368 | for (int j = _depth; j <= i; j++) { | ||
| 369 | const BufferPointVector& leaves = _depthPoints.at(j).first; | ||
| 370 | length = leaves.size() * sizeof(BufferPoint); | ||
| 371 | buffer.write(offset, leaves.constData(), length); | ||
| 372 | offset += length; | ||
| 373 | } | ||
| 374 | } | ||
| 375 | // clear the leaves now that we're done with them | ||
| 376 | for (int i = _depth; i < lastDepth; i++) { | ||
| 377 | _depthPoints[i].first.clear(); | ||
| 378 | } | ||
| 379 | buffer.release(); | ||
| 380 | info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(PointBufferPointer( | ||
| 381 | new PointBuffer(buffer, offsets, leafCount)))); | ||
| 382 | return true; | ||
| 383 | } | ||
| 384 | |||
| 385 | class PointCollector : public MetavoxelVisitor { | ||
| 386 | public: | ||
| 387 | |||
| 388 | QVector<BufferPoint> points; | ||
| 389 | |||
| 390 | PointCollector(const MetavoxelLOD& lod); | ||
| 391 | |||
| 392 | virtual int visit(MetavoxelInfo& info); | ||
| 393 | }; | ||
| 394 | |||
| 395 | PointCollector::PointCollector(const MetavoxelLOD& lod) : | ||
| 396 | MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getColorAttribute() << | ||
| 397 | AttributeRegistry::getInstance()->getNormalAttribute(), QVector<AttributePointer>(), lod) { | ||
| 398 | } | ||
| 399 | |||
| 400 | int PointCollector::visit(MetavoxelInfo& info) { | ||
| 401 | if (!info.isLeaf) { | ||
| 402 | return DEFAULT_ORDER; | ||
| 403 | } | ||
| 404 | QRgb color = info.inputValues.at(0).getInlineValue<QRgb>(); | ||
| 405 | quint8 alpha = qAlpha(color); | ||
| 406 | if (alpha <= ALPHA_RENDER_THRESHOLD) { | ||
| 407 | return STOP_RECURSION; | ||
| 408 | } | ||
| 409 | QRgb normal = info.inputValues.at(1).getInlineValue<QRgb>(); | ||
| 410 | BufferPoint point = { glm::vec4(info.minimum + glm::vec3(info.size, info.size, info.size) * 0.5f, info.size), | ||
| 411 | { quint8(qRed(color)), quint8(qGreen(color)), quint8(qBlue(color)) }, | ||
| 412 | { quint8(qRed(normal)), quint8(qGreen(normal)), quint8(qBlue(normal)) } }; | ||
| 413 | points.append(point); | ||
| 414 | return STOP_RECURSION; | ||
| 415 | } | ||
| 416 | |||
| 417 | /// Builds a point buffer. | ||
| 418 | class PointBufferBuilder : public QRunnable { | ||
| 419 | public: | ||
| 420 | |||
| 421 | PointBufferBuilder(const SharedNodePointer& node, const MetavoxelData& data, const MetavoxelLOD& lod); | ||
| 422 | |||
| 423 | virtual void run(); | ||
| 424 | |||
| 425 | private: | ||
| 426 | |||
| 427 | QWeakPointer<Node> _node; | ||
| 428 | MetavoxelData _data; | ||
| 429 | MetavoxelLOD _lod; | ||
| 430 | }; | ||
| 431 | |||
| 432 | PointBufferBuilder::PointBufferBuilder(const SharedNodePointer& node, const MetavoxelData& data, const MetavoxelLOD& lod) : | ||
| 433 | _node(node), | ||
| 434 | _data(data), | ||
| 435 | _lod(lod) { | ||
| 436 | } | ||
| 437 | |||
| 438 | void PointBufferBuilder::run() { | ||
| 439 | SharedNodePointer node = _node; | ||
| 440 | if (!node) { | ||
| 441 | return; | ||
| 442 | } | ||
| 443 | qint64 now = QDateTime::currentMSecsSinceEpoch(); | ||
| 444 | PointCollector collector(_lod); | ||
| 445 | _data.guide(collector); | ||
| 446 | QMetaObject::invokeMethod(Application::getInstance()->getMetavoxels(), "setClientPoints", | ||
| 447 | Q_ARG(const SharedNodePointer&, node), Q_ARG(const BufferPointVector&, collector.points)); | ||
| 448 | qDebug() << "collect" << (QDateTime::currentMSecsSinceEpoch() - now); | ||
| 449 | } | ||
| 450 | |||
| 451 | class AugmentVisitor : public MetavoxelVisitor { | 170 | class AugmentVisitor : public MetavoxelVisitor { |
| 452 | public: | 171 | public: |
| 453 | 172 | ||
| @@ -506,26 +225,12 @@ void Augmenter::run() { · 52 unmodified lines | |||
| 506 | } | 225 | } |
| 507 | AugmentVisitor visitor(_lod, _previousData); | 226 | AugmentVisitor visitor(_lod, _previousData); |
| 508 | _data.guide(visitor); | 227 | _data.guide(visitor); |
| 509 | QMetaObject::invokeMethod(node.data(), "setAugmentedData", Q_ARG(const MetavoxelData&, _data)); | 228 | QMutexLocker locker(&node->getMutex()); |
| 229 | QMetaObject::invokeMethod(node->getLinkedData(), "setAugmentedData", Q_ARG(const MetavoxelData&, _data)); | ||
| 510 | } | 230 | } |
| 511 | 231 | ||
| 512 | void MetavoxelSystemClient::dataChanged(const MetavoxelData& oldData) { | 232 | void MetavoxelSystemClient::dataChanged(const MetavoxelData& oldData) { |
| 513 | MetavoxelClient::dataChanged(oldData); | 233 | MetavoxelClient::dataChanged(oldData); |
| 514 | |||
| 515 | /* BufferBuilder builder(_remoteDataLOD); | ||
| 516 | const AttributePointer& pointBufferAttribute = Application::getInstance()->getMetavoxels()->getPointBufferAttribute(); | ||
| 517 | MetavoxelNode* oldRoot = oldData.getRoot(pointBufferAttribute); | ||
| 518 | if (oldRoot && oldData.getSize() == _data.getSize()) { | ||
| 519 | oldRoot->incrementReferenceCount(); | ||
| 520 | _data.setRoot(pointBufferAttribute, oldRoot); | ||
| 521 | _data.guideToDifferent(oldData, builder); | ||
| 522 | |||
| 523 | } else { | ||
| 524 | _data.clear(pointBufferAttribute); | ||
| 525 | _data.guide(builder); | ||
| 526 | } */ | ||
| 527 | QThreadPool::globalInstance()->start(new PointBufferBuilder(_node, _data, _remoteDataLOD)); | ||
| 528 | |||
| 529 | QThreadPool::globalInstance()->start(new Augmenter(_node, _data, getAugmentedData(), _remoteDataLOD)); | 234 | QThreadPool::globalInstance()->start(new Augmenter(_node, _data, getAugmentedData(), _remoteDataLOD)); |
| 530 | } | 235 | } |
| 531 | 236 | ||
| @@ -534,13 +239,42 @@ void MetavoxelSystemClient::sendDatagram(const QByteArray& data) { · 2 unmodified lines | |||
| 534 | Application::getInstance()->getBandwidthMeter()->outputStream(BandwidthMeter::METAVOXELS).updateValue(data.size()); | 239 | Application::getInstance()->getBandwidthMeter()->outputStream(BandwidthMeter::METAVOXELS).updateValue(data.size()); |
| 535 | } | 240 | } |
| 536 | 241 | ||
| 537 | PointBuffer::PointBuffer(const QOpenGLBuffer& buffer, const QVector<int>& offsets, int lastLeafCount) : | 242 | PointBuffer::PointBuffer(const QVector<BufferPointVectorPair>& levelPoints) : |
| 538 | _buffer(buffer), | 243 | _levelPoints(levelPoints) { |
| 539 | _offsets(offsets), | ||
| 540 | _lastLeafCount(lastLeafCount) { | ||
| 541 | } | 244 | } |
| 542 | 245 | ||
| 543 | void PointBuffer::render(int level) { | 246 | void PointBuffer::render(int level) { |
| 247 | // initalize buffer, etc. on first render | ||
| 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); | ||
| 256 | _buffer.create(); | ||
| 257 | _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(); | ||
| 276 | _buffer.release(); | ||
| 277 | } | ||
| 544 | int first, count; | 278 | int first, count; |
| 545 | int nextLevel = level + 1; | 279 | int nextLevel = level + 1; |
| 546 | if (nextLevel >= _offsets.size()) { | 280 | if (nextLevel >= _offsets.size()) { |
| @@ -587,6 +321,17 @@ AttributeValue PointBufferAttribute::inherit(const AttributeValue& parentValue) · 40 unmodified lines | |||
| 587 | return AttributeValue(parentValue.getAttribute()); | 321 | return AttributeValue(parentValue.getAttribute()); |
| 588 | } | 322 | } |
| 589 | 323 | ||
| 324 | void PointMetavoxelRendererImplementation::init() { | ||
| 325 | if (!_program.isLinked()) { | ||
| 326 | _program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/metavoxel_point.vert"); | ||
| 327 | _program.link(); | ||
| 328 | |||
| 329 | _program.bind(); | ||
| 330 | _pointScaleLocation = _program.uniformLocation("pointScale"); | ||
| 331 | _program.release(); | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 590 | PointMetavoxelRendererImplementation::PointMetavoxelRendererImplementation() { | 335 | PointMetavoxelRendererImplementation::PointMetavoxelRendererImplementation() { |
| 591 | } | 336 | } |
| 592 | 337 | ||
| @@ -596,6 +341,11 @@ public: · 3 unmodified lines | |||
| 596 | PointAugmentVisitor(const MetavoxelLOD& lod); | 341 | PointAugmentVisitor(const MetavoxelLOD& lod); |
| 597 | 342 | ||
| 598 | virtual int visit(MetavoxelInfo& info); | 343 | virtual int visit(MetavoxelInfo& info); |
| 344 | virtual bool postVisit(MetavoxelInfo& info); | ||
| 345 | |||
| 346 | private: | ||
| 347 | |||
| 348 | QVector<BufferPointVectorPair> _depthPoints; | ||
| 599 | }; | 349 | }; |
| 600 | 350 | ||
| 601 | PointAugmentVisitor::PointAugmentVisitor(const MetavoxelLOD& lod) : | 351 | PointAugmentVisitor::PointAugmentVisitor(const MetavoxelLOD& lod) : |
| @@ -604,11 +354,44 @@ PointAugmentVisitor::PointAugmentVisitor(const MetavoxelLOD& lod) : · 2 unmodified lines | |||
| 604 | Application::getInstance()->getMetavoxels()->getPointBufferAttribute(), lod) { | 354 | Application::getInstance()->getMetavoxels()->getPointBufferAttribute(), lod) { |
| 605 | } | 355 | } |
| 606 | 356 | ||
| 357 | const int ALPHA_RENDER_THRESHOLD = 0; | ||
| 358 | |||
| 607 | int PointAugmentVisitor::visit(MetavoxelInfo& info) { | 359 | int PointAugmentVisitor::visit(MetavoxelInfo& info) { |
| 608 | if (!info.isLeaf) { | 360 | if (_depth >= _depthPoints.size()) { |
| 609 | return DEFAULT_ORDER; | 361 | _depthPoints.resize(_depth + 1); |
| 610 | } | 362 | } |
| 611 | return STOP_RECURSION; | 363 | QRgb color = info.inputValues.at(0).getInlineValue<QRgb>(); |
| 364 | quint8 alpha = qAlpha(color); | ||
| 365 | if (alpha <= ALPHA_RENDER_THRESHOLD) { | ||
| 366 | return info.isLeaf ? STOP_RECURSION : (DEFAULT_ORDER | ALL_NODES); | ||
| 367 | } | ||
| 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; | ||
| 375 | } | ||
| 376 | _depthPoints[_depth].second.append(point); | ||
| 377 | return DEFAULT_ORDER | ALL_NODES; | ||
| 378 | } | ||
| 379 | |||
| 380 | bool PointAugmentVisitor::postVisit(MetavoxelInfo& info) { | ||
| 381 | if (_depth != 0) { | ||
| 382 | return false; | ||
| 383 | } | ||
| 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 | } | ||
| 392 | info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(PointBufferPointer( | ||
| 393 | new PointBuffer(levelPoints)))); | ||
| 394 | return true; | ||
| 612 | } | 395 | } |
| 613 | 396 | ||
| 614 | void PointMetavoxelRendererImplementation::augment(MetavoxelData& data, const MetavoxelData& previous, | 397 | void PointMetavoxelRendererImplementation::augment(MetavoxelData& data, const MetavoxelData& previous, |
| @@ -623,24 +406,65 @@ public: · 8 unmodified lines | |||
| 623 | PointRenderVisitor(const MetavoxelLOD& lod); | 406 | PointRenderVisitor(const MetavoxelLOD& lod); |
| 624 | 407 | ||
| 625 | virtual int visit(MetavoxelInfo& info); | 408 | virtual int visit(MetavoxelInfo& info); |
| 409 | |||
| 410 | private: | ||
| 411 | |||
| 412 | int _order; | ||
| 626 | }; | 413 | }; |
| 627 | 414 | ||
| 628 | PointRenderVisitor::PointRenderVisitor(const MetavoxelLOD& lod) : | 415 | PointRenderVisitor::PointRenderVisitor(const MetavoxelLOD& lod) : |
| 629 | MetavoxelVisitor(QVector<AttributePointer>() << Application::getInstance()->getMetavoxels()->getPointBufferAttribute(), | 416 | MetavoxelVisitor(QVector<AttributePointer>() << Application::getInstance()->getMetavoxels()->getPointBufferAttribute(), |
| 630 | QVector<AttributePointer>(), lod) { | 417 | QVector<AttributePointer>(), lod), |
| 418 | _order(encodeOrder(Application::getInstance()->getViewFrustum()->getDirection())) { | ||
| 631 | } | 419 | } |
| 632 | 420 | ||
| 633 | int PointRenderVisitor::visit(MetavoxelInfo& info) { | 421 | int PointRenderVisitor::visit(MetavoxelInfo& info) { |
| 634 | if (!info.isLeaf) { | 422 | PointBufferPointer buffer = info.inputValues.at(0).getInlineValue<PointBufferPointer>(); |
| 635 | return DEFAULT_ORDER; | 423 | if (buffer) { |
| 424 | buffer->render(1000); | ||
| 636 | } | 425 | } |
| 637 | return STOP_RECURSION; | 426 | return info.isLeaf ? STOP_RECURSION : _order; |
| 638 | } | 427 | } |
| 639 | 428 | ||
| 640 | void PointMetavoxelRendererImplementation::render(MetavoxelData& data, MetavoxelInfo& info, const MetavoxelLOD& lod) { | 429 | void PointMetavoxelRendererImplementation::render(MetavoxelData& data, MetavoxelInfo& info, const MetavoxelLOD& lod) { |
| 430 | int viewport[4]; | ||
| 431 | glGetIntegerv(GL_VIEWPORT, viewport); | ||
| 432 | const int VIEWPORT_WIDTH_INDEX = 2; | ||
| 433 | const int VIEWPORT_HEIGHT_INDEX = 3; | ||
| 434 | float viewportWidth = viewport[VIEWPORT_WIDTH_INDEX]; | ||
| 435 | float viewportHeight = viewport[VIEWPORT_HEIGHT_INDEX]; | ||
| 436 | float viewportDiagonal = sqrtf(viewportWidth * viewportWidth + viewportHeight * viewportHeight); | ||
| 437 | float worldDiagonal = glm::distance(Application::getInstance()->getViewFrustum()->getNearBottomLeft(), | ||
| 438 | Application::getInstance()->getViewFrustum()->getNearTopRight()); | ||
| 439 | |||
| 440 | _program.bind(); | ||
| 441 | _program.setUniformValue(_pointScaleLocation, viewportDiagonal * | ||
| 442 | Application::getInstance()->getViewFrustum()->getNearClip() / worldDiagonal); | ||
| 443 | |||
| 444 | glEnableClientState(GL_VERTEX_ARRAY); | ||
| 445 | glEnableClientState(GL_COLOR_ARRAY); | ||
| 446 | glEnableClientState(GL_NORMAL_ARRAY); | ||
| 447 | |||
| 448 | glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB); | ||
| 449 | |||
| 450 | glDisable(GL_BLEND); | ||
| 451 | |||
| 641 | PointRenderVisitor visitor(lod); | 452 | PointRenderVisitor visitor(lod); |
| 642 | data.guide(visitor, &info); | 453 | data.guide(visitor, &info); |
| 454 | |||
| 455 | glEnable(GL_BLEND); | ||
| 456 | |||
| 457 | glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB); | ||
| 458 | |||
| 459 | glDisableClientState(GL_VERTEX_ARRAY); | ||
| 460 | glDisableClientState(GL_COLOR_ARRAY); | ||
| 461 | glDisableClientState(GL_NORMAL_ARRAY); | ||
| 462 | |||
| 463 | _program.release(); | ||
| 643 | } | 464 | } |
| 465 | |||
| 466 | ProgramObject PointMetavoxelRendererImplementation::_program; | ||
| 467 | int PointMetavoxelRendererImplementation::_pointScaleLocation; | ||
| 644 | 468 | ||
| 645 | static void enableClipPlane(GLenum plane, float x, float y, float z, float w) { | 469 | static void enableClipPlane(GLenum plane, float x, float y, float z, float w) { |
| 646 | GLdouble coefficients[] = { x, y, z, w }; | 470 | GLdouble coefficients[] = { x, y, z, w }; |