History

Working on rejiggering the heightfield tiles.

Andrzej Kapolka committed 12 years ago

Commit 2c0f8ff1 parent 10315f15 files · +100 −46Browse files

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

Unified · Split

interface/src/MetavoxelSystem.cpp+75−25
@@ -366,6 +366,7 @@ void HeightfieldBuffer::render(bool cursor) { · 365 unmodified lines
366 if (_heightTextureID == 0) {366 if (_heightTextureID == 0) {
367 glGenTextures(1, &_heightTextureID);367 glGenTextures(1, &_heightTextureID);
368 glBindTexture(GL_TEXTURE_2D, _heightTextureID);368 glBindTexture(GL_TEXTURE_2D, _heightTextureID);
369 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
369 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);370 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
370 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);371 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
371 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);372 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
@@ -385,7 +386,7 @@ void HeightfieldBuffer::render(bool cursor) { · 13 unmodified lines
385 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, WHITE_COLOR);386 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, WHITE_COLOR);
386 387
387 } else {388 } else {
388 int colorSize = glm::sqrt(_color.size() / 3);389 int colorSize = glm::sqrt(_color.size() / 3);
389 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, colorSize, colorSize, 0, GL_RGB, GL_UNSIGNED_BYTE, _color.constData());390 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, colorSize, colorSize, 0, GL_RGB, GL_UNSIGNED_BYTE, _color.constData());
390 if (_clearAfterLoading) {391 if (_clearAfterLoading) {
391 _color.clear();392 _color.clear();
@@ -393,24 +394,27 @@ void HeightfieldBuffer::render(bool cursor) { · 1 unmodified line
393 }394 }
394 }395 }
395 // create the buffer objects lazily396 // create the buffer objects lazily
396 int sizeWithSkirt = _heightSize + 2;397 int innerSize = _heightSize - 2;
397 int vertexCount = sizeWithSkirt * sizeWithSkirt;398 int vertexCount = _heightSize * _heightSize;
398 int rows = sizeWithSkirt - 1;399 int rows = _heightSize - 1;
399 int indexCount = rows * rows * 4;400 int indexCount = rows * rows * 4;
400 BufferPair& bufferPair = _bufferPairs[_heightSize];401 BufferPair& bufferPair = _bufferPairs[_heightSize];
401 if (!bufferPair.first.isCreated()) {402 if (!bufferPair.first.isCreated()) {
402 QVector<HeightfieldPoint> vertices(vertexCount);403 QVector<HeightfieldPoint> vertices(vertexCount);
403 HeightfieldPoint* point = vertices.data();404 HeightfieldPoint* point = vertices.data();
404 405
405 float step = 1.0f / (_heightSize - 1);406 float vertexStep = 1.0f / (innerSize - 1);
406 float z = -step;407 float z = -vertexStep;
407 for (int i = 0; i < sizeWithSkirt; i++, z += step) {408 float textureStep = 1.0f / _heightSize;
408 float x = -step;409 float t = textureStep / 2.0f;
410 for (int i = 0; i < _heightSize; i++, z += vertexStep, t += textureStep) {
411 float x = -vertexStep;
412 float s = textureStep / 2.0f;
409 const float SKIRT_LENGTH = 0.25f;413 const float SKIRT_LENGTH = 0.25f;
410 float baseY = (i == 0 || i == sizeWithSkirt - 1) ? -SKIRT_LENGTH : 0.0f;414 float baseY = (i == 0 || i == _heightSize - 1) ? -SKIRT_LENGTH : 0.0f;
411 for (int j = 0; j < sizeWithSkirt; j++, point++, x += step) {415 for (int j = 0; j < _heightSize; j++, point++, x += vertexStep, s += textureStep) {
412 point->vertex = glm::vec3(x, (j == 0 || j == sizeWithSkirt - 1) ? -SKIRT_LENGTH : baseY, z);416 point->vertex = glm::vec3(x, (j == 0 || j == _heightSize - 1) ? -SKIRT_LENGTH : baseY, z);
413 point->textureCoord = glm::vec2(x, z);417 point->textureCoord = glm::vec2(s, t);
414 }418 }
415 }419 }
416 420
@@ -422,8 +426,8 @@ void HeightfieldBuffer::render(bool cursor) { · 5 unmodified lines
422 QVector<int> indices(indexCount);426 QVector<int> indices(indexCount);
423 int* index = indices.data();427 int* index = indices.data();
424 for (int i = 0; i < rows; i++) {428 for (int i = 0; i < rows; i++) {
425 int lineIndex = i * sizeWithSkirt;429 int lineIndex = i * _heightSize;
426 int nextLineIndex = (i + 1) * sizeWithSkirt;430 int nextLineIndex = (i + 1) * _heightSize;
427 for (int j = 0; j < rows; j++) {431 for (int j = 0; j < rows; j++) {
428 *index++ = lineIndex + j;432 *index++ = lineIndex + j;
429 *index++ = nextLineIndex + j;433 *index++ = nextLineIndex + j;
@@ -452,17 +456,15 @@ void HeightfieldBuffer::render(bool cursor) { · 22 unmodified lines
452 456
453 glBindTexture(GL_TEXTURE_2D, _heightTextureID);457 glBindTexture(GL_TEXTURE_2D, _heightTextureID);
454 458
455 int heightScaleLocation;459 if (!cursor) {
456 if (cursor) {460 DefaultMetavoxelRendererImplementation::getHeightfieldProgram().setUniformValue(
457 heightScaleLocation = DefaultMetavoxelRendererImplementation::getCursorHeightScaleLocation();461 DefaultMetavoxelRendererImplementation::getHeightScaleLocation(), 1.0f / _heightSize);
458 } else {462 DefaultMetavoxelRendererImplementation::getHeightfieldProgram().setUniformValue(
459 heightScaleLocation = DefaultMetavoxelRendererImplementation::getHeightScaleLocation();463 DefaultMetavoxelRendererImplementation::getColorScaleLocation(), (float)_heightSize / innerSize);
460 glActiveTexture(GL_TEXTURE1);464 glActiveTexture(GL_TEXTURE1);
461 glBindTexture(GL_TEXTURE_2D, _colorTextureID);465 glBindTexture(GL_TEXTURE_2D, _colorTextureID);
462 }466 }
463 467
464 DefaultMetavoxelRendererImplementation::getHeightfieldProgram().setUniformValue(heightScaleLocation, 1.0f / _heightSize);
465
466 glDrawRangeElements(GL_QUADS, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0);468 glDrawRangeElements(GL_QUADS, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0);
467 469
468 if (!cursor) {470 if (!cursor) {
@@ -548,6 +550,7 @@ void DefaultMetavoxelRendererImplementation::init() { · 79 unmodified lines
548 _heightfieldProgram.setUniformValue("heightMap", 0);550 _heightfieldProgram.setUniformValue("heightMap", 0);
549 _heightfieldProgram.setUniformValue("diffuseMap", 1);551 _heightfieldProgram.setUniformValue("diffuseMap", 1);
550 _heightScaleLocation = _heightfieldProgram.uniformLocation("heightScale");552 _heightScaleLocation = _heightfieldProgram.uniformLocation("heightScale");
553 _colorScaleLocation = _heightfieldProgram.uniformLocation("colorScale");
551 _heightfieldProgram.release();554 _heightfieldProgram.release();
552 555
553 _heightfieldCursorProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +556 _heightfieldCursorProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
@@ -558,7 +561,6 @@ void DefaultMetavoxelRendererImplementation::init() { · 4 unmodified lines
558 561
559 _heightfieldCursorProgram.bind();562 _heightfieldCursorProgram.bind();
560 _heightfieldCursorProgram.setUniformValue("heightMap", 0);563 _heightfieldCursorProgram.setUniformValue("heightMap", 0);
561 _cursorHeightScaleLocation = _heightfieldCursorProgram.uniformLocation("heightScale");
562 _heightfieldCursorProgram.release();564 _heightfieldCursorProgram.release();
563 }565 }
564}566}
@@ -648,14 +650,62 @@ HeightfieldAugmentVisitor::HeightfieldAugmentVisitor(const MetavoxelLOD& lod) : · 83 unmodified lines
648 Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), lod) {650 Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), lod) {
649}651}
650 652
653class BorderFetchVisitor : public MetavoxelVisitor {
654public:
655
656 BorderFetchVisitor(const MetavoxelLOD& lod, QByteArray& height);
657
658 virtual int visit(MetavoxelInfo& info);
659
660private:
661
662 QByteArray& _height;
663};
664
665BorderFetchVisitor::BorderFetchVisitor(const MetavoxelLOD& lod, QByteArray& height) :
666 MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getHeightfieldAttribute(),
667 QVector<AttributePointer>(), lod),
668 _height(height) {
669}
670
671int BorderFetchVisitor::visit(MetavoxelInfo& info) {
672 if (!info.isLeaf) {
673 return DEFAULT_ORDER;
674 }
675 return STOP_RECURSION;
676}
677
651int HeightfieldAugmentVisitor::visit(MetavoxelInfo& info) {678int HeightfieldAugmentVisitor::visit(MetavoxelInfo& info) {
652 if (info.isLeaf) {679 if (info.isLeaf) {
653 HeightfieldBuffer* buffer = NULL;680 HeightfieldBuffer* buffer = NULL;
654 HeightfieldDataPointer height = info.inputValues.at(0).getInlineValue<HeightfieldDataPointer>();681 HeightfieldDataPointer height = info.inputValues.at(0).getInlineValue<HeightfieldDataPointer>();
655 if (height) {682 if (height) {
683 const QByteArray& heightContents = height->getContents();
684 int size = glm::sqrt(heightContents.size());
685 int extendedSize = size + 3;
686 QByteArray extendedHeightContents(extendedSize * extendedSize, 0);
687 char* dest = extendedHeightContents.data() + extendedSize + 1;
688 const char* src = heightContents.constData();
689 for (int z = 0; z < size; z++, src += size, dest += extendedSize) {
690 memcpy(dest, src, size);
691 }
692 QByteArray extendedColorContents;
656 HeightfieldDataPointer color = info.inputValues.at(1).getInlineValue<HeightfieldDataPointer>();693 HeightfieldDataPointer color = info.inputValues.at(1).getInlineValue<HeightfieldDataPointer>();
657 buffer = new HeightfieldBuffer(info.minimum, info.size, height->getContents(),694 if (color) {
658 color ? color->getContents() : QByteArray());695 const QByteArray& colorContents = color->getContents();
696 const int BYTES_PER_PIXEL = 3;
697 int colorSize = glm::sqrt(colorContents.size() / BYTES_PER_PIXEL);
698 int extendedColorSize = colorSize + 1;
699 extendedColorContents = QByteArray(extendedColorSize * extendedColorSize * BYTES_PER_PIXEL, 0);
700 char* dest = extendedColorContents.data();
701 const char* src = colorContents.constData();
702 int srcStride = colorSize * BYTES_PER_PIXEL;
703 int destStride = extendedColorSize * BYTES_PER_PIXEL;
704 for (int z = 0; z < colorSize; z++, src += srcStride, dest += destStride) {
705 memcpy(dest, src, srcStride);
706 }
707 }
708 buffer = new HeightfieldBuffer(info.minimum, info.size, extendedHeightContents, extendedColorContents);
659 }709 }
660 info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(BufferDataPointer(buffer)));710 info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(BufferDataPointer(buffer)));
661 return STOP_RECURSION;711 return STOP_RECURSION;
@@ -856,8 +906,8 @@ ProgramObject DefaultMetavoxelRendererImplementation::_pointProgram; · 194 unmodified lines
856int DefaultMetavoxelRendererImplementation::_pointScaleLocation;906int DefaultMetavoxelRendererImplementation::_pointScaleLocation;
857ProgramObject DefaultMetavoxelRendererImplementation::_heightfieldProgram;907ProgramObject DefaultMetavoxelRendererImplementation::_heightfieldProgram;
858int DefaultMetavoxelRendererImplementation::_heightScaleLocation;908int DefaultMetavoxelRendererImplementation::_heightScaleLocation;
909int DefaultMetavoxelRendererImplementation::_colorScaleLocation;
859ProgramObject DefaultMetavoxelRendererImplementation::_heightfieldCursorProgram;910ProgramObject DefaultMetavoxelRendererImplementation::_heightfieldCursorProgram;
860int DefaultMetavoxelRendererImplementation::_cursorHeightScaleLocation;
861 911
862static void enableClipPlane(GLenum plane, float x, float y, float z, float w) {912static void enableClipPlane(GLenum plane, float x, float y, float z, float w) {
863 GLdouble coefficients[] = { x, y, z, w };913 GLdouble coefficients[] = { x, y, z, w };