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
2525 #include "MetavoxelSystem.h"
2626 #include "renderer/Model.h"
2727
28−REGISTER_META_OBJECT(PointMetavoxelRendererImplementation)
28+REGISTER_META_OBJECT(DefaultMetavoxelRendererImplementation)
2929 REGISTER_META_OBJECT(SphereRenderer)
3030 REGISTER_META_OBJECT(StaticModelRenderer)
3131
@@ -33,8 +33,10 @@ static int bufferPointVectorMetaTypeId = qRegisterMetaType<BufferPointVector>(); · 1 unmodified line
3333
3434 void MetavoxelSystem::init() {
3535 MetavoxelClientManager::init();
36− PointMetavoxelRendererImplementation::init();
37− _pointBufferAttribute = AttributeRegistry::getInstance()->registerAttribute(new PointBufferAttribute());
36+ DefaultMetavoxelRendererImplementation::init();
37+ _pointBufferAttribute = AttributeRegistry::getInstance()->registerAttribute(new BufferDataAttribute("pointBuffer"));
38+ _heightfieldBufferAttribute = AttributeRegistry::getInstance()->registerAttribute(
39+ new BufferDataAttribute("heightfieldBuffer"));
3840 }
3941
4042 MetavoxelLOD MetavoxelSystem::getLOD() {
@@ -42,28 +44,31 @@ MetavoxelLOD MetavoxelSystem::getLOD() { · 1 unmodified line
4244 return _lod;
4345 }
4446
45−class SpannerSimulateVisitor : public SpannerVisitor {
47+class SimulateVisitor : public MetavoxelVisitor {
4648 public:
4749
48− SpannerSimulateVisitor(float deltaTime);
50+ SimulateVisitor(float deltaTime, const MetavoxelLOD& lod);
4951
50− virtual bool visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize);
52+ virtual int visit(MetavoxelInfo& info);
5153
5254 private:
5355
5456 float _deltaTime;
5557 };
5658
57−SpannerSimulateVisitor::SpannerSimulateVisitor(float deltaTime) :
58− SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(),
59− QVector<AttributePointer>(), QVector<AttributePointer>(), QVector<AttributePointer>(),
60− Application::getInstance()->getMetavoxels()->getLOD()),
59+SimulateVisitor::SimulateVisitor(float deltaTime, const MetavoxelLOD& lod) :
60+ MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getRendererAttribute(),
61+ QVector<AttributePointer>(), lod),
6162 _deltaTime(deltaTime) {
6263 }
6364
64−bool SpannerSimulateVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) {
65− spanner->getRenderer()->simulate(_deltaTime);
66− return true;
65+int SimulateVisitor::visit(MetavoxelInfo& info) {
66+ if (!info.isLeaf) {
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;
6772 }
6873
6974 void MetavoxelSystem::simulate(float deltaTime) {
@@ -76,28 +81,8 @@ void MetavoxelSystem::simulate(float deltaTime) { · 6 unmodified lines
7681 BASE_LOD_THRESHOLD * Menu::getInstance()->getAvatarLODDistanceMultiplier());
7782 }
7883
79− SpannerSimulateVisitor spannerSimulateVisitor(deltaTime);
80− guide(spannerSimulateVisitor);
81−}
82−
83−class SpannerRenderVisitor : public SpannerVisitor {
84−public:
85−
86− SpannerRenderVisitor();
87−
88− virtual bool visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize);
89−};
90−
91−SpannerRenderVisitor::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−
98−bool SpannerRenderVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) {
99− spanner->getRenderer()->render(1.0f, SpannerRenderer::DEFAULT_MODE, clipMinimum, clipSize);
100− return true;
84+ SimulateVisitor simulateVisitor(deltaTime, getLOD());
85+ guideToAugmented(simulateVisitor);
10186 }
10287
10388 class RenderVisitor : public MetavoxelVisitor {
@@ -125,9 +110,6 @@ int RenderVisitor::visit(MetavoxelInfo& info) { · 21 unmodified lines
125110 void MetavoxelSystem::render() {
126111 RenderVisitor renderVisitor(getLOD());
127112 guideToAugmented(renderVisitor);
128−
129− SpannerRenderVisitor spannerRenderVisitor;
130− guide(spannerRenderVisitor);
131113 }
132114
133115 MetavoxelClient* MetavoxelSystem::createClient(const SharedNodePointer& node) {
@@ -239,6 +221,9 @@ void MetavoxelSystemClient::sendDatagram(const QByteArray& data) { · 105 unmodified lines
239221 Application::getInstance()->getBandwidthMeter()->outputStream(BandwidthMeter::METAVOXELS).updateValue(data.size());
240222 }
241223
224+BufferData::~BufferData() {
225+}
226+
242227 PointBuffer::PointBuffer(const BufferPointVector& points) :
243228 _points(points) {
244229 }
@@ -269,34 +254,76 @@ void PointBuffer::render() { · 24 unmodified lines
269254 _buffer.release();
270255 }
271256
272−PointBufferAttribute::PointBufferAttribute() :
273− InlineAttribute<PointBufferPointer>("pointBuffer") {
257+HeightfieldBuffer::HeightfieldBuffer(const QByteArray& height, const QByteArray& color, const QByteArray& normal) :
258+ _height(height),
259+ _color(color),
260+ _normal(normal),
261+ _heightTexture(QOpenGLTexture::Target2D),
262+ _colorTexture(QOpenGLTexture::Target2D),
263+ _normalTexture(QOpenGLTexture::Target2D) {
264+}
265+
266+void 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+
274291 }
275292
276−bool PointBufferAttribute::merge(void*& parent, void* children[], bool postRead) const {
277− PointBufferPointer firstChild = decodeInline<PointBufferPointer>(children[0]);
293+BufferDataAttribute::BufferDataAttribute(const QString& name) :
294+ InlineAttribute<BufferDataPointer>(name) {
295+}
296+
297+bool BufferDataAttribute::merge(void*& parent, void* children[], bool postRead) const {
298+ BufferDataPointer firstChild = decodeInline<BufferDataPointer>(children[0]);
278299 for (int i = 1; i < MERGE_COUNT; i++) {
279− if (firstChild != decodeInline<PointBufferPointer>(children[i])) {
280− *(PointBufferPointer*)&parent = _defaultValue;
300+ if (firstChild != decodeInline<BufferDataPointer>(children[i])) {
301+ *(BufferDataPointer*)&parent = _defaultValue;
281302 return false;
282303 }
283304 }
284− *(PointBufferPointer*)&parent = firstChild;
305+ *(BufferDataPointer*)&parent = firstChild;
285306 return true;
286307 }
287308
288−void PointMetavoxelRendererImplementation::init() {
289− if (!_program.isLinked()) {
290− _program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/metavoxel_point.vert");
291− _program.link();
309+void DefaultMetavoxelRendererImplementation::init() {
310+ if (!_pointProgram.isLinked()) {
311+ _pointProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + "shaders/metavoxel_point.vert");
312+ _pointProgram.link();
292313
293− _program.bind();
294− _pointScaleLocation = _program.uniformLocation("pointScale");
295− _program.release();
314+ _pointProgram.bind();
315+ _pointScaleLocation = _pointProgram.uniformLocation("pointScale");
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();
296323 }
297324 }
298325
299−PointMetavoxelRendererImplementation::PointMetavoxelRendererImplementation() {
326+DefaultMetavoxelRendererImplementation::DefaultMetavoxelRendererImplementation() {
300327 }
301328
302329 class PointAugmentVisitor : public MetavoxelVisitor {
@@ -344,7 +371,7 @@ int PointAugmentVisitor::visit(MetavoxelInfo& info) { · 41 unmodified lines
344371 if (info.size >= _pointLeafSize) {
345372 BufferPointVector swapPoints;
346373 _points.swap(swapPoints);
347− info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(PointBufferPointer(
374+ info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(BufferDataPointer(
348375 new PointBuffer(swapPoints))));
349376 }
350377 return STOP_RECURSION;
@@ -356,12 +383,30 @@ bool PointAugmentVisitor::postVisit(MetavoxelInfo& info) { · 5 unmodified lines
356383 }
357384 BufferPointVector swapPoints;
358385 _points.swap(swapPoints);
359− info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(PointBufferPointer(
386+ info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(BufferDataPointer(
360387 new PointBuffer(swapPoints))));
361388 return true;
362389 }
363390
364−void PointMetavoxelRendererImplementation::augment(MetavoxelData& data, const MetavoxelData& previous,
391+class HeightfieldAugmentVisitor : public MetavoxelVisitor {
392+public:
393+
394+ HeightfieldAugmentVisitor(const MetavoxelLOD& lod);
395+
396+ virtual int visit(MetavoxelInfo& info);
397+};
398+
399+HeightfieldAugmentVisitor::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+
405+int HeightfieldAugmentVisitor::visit(MetavoxelInfo& info) {
406+ return STOP_RECURSION;
407+}
408+
409+void DefaultMetavoxelRendererImplementation::augment(MetavoxelData& data, const MetavoxelData& previous,
365410 MetavoxelInfo& info, const MetavoxelLOD& lod) {
366411 // copy the previous buffers
367412 MetavoxelData expandedPrevious = previous;
@@ -377,12 +422,62 @@ void PointMetavoxelRendererImplementation::augment(MetavoxelData& data, const Me · 9 unmodified lines
377422
378423 PointAugmentVisitor visitor(lod);
379424 data.guideToDifferent(expandedPrevious, visitor);
425+
426+
427+}
428+
429+class SpannerSimulateVisitor : public SpannerVisitor {
430+public:
431+
432+ SpannerSimulateVisitor(float deltaTime, const MetavoxelLOD& lod);
433+
434+ virtual bool visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize);
435+
436+private:
437+
438+ float _deltaTime;
439+};
440+
441+SpannerSimulateVisitor::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+
447+bool SpannerSimulateVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) {
448+ spanner->getRenderer()->simulate(_deltaTime);
449+ return true;
450+}
451+
452+void DefaultMetavoxelRendererImplementation::simulate(MetavoxelData& data, float deltaTime,
453+ MetavoxelInfo& info, const MetavoxelLOD& lod) {
454+ SpannerSimulateVisitor spannerSimulateVisitor(deltaTime, lod);
455+ data.guide(spannerSimulateVisitor);
380456 }
381457
382−class PointRenderVisitor : public MetavoxelVisitor {
458+class SpannerRenderVisitor : public SpannerVisitor {
383459 public:
384460
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+
466+SpannerRenderVisitor::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+
472+bool 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+
477+class BufferRenderVisitor : public MetavoxelVisitor {
478+public:
479+
480+ BufferRenderVisitor(const AttributePointer& attribute, const MetavoxelLOD& lod);
386481
387482 virtual int visit(MetavoxelInfo& info);
388483
@@ -391,21 +486,23 @@ private: · 2 unmodified lines
391486 int _order;
392487 };
393488
394−PointRenderVisitor::PointRenderVisitor(const MetavoxelLOD& lod) :
395− MetavoxelVisitor(QVector<AttributePointer>() << Application::getInstance()->getMetavoxels()->getPointBufferAttribute(),
396− QVector<AttributePointer>(), lod),
489+BufferRenderVisitor::BufferRenderVisitor(const AttributePointer& attribute, const MetavoxelLOD& lod) :
490+ MetavoxelVisitor(QVector<AttributePointer>() << attribute, QVector<AttributePointer>(), lod),
397491 _order(encodeOrder(Application::getInstance()->getViewFrustum()->getDirection())) {
398492 }
399493
400−int PointRenderVisitor::visit(MetavoxelInfo& info) {
401− PointBufferPointer buffer = info.inputValues.at(0).getInlineValue<PointBufferPointer>();
494+int BufferRenderVisitor::visit(MetavoxelInfo& info) {
495+ BufferDataPointer buffer = info.inputValues.at(0).getInlineValue<BufferDataPointer>();
402496 if (buffer) {
403497 buffer->render();
404498 }
405499 return info.isLeaf ? STOP_RECURSION : _order;
406500 }
407501
408−void PointMetavoxelRendererImplementation::render(MetavoxelData& data, MetavoxelInfo& info, const MetavoxelLOD& lod) {
502+void DefaultMetavoxelRendererImplementation::render(MetavoxelData& data, MetavoxelInfo& info, const MetavoxelLOD& lod) {
503+ SpannerRenderVisitor spannerRenderVisitor(lod);
504+ data.guide(spannerRenderVisitor);
505+
409506 int viewport[4];
410507 glGetIntegerv(GL_VIEWPORT, viewport);
411508 const int VIEWPORT_WIDTH_INDEX = 2;
@@ -416,8 +513,8 @@ void PointMetavoxelRendererImplementation::render(MetavoxelData& data, Metavoxel · 4 unmodified lines
416513 float worldDiagonal = glm::distance(Application::getInstance()->getViewFrustum()->getNearBottomLeft(),
417514 Application::getInstance()->getViewFrustum()->getNearTopRight());
418515
419− _program.bind();
420− _program.setUniformValue(_pointScaleLocation, viewportDiagonal *
516+ _pointProgram.bind();
517+ _pointProgram.setUniformValue(_pointScaleLocation, viewportDiagonal *
421518 Application::getInstance()->getViewFrustum()->getNearClip() / worldDiagonal);
422519
423520 glEnableClientState(GL_VERTEX_ARRAY);
@@ -428,10 +525,8 @@ void PointMetavoxelRendererImplementation::render(MetavoxelData& data, Metavoxel · 4 unmodified lines
428525
429526 glDisable(GL_BLEND);
430527
431− PointRenderVisitor visitor(lod);
432− data.guide(visitor);
433−
434− glEnable(GL_BLEND);
528+ BufferRenderVisitor pointRenderVisitor(Application::getInstance()->getMetavoxels()->getPointBufferAttribute(), lod);
529+ data.guide(pointRenderVisitor);
435530
436531 glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
437532
@@ -439,11 +534,18 @@ void PointMetavoxelRendererImplementation::render(MetavoxelData& data, Metavoxel · 1 unmodified line
439534 glDisableClientState(GL_COLOR_ARRAY);
440535 glDisableClientState(GL_NORMAL_ARRAY);
441536
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);
443544 }
444−
445−ProgramObject PointMetavoxelRendererImplementation::_program;
446−int PointMetavoxelRendererImplementation::_pointScaleLocation;
545+
546+ProgramObject DefaultMetavoxelRendererImplementation::_pointProgram;
547+int DefaultMetavoxelRendererImplementation::_pointScaleLocation;
548+ProgramObject DefaultMetavoxelRendererImplementation::_heightfieldProgram;
447549
448550 static void enableClipPlane(GLenum plane, float x, float y, float z, float w) {
449551 GLdouble coefficients[] = { x, y, z, w };