History

Working on heightfield implementation.

Andrzej Kapolka committed 12 years ago

Commit aeee3691 parent cc909e28 files · +337 −93Browse files

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

Unified · Split

interface/src/MetavoxelSystem.cpp+176−74
@@ -25,7 +25,7 @@ · 24 unmodified lines
25#include "MetavoxelSystem.h"25#include "MetavoxelSystem.h"
26#include "renderer/Model.h"26#include "renderer/Model.h"
27 27
28REGISTER_META_OBJECT(PointMetavoxelRendererImplementation)28REGISTER_META_OBJECT(DefaultMetavoxelRendererImplementation)
29REGISTER_META_OBJECT(SphereRenderer)29REGISTER_META_OBJECT(SphereRenderer)
30REGISTER_META_OBJECT(StaticModelRenderer)30REGISTER_META_OBJECT(StaticModelRenderer)
31 31
@@ -33,8 +33,10 @@ static int bufferPointVectorMetaTypeId = qRegisterMetaType<BufferPointVector>(); · 1 unmodified line
33 33
34void MetavoxelSystem::init() {34void MetavoxelSystem::init() {
35 MetavoxelClientManager::init();35 MetavoxelClientManager::init();
36 PointMetavoxelRendererImplementation::init();36 DefaultMetavoxelRendererImplementation::init();
37 _pointBufferAttribute = AttributeRegistry::getInstance()->registerAttribute(new PointBufferAttribute());37 _pointBufferAttribute = AttributeRegistry::getInstance()->registerAttribute(new BufferDataAttribute("pointBuffer"));
38 _heightfieldBufferAttribute = AttributeRegistry::getInstance()->registerAttribute(
39 new BufferDataAttribute("heightfieldBuffer"));
38}40}
39 41
40MetavoxelLOD MetavoxelSystem::getLOD() {42MetavoxelLOD MetavoxelSystem::getLOD() {
@@ -42,28 +44,31 @@ MetavoxelLOD MetavoxelSystem::getLOD() { · 1 unmodified line
42 return _lod;44 return _lod;
43}45}
44 46
45class SpannerSimulateVisitor : public SpannerVisitor {47class SimulateVisitor : public MetavoxelVisitor {
46public:48public:
47 49
48 SpannerSimulateVisitor(float deltaTime);50 SimulateVisitor(float deltaTime, const MetavoxelLOD& lod);
49 51
50 virtual bool visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize);52 virtual int visit(MetavoxelInfo& info);
51 53
52private:54private:
53 55
54 float _deltaTime;56 float _deltaTime;
55};57};
56 58
57SpannerSimulateVisitor::SpannerSimulateVisitor(float deltaTime) :59SimulateVisitor::SimulateVisitor(float deltaTime, const MetavoxelLOD& lod) :
58 SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(),60 MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getRendererAttribute(),
59 QVector<AttributePointer>(), QVector<AttributePointer>(), QVector<AttributePointer>(),61 QVector<AttributePointer>(), lod),
60 Application::getInstance()->getMetavoxels()->getLOD()),
61 _deltaTime(deltaTime) {62 _deltaTime(deltaTime) {
62}63}
63 64
64bool SpannerSimulateVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) {65int SimulateVisitor::visit(MetavoxelInfo& info) {
65 spanner->getRenderer()->simulate(_deltaTime);66 if (!info.isLeaf) {
66 return true;67 return DEFAULT_ORDER;
68 }
69 static_cast<MetavoxelRenderer*>(info.inputValues.at(0).getInlineValue<
70 SharedObjectPointer>().data())->getImplementation()->simulate(*_data, _deltaTime, info, _lod);
71 return STOP_RECURSION;
67}72}
68 73
69void MetavoxelSystem::simulate(float deltaTime) {74void MetavoxelSystem::simulate(float deltaTime) {
@@ -76,28 +81,8 @@ void MetavoxelSystem::simulate(float deltaTime) { · 6 unmodified lines
76 BASE_LOD_THRESHOLD * Menu::getInstance()->getAvatarLODDistanceMultiplier());81 BASE_LOD_THRESHOLD * Menu::getInstance()->getAvatarLODDistanceMultiplier());
77 }82 }
78 83
79 SpannerSimulateVisitor spannerSimulateVisitor(deltaTime);84 SimulateVisitor simulateVisitor(deltaTime, getLOD());
80 guide(spannerSimulateVisitor);85 guideToAugmented(simulateVisitor);
81}
82
83class SpannerRenderVisitor : public SpannerVisitor {
84public:
85
86 SpannerRenderVisitor();
87
88 virtual bool visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize);
89};
90
91SpannerRenderVisitor::SpannerRenderVisitor() :
92 SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(),
93 QVector<AttributePointer>(), QVector<AttributePointer>(), QVector<AttributePointer>(),
94 Application::getInstance()->getMetavoxels()->getLOD(),
95 encodeOrder(Application::getInstance()->getViewFrustum()->getDirection())) {
96}
97
98bool SpannerRenderVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) {
99 spanner->getRenderer()->render(1.0f, SpannerRenderer::DEFAULT_MODE, clipMinimum, clipSize);
100 return true;
101}86}
102 87
103class RenderVisitor : public MetavoxelVisitor {88class RenderVisitor : public MetavoxelVisitor {
@@ -125,9 +110,6 @@ int RenderVisitor::visit(MetavoxelInfo& info) { · 21 unmodified lines
125void MetavoxelSystem::render() {110void MetavoxelSystem::render() {
126 RenderVisitor renderVisitor(getLOD());111 RenderVisitor renderVisitor(getLOD());
127 guideToAugmented(renderVisitor);112 guideToAugmented(renderVisitor);
128
129 SpannerRenderVisitor spannerRenderVisitor;
130 guide(spannerRenderVisitor);
131}113}
132 114
133MetavoxelClient* MetavoxelSystem::createClient(const SharedNodePointer& node) {115MetavoxelClient* MetavoxelSystem::createClient(const SharedNodePointer& node) {
@@ -239,6 +221,9 @@ void MetavoxelSystemClient::sendDatagram(const QByteArray& data) { · 105 unmodified lines
239 Application::getInstance()->getBandwidthMeter()->outputStream(BandwidthMeter::METAVOXELS).updateValue(data.size());221 Application::getInstance()->getBandwidthMeter()->outputStream(BandwidthMeter::METAVOXELS).updateValue(data.size());
240}222}
241 223
224BufferData::~BufferData() {
225}
226
242PointBuffer::PointBuffer(const BufferPointVector& points) :227PointBuffer::PointBuffer(const BufferPointVector& points) :
243 _points(points) {228 _points(points) {
244}229}
@@ -269,34 +254,76 @@ void PointBuffer::render() { · 24 unmodified lines
269 _buffer.release();254 _buffer.release();
270}255}
271 256
272PointBufferAttribute::PointBufferAttribute() :257HeightfieldBuffer::HeightfieldBuffer(const QByteArray& height, const QByteArray& color, const QByteArray& normal) :
273 InlineAttribute<PointBufferPointer>("pointBuffer") {258 _height(height),
259 _color(color),
260 _normal(normal),
261 _heightTexture(QOpenGLTexture::Target2D),
262 _colorTexture(QOpenGLTexture::Target2D),
263 _normalTexture(QOpenGLTexture::Target2D) {
264}
265
266void HeightfieldBuffer::render() {
267 // initialize textures, etc. on first render
268 if (!_heightTexture.isCreated()) {
269 int heightSize = glm::sqrt(_height.size());
270 _heightTexture.setSize(heightSize, heightSize);
271 _heightTexture.setFormat(QOpenGLTexture::LuminanceFormat);
272 _heightTexture.allocateStorage();
273 _heightTexture.setData(QOpenGLTexture::Luminance, QOpenGLTexture::UInt8, _height.data());
274 _height.clear();
275
276 int colorSize = glm::sqrt(_color.size() / 3);
277 _colorTexture.setSize(colorSize, colorSize);
278 _colorTexture.setFormat(QOpenGLTexture::RGBFormat);
279 _colorTexture.allocateStorage();
280 _colorTexture.setData(QOpenGLTexture::BGR, QOpenGLTexture::UInt8, _color.data());
281 _color.clear();
282
283 int normalSize = glm::sqrt(_normal.size() / 3);
284 _normalTexture.setSize(normalSize, normalSize);
285 _normalTexture.setFormat(QOpenGLTexture::RGBFormat);
286 _normalTexture.allocateStorage();
287 _normalTexture.setData(QOpenGLTexture::BGR, QOpenGLTexture::UInt8, _normal.data());
288 _normal.clear();
289 }
290
274}291}
275 292
276bool PointBufferAttribute::merge(void*& parent, void* children[], bool postRead) const {293BufferDataAttribute::BufferDataAttribute(const QString& name) :
277 PointBufferPointer firstChild = decodeInline<PointBufferPointer>(children[0]);294 InlineAttribute<BufferDataPointer>(name) {
295}
296
297bool BufferDataAttribute::merge(void*& parent, void* children[], bool postRead) const {
298 BufferDataPointer firstChild = decodeInline<BufferDataPointer>(children[0]);
278 for (int i = 1; i < MERGE_COUNT; i++) {299 for (int i = 1; i < MERGE_COUNT; i++) {
279 if (firstChild != decodeInline<PointBufferPointer>(children[i])) {300 if (firstChild != decodeInline<BufferDataPointer>(children[i])) {
280 *(PointBufferPointer*)&parent = _defaultValue;301 *(BufferDataPointer*)&parent = _defaultValue;
281 return false;302 return false;
282 }303 }
283 }304 }
284 *(PointBufferPointer*)&parent = firstChild;305 *(BufferDataPointer*)&parent = firstChild;
285 return true;306 return true;
286}307}
287 308
288void PointMetavoxelRendererImplementation::init() {309void DefaultMetavoxelRendererImplementation::init() {
289 if (!_program.isLinked()) {310 if (!_pointProgram.isLinked()) {
290 _program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/metavoxel_point.vert");311 _pointProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/metavoxel_point.vert");
291 _program.link();312 _pointProgram.link();
292 313
293 _program.bind();314 _pointProgram.bind();
294 _pointScaleLocation = _program.uniformLocation("pointScale");315 _pointScaleLocation = _pointProgram.uniformLocation("pointScale");
295 _program.release();316 _pointProgram.release();
317
318 _heightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
319 "shaders/metavoxel_heightfield.vert");
320 _heightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
321 "shaders/metavoxel_heightfield.frag");
322 _heightfieldProgram.link();
296 }323 }
297}324}
298 325
299PointMetavoxelRendererImplementation::PointMetavoxelRendererImplementation() {326DefaultMetavoxelRendererImplementation::DefaultMetavoxelRendererImplementation() {
300}327}
301 328
302class PointAugmentVisitor : public MetavoxelVisitor {329class PointAugmentVisitor : public MetavoxelVisitor {
@@ -344,7 +371,7 @@ int PointAugmentVisitor::visit(MetavoxelInfo& info) { · 41 unmodified lines
344 if (info.size >= _pointLeafSize) { 371 if (info.size >= _pointLeafSize) {
345 BufferPointVector swapPoints;372 BufferPointVector swapPoints;
346 _points.swap(swapPoints);373 _points.swap(swapPoints);
347 info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(PointBufferPointer(374 info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(BufferDataPointer(
348 new PointBuffer(swapPoints))));375 new PointBuffer(swapPoints))));
349 }376 }
350 return STOP_RECURSION;377 return STOP_RECURSION;
@@ -356,12 +383,30 @@ bool PointAugmentVisitor::postVisit(MetavoxelInfo& info) { · 5 unmodified lines
356 }383 }
357 BufferPointVector swapPoints;384 BufferPointVector swapPoints;
358 _points.swap(swapPoints);385 _points.swap(swapPoints);
359 info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(PointBufferPointer(386 info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(BufferDataPointer(
360 new PointBuffer(swapPoints))));387 new PointBuffer(swapPoints))));
361 return true;388 return true;
362}389}
363 390
364void PointMetavoxelRendererImplementation::augment(MetavoxelData& data, const MetavoxelData& previous,391class HeightfieldAugmentVisitor : public MetavoxelVisitor {
392public:
393
394 HeightfieldAugmentVisitor(const MetavoxelLOD& lod);
395
396 virtual int visit(MetavoxelInfo& info);
397};
398
399HeightfieldAugmentVisitor::HeightfieldAugmentVisitor(const MetavoxelLOD& lod) :
400 MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getHeightfieldAttribute() <<
401 AttributeRegistry::getInstance()->getHeightfieldColorAttribute(), QVector<AttributePointer>() <<
402 Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), lod) {
403}
404
405int HeightfieldAugmentVisitor::visit(MetavoxelInfo& info) {
406 return STOP_RECURSION;
407}
408
409void DefaultMetavoxelRendererImplementation::augment(MetavoxelData& data, const MetavoxelData& previous,
365 MetavoxelInfo& info, const MetavoxelLOD& lod) {410 MetavoxelInfo& info, const MetavoxelLOD& lod) {
366 // copy the previous buffers411 // copy the previous buffers
367 MetavoxelData expandedPrevious = previous;412 MetavoxelData expandedPrevious = previous;
@@ -377,12 +422,62 @@ void PointMetavoxelRendererImplementation::augment(MetavoxelData& data, const Me · 9 unmodified lines
377 422
378 PointAugmentVisitor visitor(lod);423 PointAugmentVisitor visitor(lod);
379 data.guideToDifferent(expandedPrevious, visitor);424 data.guideToDifferent(expandedPrevious, visitor);
425
426
427}
428
429class SpannerSimulateVisitor : public SpannerVisitor {
430public:
431
432 SpannerSimulateVisitor(float deltaTime, const MetavoxelLOD& lod);
433
434 virtual bool visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize);
435
436private:
437
438 float _deltaTime;
439};
440
441SpannerSimulateVisitor::SpannerSimulateVisitor(float deltaTime, const MetavoxelLOD& lod) :
442 SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(),
443 QVector<AttributePointer>(), QVector<AttributePointer>(), QVector<AttributePointer>(), lod),
444 _deltaTime(deltaTime) {
445}
446
447bool SpannerSimulateVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) {
448 spanner->getRenderer()->simulate(_deltaTime);
449 return true;
450}
451
452void DefaultMetavoxelRendererImplementation::simulate(MetavoxelData& data, float deltaTime,
453 MetavoxelInfo& info, const MetavoxelLOD& lod) {
454 SpannerSimulateVisitor spannerSimulateVisitor(deltaTime, lod);
455 data.guide(spannerSimulateVisitor);
380}456}
381 457
382class PointRenderVisitor : public MetavoxelVisitor {458class SpannerRenderVisitor : public SpannerVisitor {
383public:459public:
384 460
385 PointRenderVisitor(const MetavoxelLOD& lod);461 SpannerRenderVisitor(const MetavoxelLOD& lod);
462
463 virtual bool visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize);
464};
465
466SpannerRenderVisitor::SpannerRenderVisitor(const MetavoxelLOD& lod) :
467 SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(),
468 QVector<AttributePointer>(), QVector<AttributePointer>(), QVector<AttributePointer>(),
469 lod, encodeOrder(Application::getInstance()->getViewFrustum()->getDirection())) {
470}
471
472bool SpannerRenderVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) {
473 spanner->getRenderer()->render(1.0f, SpannerRenderer::DEFAULT_MODE, clipMinimum, clipSize);
474 return true;
475}
476
477class BufferRenderVisitor : public MetavoxelVisitor {
478public:
479
480 BufferRenderVisitor(const AttributePointer& attribute, const MetavoxelLOD& lod);
386 481
387 virtual int visit(MetavoxelInfo& info);482 virtual int visit(MetavoxelInfo& info);
388 483
@@ -391,21 +486,23 @@ private: · 2 unmodified lines
391 int _order;486 int _order;
392};487};
393 488
394PointRenderVisitor::PointRenderVisitor(const MetavoxelLOD& lod) :489BufferRenderVisitor::BufferRenderVisitor(const AttributePointer& attribute, const MetavoxelLOD& lod) :
395 MetavoxelVisitor(QVector<AttributePointer>() << Application::getInstance()->getMetavoxels()->getPointBufferAttribute(),490 MetavoxelVisitor(QVector<AttributePointer>() << attribute, QVector<AttributePointer>(), lod),
396 QVector<AttributePointer>(), lod),
397 _order(encodeOrder(Application::getInstance()->getViewFrustum()->getDirection())) {491 _order(encodeOrder(Application::getInstance()->getViewFrustum()->getDirection())) {
398}492}
399 493
400int PointRenderVisitor::visit(MetavoxelInfo& info) {494int BufferRenderVisitor::visit(MetavoxelInfo& info) {
401 PointBufferPointer buffer = info.inputValues.at(0).getInlineValue<PointBufferPointer>();495 BufferDataPointer buffer = info.inputValues.at(0).getInlineValue<BufferDataPointer>();
402 if (buffer) {496 if (buffer) {
403 buffer->render();497 buffer->render();
404 }498 }
405 return info.isLeaf ? STOP_RECURSION : _order;499 return info.isLeaf ? STOP_RECURSION : _order;
406}500}
407 501
408void PointMetavoxelRendererImplementation::render(MetavoxelData& data, MetavoxelInfo& info, const MetavoxelLOD& lod) {502void DefaultMetavoxelRendererImplementation::render(MetavoxelData& data, MetavoxelInfo& info, const MetavoxelLOD& lod) {
503 SpannerRenderVisitor spannerRenderVisitor(lod);
504 data.guide(spannerRenderVisitor);
505
409 int viewport[4];506 int viewport[4];
410 glGetIntegerv(GL_VIEWPORT, viewport);507 glGetIntegerv(GL_VIEWPORT, viewport);
411 const int VIEWPORT_WIDTH_INDEX = 2;508 const int VIEWPORT_WIDTH_INDEX = 2;
@@ -416,8 +513,8 @@ void PointMetavoxelRendererImplementation::render(MetavoxelData& data, Metavoxel · 4 unmodified lines
416 float worldDiagonal = glm::distance(Application::getInstance()->getViewFrustum()->getNearBottomLeft(),513 float worldDiagonal = glm::distance(Application::getInstance()->getViewFrustum()->getNearBottomLeft(),
417 Application::getInstance()->getViewFrustum()->getNearTopRight());514 Application::getInstance()->getViewFrustum()->getNearTopRight());
418 515
419 _program.bind();516 _pointProgram.bind();
420 _program.setUniformValue(_pointScaleLocation, viewportDiagonal *517 _pointProgram.setUniformValue(_pointScaleLocation, viewportDiagonal *
421 Application::getInstance()->getViewFrustum()->getNearClip() / worldDiagonal);518 Application::getInstance()->getViewFrustum()->getNearClip() / worldDiagonal);
422 519
423 glEnableClientState(GL_VERTEX_ARRAY);520 glEnableClientState(GL_VERTEX_ARRAY);
@@ -428,10 +525,8 @@ void PointMetavoxelRendererImplementation::render(MetavoxelData& data, Metavoxel · 4 unmodified lines
428 525
429 glDisable(GL_BLEND);526 glDisable(GL_BLEND);
430 527
431 PointRenderVisitor visitor(lod);528 BufferRenderVisitor pointRenderVisitor(Application::getInstance()->getMetavoxels()->getPointBufferAttribute(), lod);
432 data.guide(visitor);529 data.guide(pointRenderVisitor);
433
434 glEnable(GL_BLEND);
435 530
436 glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);531 glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
437 532
@@ -439,11 +534,18 @@ void PointMetavoxelRendererImplementation::render(MetavoxelData& data, Metavoxel · 1 unmodified line
439 glDisableClientState(GL_COLOR_ARRAY);534 glDisableClientState(GL_COLOR_ARRAY);
440 glDisableClientState(GL_NORMAL_ARRAY);535 glDisableClientState(GL_NORMAL_ARRAY);
441 536
442 _program.release();537 _pointProgram.release();
538
539 BufferRenderVisitor heightfieldRenderVisitor(Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(),
540 lod);
541 data.guide(heightfieldRenderVisitor);
542
543 glEnable(GL_BLEND);
443}544}
444 545
445ProgramObject PointMetavoxelRendererImplementation::_program;546ProgramObject DefaultMetavoxelRendererImplementation::_pointProgram;
446int PointMetavoxelRendererImplementation::_pointScaleLocation;547int DefaultMetavoxelRendererImplementation::_pointScaleLocation;
548ProgramObject DefaultMetavoxelRendererImplementation::_heightfieldProgram;
447 549
448static void enableClipPlane(GLenum plane, float x, float y, float z, float w) {550static void enableClipPlane(GLenum plane, float x, float y, float z, float w) {
449 GLdouble coefficients[] = { x, y, z, w };551 GLdouble coefficients[] = { x, y, z, w };