History

Working on texture rendering.

Andrzej Kapolka committed 12 years ago

Commit 416d9ba1 parent 7cf7f1814 files · +473 −104Browse files

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

Unified · Split

interface/src/MetavoxelSystem.cpp+173−87
@@ -602,24 +602,23 @@ const int HeightfieldBuffer::SHARED_EDGE = 1; · 601 unmodified lines
602602 const int HeightfieldBuffer::HEIGHT_EXTENSION = 2 * HeightfieldBuffer::HEIGHT_BORDER + HeightfieldBuffer::SHARED_EDGE;
603603
604604 HeightfieldBuffer::HeightfieldBuffer(const glm::vec3& translation, float scale,
605− const QByteArray& height, const QByteArray& color, const QByteArray& texture) :
605+ const QByteArray& height, const QByteArray& color, const QByteArray& texture,
606+ const QVector<SharedObjectPointer>& textures) :
606607 _translation(translation),
607608 _scale(scale),
608609 _heightBounds(translation, translation + glm::vec3(scale, scale, scale)),
609610 _colorBounds(_heightBounds),
610− _textureBounds(_heightBounds),
611611 _height(height),
612612 _color(color),
613613 _texture(texture),
614+ _textures(textures),
614615 _heightTextureID(0),
615616 _colorTextureID(0),
616617 _textureTextureID(0),
617618 _heightSize(glm::sqrt(height.size())),
618619 _heightIncrement(scale / (_heightSize - HEIGHT_EXTENSION)),
619620 _colorSize(glm::sqrt(color.size() / HeightfieldData::COLOR_BYTES)),
620− _colorIncrement(scale / (_colorSize - SHARED_EDGE)),
621− _textureSize(glm::sqrt(texture.size())),
622− _textureIncrement(scale / (_textureSize - SHARED_EDGE)) {
621+ _colorIncrement(scale / (_colorSize - SHARED_EDGE)) {
623622
624623 _heightBounds.minimum.x -= _heightIncrement * HEIGHT_BORDER;
625624 _heightBounds.minimum.z -= _heightIncrement * HEIGHT_BORDER;
@@ -628,9 +627,6 @@ HeightfieldBuffer::HeightfieldBuffer(const glm::vec3& translation, float scale, · 2 unmodified lines
628627
629628 _colorBounds.maximum.x += _colorIncrement * SHARED_EDGE;
630629 _colorBounds.maximum.z += _colorIncrement * SHARED_EDGE;
631−
632− _textureBounds.maximum.x += _textureIncrement * SHARED_EDGE;
633− _textureBounds.maximum.z += _textureIncrement * SHARED_EDGE;
634630 }
635631
636632 HeightfieldBuffer::~HeightfieldBuffer() {
@@ -712,6 +708,15 @@ void HeightfieldBuffer::render(bool cursor) { · 75 unmodified lines
712708 int textureSize = glm::sqrt(_texture.size());
713709 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, textureSize, textureSize, 0,
714710 GL_LUMINANCE, GL_UNSIGNED_BYTE, _texture.constData());
711+
712+ _networkTextures.resize(_textures.size());
713+ for (int i = 0; i < _textures.size(); i++) {
714+ const SharedObjectPointer texture = _textures.at(i);
715+ if (texture) {
716+ _networkTextures[i] = Application::getInstance()->getTextureCache()->getTexture(
717+ static_cast<HeightfieldTexture*>(texture.data())->getURL());
718+ }
719+ }
715720 }
716721 }
717722 // create the buffer objects lazily
@@ -780,7 +785,56 @@ void HeightfieldBuffer::render(bool cursor) { · 62 unmodified lines
780785
781786 glBindTexture(GL_TEXTURE_2D, _heightTextureID);
782787
783− if (!cursor) {
788+ if (cursor) {
789+ glDrawRangeElements(GL_TRIANGLES, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0);
790+
791+ } else if (!_textures.isEmpty()) {
792+ DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().bind();
793+ DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().setUniformValue(
794+ DefaultMetavoxelRendererImplementation::getBaseHeightScaleLocation(), 1.0f / _heightSize);
795+ DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().setUniformValue(
796+ DefaultMetavoxelRendererImplementation::getBaseColorScaleLocation(), (float)_heightSize / innerSize);
797+ glActiveTexture(GL_TEXTURE1);
798+ glBindTexture(GL_TEXTURE_2D, _colorTextureID);
799+
800+ glDrawRangeElements(GL_TRIANGLES, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0);
801+
802+ glDepthFunc(GL_LEQUAL);
803+ glEnable(GL_BLEND);
804+ glBlendFunc(GL_DST_COLOR, GL_ZERO);
805+ glEnable(GL_POLYGON_OFFSET_FILL);
806+ glPolygonOffset(-1.0f, -1.0f);
807+
808+ glBindTexture(GL_TEXTURE_2D, 0);
809+
810+ if (Menu::getInstance()->isOptionChecked(MenuOption::SimpleShadows)) {
811+ DefaultMetavoxelRendererImplementation::getShadowLightHeightfieldProgram().bind();
812+ DefaultMetavoxelRendererImplementation::getShadowLightHeightfieldProgram().setUniformValue(
813+ DefaultMetavoxelRendererImplementation::getShadowLightHeightScaleLocation(), 1.0f / _heightSize);
814+
815+ } else if (Menu::getInstance()->isOptionChecked(MenuOption::CascadedShadows)) {
816+ DefaultMetavoxelRendererImplementation::getCascadedShadowLightHeightfieldProgram().bind();
817+ DefaultMetavoxelRendererImplementation::getCascadedShadowLightHeightfieldProgram().setUniformValue(
818+ DefaultMetavoxelRendererImplementation::getCascadedShadowLightHeightScaleLocation(), 1.0f / _heightSize);
819+
820+ } else {
821+ DefaultMetavoxelRendererImplementation::getLightHeightfieldProgram().bind();
822+ DefaultMetavoxelRendererImplementation::getLightHeightfieldProgram().setUniformValue(
823+ DefaultMetavoxelRendererImplementation::getBaseHeightScaleLocation(), 1.0f / _heightSize);
824+ }
825+
826+ glDrawRangeElements(GL_TRIANGLES, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0);
827+
828+ DefaultMetavoxelRendererImplementation::getHeightfieldProgram().bind();
829+
830+ glDisable(GL_POLYGON_OFFSET_FILL);
831+ glDisable(GL_BLEND);
832+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE);
833+ glDepthFunc(GL_LESS);
834+
835+ glActiveTexture(GL_TEXTURE0);
836+
837+ } else {
784838 int heightScaleLocation = DefaultMetavoxelRendererImplementation::getHeightScaleLocation();
785839 int colorScaleLocation = DefaultMetavoxelRendererImplementation::getColorScaleLocation();
786840 ProgramObject* program = &DefaultMetavoxelRendererImplementation::getHeightfieldProgram();
@@ -798,11 +852,9 @@ void HeightfieldBuffer::render(bool cursor) { · 11 unmodified lines
798852 program->setUniformValue(colorScaleLocation, (float)_heightSize / innerSize);
799853 glActiveTexture(GL_TEXTURE1);
800854 glBindTexture(GL_TEXTURE_2D, _colorTextureID);
801− }
802−
803− glDrawRangeElements(GL_TRIANGLES, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0);
804−
805− if (!cursor) {
855+
856+ glDrawRangeElements(GL_TRIANGLES, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0);
857+
806858 glBindTexture(GL_TEXTURE_2D, 0);
807859 glActiveTexture(GL_TEXTURE0);
808860 }
@@ -919,6 +971,68 @@ void DefaultMetavoxelRendererImplementation::init() { · 110 unmodified lines
919971 _shadowDistancesLocation = _cascadedShadowMapHeightfieldProgram.uniformLocation("shadowDistances");
920972 _cascadedShadowMapHeightfieldProgram.release();
921973
974+ _baseHeightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
975+ "shaders/metavoxel_heightfield_base.vert");
976+ _baseHeightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
977+ "shaders/metavoxel_heightfield_base.frag");
978+ _baseHeightfieldProgram.link();
979+
980+ _baseHeightfieldProgram.bind();
981+ _baseHeightfieldProgram.setUniformValue("heightMap", 0);
982+ _baseHeightfieldProgram.setUniformValue("diffuseMap", 1);
983+ _baseHeightScaleLocation = _heightfieldProgram.uniformLocation("heightScale");
984+ _baseColorScaleLocation = _heightfieldProgram.uniformLocation("colorScale");
985+ _baseHeightfieldProgram.release();
986+
987+ _splatHeightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
988+ "shaders/metavoxel_heightfield_splat.vert");
989+ _splatHeightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
990+ "shaders/metavoxel_heightfield_splat.frag");
991+ _splatHeightfieldProgram.link();
992+
993+ _splatHeightfieldProgram.bind();
994+ _splatHeightfieldProgram.setUniformValue("heightMap", 0);
995+ _splatHeightfieldProgram.setUniformValue("diffuseMap", 1);
996+ _splatHeightScaleLocation = _splatHeightfieldProgram.uniformLocation("heightScale");
997+ _splatTextureScaleLocation = _splatHeightfieldProgram.uniformLocation("textureScale");
998+ _splatHeightfieldProgram.release();
999+
1000+ _lightHeightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
1001+ "shaders/metavoxel_heightfield_light.vert");
1002+ _lightHeightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
1003+ "shaders/metavoxel_heightfield_light.frag");
1004+ _lightHeightfieldProgram.link();
1005+
1006+ _lightHeightfieldProgram.bind();
1007+ _lightHeightfieldProgram.setUniformValue("heightMap", 0);
1008+ _lightHeightScaleLocation = _lightHeightfieldProgram.uniformLocation("heightScale");
1009+ _lightHeightfieldProgram.release();
1010+
1011+ _shadowLightHeightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
1012+ "shaders/metavoxel_heightfield_light.vert");
1013+ _shadowLightHeightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
1014+ "shaders/metavoxel_heightfield_light_shadow_map.frag");
1015+ _shadowLightHeightfieldProgram.link();
1016+
1017+ _shadowLightHeightfieldProgram.bind();
1018+ _shadowLightHeightfieldProgram.setUniformValue("heightMap", 0);
1019+ _shadowLightHeightfieldProgram.setUniformValue("shadowMap", 2);
1020+ _shadowLightHeightScaleLocation = _shadowLightHeightfieldProgram.uniformLocation("heightScale");
1021+ _shadowLightHeightfieldProgram.release();
1022+
1023+ _cascadedShadowLightHeightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
1024+ "shaders/metavoxel_heightfield_light.vert");
1025+ _cascadedShadowLightHeightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
1026+ "shaders/metavoxel_heightfield_light_cascaded_shadow_map.frag");
1027+ _cascadedShadowLightHeightfieldProgram.link();
1028+
1029+ _cascadedShadowLightHeightfieldProgram.bind();
1030+ _cascadedShadowLightHeightfieldProgram.setUniformValue("heightMap", 0);
1031+ _cascadedShadowLightHeightfieldProgram.setUniformValue("shadowMap", 2);
1032+ _cascadedShadowLightHeightScaleLocation = _cascadedShadowLightHeightfieldProgram.uniformLocation("heightScale");
1033+ _shadowLightDistancesLocation = _cascadedShadowLightHeightfieldProgram.uniformLocation("shadowDistances");
1034+ _cascadedShadowLightHeightfieldProgram.release();
1035+
9221036 _heightfieldCursorProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() +
9231037 "shaders/metavoxel_heightfield_cursor.vert");
9241038 _heightfieldCursorProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() +
@@ -1019,8 +1133,7 @@ private: · 94 unmodified lines
10191133
10201134 HeightfieldFetchVisitor::HeightfieldFetchVisitor(const MetavoxelLOD& lod, const QVector<Box>& intersections) :
10211135 MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getHeightfieldAttribute() <<
1022− AttributeRegistry::getInstance()->getHeightfieldColorAttribute() <<
1023− AttributeRegistry::getInstance()->getHeightfieldTextureAttribute(), QVector<AttributePointer>(), lod),
1136+ AttributeRegistry::getInstance()->getHeightfieldColorAttribute(), QVector<AttributePointer>(), lod),
10241137 _intersections(intersections) {
10251138 }
10261139
@@ -1135,50 +1248,6 @@ int HeightfieldFetchVisitor::visit(MetavoxelInfo& info) { · 108 unmodified lines
11351248 }
11361249 }
11371250 }
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− }
11821251 }
11831252 return STOP_RECURSION;
11841253 }
@@ -1233,42 +1302,43 @@ int HeightfieldRegionVisitor::visit(MetavoxelInfo& info) { · 48 unmodified lines
12331302 }
12341303
12351304 HeightfieldTextureDataPointer texture = info.inputValues.at(2).getInlineValue<HeightfieldTextureDataPointer>();
1236− int textureContentsSize = 0;
1305+ QByteArray textureContents;
1306+ QVector<SharedObjectPointer> textures;
12371307 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;
1308+ textureContents = texture->getContents();
1309+ textures = texture->getTextures();
12421310 }
12431311
12441312 const HeightfieldBuffer* existingBuffer = static_cast<const HeightfieldBuffer*>(
12451313 info.inputValues.at(3).getInlineValue<BufferDataPointer>().data());
12461314 Box bounds = info.getBounds();
12471315 if (existingBuffer && existingBuffer->getHeight().size() == heightContentsSize &&
1248− existingBuffer->getColor().size() == colorContentsSize &&
1249− existingBuffer->getTexture().size() == textureContentsSize) {
1316+ existingBuffer->getColor().size() == colorContentsSize) {
12501317 // we already have a buffer of the correct resolution
12511318 addRegion(bounds, existingBuffer->getHeightBounds());
1252− return STOP_RECURSION;
1319+ buffer = new HeightfieldBuffer(info.minimum, info.size, existingBuffer->getHeight(),
1320+ existingBuffer->getColor(), textureContents, textures);
1321+
1322+ } else {
1323+ // we must create a new buffer and update its borders
1324+ buffer = new HeightfieldBuffer(info.minimum, info.size, QByteArray(heightContentsSize, 0),
1325+ QByteArray(colorContentsSize, 0), textureContents, textures);
1326+ const Box& heightBounds = buffer->getHeightBounds();
1327+ addRegion(bounds, heightBounds);
1328+
1329+ _intersections.clear();
1330+ _intersections.append(Box(heightBounds.minimum,
1331+ glm::vec3(bounds.maximum.x, heightBounds.maximum.y, bounds.minimum.z)));
1332+ _intersections.append(Box(glm::vec3(bounds.maximum.x, heightBounds.minimum.y, heightBounds.minimum.z),
1333+ glm::vec3(heightBounds.maximum.x, heightBounds.maximum.y, bounds.maximum.z)));
1334+ _intersections.append(Box(glm::vec3(bounds.minimum.x, heightBounds.minimum.y, bounds.maximum.z),
1335+ heightBounds.maximum));
1336+ _intersections.append(Box(glm::vec3(heightBounds.minimum.x, heightBounds.minimum.y, bounds.minimum.z),
1337+ glm::vec3(bounds.minimum.x, heightBounds.maximum.y, heightBounds.maximum.z)));
1338+
1339+ _fetchVisitor.init(buffer);
1340+ _data->guide(_fetchVisitor);
12531341 }
1254− // we must create a new buffer and update its borders
1255− buffer = new HeightfieldBuffer(info.minimum, info.size, QByteArray(heightContentsSize, 0),
1256− QByteArray(colorContentsSize, 0), QByteArray(textureContentsSize, 0));
1257− const Box& heightBounds = buffer->getHeightBounds();
1258− addRegion(bounds, heightBounds);
1259−
1260− _intersections.clear();
1261− _intersections.append(Box(heightBounds.minimum,
1262− glm::vec3(bounds.maximum.x, heightBounds.maximum.y, bounds.minimum.z)));
1263− _intersections.append(Box(glm::vec3(bounds.maximum.x, heightBounds.minimum.y, heightBounds.minimum.z),
1264− glm::vec3(heightBounds.maximum.x, heightBounds.maximum.y, bounds.maximum.z)));
1265− _intersections.append(Box(glm::vec3(bounds.minimum.x, heightBounds.minimum.y, bounds.maximum.z),
1266− heightBounds.maximum));
1267− _intersections.append(Box(glm::vec3(heightBounds.minimum.x, heightBounds.minimum.y, bounds.minimum.z),
1268− glm::vec3(bounds.minimum.x, heightBounds.maximum.y, heightBounds.maximum.z)));
1269−
1270− _fetchVisitor.init(buffer);
1271− _data->guide(_fetchVisitor);
12721342 }
12731343 info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(BufferDataPointer(buffer)));
12741344 return STOP_RECURSION;
@@ -1326,7 +1396,7 @@ int HeightfieldUpdateVisitor::visit(MetavoxelInfo& info) { · 51 unmodified lines
13261396 return STOP_RECURSION;
13271397 }
13281398 HeightfieldBuffer* newBuffer = new HeightfieldBuffer(info.minimum, info.size,
1329− buffer->getHeight(), buffer->getColor(), buffer->getTexture());
1399+ buffer->getHeight(), buffer->getColor(), buffer->getTexture(), buffer->getTextures());
13301400 _fetchVisitor.init(newBuffer);
13311401 _data->guide(_fetchVisitor);
13321402 info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(BufferDataPointer(newBuffer)));
@@ -1514,6 +1584,9 @@ void DefaultMetavoxelRendererImplementation::render(MetavoxelData& data, Metavox · 181 unmodified lines
15141584 ProgramObject* program = &_heightfieldProgram;
15151585 if (Menu::getInstance()->getShadowsEnabled()) {
15161586 if (Menu::getInstance()->isOptionChecked(MenuOption::CascadedShadows)) {
1587+ _cascadedShadowLightHeightfieldProgram.bind();
1588+ _cascadedShadowLightHeightfieldProgram.setUniform(_shadowLightDistancesLocation,
1589+ Application::getInstance()->getShadowDistances());
15171590 program = &_cascadedShadowMapHeightfieldProgram;
15181591 program->bind();
15191592 program->setUniform(_shadowDistancesLocation, Application::getInstance()->getShadowDistances());
@@ -1559,6 +1632,19 @@ ProgramObject DefaultMetavoxelRendererImplementation::_cascadedShadowMapHeightfi · 39 unmodified lines
15591632 int DefaultMetavoxelRendererImplementation::_cascadedShadowMapHeightScaleLocation;
15601633 int DefaultMetavoxelRendererImplementation::_cascadedShadowMapColorScaleLocation;
15611634 int DefaultMetavoxelRendererImplementation::_shadowDistancesLocation;
1635+ProgramObject DefaultMetavoxelRendererImplementation::_baseHeightfieldProgram;
1636+int DefaultMetavoxelRendererImplementation::_baseHeightScaleLocation;
1637+int DefaultMetavoxelRendererImplementation::_baseColorScaleLocation;
1638+ProgramObject DefaultMetavoxelRendererImplementation::_splatHeightfieldProgram;
1639+int DefaultMetavoxelRendererImplementation::_splatHeightScaleLocation;
1640+int DefaultMetavoxelRendererImplementation::_splatTextureScaleLocation;
1641+ProgramObject DefaultMetavoxelRendererImplementation::_lightHeightfieldProgram;
1642+int DefaultMetavoxelRendererImplementation::_lightHeightScaleLocation;
1643+ProgramObject DefaultMetavoxelRendererImplementation::_shadowLightHeightfieldProgram;
1644+int DefaultMetavoxelRendererImplementation::_shadowLightHeightScaleLocation;
1645+ProgramObject DefaultMetavoxelRendererImplementation::_cascadedShadowLightHeightfieldProgram;
1646+int DefaultMetavoxelRendererImplementation::_cascadedShadowLightHeightScaleLocation;
1647+int DefaultMetavoxelRendererImplementation::_shadowLightDistancesLocation;
15621648 ProgramObject DefaultMetavoxelRendererImplementation::_heightfieldCursorProgram;
15631649
15641650 static void enableClipPlane(GLenum plane, float x, float y, float z, float w) {