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
366366 if (_heightTextureID == 0) {
367367 glGenTextures(1, &_heightTextureID);
368368 glBindTexture(GL_TEXTURE_2D, _heightTextureID);
369+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
369370 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
370371 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
371372 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
@@ -385,7 +386,7 @@ void HeightfieldBuffer::render(bool cursor) { · 13 unmodified lines
385386 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, WHITE_COLOR);
386387
387388 } else {
388− int colorSize = glm::sqrt(_color.size() / 3);
389+ int colorSize = glm::sqrt(_color.size() / 3);
389390 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, colorSize, colorSize, 0, GL_RGB, GL_UNSIGNED_BYTE, _color.constData());
390391 if (_clearAfterLoading) {
391392 _color.clear();
@@ -393,24 +394,27 @@ void HeightfieldBuffer::render(bool cursor) { · 1 unmodified line
393394 }
394395 }
395396 // create the buffer objects lazily
396− int sizeWithSkirt = _heightSize + 2;
397− int vertexCount = sizeWithSkirt * sizeWithSkirt;
398− int rows = sizeWithSkirt - 1;
397+ int innerSize = _heightSize - 2;
398+ int vertexCount = _heightSize * _heightSize;
399+ int rows = _heightSize - 1;
399400 int indexCount = rows * rows * 4;
400401 BufferPair& bufferPair = _bufferPairs[_heightSize];
401402 if (!bufferPair.first.isCreated()) {
402403 QVector<HeightfieldPoint> vertices(vertexCount);
403404 HeightfieldPoint* point = vertices.data();
404405
405− float step = 1.0f / (_heightSize - 1);
406− float z = -step;
407− for (int i = 0; i < sizeWithSkirt; i++, z += step) {
408− float x = -step;
406+ float vertexStep = 1.0f / (innerSize - 1);
407+ float z = -vertexStep;
408+ float textureStep = 1.0f / _heightSize;
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;
409413 const float SKIRT_LENGTH = 0.25f;
410− float baseY = (i == 0 || i == sizeWithSkirt - 1) ? -SKIRT_LENGTH : 0.0f;
411− for (int j = 0; j < sizeWithSkirt; j++, point++, x += step) {
412− point->vertex = glm::vec3(x, (j == 0 || j == sizeWithSkirt - 1) ? -SKIRT_LENGTH : baseY, z);
413− point->textureCoord = glm::vec2(x, z);
414+ float baseY = (i == 0 || i == _heightSize - 1) ? -SKIRT_LENGTH : 0.0f;
415+ for (int j = 0; j < _heightSize; j++, point++, x += vertexStep, s += textureStep) {
416+ point->vertex = glm::vec3(x, (j == 0 || j == _heightSize - 1) ? -SKIRT_LENGTH : baseY, z);
417+ point->textureCoord = glm::vec2(s, t);
414418 }
415419 }
416420
@@ -422,8 +426,8 @@ void HeightfieldBuffer::render(bool cursor) { · 5 unmodified lines
422426 QVector<int> indices(indexCount);
423427 int* index = indices.data();
424428 for (int i = 0; i < rows; i++) {
425− int lineIndex = i * sizeWithSkirt;
426− int nextLineIndex = (i + 1) * sizeWithSkirt;
429+ int lineIndex = i * _heightSize;
430+ int nextLineIndex = (i + 1) * _heightSize;
427431 for (int j = 0; j < rows; j++) {
428432 *index++ = lineIndex + j;
429433 *index++ = nextLineIndex + j;
@@ -452,17 +456,15 @@ void HeightfieldBuffer::render(bool cursor) { · 22 unmodified lines
452456
453457 glBindTexture(GL_TEXTURE_2D, _heightTextureID);
454458
455− int heightScaleLocation;
456− if (cursor) {
457− heightScaleLocation = DefaultMetavoxelRendererImplementation::getCursorHeightScaleLocation();
458− } else {
459− heightScaleLocation = DefaultMetavoxelRendererImplementation::getHeightScaleLocation();
459+ if (!cursor) {
460+ DefaultMetavoxelRendererImplementation::getHeightfieldProgram().setUniformValue(
461+ DefaultMetavoxelRendererImplementation::getHeightScaleLocation(), 1.0f / _heightSize);
462+ DefaultMetavoxelRendererImplementation::getHeightfieldProgram().setUniformValue(
463+ DefaultMetavoxelRendererImplementation::getColorScaleLocation(), (float)_heightSize / innerSize);
460464 glActiveTexture(GL_TEXTURE1);
461465 glBindTexture(GL_TEXTURE_2D, _colorTextureID);
462466 }
463467
464− DefaultMetavoxelRendererImplementation::getHeightfieldProgram().setUniformValue(heightScaleLocation, 1.0f / _heightSize);
465−
466468 glDrawRangeElements(GL_QUADS, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0);
467469
468470 if (!cursor) {
@@ -548,6 +550,7 @@ void DefaultMetavoxelRendererImplementation::init() { · 79 unmodified lines
548550 _heightfieldProgram.setUniformValue("heightMap", 0);
549551 _heightfieldProgram.setUniformValue("diffuseMap", 1);
550552 _heightScaleLocation = _heightfieldProgram.uniformLocation("heightScale");
553+ _colorScaleLocation = _heightfieldProgram.uniformLocation("colorScale");
551554 _heightfieldProgram.release();
552555
553556 _heightfieldCursorProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
@@ -558,7 +561,6 @@ void DefaultMetavoxelRendererImplementation::init() { · 4 unmodified lines
558561
559562 _heightfieldCursorProgram.bind();
560563 _heightfieldCursorProgram.setUniformValue("heightMap", 0);
561− _cursorHeightScaleLocation = _heightfieldCursorProgram.uniformLocation("heightScale");
562564 _heightfieldCursorProgram.release();
563565 }
564566 }
@@ -648,14 +650,62 @@ HeightfieldAugmentVisitor::HeightfieldAugmentVisitor(const MetavoxelLOD& lod) : · 83 unmodified lines
648650 Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), lod) {
649651 }
650652
653+class BorderFetchVisitor : public MetavoxelVisitor {
654+public:
655+
656+ BorderFetchVisitor(const MetavoxelLOD& lod, QByteArray& height);
657+
658+ virtual int visit(MetavoxelInfo& info);
659+
660+private:
661+
662+ QByteArray& _height;
663+};
664+
665+BorderFetchVisitor::BorderFetchVisitor(const MetavoxelLOD& lod, QByteArray& height) :
666+ MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getHeightfieldAttribute(),
667+ QVector<AttributePointer>(), lod),
668+ _height(height) {
669+}
670+
671+int BorderFetchVisitor::visit(MetavoxelInfo& info) {
672+ if (!info.isLeaf) {
673+ return DEFAULT_ORDER;
674+ }
675+ return STOP_RECURSION;
676+}
677+
651678 int HeightfieldAugmentVisitor::visit(MetavoxelInfo& info) {
652679 if (info.isLeaf) {
653680 HeightfieldBuffer* buffer = NULL;
654681 HeightfieldDataPointer height = info.inputValues.at(0).getInlineValue<HeightfieldDataPointer>();
655682 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;
656693 HeightfieldDataPointer color = info.inputValues.at(1).getInlineValue<HeightfieldDataPointer>();
657− buffer = new HeightfieldBuffer(info.minimum, info.size, height->getContents(),
658− color ? color->getContents() : QByteArray());
694+ if (color) {
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);
659709 }
660710 info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(BufferDataPointer(buffer)));
661711 return STOP_RECURSION;
@@ -856,8 +906,8 @@ ProgramObject DefaultMetavoxelRendererImplementation::_pointProgram; · 194 unmodified lines
856906 int DefaultMetavoxelRendererImplementation::_pointScaleLocation;
857907 ProgramObject DefaultMetavoxelRendererImplementation::_heightfieldProgram;
858908 int DefaultMetavoxelRendererImplementation::_heightScaleLocation;
909+int DefaultMetavoxelRendererImplementation::_colorScaleLocation;
859910 ProgramObject DefaultMetavoxelRendererImplementation::_heightfieldCursorProgram;
860−int DefaultMetavoxelRendererImplementation::_cursorHeightScaleLocation;
861911
862912 static void enableClipPlane(GLenum plane, float x, float y, float z, float w) {
863913 GLdouble coefficients[] = { x, y, z, w };