History

Yet more texture bits.

Andrzej Kapolka committed 12 years ago

Commit 7cf7f181 parent 540d0743 files · +106 −15Browse files

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

Unified · Split

interface/src/MetavoxelSystem.cpp+88−11
@@ -449,9 +449,10 @@ void MetavoxelSystem::renderHeightfieldCursor(const glm::vec3& position, float r · 448 unmodified lines
449449 glDepthFunc(GL_LESS);
450450 }
451451
452−void MetavoxelSystem::deleteTextures(int heightID, int colorID) {
452+void MetavoxelSystem::deleteTextures(int heightID, int colorID, int textureID) {
453453 glDeleteTextures(1, (GLuint*)&heightID);
454454 glDeleteTextures(1, (GLuint*)&colorID);
455+ glDeleteTextures(1, (GLuint*)&textureID);
455456 }
456457
457458 MetavoxelClient* MetavoxelSystem::createClient(const SharedNodePointer& node) {
@@ -601,19 +602,24 @@ const int HeightfieldBuffer::SHARED_EDGE = 1; · 143 unmodified lines
601602 const int HeightfieldBuffer::HEIGHT_EXTENSION = 2 * HeightfieldBuffer::HEIGHT_BORDER + HeightfieldBuffer::SHARED_EDGE;
602603
603604 HeightfieldBuffer::HeightfieldBuffer(const glm::vec3& translation, float scale,
604− const QByteArray& height, const QByteArray& color) :
605+ const QByteArray& height, const QByteArray& color, const QByteArray& texture) :
605606 _translation(translation),
606607 _scale(scale),
607608 _heightBounds(translation, translation + glm::vec3(scale, scale, scale)),
608609 _colorBounds(_heightBounds),
610+ _textureBounds(_heightBounds),
609611 _height(height),
610612 _color(color),
613+ _texture(texture),
611614 _heightTextureID(0),
612615 _colorTextureID(0),
616+ _textureTextureID(0),
613617 _heightSize(glm::sqrt(height.size())),
614618 _heightIncrement(scale / (_heightSize - HEIGHT_EXTENSION)),
615619 _colorSize(glm::sqrt(color.size() / HeightfieldData::COLOR_BYTES)),
616− _colorIncrement(scale / (_colorSize - SHARED_EDGE)) {
620+ _colorIncrement(scale / (_colorSize - SHARED_EDGE)),
621+ _textureSize(glm::sqrt(texture.size())),
622+ _textureIncrement(scale / (_textureSize - SHARED_EDGE)) {
617623
618624 _heightBounds.minimum.x -= _heightIncrement * HEIGHT_BORDER;
619625 _heightBounds.minimum.z -= _heightIncrement * HEIGHT_BORDER;
@@ -622,16 +628,20 @@ HeightfieldBuffer::HeightfieldBuffer(const glm::vec3& translation, float scale, · 2 unmodified lines
622628
623629 _colorBounds.maximum.x += _colorIncrement * SHARED_EDGE;
624630 _colorBounds.maximum.z += _colorIncrement * SHARED_EDGE;
631+
632+ _textureBounds.maximum.x += _textureIncrement * SHARED_EDGE;
633+ _textureBounds.maximum.z += _textureIncrement * SHARED_EDGE;
625634 }
626635
627636 HeightfieldBuffer::~HeightfieldBuffer() {
628637 // the textures have to be deleted on the main thread (for its opengl context)
629638 if (QThread::currentThread() != Application::getInstance()->thread()) {
630639 QMetaObject::invokeMethod(Application::getInstance()->getMetavoxels(), "deleteTextures",
631− Q_ARG(int, _heightTextureID), Q_ARG(int, _colorTextureID));
640+ Q_ARG(int, _heightTextureID), Q_ARG(int, _colorTextureID), Q_ARG(int, _textureTextureID));
632641 } else {
633642 glDeleteTextures(1, &_heightTextureID);
634643 glDeleteTextures(1, &_colorTextureID);
644+ glDeleteTextures(1, &_textureTextureID);
635645 }
636646 }
637647
@@ -692,6 +702,17 @@ void HeightfieldBuffer::render(bool cursor) { · 54 unmodified lines
692702 int colorSize = glm::sqrt(_color.size() / HeightfieldData::COLOR_BYTES);
693703 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, colorSize, colorSize, 0, GL_RGB, GL_UNSIGNED_BYTE, _color.constData());
694704 }
705+
706+ if (!_texture.isEmpty()) {
707+ glGenTextures(1, &_textureTextureID);
708+ glBindTexture(GL_TEXTURE_2D, _textureTextureID);
709+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
710+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
711+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
712+ int textureSize = glm::sqrt(_texture.size());
713+ glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, textureSize, textureSize, 0,
714+ GL_LUMINANCE, GL_UNSIGNED_BYTE, _texture.constData());
715+ }
695716 }
696717 // create the buffer objects lazily
697718 int innerSize = _heightSize - 2 * HeightfieldBuffer::HEIGHT_BORDER;
@@ -998,7 +1019,8 @@ private: · 300 unmodified lines
9981019
9991020 HeightfieldFetchVisitor::HeightfieldFetchVisitor(const MetavoxelLOD& lod, const QVector<Box>& intersections) :
10001021 MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getHeightfieldAttribute() <<
1001− AttributeRegistry::getInstance()->getHeightfieldColorAttribute(), QVector<AttributePointer>(), lod),
1022+ AttributeRegistry::getInstance()->getHeightfieldColorAttribute() <<
1023+ AttributeRegistry::getInstance()->getHeightfieldTextureAttribute(), QVector<AttributePointer>(), lod),
10021024 _intersections(intersections) {
10031025 }
10041026
@@ -1065,11 +1087,11 @@ int HeightfieldFetchVisitor::visit(MetavoxelInfo& info) { · 60 unmodified lines
10651087
10661088 int colorSize = _buffer->getColorSize();
10671089 if (colorSize == 0) {
1068− return STOP_RECURSION;
1090+ continue;
10691091 }
10701092 HeightfieldColorDataPointer color = info.inputValues.at(1).getInlineValue<HeightfieldColorDataPointer>();
10711093 if (!color) {
1072− return STOP_RECURSION;
1094+ continue;
10731095 }
10741096 const Box& colorBounds = _buffer->getColorBounds();
10751097 overlap = colorBounds.getIntersection(overlap);
@@ -1113,6 +1135,50 @@ int HeightfieldFetchVisitor::visit(MetavoxelInfo& info) { · 37 unmodified lines
11131135 }
11141136 }
11151137 }
1138+
1139+ int textureSize = _buffer->getTextureSize();
1140+ if (textureSize == 0) {
1141+ continue;
1142+ }
1143+ HeightfieldTextureDataPointer texture = info.inputValues.at(2).getInlineValue<HeightfieldTextureDataPointer>();
1144+ if (!texture) {
1145+ continue;
1146+ }
1147+ const Box& textureBounds = _buffer->getTextureBounds();
1148+ overlap = textureBounds.getIntersection(overlap);
1149+ float textureIncrement = _buffer->getTextureIncrement();
1150+ destX = (overlap.minimum.x - textureBounds.minimum.x) / textureIncrement;
1151+ destY = (overlap.minimum.z - textureBounds.minimum.z) / textureIncrement;
1152+ destWidth = glm::ceil((overlap.maximum.x - overlap.minimum.x) / textureIncrement);
1153+ destHeight = glm::ceil((overlap.maximum.z - overlap.minimum.z) / textureIncrement);
1154+ dest = _buffer->getTexture().data() + destY * textureSize + destX;
1155+
1156+ const QByteArray& srcTexture = texture->getContents();
1157+ srcSize = glm::sqrt(srcTexture.size());
1158+ srcIncrement = info.size / srcSize;
1159+
1160+ if (srcIncrement == textureIncrement) {
1161+ // easy case: same resolution
1162+ int srcX = (overlap.minimum.x - info.minimum.x) / srcIncrement;
1163+ int srcY = (overlap.minimum.z - info.minimum.z) / srcIncrement;
1164+
1165+ const char* src = srcTexture.constData() + srcY * srcSize + srcX;
1166+ for (int y = 0; y < destHeight; y++, src += srcSize, dest += textureSize) {
1167+ memcpy(dest, src, destWidth);
1168+ }
1169+ } else {
1170+ // more difficult: different resolutions
1171+ float srcX = (overlap.minimum.x - info.minimum.x) / srcIncrement;
1172+ float srcY = (overlap.minimum.z - info.minimum.z) / srcIncrement;
1173+ float srcAdvance = textureIncrement / srcIncrement;
1174+ for (int y = 0; y < destHeight; y++, dest += textureSize, srcY += srcAdvance) {
1175+ const char* src = srcTexture.constData() + (int)srcY * srcSize;
1176+ float lineSrcX = srcX;
1177+ for (char* lineDest = dest, *end = dest + destWidth; lineDest != end; lineDest++, lineSrcX += srcAdvance) {
1178+ *lineDest = src[(int)lineSrcX];
1179+ }
1180+ }
1181+ }
11161182 }
11171183 return STOP_RECURSION;
11181184 }
@@ -1138,6 +1204,7 @@ private: · 19 unmodified lines
11381204 HeightfieldRegionVisitor::HeightfieldRegionVisitor(const MetavoxelLOD& lod) :
11391205 MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getHeightfieldAttribute() <<
11401206 AttributeRegistry::getInstance()->getHeightfieldColorAttribute() <<
1207+ AttributeRegistry::getInstance()->getHeightfieldTextureAttribute() <<
11411208 Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), QVector<AttributePointer>() <<
11421209 Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), lod),
11431210 regionBounds(glm::vec3(FLT_MAX, FLT_MAX, FLT_MAX), glm::vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX)),
@@ -1165,18 +1232,28 @@ int HeightfieldRegionVisitor::visit(MetavoxelInfo& info) { · 21 unmodified lines
11651232 colorContentsSize = extendedColorSize * extendedColorSize * HeightfieldData::COLOR_BYTES;
11661233 }
11671234
1235+ HeightfieldTextureDataPointer texture = info.inputValues.at(2).getInlineValue<HeightfieldTextureDataPointer>();
1236+ int textureContentsSize = 0;
1237+ if (texture) {
1238+ const QByteArray& textureContents = texture->getContents();
1239+ int textureSize = glm::sqrt(textureContents.size());
1240+ int extendedTextureSize = textureSize + HeightfieldBuffer::SHARED_EDGE;
1241+ textureContentsSize = extendedTextureSize * extendedTextureSize;
1242+ }
1243+
11681244 const HeightfieldBuffer* existingBuffer = static_cast<const HeightfieldBuffer*>(
1169− info.inputValues.at(2).getInlineValue<BufferDataPointer>().data());
1245+ info.inputValues.at(3).getInlineValue<BufferDataPointer>().data());
11701246 Box bounds = info.getBounds();
11711247 if (existingBuffer && existingBuffer->getHeight().size() == heightContentsSize &&
1172− existingBuffer->getColor().size() == colorContentsSize) {
1248+ existingBuffer->getColor().size() == colorContentsSize &&
1249+ existingBuffer->getTexture().size() == textureContentsSize) {
11731250 // we already have a buffer of the correct resolution
11741251 addRegion(bounds, existingBuffer->getHeightBounds());
11751252 return STOP_RECURSION;
11761253 }
11771254 // we must create a new buffer and update its borders
11781255 buffer = new HeightfieldBuffer(info.minimum, info.size, QByteArray(heightContentsSize, 0),
1179− QByteArray(colorContentsSize, 0));
1256+ QByteArray(colorContentsSize, 0), QByteArray(textureContentsSize, 0));
11801257 const Box& heightBounds = buffer->getHeightBounds();
11811258 addRegion(bounds, heightBounds);
11821259
@@ -1249,7 +1326,7 @@ int HeightfieldUpdateVisitor::visit(MetavoxelInfo& info) { · 66 unmodified lines
12491326 return STOP_RECURSION;
12501327 }
12511328 HeightfieldBuffer* newBuffer = new HeightfieldBuffer(info.minimum, info.size,
1252− buffer->getHeight(), buffer->getColor());
1329+ buffer->getHeight(), buffer->getColor(), buffer->getTexture());
12531330 _fetchVisitor.init(newBuffer);
12541331 _data->guide(_fetchVisitor);
12551332 info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(BufferDataPointer(newBuffer)));