Showing the changes to interface/src/MetavoxelSystem.cpp (1 of 337 files). Show all
Unified · Split
| @@ -36,17 +36,16 @@ REGISTER_META_OBJECT(DefaultMetavoxelRendererImplementation) · 35 unmodified lines | ||
| 36 | 36 | REGISTER_META_OBJECT(SphereRenderer) |
| 37 | 37 | REGISTER_META_OBJECT(CuboidRenderer) |
| 38 | 38 | REGISTER_META_OBJECT(StaticModelRenderer) |
| 39 | − | |
| 40 | −static int bufferPointVectorMetaTypeId = qRegisterMetaType<BufferPointVector>(); | |
| 39 | +REGISTER_META_OBJECT(HeightfieldRenderer) | |
| 41 | 40 | |
| 42 | 41 | MetavoxelSystem::NetworkSimulation::NetworkSimulation(float dropRate, float repeatRate, |
| 43 | − int minimumDelay, int maximumDelay, int bandwidthLimit) : | |
| 44 | − dropRate(dropRate), | |
| 45 | − repeatRate(repeatRate), | |
| 46 | − minimumDelay(minimumDelay), | |
| 47 | − maximumDelay(maximumDelay), | |
| 48 | − bandwidthLimit(bandwidthLimit) { | |
| 49 | −} | |
| 42 | + int minimumDelay, int maximumDelay, int bandwidthLimit) : | |
| 43 | +dropRate(dropRate), | |
| 44 | +repeatRate(repeatRate), | |
| 45 | +minimumDelay(minimumDelay), | |
| 46 | +maximumDelay(maximumDelay), | |
| 47 | +bandwidthLimit(bandwidthLimit) { | |
| 48 | +} | |
| 50 | 49 | |
| 51 | 50 | MetavoxelSystem::~MetavoxelSystem() { |
| 52 | 51 | // kill the updater before we delete our network simulation objects |
| @@ -57,19 +56,50 @@ MetavoxelSystem::~MetavoxelSystem() { · 4 unmodified lines | ||
| 57 | 56 | |
| 58 | 57 | void MetavoxelSystem::init() { |
| 59 | 58 | MetavoxelClientManager::init(); |
| 60 | − DefaultMetavoxelRendererImplementation::init(); | |
| 61 | − | |
| 62 | − _pointBufferAttribute = AttributeRegistry::getInstance()->registerAttribute(new BufferDataAttribute("pointBuffer")); | |
| 63 | − | |
| 64 | − _heightfieldBufferAttribute = AttributeRegistry::getInstance()->registerAttribute( | |
| 65 | − new BufferDataAttribute("heightfieldBuffer")); | |
| 66 | − _heightfieldBufferAttribute->setLODThresholdMultiplier( | |
| 67 | − AttributeRegistry::getInstance()->getHeightfieldAttribute()->getLODThresholdMultiplier()); | |
| 68 | 59 | |
| 69 | 60 | _voxelBufferAttribute = AttributeRegistry::getInstance()->registerAttribute( |
| 70 | − new BufferDataAttribute("voxelBuffer")); | |
| 61 | + new BufferDataAttribute("voxelBuffer")); | |
| 71 | 62 | _voxelBufferAttribute->setLODThresholdMultiplier( |
| 72 | − AttributeRegistry::getInstance()->getVoxelColorAttribute()->getLODThresholdMultiplier()); | |
| 63 | + AttributeRegistry::getInstance()->getVoxelColorAttribute()->getLODThresholdMultiplier()); | |
| 64 | + | |
| 65 | + _baseHeightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + | |
| 66 | + "shaders/metavoxel_heightfield_base.vert"); | |
| 67 | + _baseHeightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + | |
| 68 | + "shaders/metavoxel_heightfield_base.frag"); | |
| 69 | + _baseHeightfieldProgram.link(); | |
| 70 | + | |
| 71 | + _baseHeightfieldProgram.bind(); | |
| 72 | + _baseHeightfieldProgram.setUniformValue("heightMap", 0); | |
| 73 | + _baseHeightfieldProgram.setUniformValue("diffuseMap", 1); | |
| 74 | + _baseHeightScaleLocation = _baseHeightfieldProgram.uniformLocation("heightScale"); | |
| 75 | + _baseColorScaleLocation = _baseHeightfieldProgram.uniformLocation("colorScale"); | |
| 76 | + _baseHeightfieldProgram.release(); | |
| 77 | + | |
| 78 | + loadSplatProgram("heightfield", _splatHeightfieldProgram, _splatHeightfieldLocations); | |
| 79 | + | |
| 80 | + _heightfieldCursorProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + | |
| 81 | + "shaders/metavoxel_heightfield_cursor.vert"); | |
| 82 | + _heightfieldCursorProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + | |
| 83 | + "shaders/metavoxel_cursor.frag"); | |
| 84 | + _heightfieldCursorProgram.link(); | |
| 85 | + | |
| 86 | + _heightfieldCursorProgram.bind(); | |
| 87 | + _heightfieldCursorProgram.setUniformValue("heightMap", 0); | |
| 88 | + _heightfieldCursorProgram.release(); | |
| 89 | + | |
| 90 | + _baseVoxelProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + | |
| 91 | + "shaders/metavoxel_voxel_base.vert"); | |
| 92 | + _baseVoxelProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + | |
| 93 | + "shaders/metavoxel_voxel_base.frag"); | |
| 94 | + _baseVoxelProgram.link(); | |
| 95 | + | |
| 96 | + loadSplatProgram("voxel", _splatVoxelProgram, _splatVoxelLocations); | |
| 97 | + | |
| 98 | + _voxelCursorProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + | |
| 99 | + "shaders/metavoxel_voxel_cursor.vert"); | |
| 100 | + _voxelCursorProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + | |
| 101 | + "shaders/metavoxel_cursor.frag"); | |
| 102 | + _voxelCursorProgram.link(); | |
| 73 | 103 | } |
| 74 | 104 | |
| 75 | 105 | MetavoxelLOD MetavoxelSystem::getLOD() { |
| @@ -93,16 +123,16 @@ public: · 17 unmodified lines | ||
| 93 | 123 | SimulateVisitor(float deltaTime, const MetavoxelLOD& lod); |
| 94 | 124 | |
| 95 | 125 | virtual int visit(MetavoxelInfo& info); |
| 96 | − | |
| 126 | + | |
| 97 | 127 | private: |
| 98 | 128 | |
| 99 | 129 | float _deltaTime; |
| 100 | 130 | }; |
| 101 | 131 | |
| 102 | 132 | SimulateVisitor::SimulateVisitor(float deltaTime, const MetavoxelLOD& lod) : |
| 103 | − MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getRendererAttribute(), | |
| 104 | − QVector<AttributePointer>(), lod), | |
| 105 | − _deltaTime(deltaTime) { | |
| 133 | +MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getRendererAttribute(), | |
| 134 | + QVector<AttributePointer>(), lod), | |
| 135 | +_deltaTime(deltaTime) { | |
| 106 | 136 | } |
| 107 | 137 | |
| 108 | 138 | int SimulateVisitor::visit(MetavoxelInfo& info) { |
| @@ -110,7 +140,7 @@ int SimulateVisitor::visit(MetavoxelInfo& info) { · 1 unmodified line | ||
| 110 | 140 | return DEFAULT_ORDER; |
| 111 | 141 | } |
| 112 | 142 | static_cast<MetavoxelRenderer*>(info.inputValues.at(0).getInlineValue< |
| 113 | − SharedObjectPointer>().data())->getImplementation()->simulate(*_data, _deltaTime, info, _lod); | |
| 143 | + SharedObjectPointer>().data())->getImplementation()->simulate(*_data, _deltaTime, info, _lod); | |
| 114 | 144 | return STOP_RECURSION; |
| 115 | 145 | } |
| 116 | 146 | |
| @@ -121,7 +151,7 @@ void MetavoxelSystem::simulate(float deltaTime) { · 4 unmodified lines | ||
| 121 | 151 | const float DEFAULT_LOD_THRESHOLD = 0.01f; |
| 122 | 152 | _lod = MetavoxelLOD(Application::getInstance()->getCamera()->getPosition(), DEFAULT_LOD_THRESHOLD); |
| 123 | 153 | } |
| 124 | − | |
| 154 | + | |
| 125 | 155 | SimulateVisitor simulateVisitor(deltaTime, getLOD()); |
| 126 | 156 | guideToAugmented(simulateVisitor); |
| 127 | 157 | } |
| @@ -135,8 +165,8 @@ public: · 7 unmodified lines | ||
| 135 | 165 | }; |
| 136 | 166 | |
| 137 | 167 | RenderVisitor::RenderVisitor(const MetavoxelLOD& lod) : |
| 138 | − MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getRendererAttribute(), | |
| 139 | − QVector<AttributePointer>(), lod) { | |
| 168 | +MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getRendererAttribute(), | |
| 169 | + QVector<AttributePointer>(), lod) { | |
| 140 | 170 | } |
| 141 | 171 | |
| 142 | 172 | int RenderVisitor::visit(MetavoxelInfo& info) { |
| @@ -144,213 +174,307 @@ int RenderVisitor::visit(MetavoxelInfo& info) { · 1 unmodified line | ||
| 144 | 174 | return DEFAULT_ORDER; |
| 145 | 175 | } |
| 146 | 176 | static_cast<MetavoxelRenderer*>(info.inputValues.at(0).getInlineValue< |
| 147 | − SharedObjectPointer>().data())->getImplementation()->render(*_data, info, _lod); | |
| 177 | + SharedObjectPointer>().data())->getImplementation()->render(*_data, info, _lod); | |
| 148 | 178 | return STOP_RECURSION; |
| 149 | 179 | } |
| 150 | 180 | |
| 151 | −void MetavoxelSystem::render() { | |
| 152 | − // update the frustum | |
| 153 | − ViewFrustum* viewFrustum = Application::getInstance()->getDisplayViewFrustum(); | |
| 154 | − _frustum.set(viewFrustum->getFarTopLeft(), viewFrustum->getFarTopRight(), viewFrustum->getFarBottomLeft(), | |
| 155 | − viewFrustum->getFarBottomRight(), viewFrustum->getNearTopLeft(), viewFrustum->getNearTopRight(), | |
| 156 | − viewFrustum->getNearBottomLeft(), viewFrustum->getNearBottomRight()); | |
| 157 | − | |
| 158 | − RenderVisitor renderVisitor(getLOD()); | |
| 159 | − guideToAugmented(renderVisitor, true); | |
| 160 | − | |
| 161 | − // give external parties a chance to join in | |
| 162 | − emit rendering(); | |
| 163 | −} | |
| 164 | − | |
| 165 | −void MetavoxelSystem::refreshVoxelData() { | |
| 166 | − NodeList::getInstance()->eachNode([](const SharedNodePointer& node){ | |
| 167 | − if (node->getType() == NodeType::MetavoxelServer) { | |
| 168 | − QMutexLocker locker(&node->getMutex()); | |
| 169 | − MetavoxelSystemClient* client = static_cast<MetavoxelSystemClient*>(node->getLinkedData()); | |
| 170 | − if (client) { | |
| 171 | − QMetaObject::invokeMethod(client, "refreshVoxelData"); | |
| 172 | − } | |
| 173 | − } | |
| 174 | − }); | |
| 175 | −} | |
| 176 | − | |
| 177 | −class RayHeightfieldIntersectionVisitor : public RayIntersectionVisitor { | |
| 181 | +class HeightfieldPoint { | |
| 178 | 182 | public: |
| 179 | − | |
| 180 | − float intersectionDistance; | |
| 181 | − | |
| 182 | − RayHeightfieldIntersectionVisitor(const glm::vec3& origin, const glm::vec3& direction, const MetavoxelLOD& lod); | |
| 183 | − | |
| 184 | − virtual int visit(MetavoxelInfo& info, float distance); | |
| 183 | + glm::vec3 vertex; | |
| 184 | + glm::vec2 textureCoord; | |
| 185 | 185 | }; |
| 186 | 186 | |
| 187 | −RayHeightfieldIntersectionVisitor::RayHeightfieldIntersectionVisitor(const glm::vec3& origin, | |
| 188 | − const glm::vec3& direction, const MetavoxelLOD& lod) : | |
| 189 | − RayIntersectionVisitor(origin, direction, QVector<AttributePointer>() << | |
| 190 | − Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), QVector<AttributePointer>(), lod), | |
| 191 | − intersectionDistance(FLT_MAX) { | |
| 192 | −} | |
| 187 | +const int SPLAT_COUNT = 4; | |
| 188 | +const GLint SPLAT_TEXTURE_UNITS[] = { 3, 4, 5, 6 }; | |
| 193 | 189 | |
| 194 | 190 | static const int EIGHT_BIT_MAXIMUM = 255; |
| 195 | 191 | static const float EIGHT_BIT_MAXIMUM_RECIPROCAL = 1.0f / EIGHT_BIT_MAXIMUM; |
| 196 | 192 | |
| 197 | −int RayHeightfieldIntersectionVisitor::visit(MetavoxelInfo& info, float distance) { | |
| 198 | − if (!info.isLeaf) { | |
| 199 | − return _order; | |
| 200 | − } | |
| 201 | − const HeightfieldBuffer* buffer = static_cast<HeightfieldBuffer*>( | |
| 202 | − info.inputValues.at(0).getInlineValue<BufferDataPointer>().data()); | |
| 203 | − if (!buffer) { | |
| 204 | − return STOP_RECURSION; | |
| 205 | − } | |
| 206 | − const QByteArray& contents = buffer->getHeight(); | |
| 207 | − const uchar* src = (const uchar*)contents.constData(); | |
| 208 | − int size = glm::sqrt((float)contents.size()); | |
| 209 | − int unextendedSize = size - HeightfieldBuffer::HEIGHT_EXTENSION; | |
| 210 | − int highest = HeightfieldBuffer::HEIGHT_BORDER + unextendedSize; | |
| 211 | − float heightScale = unextendedSize * EIGHT_BIT_MAXIMUM_RECIPROCAL; | |
| 212 | − | |
| 213 | − // find the initial location in heightfield coordinates | |
| 214 | − glm::vec3 entry = (_origin + distance * _direction - info.minimum) * (float)unextendedSize / info.size; | |
| 215 | − entry.x += HeightfieldBuffer::HEIGHT_BORDER; | |
| 216 | − entry.z += HeightfieldBuffer::HEIGHT_BORDER; | |
| 217 | − glm::vec3 floors = glm::floor(entry); | |
| 218 | − glm::vec3 ceils = glm::ceil(entry); | |
| 219 | − if (floors.x == ceils.x) { | |
| 220 | − if (_direction.x > 0.0f) { | |
| 221 | − ceils.x += 1.0f; | |
| 222 | − } else { | |
| 223 | − floors.x -= 1.0f; | |
| 224 | − } | |
| 225 | − } | |
| 226 | − if (floors.z == ceils.z) { | |
| 227 | − if (_direction.z > 0.0f) { | |
| 228 | − ceils.z += 1.0f; | |
| 229 | − } else { | |
| 230 | − floors.z -= 1.0f; | |
| 231 | − } | |
| 232 | − } | |
| 193 | +void MetavoxelSystem::render() { | |
| 194 | + // update the frustum | |
| 195 | + ViewFrustum* viewFrustum = Application::getInstance()->getDisplayViewFrustum(); | |
| 196 | + _frustum.set(viewFrustum->getFarTopLeft(), viewFrustum->getFarTopRight(), viewFrustum->getFarBottomLeft(), | |
| 197 | + viewFrustum->getFarBottomRight(), viewFrustum->getNearTopLeft(), viewFrustum->getNearTopRight(), | |
| 198 | + viewFrustum->getNearBottomLeft(), viewFrustum->getNearBottomRight()); | |
| 199 | + | |
| 200 | + RenderVisitor renderVisitor(getLOD()); | |
| 201 | + guideToAugmented(renderVisitor, true); | |
| 233 | 202 | |
| 234 | − bool withinBounds = true; | |
| 235 | − float accumulatedDistance = 0.0f; | |
| 236 | − while (withinBounds) { | |
| 237 | − // find the heights at the corners of the current cell | |
| 238 | − int floorX = qMin(qMax((int)floors.x, HeightfieldBuffer::HEIGHT_BORDER), highest); | |
| 239 | − int floorZ = qMin(qMax((int)floors.z, HeightfieldBuffer::HEIGHT_BORDER), highest); | |
| 240 | − int ceilX = qMin(qMax((int)ceils.x, HeightfieldBuffer::HEIGHT_BORDER), highest); | |
| 241 | − int ceilZ = qMin(qMax((int)ceils.z, HeightfieldBuffer::HEIGHT_BORDER), highest); | |
| 242 | − float upperLeft = src[floorZ * size + floorX] * heightScale; | |
| 243 | − float upperRight = src[floorZ * size + ceilX] * heightScale; | |
| 244 | − float lowerLeft = src[ceilZ * size + floorX] * heightScale; | |
| 245 | − float lowerRight = src[ceilZ * size + ceilX] * heightScale; | |
| 203 | + if (!_heightfieldBaseBatches.isEmpty()) { | |
| 204 | + glEnableClientState(GL_VERTEX_ARRAY); | |
| 205 | + glEnableClientState(GL_TEXTURE_COORD_ARRAY); | |
| 246 | 206 | |
| 247 | − // find the distance to the next x coordinate | |
| 248 | − float xDistance = FLT_MAX; | |
| 249 | − if (_direction.x > 0.0f) { | |
| 250 | − xDistance = (ceils.x - entry.x) / _direction.x; | |
| 251 | − } else if (_direction.x < 0.0f) { | |
| 252 | − xDistance = (floors.x - entry.x) / _direction.x; | |
| 253 | − } | |
| 207 | + Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, true); | |
| 208 | + | |
| 209 | + glDisable(GL_BLEND); | |
| 210 | + glEnable(GL_CULL_FACE); | |
| 211 | + glEnable(GL_ALPHA_TEST); | |
| 212 | + glAlphaFunc(GL_EQUAL, 0.0f); | |
| 213 | + | |
| 214 | + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); | |
| 254 | 215 | |
| 255 | − // and the distance to the next z coordinate | |
| 256 | − float zDistance = FLT_MAX; | |
| 257 | − if (_direction.z > 0.0f) { | |
| 258 | − zDistance = (ceils.z - entry.z) / _direction.z; | |
| 259 | − } else if (_direction.z < 0.0f) { | |
| 260 | − zDistance = (floors.z - entry.z) / _direction.z; | |
| 216 | + _baseHeightfieldProgram.bind(); | |
| 217 | + | |
| 218 | + foreach (const HeightfieldBaseLayerBatch& batch, _heightfieldBaseBatches) { | |
| 219 | + glPushMatrix(); | |
| 220 | + glTranslatef(batch.translation.x, batch.translation.y, batch.translation.z); | |
| 221 | + glm::vec3 axis = glm::axis(batch.rotation); | |
| 222 | + glRotatef(glm::degrees(glm::angle(batch.rotation)), axis.x, axis.y, axis.z); | |
| 223 | + glScalef(batch.scale.x, batch.scale.y, batch.scale.z); | |
| 224 | + | |
| 225 | + batch.vertexBuffer->bind(); | |
| 226 | + batch.indexBuffer->bind(); | |
| 227 | + | |
| 228 | + HeightfieldPoint* point = 0; | |
| 229 | + glVertexPointer(3, GL_FLOAT, sizeof(HeightfieldPoint), &point->vertex); | |
| 230 | + glTexCoordPointer(2, GL_FLOAT, sizeof(HeightfieldPoint), &point->textureCoord); | |
| 231 | + | |
| 232 | + glBindTexture(GL_TEXTURE_2D, batch.heightTextureID); | |
| 233 | + | |
| 234 | + _baseHeightfieldProgram.setUniform(_baseHeightScaleLocation, batch.heightScale); | |
| 235 | + _baseHeightfieldProgram.setUniform(_baseColorScaleLocation, batch.colorScale); | |
| 236 | + | |
| 237 | + glActiveTexture(GL_TEXTURE1); | |
| 238 | + glBindTexture(GL_TEXTURE_2D, batch.colorTextureID); | |
| 239 | + | |
| 240 | + glDrawRangeElements(GL_TRIANGLES, 0, batch.vertexCount - 1, batch.indexCount, GL_UNSIGNED_INT, 0); | |
| 241 | + | |
| 242 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 243 | + | |
| 244 | + glActiveTexture(GL_TEXTURE0); | |
| 245 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 246 | + | |
| 247 | + batch.vertexBuffer->release(); | |
| 248 | + batch.indexBuffer->release(); | |
| 249 | + | |
| 250 | + glPopMatrix(); | |
| 261 | 251 | } |
| 262 | 252 | |
| 263 | − // the exit distance is the lower of those two | |
| 264 | − float exitDistance = qMin(xDistance, zDistance); | |
| 265 | − glm::vec3 exit, nextFloors = floors, nextCeils = ceils; | |
| 266 | − if (exitDistance == FLT_MAX) { | |
| 267 | − if (_direction.y > 0.0f) { | |
| 268 | − return SHORT_CIRCUIT; // line points upwards; no collisions possible | |
| 269 | − } | |
| 270 | − withinBounds = false; // line points downwards; check this cell only | |
| 253 | + Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, false); | |
| 254 | + | |
| 255 | + _baseHeightfieldProgram.release(); | |
| 256 | + | |
| 257 | + glDisable(GL_ALPHA_TEST); | |
| 258 | + glEnable(GL_BLEND); | |
| 259 | + | |
| 260 | + if (!_heightfieldSplatBatches.isEmpty()) { | |
| 261 | + glDepthFunc(GL_LEQUAL); | |
| 262 | + glDepthMask(false); | |
| 263 | + glEnable(GL_POLYGON_OFFSET_FILL); | |
| 264 | + glPolygonOffset(-1.0f, -1.0f); | |
| 271 | 265 | |
| 272 | − } else { | |
| 273 | − // find the exit point and the next cell, and determine whether it's still within the bounds | |
| 274 | − exit = entry + exitDistance * _direction; | |
| 275 | − withinBounds = (exit.y >= HeightfieldBuffer::HEIGHT_BORDER && exit.y <= highest); | |
| 276 | − if (exitDistance == xDistance) { | |
| 277 | − if (_direction.x > 0.0f) { | |
| 278 | − nextFloors.x += 1.0f; | |
| 279 | − withinBounds &= (nextCeils.x += 1.0f) <= highest; | |
| 280 | − } else { | |
| 281 | − withinBounds &= (nextFloors.x -= 1.0f) >= HeightfieldBuffer::HEIGHT_BORDER; | |
| 282 | − nextCeils.x -= 1.0f; | |
| 266 | + _splatHeightfieldProgram.bind(); | |
| 267 | + | |
| 268 | + foreach (const HeightfieldSplatBatch& batch, _heightfieldSplatBatches) { | |
| 269 | + glPushMatrix(); | |
| 270 | + glTranslatef(batch.translation.x, batch.translation.y, batch.translation.z); | |
| 271 | + glm::vec3 axis = glm::axis(batch.rotation); | |
| 272 | + glRotatef(glm::degrees(glm::angle(batch.rotation)), axis.x, axis.y, axis.z); | |
| 273 | + glScalef(batch.scale.x, batch.scale.y, batch.scale.z); | |
| 274 | + | |
| 275 | + batch.vertexBuffer->bind(); | |
| 276 | + batch.indexBuffer->bind(); | |
| 277 | + | |
| 278 | + HeightfieldPoint* point = 0; | |
| 279 | + glVertexPointer(3, GL_FLOAT, sizeof(HeightfieldPoint), &point->vertex); | |
| 280 | + glTexCoordPointer(2, GL_FLOAT, sizeof(HeightfieldPoint), &point->textureCoord); | |
| 281 | + | |
| 282 | + glBindTexture(GL_TEXTURE_2D, batch.heightTextureID); | |
| 283 | + | |
| 284 | + _splatHeightfieldProgram.setUniformValue(_splatHeightfieldLocations.heightScale, | |
| 285 | + batch.heightScale.x, batch.heightScale.y); | |
| 286 | + _splatHeightfieldProgram.setUniform(_splatHeightfieldLocations.textureScale, batch.textureScale); | |
| 287 | + _splatHeightfieldProgram.setUniform(_splatHeightfieldLocations.splatTextureOffset, batch.splatTextureOffset); | |
| 288 | + | |
| 289 | + const float QUARTER_STEP = 0.25f * EIGHT_BIT_MAXIMUM_RECIPROCAL; | |
| 290 | + _splatHeightfieldProgram.setUniform(_splatHeightfieldLocations.splatTextureScalesS, batch.splatTextureScalesS); | |
| 291 | + _splatHeightfieldProgram.setUniform(_splatHeightfieldLocations.splatTextureScalesT, batch.splatTextureScalesT); | |
| 292 | + _splatHeightfieldProgram.setUniformValue( | |
| 293 | + _splatHeightfieldLocations.textureValueMinima, | |
| 294 | + (batch.materialIndex + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, | |
| 295 | + (batch.materialIndex + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, | |
| 296 | + (batch.materialIndex + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, | |
| 297 | + (batch.materialIndex + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP); | |
| 298 | + _splatHeightfieldProgram.setUniformValue( | |
| 299 | + _splatHeightfieldLocations.textureValueMaxima, | |
| 300 | + (batch.materialIndex + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, | |
| 301 | + (batch.materialIndex + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, | |
| 302 | + (batch.materialIndex + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, | |
| 303 | + (batch.materialIndex + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP); | |
| 304 | + | |
| 305 | + glActiveTexture(GL_TEXTURE1); | |
| 306 | + glBindTexture(GL_TEXTURE_2D, batch.materialTextureID); | |
| 307 | + | |
| 308 | + for (int i = 0; i < SPLAT_COUNT; i++) { | |
| 309 | + glActiveTexture(GL_TEXTURE0 + SPLAT_TEXTURE_UNITS[i]); | |
| 310 | + glBindTexture(GL_TEXTURE_2D, batch.splatTextureIDs[i]); | |
| 283 | 311 | } |
| 284 | − } | |
| 285 | − if (exitDistance == zDistance) { | |
| 286 | − if (_direction.z > 0.0f) { | |
| 287 | − nextFloors.z += 1.0f; | |
| 288 | − withinBounds &= (nextCeils.z += 1.0f) <= highest; | |
| 289 | − } else { | |
| 290 | − withinBounds &= (nextFloors.z -= 1.0f) >= HeightfieldBuffer::HEIGHT_BORDER; | |
| 291 | − nextCeils.z -= 1.0f; | |
| 312 | + | |
| 313 | + glDrawRangeElements(GL_TRIANGLES, 0, batch.vertexCount - 1, batch.indexCount, GL_UNSIGNED_INT, 0); | |
| 314 | + | |
| 315 | + for (int i = 0; i < SPLAT_COUNT; i++) { | |
| 316 | + glActiveTexture(GL_TEXTURE0 + SPLAT_TEXTURE_UNITS[i]); | |
| 317 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 292 | 318 | } |
| 319 | + | |
| 320 | + glActiveTexture(GL_TEXTURE1); | |
| 321 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 322 | + | |
| 323 | + glActiveTexture(GL_TEXTURE0); | |
| 324 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 325 | + | |
| 326 | + batch.vertexBuffer->release(); | |
| 327 | + batch.indexBuffer->release(); | |
| 328 | + | |
| 329 | + glPopMatrix(); | |
| 293 | 330 | } |
| 294 | − // check the vertical range of the ray against the ranges of the cell heights | |
| 295 | − if (qMin(entry.y, exit.y) > qMax(qMax(upperLeft, upperRight), qMax(lowerLeft, lowerRight)) || | |
| 296 | − qMax(entry.y, exit.y) < qMin(qMin(upperLeft, upperRight), qMin(lowerLeft, lowerRight))) { | |
| 297 | − entry = exit; | |
| 298 | − floors = nextFloors; | |
| 299 | − ceils = nextCeils; | |
| 300 | − accumulatedDistance += exitDistance; | |
| 301 | − continue; | |
| 302 | − } | |
| 331 | + | |
| 332 | + _splatHeightfieldProgram.release(); | |
| 333 | + | |
| 334 | + glDisable(GL_POLYGON_OFFSET_FILL); | |
| 335 | + glDepthMask(true); | |
| 336 | + glDepthFunc(GL_LESS); | |
| 337 | + | |
| 338 | + _heightfieldSplatBatches.clear(); | |
| 303 | 339 | } |
| 304 | − // having passed the bounds check, we must check against the planes | |
| 305 | − glm::vec3 relativeEntry = entry - glm::vec3(floors.x, upperLeft, floors.z); | |
| 306 | 340 | |
| 307 | − // first check the triangle including the Z+ segment | |
| 308 | − glm::vec3 lowerNormal(lowerLeft - lowerRight, 1.0f, upperLeft - lowerLeft); | |
| 309 | − float lowerProduct = glm::dot(lowerNormal, _direction); | |
| 310 | − if (lowerProduct < 0.0f) { | |
| 311 | − float planeDistance = -glm::dot(lowerNormal, relativeEntry) / lowerProduct; | |
| 312 | − glm::vec3 intersection = relativeEntry + planeDistance * _direction; | |
| 313 | − if (intersection.x >= 0.0f && intersection.x <= 1.0f && intersection.z >= 0.0f && intersection.z <= 1.0f && | |
| 314 | − intersection.z >= intersection.x) { | |
| 315 | − intersectionDistance = qMin(intersectionDistance, distance + | |
| 316 | − (accumulatedDistance + planeDistance) * (info.size / unextendedSize)); | |
| 317 | − return SHORT_CIRCUIT; | |
| 318 | − } | |
| 341 | + glDisable(GL_CULL_FACE); | |
| 342 | + | |
| 343 | + glDisableClientState(GL_TEXTURE_COORD_ARRAY); | |
| 344 | + glDisableClientState(GL_VERTEX_ARRAY); | |
| 345 | + | |
| 346 | + _heightfieldBaseBatches.clear(); | |
| 347 | + } | |
| 348 | + | |
| 349 | + if (!_voxelBaseBatches.isEmpty()) { | |
| 350 | + Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, true); | |
| 351 | + | |
| 352 | + glEnableClientState(GL_VERTEX_ARRAY); | |
| 353 | + glDisable(GL_BLEND); | |
| 354 | + glEnable(GL_CULL_FACE); | |
| 355 | + glEnable(GL_ALPHA_TEST); | |
| 356 | + glAlphaFunc(GL_EQUAL, 0.0f); | |
| 357 | + | |
| 358 | + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); | |
| 359 | + | |
| 360 | + glEnableClientState(GL_COLOR_ARRAY); | |
| 361 | + glEnableClientState(GL_NORMAL_ARRAY); | |
| 362 | + | |
| 363 | + _baseVoxelProgram.bind(); | |
| 364 | + | |
| 365 | + foreach (const VoxelBatch& batch, _voxelBaseBatches) { | |
| 366 | + batch.vertexBuffer->bind(); | |
| 367 | + batch.indexBuffer->bind(); | |
| 368 | + | |
| 369 | + VoxelPoint* point = 0; | |
| 370 | + glVertexPointer(3, GL_FLOAT, sizeof(VoxelPoint), &point->vertex); | |
| 371 | + glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(VoxelPoint), &point->color); | |
| 372 | + glNormalPointer(GL_BYTE, sizeof(VoxelPoint), &point->normal); | |
| 373 | + | |
| 374 | + glDrawRangeElements(GL_QUADS, 0, batch.vertexCount - 1, batch.indexCount, GL_UNSIGNED_INT, 0); | |
| 375 | + | |
| 376 | + batch.vertexBuffer->release(); | |
| 377 | + batch.indexBuffer->release(); | |
| 319 | 378 | } |
| 320 | 379 | |
| 321 | − // then the one with the X+ segment | |
| 322 | − glm::vec3 upperNormal(upperLeft - upperRight, 1.0f, upperRight - lowerRight); | |
| 323 | − float upperProduct = glm::dot(upperNormal, _direction); | |
| 324 | − if (upperProduct < 0.0f) { | |
| 325 | − float planeDistance = -glm::dot(upperNormal, relativeEntry) / upperProduct; | |
| 326 | − glm::vec3 intersection = relativeEntry + planeDistance * _direction; | |
| 327 | − if (intersection.x >= 0.0f && intersection.x <= 1.0f && intersection.z >= 0.0f && intersection.z <= 1.0f && | |
| 328 | − intersection.x >= intersection.z) { | |
| 329 | − intersectionDistance = qMin(intersectionDistance, distance + | |
| 330 | − (accumulatedDistance + planeDistance) * (info.size / unextendedSize)); | |
| 331 | − return SHORT_CIRCUIT; | |
| 380 | + _baseVoxelProgram.release(); | |
| 381 | + | |
| 382 | + glDisable(GL_ALPHA_TEST); | |
| 383 | + glEnable(GL_BLEND); | |
| 384 | + | |
| 385 | + Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, false); | |
| 386 | + | |
| 387 | + if (!_voxelSplatBatches.isEmpty()) { | |
| 388 | + glDepthFunc(GL_LEQUAL); | |
| 389 | + glDepthMask(false); | |
| 390 | + glEnable(GL_POLYGON_OFFSET_FILL); | |
| 391 | + glPolygonOffset(-1.0f, -1.0f); | |
| 392 | + | |
| 393 | + _splatVoxelProgram.bind(); | |
| 394 | + | |
| 395 | + _splatVoxelProgram.enableAttributeArray(_splatVoxelLocations.materials); | |
| 396 | + _splatVoxelProgram.enableAttributeArray(_splatVoxelLocations.materialWeights); | |
| 397 | + | |
| 398 | + foreach (const VoxelSplatBatch& batch, _voxelSplatBatches) { | |
| 399 | + batch.vertexBuffer->bind(); | |
| 400 | + batch.indexBuffer->bind(); | |
| 401 | + | |
| 402 | + VoxelPoint* point = 0; | |
| 403 | + glVertexPointer(3, GL_FLOAT, sizeof(VoxelPoint), &point->vertex); | |
| 404 | + glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(VoxelPoint), &point->color); | |
| 405 | + glNormalPointer(GL_BYTE, sizeof(VoxelPoint), &point->normal); | |
| 406 | + | |
| 407 | + _splatVoxelProgram.setAttributeBuffer(_splatVoxelLocations.materials, | |
| 408 | + GL_UNSIGNED_BYTE, (qint64)&point->materials, SPLAT_COUNT, sizeof(VoxelPoint)); | |
| 409 | + _splatVoxelProgram.setAttributeBuffer(_splatVoxelLocations.materialWeights, | |
| 410 | + GL_UNSIGNED_BYTE, (qint64)&point->materialWeights, SPLAT_COUNT, sizeof(VoxelPoint)); | |
| 411 | + | |
| 412 | + const float QUARTER_STEP = 0.25f * EIGHT_BIT_MAXIMUM_RECIPROCAL; | |
| 413 | + _splatVoxelProgram.setUniform(_splatVoxelLocations.splatTextureScalesS, batch.splatTextureScalesS); | |
| 414 | + _splatVoxelProgram.setUniform(_splatVoxelLocations.splatTextureScalesT, batch.splatTextureScalesT); | |
| 415 | + _splatVoxelProgram.setUniformValue( | |
| 416 | + _splatVoxelLocations.textureValueMinima, | |
| 417 | + (batch.materialIndex + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, | |
| 418 | + (batch.materialIndex + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, | |
| 419 | + (batch.materialIndex + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, | |
| 420 | + (batch.materialIndex + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP); | |
| 421 | + _splatVoxelProgram.setUniformValue( | |
| 422 | + _splatVoxelLocations.textureValueMaxima, | |
| 423 | + (batch.materialIndex + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, | |
| 424 | + (batch.materialIndex + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, | |
| 425 | + (batch.materialIndex + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, | |
| 426 | + (batch.materialIndex + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP); | |
| 427 | + | |
| 428 | + for (int i = 0; i < SPLAT_COUNT; i++) { | |
| 429 | + glActiveTexture(GL_TEXTURE0 + SPLAT_TEXTURE_UNITS[i]); | |
| 430 | + glBindTexture(GL_TEXTURE_2D, batch.splatTextureIDs[i]); | |
| 431 | + } | |
| 432 | + | |
| 433 | + glDrawRangeElements(GL_QUADS, 0, batch.vertexCount - 1, batch.indexCount, GL_UNSIGNED_INT, 0); | |
| 434 | + | |
| 435 | + for (int i = 0; i < SPLAT_COUNT; i++) { | |
| 436 | + glActiveTexture(GL_TEXTURE0 + SPLAT_TEXTURE_UNITS[i]); | |
| 437 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 438 | + } | |
| 439 | + | |
| 440 | + glActiveTexture(GL_TEXTURE0); | |
| 441 | + | |
| 442 | + batch.vertexBuffer->release(); | |
| 443 | + batch.indexBuffer->release(); | |
| 332 | 444 | } |
| 445 | + | |
| 446 | + glDisable(GL_POLYGON_OFFSET_FILL); | |
| 447 | + glDepthMask(true); | |
| 448 | + glDepthFunc(GL_LESS); | |
| 449 | + | |
| 450 | + _splatVoxelProgram.disableAttributeArray(_splatVoxelLocations.materials); | |
| 451 | + _splatVoxelProgram.disableAttributeArray(_splatVoxelLocations.materialWeights); | |
| 452 | + | |
| 453 | + _voxelSplatBatches.clear(); | |
| 333 | 454 | } |
| 334 | 455 | |
| 335 | − // no joy; continue on our way | |
| 336 | − entry = exit; | |
| 337 | − floors = nextFloors; | |
| 338 | − ceils = nextCeils; | |
| 339 | − accumulatedDistance += exitDistance; | |
| 456 | + glDisableClientState(GL_VERTEX_ARRAY); | |
| 457 | + glDisableClientState(GL_COLOR_ARRAY); | |
| 458 | + glDisableClientState(GL_NORMAL_ARRAY); | |
| 459 | + glDisable(GL_CULL_FACE); | |
| 460 | + | |
| 461 | + _voxelBaseBatches.clear(); | |
| 340 | 462 | } |
| 341 | 463 | |
| 342 | − return STOP_RECURSION; | |
| 464 | + // give external parties a chance to join in | |
| 465 | + emit rendering(); | |
| 343 | 466 | } |
| 344 | 467 | |
| 345 | −bool MetavoxelSystem::findFirstRayHeightfieldIntersection(const glm::vec3& origin, | |
| 346 | − const glm::vec3& direction, float& distance) { | |
| 347 | − RayHeightfieldIntersectionVisitor visitor(origin, direction, getLOD()); | |
| 348 | − guideToAugmented(visitor); | |
| 349 | − if (visitor.intersectionDistance == FLT_MAX) { | |
| 350 | − return false; | |
| 351 | − } | |
| 352 | − distance = visitor.intersectionDistance; | |
| 353 | − return true; | |
| 468 | +void MetavoxelSystem::refreshVoxelData() { | |
| 469 | + NodeList::getInstance()->eachNode([](const SharedNodePointer& node){ | |
| 470 | + if (node->getType() == NodeType::MetavoxelServer) { | |
| 471 | + QMutexLocker locker(&node->getMutex()); | |
| 472 | + MetavoxelSystemClient* client = static_cast<MetavoxelSystemClient*>(node->getLinkedData()); | |
| 473 | + if (client) { | |
| 474 | + QMetaObject::invokeMethod(client, "refreshVoxelData"); | |
| 475 | + } | |
| 476 | + } | |
| 477 | + }); | |
| 354 | 478 | } |
| 355 | 479 | |
| 356 | 480 | class RayVoxelIntersectionVisitor : public RayIntersectionVisitor { |
| @@ -364,10 +488,10 @@ public: · 7 unmodified lines | ||
| 364 | 488 | }; |
| 365 | 489 | |
| 366 | 490 | RayVoxelIntersectionVisitor::RayVoxelIntersectionVisitor(const glm::vec3& origin, |
| 367 | − const glm::vec3& direction, const MetavoxelLOD& lod) : | |
| 368 | − RayIntersectionVisitor(origin, direction, QVector<AttributePointer>() << | |
| 369 | − Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute(), QVector<AttributePointer>(), lod), | |
| 370 | − intersectionDistance(FLT_MAX) { | |
| 491 | + const glm::vec3& direction, const MetavoxelLOD& lod) : | |
| 492 | +RayIntersectionVisitor(origin, direction, QVector<AttributePointer>() << | |
| 493 | + Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute(), QVector<AttributePointer>(), lod), | |
| 494 | +intersectionDistance(FLT_MAX) { | |
| 371 | 495 | } |
| 372 | 496 | |
| 373 | 497 | int RayVoxelIntersectionVisitor::visit(MetavoxelInfo& info, float distance) { |
| @@ -375,7 +499,7 @@ int RayVoxelIntersectionVisitor::visit(MetavoxelInfo& info, float distance) { · 1 unmodified line | ||
| 375 | 499 | return _order; |
| 376 | 500 | } |
| 377 | 501 | const VoxelBuffer* buffer = static_cast<VoxelBuffer*>( |
| 378 | − info.inputValues.at(0).getInlineValue<BufferDataPointer>().data()); | |
| 502 | + info.inputValues.at(0).getInlineValue<BufferDataPointer>().data()); | |
| 379 | 503 | if (!buffer) { |
| 380 | 504 | return STOP_RECURSION; |
| 381 | 505 | } |
| @@ -396,89 +520,6 @@ bool MetavoxelSystem::findFirstRayVoxelIntersection(const glm::vec3& origin, con · 14 unmodified lines | ||
| 396 | 520 | return true; |
| 397 | 521 | } |
| 398 | 522 | |
| 399 | −class HeightfieldHeightVisitor : public MetavoxelVisitor { | |
| 400 | −public: | |
| 401 | − | |
| 402 | − float height; | |
| 403 | − | |
| 404 | − HeightfieldHeightVisitor(const MetavoxelLOD& lod, const glm::vec3& location); | |
| 405 | − | |
| 406 | − virtual int visit(MetavoxelInfo& info); | |
| 407 | − | |
| 408 | −private: | |
| 409 | − | |
| 410 | − glm::vec3 _location; | |
| 411 | −}; | |
| 412 | − | |
| 413 | −HeightfieldHeightVisitor::HeightfieldHeightVisitor(const MetavoxelLOD& lod, const glm::vec3& location) : | |
| 414 | − MetavoxelVisitor(QVector<AttributePointer>() << | |
| 415 | − Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), QVector<AttributePointer>(), lod), | |
| 416 | − height(-FLT_MAX), | |
| 417 | − _location(location) { | |
| 418 | −} | |
| 419 | − | |
| 420 | −static const int REVERSE_ORDER = MetavoxelVisitor::encodeOrder(7, 6, 5, 4, 3, 2, 1, 0); | |
| 421 | − | |
| 422 | −int HeightfieldHeightVisitor::visit(MetavoxelInfo& info) { | |
| 423 | − glm::vec3 relative = _location - info.minimum; | |
| 424 | − if (relative.x < 0.0f || relative.z < 0.0f || relative.x > info.size || relative.z > info.size || | |
| 425 | − height >= info.minimum.y + info.size) { | |
| 426 | − return STOP_RECURSION; | |
| 427 | − } | |
| 428 | − if (!info.isLeaf) { | |
| 429 | − return REVERSE_ORDER; | |
| 430 | − } | |
| 431 | − const HeightfieldBuffer* buffer = static_cast<HeightfieldBuffer*>( | |
| 432 | − info.inputValues.at(0).getInlineValue<BufferDataPointer>().data()); | |
| 433 | − if (!buffer) { | |
| 434 | − return STOP_RECURSION; | |
| 435 | − } | |
| 436 | − const QByteArray& contents = buffer->getHeight(); | |
| 437 | − const uchar* src = (const uchar*)contents.constData(); | |
| 438 | − int size = glm::sqrt((float)contents.size()); | |
| 439 | − int unextendedSize = size - HeightfieldBuffer::HEIGHT_EXTENSION; | |
| 440 | − int highest = HeightfieldBuffer::HEIGHT_BORDER + unextendedSize; | |
| 441 | − relative *= unextendedSize / info.size; | |
| 442 | − relative.x += HeightfieldBuffer::HEIGHT_BORDER; | |
| 443 | − relative.z += HeightfieldBuffer::HEIGHT_BORDER; | |
| 444 | − | |
| 445 | − // find the bounds of the cell containing the point and the shared vertex heights | |
| 446 | − glm::vec3 floors = glm::floor(relative); | |
| 447 | − glm::vec3 ceils = glm::ceil(relative); | |
| 448 | − glm::vec3 fracts = glm::fract(relative); | |
| 449 | − int floorX = qMin(qMax((int)floors.x, HeightfieldBuffer::HEIGHT_BORDER), highest); | |
| 450 | − int floorZ = qMin(qMax((int)floors.z, HeightfieldBuffer::HEIGHT_BORDER), highest); | |
| 451 | − int ceilX = qMin(qMax((int)ceils.x, HeightfieldBuffer::HEIGHT_BORDER), highest); | |
| 452 | − int ceilZ = qMin(qMax((int)ceils.z, HeightfieldBuffer::HEIGHT_BORDER), highest); | |
| 453 | − float upperLeft = src[floorZ * size + floorX]; | |
| 454 | − float lowerRight = src[ceilZ * size + ceilX]; | |
| 455 | − float interpolatedHeight = glm::mix(upperLeft, lowerRight, fracts.z); | |
| 456 | − | |
| 457 | − // the final vertex (and thus which triangle we check) depends on which half we're on | |
| 458 | − if (fracts.x >= fracts.z) { | |
| 459 | − float upperRight = src[floorZ * size + ceilX]; | |
| 460 | − interpolatedHeight = glm::mix(interpolatedHeight, glm::mix(upperRight, lowerRight, fracts.z), | |
| 461 | − (fracts.x - fracts.z) / (1.0f - fracts.z)); | |
| 462 | − | |
| 463 | − } else { | |
| 464 | − float lowerLeft = src[ceilZ * size + floorX]; | |
| 465 | − interpolatedHeight = glm::mix(glm::mix(upperLeft, lowerLeft, fracts.z), interpolatedHeight, fracts.x / fracts.z); | |
| 466 | − } | |
| 467 | − if (interpolatedHeight == 0.0f) { | |
| 468 | − return STOP_RECURSION; // ignore zero values | |
| 469 | − } | |
| 470 | − | |
| 471 | − // convert the interpolated height into world space | |
| 472 | − height = qMax(height, info.minimum.y + interpolatedHeight * info.size * EIGHT_BIT_MAXIMUM_RECIPROCAL); | |
| 473 | − return SHORT_CIRCUIT; | |
| 474 | −} | |
| 475 | − | |
| 476 | −float MetavoxelSystem::getHeightfieldHeight(const glm::vec3& location) { | |
| 477 | − HeightfieldHeightVisitor visitor(getLOD(), location); | |
| 478 | − guideToAugmented(visitor); | |
| 479 | − return visitor.height; | |
| 480 | −} | |
| 481 | − | |
| 482 | 523 | void MetavoxelSystem::paintHeightfieldColor(const glm::vec3& position, float radius, const QColor& color) { |
| 483 | 524 | MetavoxelEditMessage edit = { QVariant::fromValue(PaintHeightfieldMaterialEdit(position, radius, SharedObjectPointer(), color)) }; |
| 484 | 525 | applyEdit(edit, true); |
| @@ -509,35 +550,34 @@ void MetavoxelSystem::setVoxelMaterial(const SharedObjectPointer& spanner, const · 24 unmodified lines | ||
| 509 | 550 | applyMaterialEdit(edit, true); |
| 510 | 551 | } |
| 511 | 552 | |
| 512 | −class CursorRenderVisitor : public MetavoxelVisitor { | |
| 553 | +class SpannerCursorRenderVisitor : public SpannerVisitor { | |
| 513 | 554 | public: |
| 514 | 555 | |
| 515 | − CursorRenderVisitor(const AttributePointer& attribute, const Box& bounds); | |
| 556 | + SpannerCursorRenderVisitor(const Box& bounds); | |
| 557 | + | |
| 558 | + virtual bool visit(Spanner* spanner); | |
| 516 | 559 | |
| 517 | 560 | virtual int visit(MetavoxelInfo& info); |
| 518 | − | |
| 561 | + | |
| 519 | 562 | private: |
| 520 | 563 | |
| 521 | 564 | Box _bounds; |
| 522 | 565 | }; |
| 523 | 566 | |
| 524 | −CursorRenderVisitor::CursorRenderVisitor(const AttributePointer& attribute, const Box& bounds) : | |
| 525 | − MetavoxelVisitor(QVector<AttributePointer>() << attribute), | |
| 526 | − _bounds(bounds) { | |
| 567 | +SpannerCursorRenderVisitor::SpannerCursorRenderVisitor(const Box& bounds) : | |
| 568 | +SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute()), | |
| 569 | +_bounds(bounds) { | |
| 527 | 570 | } |
| 528 | 571 | |
| 529 | −int CursorRenderVisitor::visit(MetavoxelInfo& info) { | |
| 530 | − if (!info.getBounds().intersects(_bounds)) { | |
| 531 | − return STOP_RECURSION; | |
| 532 | − } | |
| 533 | − if (!info.isLeaf) { | |
| 534 | − return DEFAULT_ORDER; | |
| 572 | +bool SpannerCursorRenderVisitor::visit(Spanner* spanner) { | |
| 573 | + if (spanner->isHeightfield()) { | |
| 574 | + spanner->getRenderer()->render(true); | |
| 535 | 575 | } |
| 536 | − BufferDataPointer buffer = info.inputValues.at(0).getInlineValue<BufferDataPointer>(); | |
| 537 | − if (buffer) { | |
| 538 | − buffer->render(true); | |
| 539 | − } | |
| 540 | − return STOP_RECURSION; | |
| 576 | + return true; | |
| 577 | +} | |
| 578 | + | |
| 579 | +int SpannerCursorRenderVisitor::visit(MetavoxelInfo& info) { | |
| 580 | + return info.getBounds().intersects(_bounds) ? SpannerVisitor::visit(info) : STOP_RECURSION; | |
| 541 | 581 | } |
| 542 | 582 | |
| 543 | 583 | void MetavoxelSystem::renderHeightfieldCursor(const glm::vec3& position, float radius) { |
| @@ -551,7 +591,7 @@ void MetavoxelSystem::renderHeightfieldCursor(const glm::vec3& position, float r · 7 unmodified lines | ||
| 551 | 591 | glEnableClientState(GL_VERTEX_ARRAY); |
| 552 | 592 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 553 | 593 | |
| 554 | − DefaultMetavoxelRendererImplementation::getHeightfieldCursorProgram().bind(); | |
| 594 | + _heightfieldCursorProgram.bind(); | |
| 555 | 595 | |
| 556 | 596 | glActiveTexture(GL_TEXTURE4); |
| 557 | 597 | float scale = 1.0f / radius; |
| @@ -564,11 +604,10 @@ void MetavoxelSystem::renderHeightfieldCursor(const glm::vec3& position, float r · 6 unmodified lines | ||
| 564 | 604 | glActiveTexture(GL_TEXTURE0); |
| 565 | 605 | |
| 566 | 606 | glm::vec3 extents(radius, radius, radius); |
| 567 | − CursorRenderVisitor visitor(Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), | |
| 568 | − Box(position - extents, position + extents)); | |
| 569 | − guideToAugmented(visitor); | |
| 607 | + SpannerCursorRenderVisitor visitor(Box(position - extents, position + extents)); | |
| 608 | + guide(visitor); | |
| 570 | 609 | |
| 571 | − DefaultMetavoxelRendererImplementation::getHeightfieldCursorProgram().release(); | |
| 610 | + _heightfieldCursorProgram.release(); | |
| 572 | 611 | |
| 573 | 612 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 574 | 613 | glDisableClientState(GL_VERTEX_ARRAY); |
| @@ -578,6 +617,34 @@ void MetavoxelSystem::renderHeightfieldCursor(const glm::vec3& position, float r · 3 unmodified lines | ||
| 578 | 617 | glDepthFunc(GL_LESS); |
| 579 | 618 | } |
| 580 | 619 | |
| 620 | +class BufferCursorRenderVisitor : public MetavoxelVisitor { | |
| 621 | +public: | |
| 622 | + | |
| 623 | + BufferCursorRenderVisitor(const AttributePointer& attribute, const Box& bounds); | |
| 624 | + | |
| 625 | + virtual int visit(MetavoxelInfo& info); | |
| 626 | + | |
| 627 | +private: | |
| 628 | + | |
| 629 | + Box _bounds; | |
| 630 | +}; | |
| 631 | + | |
| 632 | +BufferCursorRenderVisitor::BufferCursorRenderVisitor(const AttributePointer& attribute, const Box& bounds) : | |
| 633 | +MetavoxelVisitor(QVector<AttributePointer>() << attribute), | |
| 634 | +_bounds(bounds) { | |
| 635 | +} | |
| 636 | + | |
| 637 | +int BufferCursorRenderVisitor::visit(MetavoxelInfo& info) { | |
| 638 | + if (!info.getBounds().intersects(_bounds)) { | |
| 639 | + return STOP_RECURSION; | |
| 640 | + } | |
| 641 | + BufferData* buffer = info.inputValues.at(0).getInlineValue<BufferDataPointer>().data(); | |
| 642 | + if (buffer) { | |
| 643 | + buffer->render(true); | |
| 644 | + } | |
| 645 | + return info.isLeaf ? STOP_RECURSION : DEFAULT_ORDER; | |
| 646 | +} | |
| 647 | + | |
| 581 | 648 | void MetavoxelSystem::renderVoxelCursor(const glm::vec3& position, float radius) { |
| 582 | 649 | glDepthFunc(GL_LEQUAL); |
| 583 | 650 | glEnable(GL_CULL_FACE); |
| @@ -588,7 +655,7 @@ void MetavoxelSystem::renderVoxelCursor(const glm::vec3& position, float radius) · 4 unmodified lines | ||
| 588 | 655 | |
| 589 | 656 | glEnableClientState(GL_VERTEX_ARRAY); |
| 590 | 657 | |
| 591 | − DefaultMetavoxelRendererImplementation::getVoxelCursorProgram().bind(); | |
| 658 | + _voxelCursorProgram.bind(); | |
| 592 | 659 | |
| 593 | 660 | glActiveTexture(GL_TEXTURE4); |
| 594 | 661 | float scale = 1.0f / radius; |
| @@ -602,22 +669,22 @@ void MetavoxelSystem::renderVoxelCursor(const glm::vec3& position, float radius) · 7 unmodified lines | ||
| 602 | 669 | |
| 603 | 670 | glm::vec3 extents(radius, radius, radius); |
| 604 | 671 | Box bounds(position - extents, position + extents); |
| 605 | − CursorRenderVisitor voxelVisitor(Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute(), bounds); | |
| 672 | + BufferCursorRenderVisitor voxelVisitor(Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute(), bounds); | |
| 606 | 673 | guideToAugmented(voxelVisitor); |
| 607 | 674 | |
| 608 | − DefaultMetavoxelRendererImplementation::getVoxelCursorProgram().release(); | |
| 675 | + _voxelCursorProgram.release(); | |
| 609 | 676 | |
| 610 | 677 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 611 | 678 | |
| 612 | − DefaultMetavoxelRendererImplementation::getHeightfieldCursorProgram().bind(); | |
| 679 | + _heightfieldCursorProgram.bind(); | |
| 613 | 680 | |
| 614 | − CursorRenderVisitor heightfieldVisitor(Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), | |
| 615 | − bounds); | |
| 616 | − guideToAugmented(heightfieldVisitor); | |
| 681 | + SpannerCursorRenderVisitor spannerVisitor(bounds); | |
| 682 | + guide(spannerVisitor); | |
| 617 | 683 | |
| 618 | − DefaultMetavoxelRendererImplementation::getHeightfieldCursorProgram().release(); | |
| 684 | + _heightfieldCursorProgram.release(); | |
| 619 | 685 | |
| 620 | 686 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 687 | + | |
| 621 | 688 | glDisableClientState(GL_VERTEX_ARRAY); |
| 622 | 689 | |
| 623 | 690 | glDisable(GL_POLYGON_OFFSET_FILL); |
| @@ -625,19 +692,13 @@ void MetavoxelSystem::renderVoxelCursor(const glm::vec3& position, float radius) · 1 unmodified line | ||
| 625 | 692 | glDepthFunc(GL_LESS); |
| 626 | 693 | } |
| 627 | 694 | |
| 628 | −void MetavoxelSystem::deleteTextures(int heightID, int colorID, int textureID) { | |
| 629 | − glDeleteTextures(1, (GLuint*)&heightID); | |
| 630 | − glDeleteTextures(1, (GLuint*)&colorID); | |
| 631 | − glDeleteTextures(1, (GLuint*)&textureID); | |
| 632 | −} | |
| 633 | − | |
| 634 | 695 | class MaterialEditApplier : public SignalHandler { |
| 635 | 696 | public: |
| 636 | − | |
| 697 | + | |
| 637 | 698 | MaterialEditApplier(const MetavoxelEditMessage& message, const QSharedPointer<NetworkTexture> texture); |
| 638 | 699 | |
| 639 | 700 | virtual void handle(); |
| 640 | − | |
| 701 | + | |
| 641 | 702 | protected: |
| 642 | 703 | |
| 643 | 704 | MetavoxelEditMessage _message; |
| @@ -645,8 +706,8 @@ protected: · 1 unmodified line | ||
| 645 | 706 | }; |
| 646 | 707 | |
| 647 | 708 | MaterialEditApplier::MaterialEditApplier(const MetavoxelEditMessage& message, const QSharedPointer<NetworkTexture> texture) : |
| 648 | − _message(message), | |
| 649 | − _texture(texture) { | |
| 709 | +_message(message), | |
| 710 | +_texture(texture) { | |
| 650 | 711 | } |
| 651 | 712 | |
| 652 | 713 | void MaterialEditApplier::handle() { |
| @@ -661,16 +722,16 @@ void MetavoxelSystem::applyMaterialEdit(const MetavoxelEditMessage& message, boo · 8 unmodified lines | ||
| 661 | 722 | if (material && material->getDiffuse().isValid()) { |
| 662 | 723 | if (QThread::currentThread() != thread()) { |
| 663 | 724 | QMetaObject::invokeMethod(this, "applyMaterialEdit", Q_ARG(const MetavoxelEditMessage&, message), |
| 664 | − Q_ARG(bool, reliable)); | |
| 725 | + Q_ARG(bool, reliable)); | |
| 665 | 726 | return; |
| 666 | 727 | } |
| 667 | 728 | QSharedPointer<NetworkTexture> texture = Application::getInstance()->getTextureCache()->getTexture( |
| 668 | − material->getDiffuse(), SPLAT_TEXTURE); | |
| 729 | + material->getDiffuse(), SPLAT_TEXTURE); | |
| 669 | 730 | if (texture->isLoaded()) { |
| 670 | 731 | MetavoxelEditMessage newMessage = message; |
| 671 | 732 | static_cast<MaterialEdit*>(newMessage.edit.data())->averageColor = texture->getAverageColor(); |
| 672 | − applyEdit(newMessage, true); | |
| 673 | − | |
| 733 | + applyEdit(newMessage, true); | |
| 734 | + | |
| 674 | 735 | } else { |
| 675 | 736 | MaterialEditApplier* applier = new MaterialEditApplier(message, texture); |
| 676 | 737 | connect(texture.data(), &Resource::loaded, applier, &SignalHandler::handle); |
| @@ -702,9 +763,32 @@ void MetavoxelSystem::guideToAugmented(MetavoxelVisitor& visitor, bool render) { · 25 unmodified lines | ||
| 702 | 763 | }); |
| 703 | 764 | } |
| 704 | 765 | |
| 766 | +void MetavoxelSystem::loadSplatProgram(const char* type, ProgramObject& program, SplatLocations& locations) { | |
| 767 | + program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + | |
| 768 | + "shaders/metavoxel_" + type + "_splat.vert"); | |
| 769 | + program.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + | |
| 770 | + "shaders/metavoxel_" + type + "_splat.frag"); | |
| 771 | + program.link(); | |
| 772 | + | |
| 773 | + program.bind(); | |
| 774 | + program.setUniformValue("heightMap", 0); | |
| 775 | + program.setUniformValue("textureMap", 1); | |
| 776 | + program.setUniformValueArray("diffuseMaps", SPLAT_TEXTURE_UNITS, SPLAT_COUNT); | |
| 777 | + locations.heightScale = program.uniformLocation("heightScale"); | |
| 778 | + locations.textureScale = program.uniformLocation("textureScale"); | |
| 779 | + locations.splatTextureOffset = program.uniformLocation("splatTextureOffset"); | |
| 780 | + locations.splatTextureScalesS = program.uniformLocation("splatTextureScalesS"); | |
| 781 | + locations.splatTextureScalesT = program.uniformLocation("splatTextureScalesT"); | |
| 782 | + locations.textureValueMinima = program.uniformLocation("textureValueMinima"); | |
| 783 | + locations.textureValueMaxima = program.uniformLocation("textureValueMaxima"); | |
| 784 | + locations.materials = program.attributeLocation("materials"); | |
| 785 | + locations.materialWeights = program.attributeLocation("materialWeights"); | |
| 786 | + program.release(); | |
| 787 | +} | |
| 788 | + | |
| 705 | 789 | Throttle::Throttle() : |
| 706 | − _limit(INT_MAX), | |
| 707 | − _total(0) { | |
| 790 | +_limit(INT_MAX), | |
| 791 | +_total(0) { | |
| 708 | 792 | } |
| 709 | 793 | |
| 710 | 794 | bool Throttle::shouldThrottle(int bytes) { |
| @@ -725,7 +809,7 @@ bool Throttle::shouldThrottle(int bytes) { · 14 unmodified lines | ||
| 725 | 809 | } |
| 726 | 810 | |
| 727 | 811 | MetavoxelSystemClient::MetavoxelSystemClient(const SharedNodePointer& node, MetavoxelUpdater* updater) : |
| 728 | − MetavoxelClient(node, updater) { | |
| 812 | +MetavoxelClient(node, updater) { | |
| 729 | 813 | } |
| 730 | 814 | |
| 731 | 815 | void MetavoxelSystemClient::setAugmentedData(const MetavoxelData& data) { |
| @@ -742,11 +826,11 @@ class ReceiveDelayer : public QObject { · 10 unmodified lines | ||
| 742 | 826 | public: |
| 743 | 827 | |
| 744 | 828 | ReceiveDelayer(const SharedNodePointer& node, const QByteArray& packet); |
| 745 | − | |
| 829 | + | |
| 746 | 830 | protected: |
| 747 | − | |
| 831 | + | |
| 748 | 832 | virtual void timerEvent(QTimerEvent* event); |
| 749 | − | |
| 833 | + | |
| 750 | 834 | private: |
| 751 | 835 | |
| 752 | 836 | SharedNodePointer _node; |
| @@ -754,8 +838,8 @@ private: · 1 unmodified line | ||
| 754 | 838 | }; |
| 755 | 839 | |
| 756 | 840 | ReceiveDelayer::ReceiveDelayer(const SharedNodePointer& node, const QByteArray& packet) : |
| 757 | − _node(node), | |
| 758 | − _packet(packet) { | |
| 841 | +_node(node), | |
| 842 | +_packet(packet) { | |
| 759 | 843 | } |
| 760 | 844 | |
| 761 | 845 | void ReceiveDelayer::timerEvent(QTimerEvent* event) { |
| @@ -800,16 +884,16 @@ public: · 38 unmodified lines | ||
| 800 | 884 | AugmentVisitor(const MetavoxelLOD& lod, const MetavoxelData& previousData); |
| 801 | 885 | |
| 802 | 886 | virtual int visit(MetavoxelInfo& info); |
| 803 | − | |
| 887 | + | |
| 804 | 888 | private: |
| 805 | 889 | |
| 806 | 890 | const MetavoxelData& _previousData; |
| 807 | 891 | }; |
| 808 | 892 | |
| 809 | 893 | AugmentVisitor::AugmentVisitor(const MetavoxelLOD& lod, const MetavoxelData& previousData) : |
| 810 | − MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getRendererAttribute(), | |
| 811 | − QVector<AttributePointer>(), lod), | |
| 812 | − _previousData(previousData) { | |
| 894 | +MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getRendererAttribute(), | |
| 895 | + QVector<AttributePointer>(), lod), | |
| 896 | +_previousData(previousData) { | |
| 813 | 897 | } |
| 814 | 898 | |
| 815 | 899 | int AugmentVisitor::visit(MetavoxelInfo& info) { |
| @@ -817,7 +901,7 @@ int AugmentVisitor::visit(MetavoxelInfo& info) { · 1 unmodified line | ||
| 817 | 901 | return DEFAULT_ORDER; |
| 818 | 902 | } |
| 819 | 903 | static_cast<MetavoxelRenderer*>(info.inputValues.at(0).getInlineValue< |
| 820 | − SharedObjectPointer>().data())->getImplementation()->augment(*_data, _previousData, info, _lod); | |
| 904 | + SharedObjectPointer>().data())->getImplementation()->augment(*_data, _previousData, info, _lod); | |
| 821 | 905 | return STOP_RECURSION; |
| 822 | 906 | } |
| 823 | 907 | |
| @@ -825,10 +909,10 @@ class Augmenter : public QRunnable { · 1 unmodified line | ||
| 825 | 909 | public: |
| 826 | 910 | |
| 827 | 911 | Augmenter(const SharedNodePointer& node, const MetavoxelData& data, |
| 828 | − const MetavoxelData& previousData, const MetavoxelLOD& lod); | |
| 912 | + const MetavoxelData& previousData, const MetavoxelLOD& lod); | |
| 829 | 913 | |
| 830 | 914 | virtual void run(); |
| 831 | − | |
| 915 | + | |
| 832 | 916 | private: |
| 833 | 917 | |
| 834 | 918 | QWeakPointer<Node> _node; |
| @@ -838,11 +922,11 @@ private: · 3 unmodified lines | ||
| 838 | 922 | }; |
| 839 | 923 | |
| 840 | 924 | Augmenter::Augmenter(const SharedNodePointer& node, const MetavoxelData& data, |
| 841 | − const MetavoxelData& previousData, const MetavoxelLOD& lod) : | |
| 842 | − _node(node), | |
| 843 | − _data(data), | |
| 844 | − _previousData(previousData), | |
| 845 | − _lod(lod) { | |
| 925 | + const MetavoxelData& previousData, const MetavoxelLOD& lod) : | |
| 926 | +_node(node), | |
| 927 | +_data(data), | |
| 928 | +_previousData(previousData), | |
| 929 | +_lod(lod) { | |
| 846 | 930 | } |
| 847 | 931 | |
| 848 | 932 | void Augmenter::run() { |
| @@ -860,7 +944,7 @@ void MetavoxelSystemClient::refreshVoxelData() { · 11 unmodified lines | ||
| 860 | 944 | // make it look as if all the colors have changed |
| 861 | 945 | MetavoxelData oldData = getAugmentedData(); |
| 862 | 946 | oldData.touch(AttributeRegistry::getInstance()->getVoxelColorAttribute()); |
| 863 | − | |
| 947 | + | |
| 864 | 948 | QThreadPool::globalInstance()->start(new Augmenter(_node, _data, oldData, _remoteDataLOD)); |
| 865 | 949 | } |
| 866 | 950 | |
| @@ -873,9 +957,9 @@ class SendDelayer : public QObject { · 6 unmodified lines | ||
| 873 | 957 | public: |
| 874 | 958 | |
| 875 | 959 | SendDelayer(const SharedNodePointer& node, const QByteArray& data); |
| 876 | − | |
| 960 | + | |
| 877 | 961 | virtual void timerEvent(QTimerEvent* event); |
| 878 | − | |
| 962 | + | |
| 879 | 963 | private: |
| 880 | 964 | |
| 881 | 965 | SharedNodePointer _node; |
| @@ -883,8 +967,8 @@ private: · 1 unmodified line | ||
| 883 | 967 | }; |
| 884 | 968 | |
| 885 | 969 | SendDelayer::SendDelayer(const SharedNodePointer& node, const QByteArray& data) : |
| 886 | − _node(node), | |
| 887 | − _data(data.constData(), data.size()) { | |
| 970 | +_node(node), | |
| 971 | +_data(data.constData(), data.size()) { | |
| 888 | 972 | } |
| 889 | 973 | |
| 890 | 974 | void SendDelayer::timerEvent(QTimerEvent* event) { |
| @@ -920,384 +1004,6 @@ void MetavoxelSystemClient::sendDatagram(const QByteArray& data) { · 29 unmodified lines | ||
| 920 | 1004 | BufferData::~BufferData() { |
| 921 | 1005 | } |
| 922 | 1006 | |
| 923 | −PointBuffer::PointBuffer(const BufferPointVector& points) : | |
| 924 | − _points(points) { | |
| 925 | −} | |
| 926 | − | |
| 927 | −void PointBuffer::render(bool cursor) { | |
| 928 | − // initialize buffer, etc. on first render | |
| 929 | − if (!_buffer.isCreated()) { | |
| 930 | − _buffer.setUsagePattern(QOpenGLBuffer::StaticDraw); | |
| 931 | − _buffer.create(); | |
| 932 | − _buffer.bind(); | |
| 933 | − _pointCount = _points.size(); | |
| 934 | − _buffer.allocate(_points.constData(), _pointCount * sizeof(BufferPoint)); | |
| 935 | − _points.clear(); | |
| 936 | − _buffer.release(); | |
| 937 | − } | |
| 938 | − if (_pointCount == 0) { | |
| 939 | − return; | |
| 940 | − } | |
| 941 | − _buffer.bind(); | |
| 942 | − | |
| 943 | − BufferPoint* point = 0; | |
| 944 | − glVertexPointer(4, GL_FLOAT, sizeof(BufferPoint), &point->vertex); | |
| 945 | − glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(BufferPoint), &point->color); | |
| 946 | − glNormalPointer(GL_BYTE, sizeof(BufferPoint), &point->normal); | |
| 947 | − | |
| 948 | − glDrawArrays(GL_POINTS, 0, _pointCount); | |
| 949 | − | |
| 950 | − _buffer.release(); | |
| 951 | −} | |
| 952 | − | |
| 953 | −const int HeightfieldBuffer::HEIGHT_BORDER = 1; | |
| 954 | −const int HeightfieldBuffer::SHARED_EDGE = 1; | |
| 955 | −const int HeightfieldBuffer::HEIGHT_EXTENSION = 2 * HeightfieldBuffer::HEIGHT_BORDER + HeightfieldBuffer::SHARED_EDGE; | |
| 956 | − | |
| 957 | −HeightfieldBuffer::HeightfieldBuffer(const glm::vec3& translation, float scale, | |
| 958 | − const QByteArray& height, const QByteArray& color, const QByteArray& material, | |
| 959 | − const QVector<SharedObjectPointer>& materials) : | |
| 960 | − _translation(translation), | |
| 961 | − _scale(scale), | |
| 962 | − _heightBounds(translation, translation + glm::vec3(scale, scale, scale)), | |
| 963 | − _colorBounds(_heightBounds), | |
| 964 | − _materialBounds(_heightBounds), | |
| 965 | − _height(height), | |
| 966 | − _color(color), | |
| 967 | − _material(material), | |
| 968 | − _materials(materials), | |
| 969 | − _heightTextureID(0), | |
| 970 | − _colorTextureID(0), | |
| 971 | − _materialTextureID(0), | |
| 972 | − _heightSize(glm::sqrt((float)height.size())), | |
| 973 | − _heightIncrement(scale / (_heightSize - HEIGHT_EXTENSION)), | |
| 974 | − _colorSize(glm::sqrt((float)color.size() / DataBlock::COLOR_BYTES)), | |
| 975 | − _colorIncrement(scale / (_colorSize - SHARED_EDGE)), | |
| 976 | − _materialSize(glm::sqrt((float)material.size())), | |
| 977 | − _materialIncrement(scale / (_materialSize - SHARED_EDGE)) { | |
| 978 | − | |
| 979 | − _heightBounds.minimum.x -= _heightIncrement * HEIGHT_BORDER; | |
| 980 | − _heightBounds.minimum.z -= _heightIncrement * HEIGHT_BORDER; | |
| 981 | − _heightBounds.maximum.x += _heightIncrement * (SHARED_EDGE + HEIGHT_BORDER); | |
| 982 | − _heightBounds.maximum.z += _heightIncrement * (SHARED_EDGE + HEIGHT_BORDER); | |
| 983 | − | |
| 984 | − _colorBounds.maximum.x += _colorIncrement * SHARED_EDGE; | |
| 985 | − _colorBounds.maximum.z += _colorIncrement * SHARED_EDGE; | |
| 986 | − | |
| 987 | − _materialBounds.maximum.x += _materialIncrement * SHARED_EDGE; | |
| 988 | − _materialBounds.maximum.z += _materialIncrement * SHARED_EDGE; | |
| 989 | −} | |
| 990 | − | |
| 991 | −HeightfieldBuffer::~HeightfieldBuffer() { | |
| 992 | − // the textures have to be deleted on the main thread (for its opengl context) | |
| 993 | − if (QThread::currentThread() != Application::getInstance()->thread()) { | |
| 994 | − QMetaObject::invokeMethod(Application::getInstance()->getMetavoxels(), "deleteTextures", | |
| 995 | − Q_ARG(int, _heightTextureID), Q_ARG(int, _colorTextureID), Q_ARG(int, _materialTextureID)); | |
| 996 | − } else { | |
| 997 | − glDeleteTextures(1, &_heightTextureID); | |
| 998 | − glDeleteTextures(1, &_colorTextureID); | |
| 999 | − glDeleteTextures(1, &_materialTextureID); | |
| 1000 | − } | |
| 1001 | −} | |
| 1002 | − | |
| 1003 | −QByteArray HeightfieldBuffer::getUnextendedHeight() const { | |
| 1004 | − int srcSize = glm::sqrt(float(_height.size())); | |
| 1005 | − int destSize = srcSize - 3; | |
| 1006 | − QByteArray unextended(destSize * destSize, 0); | |
| 1007 | − const char* src = _height.constData() + srcSize + 1; | |
| 1008 | − char* dest = unextended.data(); | |
| 1009 | − for (int z = 0; z < destSize; z++, src += srcSize, dest += destSize) { | |
| 1010 | − memcpy(dest, src, destSize); | |
| 1011 | − } | |
| 1012 | − return unextended; | |
| 1013 | −} | |
| 1014 | − | |
| 1015 | −QByteArray HeightfieldBuffer::getUnextendedColor(int x, int y) const { | |
| 1016 | − int unextendedSize = _heightSize - HEIGHT_EXTENSION; | |
| 1017 | − QByteArray unextended(unextendedSize * unextendedSize * DataBlock::COLOR_BYTES, 0); | |
| 1018 | − char* dest = unextended.data(); | |
| 1019 | − const char* src = _color.constData() + (y * _colorSize + x) * unextendedSize * DataBlock::COLOR_BYTES; | |
| 1020 | − for (int z = 0; z < unextendedSize; z++, dest += unextendedSize * DataBlock::COLOR_BYTES, | |
| 1021 | − src += _colorSize * DataBlock::COLOR_BYTES) { | |
| 1022 | − memcpy(dest, src, unextendedSize * DataBlock::COLOR_BYTES); | |
| 1023 | − } | |
| 1024 | − return unextended; | |
| 1025 | −} | |
| 1026 | − | |
| 1027 | −class HeightfieldPoint { | |
| 1028 | −public: | |
| 1029 | − glm::vec2 textureCoord; | |
| 1030 | − glm::vec3 vertex; | |
| 1031 | −}; | |
| 1032 | − | |
| 1033 | −const int SPLAT_COUNT = 4; | |
| 1034 | −const GLint SPLAT_TEXTURE_UNITS[] = { 3, 4, 5, 6 }; | |
| 1035 | − | |
| 1036 | −void HeightfieldBuffer::render(bool cursor) { | |
| 1037 | − // initialize textures, etc. on first render | |
| 1038 | − if (_heightTextureID == 0) { | |
| 1039 | − glGenTextures(1, &_heightTextureID); | |
| 1040 | − glBindTexture(GL_TEXTURE_2D, _heightTextureID); | |
| 1041 | − glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | |
| 1042 | − glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
| 1043 | − glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
| 1044 | − glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 1045 | − glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 1046 | − glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, _heightSize, _heightSize, 0, | |
| 1047 | − GL_LUMINANCE, GL_UNSIGNED_BYTE, _height.constData()); | |
| 1048 | − | |
| 1049 | − glGenTextures(1, &_colorTextureID); | |
| 1050 | − glBindTexture(GL_TEXTURE_2D, _colorTextureID); | |
| 1051 | − glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 1052 | − glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 1053 | − glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 1054 | − if (_color.isEmpty()) { | |
| 1055 | − const quint8 WHITE_COLOR[] = { 255, 255, 255 }; | |
| 1056 | − glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, WHITE_COLOR); | |
| 1057 | − | |
| 1058 | − } else { | |
| 1059 | − int colorSize = glm::sqrt(float(_color.size() / DataBlock::COLOR_BYTES)); | |
| 1060 | − glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, colorSize, colorSize, 0, GL_RGB, GL_UNSIGNED_BYTE, _color.constData()); | |
| 1061 | − } | |
| 1062 | − | |
| 1063 | − if (!_material.isEmpty()) { | |
| 1064 | − glGenTextures(1, &_materialTextureID); | |
| 1065 | − glBindTexture(GL_TEXTURE_2D, _materialTextureID); | |
| 1066 | − glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
| 1067 | − glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
| 1068 | − glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 1069 | − glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 1070 | − int materialSize = glm::sqrt(float(_material.size())); | |
| 1071 | − glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, materialSize, materialSize, 0, | |
| 1072 | − GL_LUMINANCE, GL_UNSIGNED_BYTE, _material.constData()); | |
| 1073 | − | |
| 1074 | − _networkTextures.resize(_materials.size()); | |
| 1075 | − for (int i = 0; i < _materials.size(); i++) { | |
| 1076 | − const SharedObjectPointer material = _materials.at(i); | |
| 1077 | − if (material) { | |
| 1078 | − _networkTextures[i] = Application::getInstance()->getTextureCache()->getTexture( | |
| 1079 | − static_cast<MaterialObject*>(material.data())->getDiffuse(), SPLAT_TEXTURE); | |
| 1080 | − } | |
| 1081 | − } | |
| 1082 | − } | |
| 1083 | − } | |
| 1084 | − // create the buffer objects lazily | |
| 1085 | − int innerSize = _heightSize - 2 * HeightfieldBuffer::HEIGHT_BORDER; | |
| 1086 | − int vertexCount = _heightSize * _heightSize; | |
| 1087 | − int rows = _heightSize - 1; | |
| 1088 | − int indexCount = rows * rows * 3 * 2; | |
| 1089 | − BufferPair& bufferPair = _bufferPairs[_heightSize]; | |
| 1090 | − if (!bufferPair.first.isCreated()) { | |
| 1091 | − QVector<HeightfieldPoint> vertices(vertexCount); | |
| 1092 | − HeightfieldPoint* point = vertices.data(); | |
| 1093 | − | |
| 1094 | − float vertexStep = 1.0f / (innerSize - 1); | |
| 1095 | − float z = -vertexStep; | |
| 1096 | − float textureStep = 1.0f / _heightSize; | |
| 1097 | − float t = textureStep / 2.0f; | |
| 1098 | − for (int i = 0; i < _heightSize; i++, z += vertexStep, t += textureStep) { | |
| 1099 | − float x = -vertexStep; | |
| 1100 | − float s = textureStep / 2.0f; | |
| 1101 | − const float SKIRT_LENGTH = 0.25f; | |
| 1102 | − float baseY = (i == 0 || i == _heightSize - 1) ? -SKIRT_LENGTH : 0.0f; | |
| 1103 | − for (int j = 0; j < _heightSize; j++, point++, x += vertexStep, s += textureStep) { | |
| 1104 | − point->vertex = glm::vec3(x, (j == 0 || j == _heightSize - 1) ? -SKIRT_LENGTH : baseY, z); | |
| 1105 | − point->textureCoord = glm::vec2(s, t); | |
| 1106 | − } | |
| 1107 | − } | |
| 1108 | − | |
| 1109 | − bufferPair.first.setUsagePattern(QOpenGLBuffer::StaticDraw); | |
| 1110 | − bufferPair.first.create(); | |
| 1111 | − bufferPair.first.bind(); | |
| 1112 | − bufferPair.first.allocate(vertices.constData(), vertexCount * sizeof(HeightfieldPoint)); | |
| 1113 | − | |
| 1114 | − QVector<int> indices(indexCount); | |
| 1115 | − int* index = indices.data(); | |
| 1116 | − for (int i = 0; i < rows; i++) { | |
| 1117 | − int lineIndex = i * _heightSize; | |
| 1118 | − int nextLineIndex = (i + 1) * _heightSize; | |
| 1119 | − for (int j = 0; j < rows; j++) { | |
| 1120 | − *index++ = lineIndex + j; | |
| 1121 | − *index++ = nextLineIndex + j; | |
| 1122 | − *index++ = nextLineIndex + j + 1; | |
| 1123 | − | |
| 1124 | − *index++ = nextLineIndex + j + 1; | |
| 1125 | − *index++ = lineIndex + j + 1; | |
| 1126 | − *index++ = lineIndex + j; | |
| 1127 | − } | |
| 1128 | − } | |
| 1129 | − | |
| 1130 | − bufferPair.second = QOpenGLBuffer(QOpenGLBuffer::IndexBuffer); | |
| 1131 | − bufferPair.second.create(); | |
| 1132 | − bufferPair.second.bind(); | |
| 1133 | − bufferPair.second.allocate(indices.constData(), indexCount * sizeof(int)); | |
| 1134 | − | |
| 1135 | − } else { | |
| 1136 | − bufferPair.first.bind(); | |
| 1137 | − bufferPair.second.bind(); | |
| 1138 | − } | |
| 1139 | − | |
| 1140 | − HeightfieldPoint* point = 0; | |
| 1141 | − glVertexPointer(3, GL_FLOAT, sizeof(HeightfieldPoint), &point->vertex); | |
| 1142 | − glTexCoordPointer(2, GL_FLOAT, sizeof(HeightfieldPoint), &point->textureCoord); | |
| 1143 | − | |
| 1144 | − glPushMatrix(); | |
| 1145 | − glTranslatef(_translation.x, _translation.y, _translation.z); | |
| 1146 | − glScalef(_scale, _scale, _scale); | |
| 1147 | − | |
| 1148 | − glBindTexture(GL_TEXTURE_2D, _heightTextureID); | |
| 1149 | − | |
| 1150 | − if (cursor) { | |
| 1151 | − glDrawRangeElements(GL_TRIANGLES, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0); | |
| 1152 | − | |
| 1153 | − } else if (!_materials.isEmpty()) { | |
| 1154 | − DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().setUniformValue( | |
| 1155 | − DefaultMetavoxelRendererImplementation::getBaseHeightScaleLocation(), 1.0f / _heightSize); | |
| 1156 | − DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().setUniformValue( | |
| 1157 | − DefaultMetavoxelRendererImplementation::getBaseColorScaleLocation(), (float)_heightSize / innerSize); | |
| 1158 | − glActiveTexture(GL_TEXTURE1); | |
| 1159 | − glBindTexture(GL_TEXTURE_2D, _colorTextureID); | |
| 1160 | − | |
| 1161 | − glDrawRangeElements(GL_TRIANGLES, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0); | |
| 1162 | − | |
| 1163 | − Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, false); | |
| 1164 | − | |
| 1165 | − glDepthFunc(GL_LEQUAL); | |
| 1166 | − glDepthMask(false); | |
| 1167 | − glEnable(GL_BLEND); | |
| 1168 | − glDisable(GL_ALPHA_TEST); | |
| 1169 | − glEnable(GL_POLYGON_OFFSET_FILL); | |
| 1170 | − glPolygonOffset(-1.0f, -1.0f); | |
| 1171 | − | |
| 1172 | − DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().bind(); | |
| 1173 | − const DefaultMetavoxelRendererImplementation::SplatLocations& locations = | |
| 1174 | − DefaultMetavoxelRendererImplementation::getSplatHeightfieldLocations(); | |
| 1175 | − DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue( | |
| 1176 | − locations.heightScale, 1.0f / _heightSize); | |
| 1177 | − DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue( | |
| 1178 | − locations.textureScale, (float)_heightSize / innerSize); | |
| 1179 | − DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue( | |
| 1180 | − locations.splatTextureOffset, _translation.x / _scale, _translation.z / _scale); | |
| 1181 | − | |
| 1182 | − glBindTexture(GL_TEXTURE_2D, _materialTextureID); | |
| 1183 | − | |
| 1184 | − for (int i = 0; i < _materials.size(); i += SPLAT_COUNT) { | |
| 1185 | − QVector4D scalesS, scalesT; | |
| 1186 | − | |
| 1187 | − for (int j = 0; j < SPLAT_COUNT; j++) { | |
| 1188 | − glActiveTexture(GL_TEXTURE0 + SPLAT_TEXTURE_UNITS[j]); | |
| 1189 | − int index = i + j; | |
| 1190 | − if (index < _networkTextures.size()) { | |
| 1191 | − const NetworkTexturePointer& texture = _networkTextures.at(index); | |
| 1192 | − if (texture) { | |
| 1193 | − MaterialObject* material = static_cast<MaterialObject*>(_materials.at(index).data()); | |
| 1194 | − scalesS[j] = _scale / material->getScaleS(); | |
| 1195 | − scalesT[j] = _scale / material->getScaleT(); | |
| 1196 | − glBindTexture(GL_TEXTURE_2D, texture->getID()); | |
| 1197 | − } else { | |
| 1198 | − glBindTexture(GL_TEXTURE_2D, 0); | |
| 1199 | − } | |
| 1200 | − } else { | |
| 1201 | − glBindTexture(GL_TEXTURE_2D, 0); | |
| 1202 | − } | |
| 1203 | − } | |
| 1204 | − const float QUARTER_STEP = 0.25f * EIGHT_BIT_MAXIMUM_RECIPROCAL; | |
| 1205 | − DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue( | |
| 1206 | − locations.splatTextureScalesS, scalesS); | |
| 1207 | − DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue( | |
| 1208 | − locations.splatTextureScalesT, scalesT); | |
| 1209 | − DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue( | |
| 1210 | − locations.textureValueMinima, | |
| 1211 | − (i + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, (i + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, | |
| 1212 | − (i + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, (i + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP); | |
| 1213 | − DefaultMetavoxelRendererImplementation::getSplatHeightfieldProgram().setUniformValue( | |
| 1214 | − locations.textureValueMaxima, | |
| 1215 | − (i + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, (i + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, | |
| 1216 | − (i + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, (i + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP); | |
| 1217 | − glDrawRangeElements(GL_TRIANGLES, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0); | |
| 1218 | − } | |
| 1219 | − | |
| 1220 | − for (int i = 0; i < SPLAT_COUNT; i++) { | |
| 1221 | − glActiveTexture(GL_TEXTURE0 + SPLAT_TEXTURE_UNITS[i]); | |
| 1222 | − glBindTexture(GL_TEXTURE_2D, 0); | |
| 1223 | − } | |
| 1224 | − | |
| 1225 | − glActiveTexture(GL_TEXTURE1); | |
| 1226 | − glBindTexture(GL_TEXTURE_2D, 0); | |
| 1227 | − | |
| 1228 | − glActiveTexture(GL_TEXTURE0); | |
| 1229 | − | |
| 1230 | − glDisable(GL_POLYGON_OFFSET_FILL); | |
| 1231 | − glEnable(GL_ALPHA_TEST); | |
| 1232 | − glDisable(GL_BLEND); | |
| 1233 | − glDepthMask(true); | |
| 1234 | − glDepthFunc(GL_LESS); | |
| 1235 | − | |
| 1236 | − Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, true); | |
| 1237 | − | |
| 1238 | − DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().bind(); | |
| 1239 | − | |
| 1240 | − } else { | |
| 1241 | − DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().setUniformValue( | |
| 1242 | − DefaultMetavoxelRendererImplementation::getBaseHeightScaleLocation(), 1.0f / _heightSize); | |
| 1243 | − DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().setUniformValue( | |
| 1244 | − DefaultMetavoxelRendererImplementation::getBaseColorScaleLocation(), (float)_heightSize / innerSize); | |
| 1245 | − glActiveTexture(GL_TEXTURE1); | |
| 1246 | − glBindTexture(GL_TEXTURE_2D, _colorTextureID); | |
| 1247 | − | |
| 1248 | − glDrawRangeElements(GL_TRIANGLES, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0); | |
| 1249 | − | |
| 1250 | − glBindTexture(GL_TEXTURE_2D, 0); | |
| 1251 | − glActiveTexture(GL_TEXTURE0); | |
| 1252 | − } | |
| 1253 | − | |
| 1254 | − glBindTexture(GL_TEXTURE_2D, 0); | |
| 1255 | − | |
| 1256 | − glPopMatrix(); | |
| 1257 | − | |
| 1258 | − bufferPair.first.release(); | |
| 1259 | − bufferPair.second.release(); | |
| 1260 | −} | |
| 1261 | − | |
| 1262 | −QHash<int, HeightfieldBuffer::BufferPair> HeightfieldBuffer::_bufferPairs; | |
| 1263 | − | |
| 1264 | −void HeightfieldPreview::render(const glm::vec3& translation, float scale) const { | |
| 1265 | − Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, true); | |
| 1266 | − | |
| 1267 | − glDisable(GL_BLEND); | |
| 1268 | − glEnable(GL_CULL_FACE); | |
| 1269 | − glEnable(GL_ALPHA_TEST); | |
| 1270 | − glAlphaFunc(GL_EQUAL, 0.0f); | |
| 1271 | − | |
| 1272 | − glColor4f(1.0f, 1.0f, 1.0f, 1.0f); | |
| 1273 | − | |
| 1274 | − glEnableClientState(GL_VERTEX_ARRAY); | |
| 1275 | − glEnableClientState(GL_TEXTURE_COORD_ARRAY); | |
| 1276 | − | |
| 1277 | − DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().bind(); | |
| 1278 | − | |
| 1279 | − glPushMatrix(); | |
| 1280 | − glTranslatef(translation.x, translation.y, translation.z); | |
| 1281 | − glScalef(scale, scale, scale); | |
| 1282 | − | |
| 1283 | − foreach (const BufferDataPointer& buffer, _buffers) { | |
| 1284 | − buffer->render(); | |
| 1285 | − } | |
| 1286 | − | |
| 1287 | − glPopMatrix(); | |
| 1288 | − | |
| 1289 | − DefaultMetavoxelRendererImplementation::getBaseHeightfieldProgram().release(); | |
| 1290 | − | |
| 1291 | − glDisableClientState(GL_TEXTURE_COORD_ARRAY); | |
| 1292 | − glDisableClientState(GL_VERTEX_ARRAY); | |
| 1293 | − | |
| 1294 | − glDisable(GL_ALPHA_TEST); | |
| 1295 | − glDisable(GL_CULL_FACE); | |
| 1296 | − glEnable(GL_BLEND); | |
| 1297 | − | |
| 1298 | − Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, false); | |
| 1299 | −} | |
| 1300 | − | |
| 1301 | 1007 | void VoxelPoint::setNormal(const glm::vec3& normal) { |
| 1302 | 1008 | this->normal[0] = (char)(normal.x * 127.0f); |
| 1303 | 1009 | this->normal[1] = (char)(normal.y * 127.0f); |
| @@ -1305,21 +1011,21 @@ void VoxelPoint::setNormal(const glm::vec3& normal) { · 1 unmodified line | ||
| 1305 | 1011 | } |
| 1306 | 1012 | |
| 1307 | 1013 | VoxelBuffer::VoxelBuffer(const QVector<VoxelPoint>& vertices, const QVector<int>& indices, const QVector<glm::vec3>& hermite, |
| 1308 | − const QMultiHash<QRgb, int>& quadIndices, int size, const QVector<SharedObjectPointer>& materials) : | |
| 1309 | − _vertices(vertices), | |
| 1310 | − _indices(indices), | |
| 1311 | − _hermite(hermite), | |
| 1312 | − _quadIndices(quadIndices), | |
| 1313 | − _size(size), | |
| 1314 | − _vertexCount(vertices.size()), | |
| 1315 | − _indexCount(indices.size()), | |
| 1316 | − _hermiteCount(hermite.size()), | |
| 1317 | − _indexBuffer(QOpenGLBuffer::IndexBuffer), | |
| 1318 | − _materials(materials) { | |
| 1014 | + const QMultiHash<VoxelCoord, int>& quadIndices, int size, const QVector<SharedObjectPointer>& materials) : | |
| 1015 | +_vertices(vertices), | |
| 1016 | +_indices(indices), | |
| 1017 | +_hermite(hermite), | |
| 1018 | +_quadIndices(quadIndices), | |
| 1019 | +_size(size), | |
| 1020 | +_vertexCount(vertices.size()), | |
| 1021 | +_indexCount(indices.size()), | |
| 1022 | +_hermiteCount(hermite.size()), | |
| 1023 | +_indexBuffer(QOpenGLBuffer::IndexBuffer), | |
| 1024 | +_materials(materials) { | |
| 1319 | 1025 | } |
| 1320 | 1026 | |
| 1321 | 1027 | static bool findRayTriangleIntersection(const glm::vec3& origin, const glm::vec3& direction, |
| 1322 | − const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2, float& distance) { | |
| 1028 | + const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2, float& distance) { | |
| 1323 | 1029 | glm::vec3 firstSide = v0 - v1; |
| 1324 | 1030 | glm::vec3 secondSide = v2 - v1; |
| 1325 | 1031 | glm::vec3 normal = glm::cross(secondSide, firstSide); |
| @@ -1334,8 +1040,8 @@ static bool findRayTriangleIntersection(const glm::vec3& origin, const glm::vec3 · 8 unmodified lines | ||
| 1334 | 1040 | float t = dividend / divisor; |
| 1335 | 1041 | glm::vec3 point = origin + direction * t; |
| 1336 | 1042 | if (glm::dot(normal, glm::cross(point - v1, firstSide)) > 0.0f && |
| 1337 | − glm::dot(normal, glm::cross(secondSide, point - v1)) > 0.0f && | |
| 1338 | − glm::dot(normal, glm::cross(point - v0, v2 - v0)) > 0.0f) { | |
| 1043 | + glm::dot(normal, glm::cross(secondSide, point - v1)) > 0.0f && | |
| 1044 | + glm::dot(normal, glm::cross(point - v0, v2 - v0)) > 0.0f) { | |
| 1339 | 1045 | distance = t; |
| 1340 | 1046 | return true; |
| 1341 | 1047 | } |
| @@ -1343,22 +1049,22 @@ static bool findRayTriangleIntersection(const glm::vec3& origin, const glm::vec3 · 1 unmodified line | ||
| 1343 | 1049 | } |
| 1344 | 1050 | |
| 1345 | 1051 | bool VoxelBuffer::findFirstRayIntersection(const glm::vec3& entry, const glm::vec3& origin, |
| 1346 | − const glm::vec3& direction, float& distance) const { | |
| 1052 | + const glm::vec3& direction, float& distance) const { | |
| 1347 | 1053 | float highest = _size - 1.0f; |
| 1348 | 1054 | glm::vec3 position = entry * highest; |
| 1349 | 1055 | glm::vec3 floors = glm::floor(position); |
| 1350 | 1056 | int max = _size - 2; |
| 1351 | 1057 | int x = qMin((int)floors.x, max), y = qMin((int)floors.y, max), z = qMin((int)floors.z, max); |
| 1352 | 1058 | forever { |
| 1353 | − for (QMultiHash<QRgb, int>::const_iterator it = _quadIndices.constFind(qRgb(x + 1, y + 1, z + 1)); | |
| 1354 | − it != _quadIndices.constEnd(); it++) { | |
| 1059 | + for (QMultiHash<VoxelCoord, int>::const_iterator it = _quadIndices.constFind(qRgb(x + 1, y + 1, z + 1)); | |
| 1060 | + it != _quadIndices.constEnd(); it++) { | |
| 1355 | 1061 | const int* indices = _indices.constData() + *it; |
| 1356 | 1062 | if (findRayTriangleIntersection(origin, direction, _vertices.at(indices[0]).vertex, |
| 1357 | − _vertices.at(indices[1]).vertex, _vertices.at(indices[2]).vertex, distance) || | |
| 1063 | + _vertices.at(indices[1]).vertex, _vertices.at(indices[2]).vertex, distance) || | |
| 1358 | 1064 | findRayTriangleIntersection(origin, direction, _vertices.at(indices[0]).vertex, |
| 1359 | − _vertices.at(indices[2]).vertex, _vertices.at(indices[3]).vertex, distance)) { | |
| 1360 | − return true; | |
| 1361 | − } | |
| 1065 | + _vertices.at(indices[2]).vertex, _vertices.at(indices[3]).vertex, distance)) { | |
| 1066 | + return true; | |
| 1067 | + } | |
| 1362 | 1068 | } |
| 1363 | 1069 | float xDistance = FLT_MAX, yDistance = FLT_MAX, zDistance = FLT_MAX; |
| 1364 | 1070 | if (direction.x > 0.0f) { |
| @@ -1414,10 +1120,12 @@ void VoxelBuffer::render(bool cursor) { · 49 unmodified lines | ||
| 1414 | 1120 | _vertexBuffer.create(); |
| 1415 | 1121 | _vertexBuffer.bind(); |
| 1416 | 1122 | _vertexBuffer.allocate(_vertices.constData(), _vertices.size() * sizeof(VoxelPoint)); |
| 1123 | + _vertexBuffer.release(); | |
| 1417 | 1124 | |
| 1418 | 1125 | _indexBuffer.create(); |
| 1419 | 1126 | _indexBuffer.bind(); |
| 1420 | 1127 | _indexBuffer.allocate(_indices.constData(), _indices.size() * sizeof(int)); |
| 1128 | + _indexBuffer.release(); | |
| 1421 | 1129 | |
| 1422 | 1130 | if (!_materials.isEmpty()) { |
| 1423 | 1131 | _networkTextures.resize(_materials.size()); |
| @@ -1425,118 +1133,76 @@ void VoxelBuffer::render(bool cursor) { · 1 unmodified line | ||
| 1425 | 1133 | const SharedObjectPointer material = _materials.at(i); |
| 1426 | 1134 | if (material) { |
| 1427 | 1135 | _networkTextures[i] = Application::getInstance()->getTextureCache()->getTexture( |
| 1428 | − static_cast<MaterialObject*>(material.data())->getDiffuse(), SPLAT_TEXTURE); | |
| 1136 | + static_cast<MaterialObject*>(material.data())->getDiffuse(), SPLAT_TEXTURE); | |
| 1429 | 1137 | } |
| 1430 | 1138 | } |
| 1431 | 1139 | } |
| 1432 | − } else { | |
| 1433 | − _vertexBuffer.bind(); | |
| 1434 | − _indexBuffer.bind(); | |
| 1435 | 1140 | } |
| 1436 | 1141 | |
| 1437 | − VoxelPoint* point = 0; | |
| 1438 | − glVertexPointer(3, GL_FLOAT, sizeof(VoxelPoint), &point->vertex); | |
| 1439 | − glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(VoxelPoint), &point->color); | |
| 1440 | − glNormalPointer(GL_BYTE, sizeof(VoxelPoint), &point->normal); | |
| 1441 | − | |
| 1442 | − glDrawRangeElements(GL_QUADS, 0, _vertexCount - 1, _indexCount, GL_UNSIGNED_INT, 0); | |
| 1443 | − | |
| 1444 | − if (!(_materials.isEmpty() || cursor)) { | |
| 1445 | − Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, false); | |
| 1446 | − | |
| 1447 | − glDepthFunc(GL_LEQUAL); | |
| 1448 | − glDepthMask(false); | |
| 1449 | − glEnable(GL_BLEND); | |
| 1450 | − glDisable(GL_ALPHA_TEST); | |
| 1451 | − glEnable(GL_POLYGON_OFFSET_FILL); | |
| 1452 | − glPolygonOffset(-1.0f, -1.0f); | |
| 1142 | + if (cursor) { | |
| 1143 | + _vertexBuffer.bind(); | |
| 1144 | + _indexBuffer.bind(); | |
| 1453 | 1145 | |
| 1454 | − DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().bind(); | |
| 1455 | − const DefaultMetavoxelRendererImplementation::SplatLocations& locations = | |
| 1456 | − DefaultMetavoxelRendererImplementation::getSplatVoxelLocations(); | |
| 1146 | + VoxelPoint* point = 0; | |
| 1147 | + glVertexPointer(3, GL_FLOAT, sizeof(VoxelPoint), &point->vertex); | |
| 1148 | + glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(VoxelPoint), &point->color); | |
| 1149 | + glNormalPointer(GL_BYTE, sizeof(VoxelPoint), &point->normal); | |
| 1457 | 1150 | |
| 1458 | − DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setAttributeBuffer(locations.materials, | |
| 1459 | − GL_UNSIGNED_BYTE, (qint64)&point->materials, SPLAT_COUNT, sizeof(VoxelPoint)); | |
| 1460 | − DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().enableAttributeArray(locations.materials); | |
| 1151 | + glDrawRangeElements(GL_QUADS, 0, _vertexCount - 1, _indexCount, GL_UNSIGNED_INT, 0); | |
| 1461 | 1152 | |
| 1462 | − DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setAttributeBuffer(locations.materialWeights, | |
| 1463 | − GL_UNSIGNED_BYTE, (qint64)&point->materialWeights, SPLAT_COUNT, sizeof(VoxelPoint)); | |
| 1464 | − DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().enableAttributeArray(locations.materialWeights); | |
| 1153 | + _vertexBuffer.release(); | |
| 1154 | + _indexBuffer.release(); | |
| 1155 | + return; | |
| 1156 | + } | |
| 1157 | + | |
| 1158 | + VoxelBatch baseBatch; | |
| 1159 | + baseBatch.vertexBuffer = &_vertexBuffer; | |
| 1160 | + baseBatch.indexBuffer = &_indexBuffer; | |
| 1161 | + baseBatch.vertexCount = _vertexCount; | |
| 1162 | + baseBatch.indexCount = _indexCount; | |
| 1163 | + Application::getInstance()->getMetavoxels()->addVoxelBaseBatch(baseBatch); | |
| 1164 | + | |
| 1165 | + if (!_materials.isEmpty()) { | |
| 1166 | + VoxelSplatBatch splatBatch; | |
| 1167 | + splatBatch.vertexBuffer = &_vertexBuffer; | |
| 1168 | + splatBatch.indexBuffer = &_indexBuffer; | |
| 1169 | + splatBatch.vertexCount = _vertexCount; | |
| 1170 | + splatBatch.indexCount = _indexCount; | |
| 1465 | 1171 | |
| 1466 | 1172 | for (int i = 0; i < _materials.size(); i += SPLAT_COUNT) { |
| 1467 | − QVector4D scalesS, scalesT; | |
| 1468 | − | |
| 1469 | 1173 | for (int j = 0; j < SPLAT_COUNT; j++) { |
| 1470 | − glActiveTexture(GL_TEXTURE0 + SPLAT_TEXTURE_UNITS[j]); | |
| 1471 | 1174 | int index = i + j; |
| 1472 | 1175 | if (index < _networkTextures.size()) { |
| 1473 | 1176 | const NetworkTexturePointer& texture = _networkTextures.at(index); |
| 1474 | 1177 | if (texture) { |
| 1475 | 1178 | MaterialObject* material = static_cast<MaterialObject*>(_materials.at(index).data()); |
| 1476 | − scalesS[j] = 1.0f / material->getScaleS(); | |
| 1477 | − scalesT[j] = 1.0f / material->getScaleT(); | |
| 1478 | − glBindTexture(GL_TEXTURE_2D, texture->getID()); | |
| 1179 | + splatBatch.splatTextureScalesS[j] = 1.0f / material->getScaleS(); | |
| 1180 | + splatBatch.splatTextureScalesT[j] = 1.0f / material->getScaleT(); | |
| 1181 | + splatBatch.splatTextureIDs[j] = texture->getID(); | |
| 1182 | + | |
| 1479 | 1183 | } else { |
| 1480 | − glBindTexture(GL_TEXTURE_2D, 0); | |
| 1184 | + splatBatch.splatTextureIDs[j] = 0; | |
| 1481 | 1185 | } |
| 1482 | 1186 | } else { |
| 1483 | − glBindTexture(GL_TEXTURE_2D, 0); | |
| 1187 | + splatBatch.splatTextureIDs[j] = 0; | |
| 1484 | 1188 | } |
| 1485 | 1189 | } |
| 1486 | − const float QUARTER_STEP = 0.25f * EIGHT_BIT_MAXIMUM_RECIPROCAL; | |
| 1487 | − DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setUniformValue( | |
| 1488 | − locations.splatTextureScalesS, scalesS); | |
| 1489 | − DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setUniformValue( | |
| 1490 | − locations.splatTextureScalesT, scalesT); | |
| 1491 | − DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setUniformValue( | |
| 1492 | − locations.textureValueMinima, | |
| 1493 | − (i + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, (i + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, | |
| 1494 | − (i + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP, (i + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL - QUARTER_STEP); | |
| 1495 | − DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().setUniformValue( | |
| 1496 | − locations.textureValueMaxima, | |
| 1497 | − (i + 1) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, (i + 2) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, | |
| 1498 | − (i + 3) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP, (i + 4) * EIGHT_BIT_MAXIMUM_RECIPROCAL + QUARTER_STEP); | |
| 1499 | − glDrawRangeElements(GL_QUADS, 0, _vertexCount - 1, _indexCount, GL_UNSIGNED_INT, 0); | |
| 1190 | + splatBatch.materialIndex = i; | |
| 1191 | + Application::getInstance()->getMetavoxels()->addVoxelSplatBatch(splatBatch); | |
| 1500 | 1192 | } |
| 1501 | − | |
| 1502 | − for (int i = 0; i < SPLAT_COUNT; i++) { | |
| 1503 | − glActiveTexture(GL_TEXTURE0 + SPLAT_TEXTURE_UNITS[i]); | |
| 1504 | − glBindTexture(GL_TEXTURE_2D, 0); | |
| 1505 | − } | |
| 1506 | − | |
| 1507 | − glActiveTexture(GL_TEXTURE0); | |
| 1508 | − | |
| 1509 | − glDisable(GL_POLYGON_OFFSET_FILL); | |
| 1510 | − glEnable(GL_ALPHA_TEST); | |
| 1511 | − glDisable(GL_BLEND); | |
| 1512 | − glDepthMask(true); | |
| 1513 | − glDepthFunc(GL_LESS); | |
| 1514 | − | |
| 1515 | − Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, true); | |
| 1516 | − | |
| 1517 | − DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().disableAttributeArray(locations.materials); | |
| 1518 | − DefaultMetavoxelRendererImplementation::getSplatVoxelProgram().disableAttributeArray(locations.materialWeights); | |
| 1519 | − | |
| 1520 | − DefaultMetavoxelRendererImplementation::getBaseVoxelProgram().bind(); | |
| 1521 | 1193 | } |
| 1522 | 1194 | |
| 1523 | − _vertexBuffer.release(); | |
| 1524 | − _indexBuffer.release(); | |
| 1525 | − | |
| 1526 | − if (_hermiteCount > 0 && Menu::getInstance()->isOptionChecked(MenuOption::DisplayHermiteData) && !cursor) { | |
| 1195 | + if (_hermiteCount > 0 && Menu::getInstance()->isOptionChecked(MenuOption::DisplayHermiteData)) { | |
| 1527 | 1196 | if (!_hermiteBuffer.isCreated()) { |
| 1528 | 1197 | _hermiteBuffer.create(); |
| 1529 | 1198 | _hermiteBuffer.bind(); |
| 1530 | 1199 | _hermiteBuffer.allocate(_hermite.constData(), _hermite.size() * sizeof(glm::vec3)); |
| 1531 | 1200 | _hermite.clear(); |
| 1532 | − | |
| 1201 | + | |
| 1533 | 1202 | } else { |
| 1534 | 1203 | _hermiteBuffer.bind(); |
| 1535 | 1204 | } |
| 1536 | 1205 | |
| 1537 | − glDisableClientState(GL_COLOR_ARRAY); | |
| 1538 | − glDisableClientState(GL_NORMAL_ARRAY); | |
| 1539 | − | |
| 1540 | 1206 | glVertexPointer(3, GL_FLOAT, 0, 0); |
| 1541 | 1207 | |
| 1542 | 1208 | Application::getInstance()->getDeferredLightingEffect()->getSimpleProgram().bind(); |
| @@ -1548,612 +1214,46 @@ void VoxelBuffer::render(bool cursor) { · 5 unmodified lines | ||
| 1548 | 1214 | |
| 1549 | 1215 | glDrawArrays(GL_LINES, 0, _hermiteCount); |
| 1550 | 1216 | |
| 1551 | − DefaultMetavoxelRendererImplementation::getBaseVoxelProgram().bind(); | |
| 1552 | − | |
| 1553 | − glEnableClientState(GL_COLOR_ARRAY); | |
| 1554 | − glEnableClientState(GL_NORMAL_ARRAY); | |
| 1217 | + Application::getInstance()->getDeferredLightingEffect()->getSimpleProgram().release(); | |
| 1555 | 1218 | |
| 1556 | 1219 | _hermiteBuffer.release(); |
| 1557 | 1220 | } |
| 1558 | −} | |
| 1559 | − | |
| 1560 | −BufferDataAttribute::BufferDataAttribute(const QString& name) : | |
| 1561 | − InlineAttribute<BufferDataPointer>(name) { | |
| 1562 | −} | |
| 1563 | − | |
| 1564 | −bool BufferDataAttribute::merge(void*& parent, void* children[], bool postRead) const { | |
| 1565 | − *(BufferDataPointer*)&parent = _defaultValue; | |
| 1566 | − for (int i = 0; i < MERGE_COUNT; i++) { | |
| 1567 | − if (decodeInline<BufferDataPointer>(children[i])) { | |
| 1568 | − return false; | |
| 1569 | − } | |
| 1570 | − } | |
| 1571 | − return true; | |
| 1572 | −} | |
| 1573 | − | |
| 1574 | −AttributeValue BufferDataAttribute::inherit(const AttributeValue& parentValue) const { | |
| 1575 | − return AttributeValue(parentValue.getAttribute()); | |
| 1576 | −} | |
| 1577 | − | |
| 1578 | −void DefaultMetavoxelRendererImplementation::init() { | |
| 1579 | − if (!_pointProgram.isLinked()) { | |
| 1580 | − _pointProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + | |
| 1581 | − "shaders/metavoxel_point.vert"); | |
| 1582 | − _pointProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + | |
| 1583 | − "shaders/metavoxel_voxel_base.frag"); | |
| 1584 | − _pointProgram.link(); | |
| 1585 | − | |
| 1586 | − _pointProgram.bind(); | |
| 1587 | − _pointScaleLocation = _pointProgram.uniformLocation("pointScale"); | |
| 1588 | − _pointProgram.release(); | |
| 1589 | − | |
| 1590 | − _baseHeightfieldProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + | |
| 1591 | − "shaders/metavoxel_heightfield_base.vert"); | |
| 1592 | − _baseHeightfieldProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + | |
| 1593 | − "shaders/metavoxel_heightfield_base.frag"); | |
| 1594 | − _baseHeightfieldProgram.link(); | |
| 1595 | − | |
| 1596 | − _baseHeightfieldProgram.bind(); | |
| 1597 | − _baseHeightfieldProgram.setUniformValue("heightMap", 0); | |
| 1598 | − _baseHeightfieldProgram.setUniformValue("diffuseMap", 1); | |
| 1599 | − _baseHeightScaleLocation = _baseHeightfieldProgram.uniformLocation("heightScale"); | |
| 1600 | − _baseColorScaleLocation = _baseHeightfieldProgram.uniformLocation("colorScale"); | |
| 1601 | − _baseHeightfieldProgram.release(); | |
| 1602 | − | |
| 1603 | − loadSplatProgram("heightfield", _splatHeightfieldProgram, _splatHeightfieldLocations); | |
| 1604 | − | |
| 1605 | − _heightfieldCursorProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + | |
| 1606 | − "shaders/metavoxel_heightfield_cursor.vert"); | |
| 1607 | − _heightfieldCursorProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + | |
| 1608 | − "shaders/metavoxel_cursor.frag"); | |
| 1609 | − _heightfieldCursorProgram.link(); | |
| 1610 | − | |
| 1611 | − _heightfieldCursorProgram.bind(); | |
| 1612 | − _heightfieldCursorProgram.setUniformValue("heightMap", 0); | |
| 1613 | − _heightfieldCursorProgram.release(); | |
| 1614 | − | |
| 1615 | − _baseVoxelProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + | |
| 1616 | − "shaders/metavoxel_voxel_base.vert"); | |
| 1617 | − _baseVoxelProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + | |
| 1618 | − "shaders/metavoxel_voxel_base.frag"); | |
| 1619 | − _baseVoxelProgram.link(); | |
| 1620 | − | |
| 1621 | − loadSplatProgram("voxel", _splatVoxelProgram, _splatVoxelLocations); | |
| 1622 | − | |
| 1623 | − _voxelCursorProgram.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + | |
| 1624 | − "shaders/metavoxel_voxel_cursor.vert"); | |
| 1625 | − _voxelCursorProgram.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + | |
| 1626 | − "shaders/metavoxel_cursor.frag"); | |
| 1627 | − _voxelCursorProgram.link(); | |
| 1628 | − } | |
| 1629 | −} | |
| 1630 | − | |
| 1631 | −DefaultMetavoxelRendererImplementation::DefaultMetavoxelRendererImplementation() { | |
| 1632 | −} | |
| 1633 | − | |
| 1634 | −class PointAugmentVisitor : public MetavoxelVisitor { | |
| 1635 | −public: | |
| 1636 | − | |
| 1637 | − PointAugmentVisitor(const MetavoxelLOD& lod); | |
| 1638 | − | |
| 1639 | − virtual void prepare(MetavoxelData* data); | |
| 1640 | − virtual int visit(MetavoxelInfo& info); | |
| 1641 | − virtual bool postVisit(MetavoxelInfo& info); | |
| 1642 | − | |
| 1643 | −private: | |
| 1644 | − | |
| 1645 | − BufferPointVector _points; | |
| 1646 | − float _pointLeafSize; | |
| 1647 | −}; | |
| 1648 | − | |
| 1649 | −PointAugmentVisitor::PointAugmentVisitor(const MetavoxelLOD& lod) : | |
| 1650 | − MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getColorAttribute() << | |
| 1651 | − AttributeRegistry::getInstance()->getNormalAttribute(), QVector<AttributePointer>() << | |
| 1652 | − Application::getInstance()->getMetavoxels()->getPointBufferAttribute(), lod) { | |
| 1653 | −} | |
| 1654 | − | |
| 1655 | −const int ALPHA_RENDER_THRESHOLD = 0; | |
| 1656 | − | |
| 1657 | −void PointAugmentVisitor::prepare(MetavoxelData* data) { | |
| 1658 | − MetavoxelVisitor::prepare(data); | |
| 1659 | − const float MAX_POINT_LEAF_SIZE = 64.0f; | |
| 1660 | − _pointLeafSize = qMin(data->getSize(), MAX_POINT_LEAF_SIZE); | |
| 1661 | −} | |
| 1662 | − | |
| 1663 | −int PointAugmentVisitor::visit(MetavoxelInfo& info) { | |
| 1664 | − if (!info.isLeaf) { | |
| 1665 | − return (info.size > _pointLeafSize) ? DEFAULT_ORDER : (DEFAULT_ORDER | ALL_NODES_REST); | |
| 1666 | − } | |
| 1667 | − QRgb color = info.inputValues.at(0).getInlineValue<QRgb>(); | |
| 1668 | − quint8 alpha = qAlpha(color); | |
| 1669 | − if (alpha > ALPHA_RENDER_THRESHOLD) { | |
| 1670 | − QRgb normal = info.inputValues.at(1).getInlineValue<QRgb>(); | |
| 1671 | − BufferPoint point = { glm::vec4(info.minimum + glm::vec3(info.size, info.size, info.size) * 0.5f, info.size), | |
| 1672 | − { quint8(qRed(color)), quint8(qGreen(color)), quint8(qBlue(color)) }, | |
| 1673 | − { quint8(qRed(normal)), quint8(qGreen(normal)), quint8(qBlue(normal)) } }; | |
| 1674 | − _points.append(point); | |
| 1675 | − } | |
| 1676 | − if (info.size >= _pointLeafSize) { | |
| 1677 | − PointBuffer* buffer = NULL; | |
| 1678 | − if (!_points.isEmpty()) { | |
| 1679 | − BufferPointVector swapPoints; | |
| 1680 | − _points.swap(swapPoints); | |
| 1681 | − buffer = new PointBuffer(swapPoints); | |
| 1682 | − } | |
| 1683 | − BufferDataPointer pointer(buffer); | |
| 1684 | − info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(pointer)); | |
| 1685 | − } | |
| 1686 | − return STOP_RECURSION; | |
| 1687 | −} | |
| 1688 | − | |
| 1689 | −bool PointAugmentVisitor::postVisit(MetavoxelInfo& info) { | |
| 1690 | − if (info.size != _pointLeafSize) { | |
| 1691 | − return false; | |
| 1692 | − } | |
| 1693 | − PointBuffer* buffer = NULL; | |
| 1694 | − if (!_points.isEmpty()) { | |
| 1695 | − BufferPointVector swapPoints; | |
| 1696 | − _points.swap(swapPoints); | |
| 1697 | − buffer = new PointBuffer(swapPoints); | |
| 1698 | − } | |
| 1699 | − BufferDataPointer pointer(buffer); | |
| 1700 | − info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(pointer)); | |
| 1701 | − return true; | |
| 1702 | −} | |
| 1703 | − | |
| 1704 | −class HeightfieldFetchVisitor : public MetavoxelVisitor { | |
| 1705 | −public: | |
| 1706 | − | |
| 1707 | − HeightfieldFetchVisitor(const MetavoxelLOD& lod, const QVector<Box>& intersections); | |
| 1708 | − | |
| 1709 | − void init(HeightfieldBuffer* buffer); | |
| 1710 | − | |
| 1711 | − virtual int visit(MetavoxelInfo& info); | |
| 1712 | − virtual bool postVisit(MetavoxelInfo& info); | |
| 1713 | − | |
| 1714 | −private: | |
| 1715 | − | |
| 1716 | − const QVector<Box>& _intersections; | |
| 1717 | − HeightfieldBuffer* _buffer; | |
| 1718 | − | |
| 1719 | − QVector<int> _depthFlags; | |
| 1720 | −}; | |
| 1721 | − | |
| 1722 | −enum DepthFlags { HEIGHT_FLAG = 0x01, COLOR_FLAG = 0x02, MATERIAL_FLAG = 0x04 }; | |
| 1723 | − | |
| 1724 | −HeightfieldFetchVisitor::HeightfieldFetchVisitor(const MetavoxelLOD& lod, const QVector<Box>& intersections) : | |
| 1725 | − MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getHeightfieldAttribute() << | |
| 1726 | − AttributeRegistry::getInstance()->getHeightfieldColorAttribute() << | |
| 1727 | − AttributeRegistry::getInstance()->getHeightfieldMaterialAttribute(), QVector<AttributePointer>(), lod), | |
| 1728 | − _intersections(intersections) { | |
| 1729 | −} | |
| 1730 | − | |
| 1731 | −void HeightfieldFetchVisitor::init(HeightfieldBuffer* buffer) { | |
| 1732 | − _buffer = buffer; | |
| 1733 | −} | |
| 1734 | − | |
| 1735 | −int HeightfieldFetchVisitor::visit(MetavoxelInfo& info) { | |
| 1736 | − if (!info.getBounds().intersects(_buffer->getHeightBounds())) { | |
| 1737 | − return STOP_RECURSION; | |
| 1738 | − } | |
| 1739 | − if (_depthFlags.size() > _depth) { | |
| 1740 | − _depthFlags[_depth] = 0; | |
| 1741 | − } else { | |
| 1742 | − _depthFlags.append(0); | |
| 1743 | − } | |
| 1744 | − if (!info.isLeaf) { | |
| 1745 | − return DEFAULT_ORDER; | |
| 1746 | − } | |
| 1747 | − postVisit(info); | |
| 1748 | − return STOP_RECURSION; | |
| 1749 | −} | |
| 1750 | − | |
| 1751 | −bool HeightfieldFetchVisitor::postVisit(MetavoxelInfo& info) { | |
| 1752 | − HeightfieldHeightDataPointer height = info.inputValues.at(0).getInlineValue<HeightfieldHeightDataPointer>(); | |
| 1753 | − int flags = _depthFlags.at(_depth); | |
| 1754 | − if (height) { | |
| 1755 | − // to handle borders correctly, make sure we only sample nodes with resolution <= ours | |
| 1756 | − int heightSize = glm::sqrt((float)height->getContents().size()); | |
| 1757 | − float heightIncrement = info.size / heightSize; | |
| 1758 | − if (heightIncrement < _buffer->getHeightIncrement() || (flags & HEIGHT_FLAG)) { | |
| 1759 | − height.reset(); | |
| 1760 | − } else { | |
| 1761 | − flags |= HEIGHT_FLAG; | |
| 1762 | − } | |
| 1763 | − } | |
| 1764 | − HeightfieldColorDataPointer color = info.inputValues.at(1).getInlineValue<HeightfieldColorDataPointer>(); | |
| 1765 | − if (color) { | |
| 1766 | − int colorSize = glm::sqrt((float)color->getContents().size() / DataBlock::COLOR_BYTES); | |
| 1767 | − float colorIncrement = info.size / colorSize; | |
| 1768 | − if (colorIncrement < _buffer->getColorIncrement() || (flags & COLOR_FLAG)) { | |
| 1769 | − color.reset(); | |
| 1770 | − } else { | |
| 1771 | − flags |= COLOR_FLAG; | |
| 1772 | − } | |
| 1773 | − } | |
| 1774 | − HeightfieldMaterialDataPointer material = info.inputValues.at(2).getInlineValue<HeightfieldMaterialDataPointer>(); | |
| 1775 | − if (material) { | |
| 1776 | − int materialSize = glm::sqrt((float)material->getContents().size()); | |
| 1777 | − float materialIncrement = info.size / materialSize; | |
| 1778 | − if (materialIncrement < _buffer->getMaterialIncrement() || (flags & MATERIAL_FLAG)) { | |
| 1779 | − material.reset(); | |
| 1780 | − } else { | |
| 1781 | − flags |= MATERIAL_FLAG; | |
| 1782 | − } | |
| 1783 | − } | |
| 1784 | − if (_depth > 0) { | |
| 1785 | − _depthFlags[_depth - 1] |= flags; | |
| 1786 | − } | |
| 1787 | − if (!(height || color || material)) { | |
| 1788 | − return false; | |
| 1789 | − } | |
| 1790 | − Box bounds = info.getBounds(); | |
| 1791 | − foreach (const Box& intersection, _intersections) { | |
| 1792 | − Box overlap = intersection.getIntersection(bounds); | |
| 1793 | − if (overlap.isEmpty()) { | |
| 1794 | − continue; | |
| 1795 | − } | |
| 1796 | − if (height) { | |
| 1797 | − float heightIncrement = _buffer->getHeightIncrement(); | |
| 1798 | − const Box& heightBounds = _buffer->getHeightBounds(); | |
| 1799 | − int destX = (overlap.minimum.x - heightBounds.minimum.x) / heightIncrement; | |
| 1800 | − int destY = (overlap.minimum.z - heightBounds.minimum.z) / heightIncrement; | |
| 1801 | − int destWidth = glm::ceil((overlap.maximum.x - overlap.minimum.x) / heightIncrement); | |
| 1802 | − int destHeight = glm::ceil((overlap.maximum.z - overlap.minimum.z) / heightIncrement); | |
| 1803 | − int heightSize = _buffer->getHeightSize(); | |
| 1804 | − char* dest = _buffer->getHeight().data() + destY * heightSize + destX; | |
| 1805 | − | |
| 1806 | − const QByteArray& srcHeight = height->getContents(); | |
| 1807 | − int srcSize = glm::sqrt((float)srcHeight.size()); | |
| 1808 | − float srcIncrement = info.size / srcSize; | |
| 1809 | − | |
| 1810 | − if (info.size == _buffer->getScale() && srcSize == (heightSize - HeightfieldBuffer::HEIGHT_EXTENSION)) { | |
| 1811 | − // easy case: same resolution | |
| 1812 | − int srcX = (overlap.minimum.x - info.minimum.x) / srcIncrement; | |
| 1813 | − int srcY = (overlap.minimum.z - info.minimum.z) / srcIncrement; | |
| 1814 | − | |
| 1815 | − const char* src = srcHeight.constData() + srcY * srcSize + srcX; | |
| 1816 | − for (int y = 0; y < destHeight; y++, src += srcSize, dest += heightSize) { | |
| 1817 | − memcpy(dest, src, destWidth); | |
| 1818 | − } | |
| 1819 | − } else { | |
| 1820 | − // more difficult: different resolutions | |
| 1821 | − float srcX = (overlap.minimum.x - info.minimum.x) / srcIncrement; | |
| 1822 | − float srcY = (overlap.minimum.z - info.minimum.z) / srcIncrement; | |
| 1823 | − float srcAdvance = heightIncrement / srcIncrement; | |
| 1824 | − int shift = 0; | |
| 1825 | − float size = _buffer->getScale(); | |
| 1826 | − while (size < info.size) { | |
| 1827 | − shift++; | |
| 1828 | − size *= 2.0f; | |
| 1829 | − } | |
| 1830 | − int subtract = (_buffer->getTranslation().y - info.minimum.y) * EIGHT_BIT_MAXIMUM / _buffer->getScale(); | |
| 1831 | − for (int y = 0; y < destHeight; y++, dest += heightSize, srcY += srcAdvance) { | |
| 1832 | − const uchar* src = (const uchar*)srcHeight.constData() + (int)srcY * srcSize; | |
| 1833 | − float lineSrcX = srcX; | |
| 1834 | − for (char* lineDest = dest, *end = dest + destWidth; lineDest != end; lineDest++, lineSrcX += srcAdvance) { | |
| 1835 | − *lineDest = qMin(qMax(0, (src[(int)lineSrcX] << shift) - subtract), EIGHT_BIT_MAXIMUM); | |
| 1836 | − } | |
| 1837 | − } | |
| 1838 | − } | |
| 1839 | − } | |
| 1840 | − if (color) { | |
| 1841 | − const Box& colorBounds = _buffer->getColorBounds(); | |
| 1842 | − overlap = colorBounds.getIntersection(overlap); | |
| 1843 | − float colorIncrement = _buffer->getColorIncrement(); | |
| 1844 | − int destX = (overlap.minimum.x - colorBounds.minimum.x) / colorIncrement; | |
| 1845 | − int destY = (overlap.minimum.z - colorBounds.minimum.z) / colorIncrement; | |
| 1846 | − int destWidth = glm::ceil((overlap.maximum.x - overlap.minimum.x) / colorIncrement); | |
| 1847 | − int destHeight = glm::ceil((overlap.maximum.z - overlap.minimum.z) / colorIncrement); | |
| 1848 | − int colorSize = _buffer->getColorSize(); | |
| 1849 | − char* dest = _buffer->getColor().data() + (destY * colorSize + destX) * DataBlock::COLOR_BYTES; | |
| 1850 | − int destStride = colorSize * DataBlock::COLOR_BYTES; | |
| 1851 | − int destBytes = destWidth * DataBlock::COLOR_BYTES; | |
| 1852 | − | |
| 1853 | − const QByteArray& srcColor = color->getContents(); | |
| 1854 | − int srcSize = glm::sqrt(float(srcColor.size() / DataBlock::COLOR_BYTES)); | |
| 1855 | − int srcStride = srcSize * DataBlock::COLOR_BYTES; | |
| 1856 | − float srcIncrement = info.size / srcSize; | |
| 1857 | − | |
| 1858 | − if (srcIncrement == colorIncrement) { | |
| 1859 | − // easy case: same resolution | |
| 1860 | − int srcX = (overlap.minimum.x - info.minimum.x) / srcIncrement; | |
| 1861 | − int srcY = (overlap.minimum.z - info.minimum.z) / srcIncrement; | |
| 1862 | − | |
| 1863 | − const char* src = srcColor.constData() + (srcY * srcSize + srcX) * DataBlock::COLOR_BYTES; | |
| 1864 | − for (int y = 0; y < destHeight; y++, src += srcStride, dest += destStride) { | |
| 1865 | − memcpy(dest, src, destBytes); | |
| 1866 | − } | |
| 1867 | − } else { | |
| 1868 | − // more difficult: different resolutions | |
| 1869 | − float srcX = (overlap.minimum.x - info.minimum.x) / srcIncrement; | |
| 1870 | − float srcY = (overlap.minimum.z - info.minimum.z) / srcIncrement; | |
| 1871 | − float srcAdvance = colorIncrement / srcIncrement; | |
| 1872 | − for (int y = 0; y < destHeight; y++, dest += destStride, srcY += srcAdvance) { | |
| 1873 | − const char* src = srcColor.constData() + (int)srcY * srcStride; | |
| 1874 | − float lineSrcX = srcX; | |
| 1875 | − for (char* lineDest = dest, *end = dest + destBytes; lineDest != end; lineDest += DataBlock::COLOR_BYTES, | |
| 1876 | − lineSrcX += srcAdvance) { | |
| 1877 | − const char* lineSrc = src + (int)lineSrcX * DataBlock::COLOR_BYTES; | |
| 1878 | − lineDest[0] = lineSrc[0]; | |
| 1879 | − lineDest[1] = lineSrc[1]; | |
| 1880 | − lineDest[2] = lineSrc[2]; | |
| 1881 | − } | |
| 1882 | − } | |
| 1883 | − } | |
| 1884 | − } | |
| 1885 | − if (material) { | |
| 1886 | − const Box& materialBounds = _buffer->getMaterialBounds(); | |
| 1887 | − overlap = materialBounds.getIntersection(overlap); | |
| 1888 | − float materialIncrement = _buffer->getMaterialIncrement(); | |
| 1889 | − int destX = (overlap.minimum.x - materialBounds.minimum.x) / materialIncrement; | |
| 1890 | − int destY = (overlap.minimum.z - materialBounds.minimum.z) / materialIncrement; | |
| 1891 | − int destWidth = glm::ceil((overlap.maximum.x - overlap.minimum.x) / materialIncrement); | |
| 1892 | − int destHeight = glm::ceil((overlap.maximum.z - overlap.minimum.z) / materialIncrement); | |
| 1893 | − int materialSize = _buffer->getMaterialSize(); | |
| 1894 | − char* dest = _buffer->getMaterial().data() + destY * materialSize + destX; | |
| 1895 | − | |
| 1896 | − const QByteArray& srcMaterial = material->getContents(); | |
| 1897 | − const QVector<SharedObjectPointer> srcMaterials = material->getMaterials(); | |
| 1898 | − int srcSize = glm::sqrt((float)srcMaterial.size()); | |
| 1899 | − float srcIncrement = info.size / srcSize; | |
| 1900 | − QHash<int, int> materialMappings; | |
| 1901 | − | |
| 1902 | − if (srcIncrement == materialIncrement) { | |
| 1903 | − // easy case: same resolution | |
| 1904 | − int srcX = (overlap.minimum.x - info.minimum.x) / srcIncrement; | |
| 1905 | − int srcY = (overlap.minimum.z - info.minimum.z) / srcIncrement; | |
| 1906 | − | |
| 1907 | − const uchar* src = (const uchar*)srcMaterial.constData() + srcY * srcSize + srcX; | |
| 1908 | − for (int y = 0; y < destHeight; y++, src += srcSize, dest += materialSize) { | |
| 1909 | − const uchar* lineSrc = src; | |
| 1910 | − for (char* lineDest = dest, *end = dest + destWidth; lineDest != end; lineDest++, lineSrc++) { | |
| 1911 | − int value = *lineSrc; | |
| 1912 | − if (value != 0) { | |
| 1913 | − int& mapping = materialMappings[value]; | |
| 1914 | − if (mapping == 0) { | |
| 1915 | − mapping = getMaterialIndex(material->getMaterials().at(value - 1), | |
| 1916 | − _buffer->getMaterials(), _buffer->getMaterial()); | |
| 1917 | − } | |
| 1918 | − value = mapping; | |
| 1919 | − } | |
| 1920 | − *lineDest = value; | |
| 1921 | − } | |
| 1922 | − } | |
| 1923 | − } else { | |
| 1924 | − // more difficult: different resolutions | |
| 1925 | − float srcX = (overlap.minimum.x - info.minimum.x) / srcIncrement; | |
| 1926 | − float srcY = (overlap.minimum.z - info.minimum.z) / srcIncrement; | |
| 1927 | − float srcAdvance = materialIncrement / srcIncrement; | |
| 1928 | − for (int y = 0; y < destHeight; y++, dest += materialSize, srcY += srcAdvance) { | |
| 1929 | − const uchar* src = (const uchar*)srcMaterial.constData() + (int)srcY * srcSize; | |
| 1930 | − float lineSrcX = srcX; | |
| 1931 | − for (char* lineDest = dest, *end = dest + destWidth; lineDest != end; lineDest++, lineSrcX += srcAdvance) { | |
| 1932 | − int value = src[(int)lineSrcX]; | |
| 1933 | − if (value != 0) { | |
| 1934 | − int& mapping = materialMappings[value]; | |
| 1935 | − if (mapping == 0) { | |
| 1936 | − mapping = getMaterialIndex(material->getMaterials().at(value - 1), | |
| 1937 | − _buffer->getMaterials(), _buffer->getMaterial()); | |
| 1938 | − } | |
| 1939 | − value = mapping; | |
| 1940 | − } | |
| 1941 | − *lineDest = value; | |
| 1942 | − } | |
| 1943 | − } | |
| 1944 | − } | |
| 1945 | − clearUnusedMaterials(_buffer->getMaterials(), _buffer->getMaterial()); | |
| 1946 | − } | |
| 1947 | − } | |
| 1948 | − return false; | |
| 1949 | −} | |
| 1950 | − | |
| 1951 | −class HeightfieldRegionVisitor : public MetavoxelVisitor { | |
| 1952 | −public: | |
| 1953 | − | |
| 1954 | − QVector<Box> regions; | |
| 1955 | − Box regionBounds; | |
| 1956 | − | |
| 1957 | − HeightfieldRegionVisitor(const MetavoxelLOD& lod); | |
| 1958 | − | |
| 1959 | − virtual int visit(MetavoxelInfo& info); | |
| 1960 | − virtual bool postVisit(MetavoxelInfo& info); | |
| 1961 | − | |
| 1962 | −private: | |
| 1963 | − | |
| 1964 | − void addRegion(const Box& unextended, const Box& extended); | |
| 1965 | − | |
| 1966 | − class DepthInfo { | |
| 1967 | − public: | |
| 1968 | − float minimumColorIncrement; | |
| 1969 | − float minimumMaterialIncrement; | |
| 1970 | − | |
| 1971 | − DepthInfo() : minimumColorIncrement(FLT_MAX), minimumMaterialIncrement(FLT_MAX) { } | |
| 1972 | − }; | |
| 1973 | − | |
| 1974 | − QVector<DepthInfo> _depthInfo; | |
| 1975 | − | |
| 1976 | − QVector<Box> _intersections; | |
| 1977 | − HeightfieldFetchVisitor _fetchVisitor; | |
| 1978 | −}; | |
| 1979 | − | |
| 1980 | −HeightfieldRegionVisitor::HeightfieldRegionVisitor(const MetavoxelLOD& lod) : | |
| 1981 | − MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getHeightfieldAttribute() << | |
| 1982 | − AttributeRegistry::getInstance()->getHeightfieldColorAttribute() << | |
| 1983 | − AttributeRegistry::getInstance()->getHeightfieldMaterialAttribute() << | |
| 1984 | − Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), QVector<AttributePointer>() << | |
| 1985 | − Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), lod), | |
| 1986 | − regionBounds(glm::vec3(FLT_MAX, FLT_MAX, FLT_MAX), glm::vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX)), | |
| 1987 | − _fetchVisitor(lod, _intersections) { | |
| 1988 | −} | |
| 1989 | − | |
| 1990 | −int HeightfieldRegionVisitor::visit(MetavoxelInfo& info) { | |
| 1991 | − DepthInfo depthInfo; | |
| 1992 | − HeightfieldColorDataPointer color = info.inputValues.at(1).getInlineValue<HeightfieldColorDataPointer>(); | |
| 1993 | − if (color) { | |
| 1994 | − int colorSize = glm::sqrt((float)color->getContents().size() / DataBlock::COLOR_BYTES); | |
| 1995 | − depthInfo.minimumColorIncrement = info.size / colorSize; | |
| 1996 | − } | |
| 1997 | − HeightfieldMaterialDataPointer material = info.inputValues.at(2).getInlineValue<HeightfieldMaterialDataPointer>(); | |
| 1998 | − if (material) { | |
| 1999 | − int materialSize = glm::sqrt((float)material->getContents().size()); | |
| 2000 | − depthInfo.minimumMaterialIncrement = info.size / materialSize; | |
| 2001 | − } | |
| 2002 | − if (_depth < _depthInfo.size()) { | |
| 2003 | − _depthInfo[_depth] = depthInfo; | |
| 2004 | − } else { | |
| 2005 | − _depthInfo.append(depthInfo); | |
| 2006 | − } | |
| 2007 | − if (!info.isLeaf) { | |
| 2008 | − return _visitations.at(_depth).isInputLeaf(0) ? (DEFAULT_ORDER | ALL_NODES_REST) : DEFAULT_ORDER; | |
| 2009 | − } | |
| 2010 | − postVisit(info); | |
| 2011 | − return STOP_RECURSION; | |
| 2012 | −} | |
| 2013 | − | |
| 2014 | −bool HeightfieldRegionVisitor::postVisit(MetavoxelInfo& info) { | |
| 2015 | − const DepthInfo& depthInfo = _depthInfo.at(_depth); | |
| 2016 | − if (_depth > 0) { | |
| 2017 | − DepthInfo& parentDepthInfo = _depthInfo[_depth - 1]; | |
| 2018 | − parentDepthInfo.minimumColorIncrement = qMin(parentDepthInfo.minimumColorIncrement, depthInfo.minimumColorIncrement); | |
| 2019 | − parentDepthInfo.minimumMaterialIncrement = qMin(parentDepthInfo.minimumMaterialIncrement, | |
| 2020 | − depthInfo.minimumMaterialIncrement); | |
| 2021 | − } | |
| 2022 | − if (_visitations.at(_depth).isInputLeaf(0)) { | |
| 2023 | − HeightfieldBuffer* buffer = NULL; | |
| 2024 | − HeightfieldHeightDataPointer height = info.inputValues.at(0).getInlineValue<HeightfieldHeightDataPointer>(); | |
| 2025 | − if (height) { | |
| 2026 | − int heightSize = glm::sqrt((float)height->getContents().size()); | |
| 2027 | − int extendedHeightSize = heightSize + HeightfieldBuffer::HEIGHT_EXTENSION; | |
| 2028 | − int heightContentsSize = extendedHeightSize * extendedHeightSize; | |
| 2029 | − float minimumColorIncrement = depthInfo.minimumColorIncrement; | |
| 2030 | − float minimumMaterialIncrement = depthInfo.minimumMaterialIncrement; | |
| 2031 | − for (int i = _depth - 1; i >= 0 && qMax(minimumColorIncrement, minimumMaterialIncrement) == FLT_MAX; i--) { | |
| 2032 | − const DepthInfo& ancestorDepthInfo = _depthInfo.at(i); | |
| 2033 | − minimumColorIncrement = qMin(minimumColorIncrement, ancestorDepthInfo.minimumColorIncrement); | |
| 2034 | − minimumMaterialIncrement = qMin(minimumMaterialIncrement, ancestorDepthInfo.minimumMaterialIncrement); | |
| 2035 | − } | |
| 2036 | − int colorContentsSize = 0; | |
| 2037 | − if (minimumColorIncrement != FLT_MAX) { | |
| 2038 | − int colorSize = (int)glm::round(info.size / minimumColorIncrement) + HeightfieldBuffer::SHARED_EDGE; | |
| 2039 | − colorContentsSize = colorSize * colorSize * DataBlock::COLOR_BYTES; | |
| 2040 | − } | |
| 2041 | − int materialContentsSize = 0; | |
| 2042 | − if (minimumMaterialIncrement != FLT_MAX) { | |
| 2043 | − int materialSize = (int)glm::round(info.size / minimumMaterialIncrement) + HeightfieldBuffer::SHARED_EDGE; | |
| 2044 | − materialContentsSize = materialSize * materialSize; | |
| 2045 | − } | |
| 2046 | − const HeightfieldBuffer* existingBuffer = static_cast<const HeightfieldBuffer*>( | |
| 2047 | − info.inputValues.at(3).getInlineValue<BufferDataPointer>().data()); | |
| 2048 | − Box bounds = info.getBounds(); | |
| 2049 | − if (existingBuffer && existingBuffer->getHeight().size() == heightContentsSize && | |
| 2050 | − existingBuffer->getColor().size() == colorContentsSize && | |
| 2051 | − existingBuffer->getMaterial().size() == materialContentsSize) { | |
| 2052 | − // we already have a buffer of the correct resolution | |
| 2053 | − addRegion(bounds, existingBuffer->getHeightBounds()); | |
| 2054 | − buffer = new HeightfieldBuffer(info.minimum, info.size, existingBuffer->getHeight(), | |
| 2055 | − existingBuffer->getColor(), existingBuffer->getMaterial(), existingBuffer->getMaterials()); | |
| 2056 | − | |
| 2057 | − } else { | |
| 2058 | − // we must create a new buffer and update its borders | |
| 2059 | − buffer = new HeightfieldBuffer(info.minimum, info.size, QByteArray(heightContentsSize, 0), | |
| 2060 | − QByteArray(colorContentsSize, 0), QByteArray(materialContentsSize, 0)); | |
| 2061 | − const Box& heightBounds = buffer->getHeightBounds(); | |
| 2062 | − addRegion(bounds, heightBounds); | |
| 2063 | − | |
| 2064 | − _intersections.clear(); | |
| 2065 | − _intersections.append(Box(heightBounds.minimum, | |
| 2066 | − glm::vec3(bounds.maximum.x, heightBounds.maximum.y, bounds.minimum.z))); | |
| 2067 | − _intersections.append(Box(glm::vec3(bounds.maximum.x, heightBounds.minimum.y, heightBounds.minimum.z), | |
| 2068 | − glm::vec3(heightBounds.maximum.x, heightBounds.maximum.y, bounds.maximum.z))); | |
| 2069 | − _intersections.append(Box(glm::vec3(bounds.minimum.x, heightBounds.minimum.y, bounds.maximum.z), | |
| 2070 | − heightBounds.maximum)); | |
| 2071 | − _intersections.append(Box(glm::vec3(heightBounds.minimum.x, heightBounds.minimum.y, bounds.minimum.z), | |
| 2072 | − glm::vec3(bounds.minimum.x, heightBounds.maximum.y, heightBounds.maximum.z))); | |
| 2073 | − | |
| 2074 | − _fetchVisitor.init(buffer); | |
| 2075 | − _data->guide(_fetchVisitor); | |
| 2076 | − } | |
| 2077 | − } | |
| 2078 | − BufferDataPointer pointer(buffer); | |
| 2079 | − info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(pointer)); | |
| 2080 | − } | |
| 2081 | − return true; | |
| 2082 | −} | |
| 2083 | − | |
| 2084 | −void HeightfieldRegionVisitor::addRegion(const Box& unextended, const Box& extended) { | |
| 2085 | − regions.append(unextended); | |
| 2086 | − regionBounds.add(extended); | |
| 2087 | −} | |
| 2088 | − | |
| 2089 | −class HeightfieldUpdateVisitor : public MetavoxelVisitor { | |
| 2090 | −public: | |
| 2091 | − | |
| 2092 | − HeightfieldUpdateVisitor(const MetavoxelLOD& lod, const QVector<Box>& regions, const Box& regionBounds); | |
| 2093 | − | |
| 2094 | − virtual int visit(MetavoxelInfo& info); | |
| 2095 | − | |
| 2096 | −private: | |
| 2097 | − | |
| 2098 | − const QVector<Box>& _regions; | |
| 2099 | − const Box& _regionBounds; | |
| 2100 | − QVector<Box> _intersections; | |
| 2101 | − HeightfieldFetchVisitor _fetchVisitor; | |
| 2102 | −}; | |
| 2103 | − | |
| 2104 | −HeightfieldUpdateVisitor::HeightfieldUpdateVisitor(const MetavoxelLOD& lod, const QVector<Box>& regions, | |
| 2105 | − const Box& regionBounds) : | |
| 2106 | − MetavoxelVisitor(QVector<AttributePointer>() << | |
| 2107 | − Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), QVector<AttributePointer>() << | |
| 2108 | − Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(), lod), | |
| 2109 | − _regions(regions), | |
| 2110 | − _regionBounds(regionBounds), | |
| 2111 | − _fetchVisitor(lod, _intersections) { | |
| 2112 | −} | |
| 2113 | − | |
| 2114 | −int HeightfieldUpdateVisitor::visit(MetavoxelInfo& info) { | |
| 2115 | − if (!info.getBounds().intersects(_regionBounds)) { | |
| 2116 | − return STOP_RECURSION; | |
| 2117 | − } | |
| 2118 | − if (!info.isLeaf) { | |
| 2119 | − return DEFAULT_ORDER; | |
| 2120 | − } | |
| 2121 | − const HeightfieldBuffer* buffer = static_cast<const HeightfieldBuffer*>( | |
| 2122 | − info.inputValues.at(0).getInlineValue<BufferDataPointer>().data()); | |
| 2123 | − if (!buffer) { | |
| 2124 | − return STOP_RECURSION; | |
| 2125 | − } | |
| 2126 | − _intersections.clear(); | |
| 2127 | − foreach (const Box& region, _regions) { | |
| 2128 | − if (region.intersects(buffer->getHeightBounds())) { | |
| 2129 | − _intersections.append(region.getIntersection(buffer->getHeightBounds())); | |
| 1221 | +} | |
| 1222 | + | |
| 1223 | +BufferDataAttribute::BufferDataAttribute(const QString& name) : | |
| 1224 | +InlineAttribute<BufferDataPointer>(name) { | |
| 1225 | +} | |
| 1226 | + | |
| 1227 | +bool BufferDataAttribute::merge(void*& parent, void* children[], bool postRead) const { | |
| 1228 | + *(BufferDataPointer*)&parent = _defaultValue; | |
| 1229 | + for (int i = 0; i < MERGE_COUNT; i++) { | |
| 1230 | + if (decodeInline<BufferDataPointer>(children[i])) { | |
| 1231 | + return false; | |
| 2130 | 1232 | } |
| 2131 | 1233 | } |
| 2132 | − if (_intersections.isEmpty()) { | |
| 2133 | − return STOP_RECURSION; | |
| 2134 | − } | |
| 2135 | − HeightfieldBuffer* newBuffer = new HeightfieldBuffer(info.minimum, info.size, | |
| 2136 | − buffer->getHeight(), buffer->getColor(), buffer->getMaterial(), buffer->getMaterials()); | |
| 2137 | − _fetchVisitor.init(newBuffer); | |
| 2138 | − _data->guide(_fetchVisitor); | |
| 2139 | − BufferDataPointer pointer(newBuffer); | |
| 2140 | − info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(pointer)); | |
| 2141 | − return STOP_RECURSION; | |
| 1234 | + return true; | |
| 1235 | +} | |
| 1236 | + | |
| 1237 | +AttributeValue BufferDataAttribute::inherit(const AttributeValue& parentValue) const { | |
| 1238 | + return AttributeValue(parentValue.getAttribute()); | |
| 1239 | +} | |
| 1240 | + | |
| 1241 | +DefaultMetavoxelRendererImplementation::DefaultMetavoxelRendererImplementation() { | |
| 2142 | 1242 | } |
| 2143 | 1243 | |
| 2144 | 1244 | class VoxelAugmentVisitor : public MetavoxelVisitor { |
| 2145 | 1245 | public: |
| 2146 | − | |
| 1246 | + | |
| 2147 | 1247 | VoxelAugmentVisitor(const MetavoxelLOD& lod); |
| 2148 | 1248 | |
| 2149 | 1249 | virtual int visit(MetavoxelInfo& info); |
| 2150 | 1250 | }; |
| 2151 | 1251 | |
| 2152 | 1252 | VoxelAugmentVisitor::VoxelAugmentVisitor(const MetavoxelLOD& lod) : |
| 2153 | − MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getVoxelColorAttribute() << | |
| 2154 | − AttributeRegistry::getInstance()->getVoxelMaterialAttribute() << | |
| 2155 | − AttributeRegistry::getInstance()->getVoxelHermiteAttribute(), QVector<AttributePointer>() << | |
| 2156 | − Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute(), lod) { | |
| 1253 | +MetavoxelVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getVoxelColorAttribute() << | |
| 1254 | + AttributeRegistry::getInstance()->getVoxelMaterialAttribute() << | |
| 1255 | + AttributeRegistry::getInstance()->getVoxelHermiteAttribute(), QVector<AttributePointer>() << | |
| 1256 | + Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute(), lod) { | |
| 2157 | 1257 | } |
| 2158 | 1258 | |
| 2159 | 1259 | class EdgeCrossing { |
| @@ -2162,16 +1262,37 @@ public: · 2 unmodified lines | ||
| 2162 | 1262 | glm::vec3 normal; |
| 2163 | 1263 | QRgb color; |
| 2164 | 1264 | char material; |
| 2165 | − int axis; | |
| 2166 | 1265 | }; |
| 2167 | 1266 | |
| 2168 | −class AxisIndex { | |
| 1267 | +const int MAX_NORMALS_PER_VERTEX = 4; | |
| 1268 | + | |
| 1269 | +class NormalIndex { | |
| 2169 | 1270 | public: |
| 2170 | − int x, y, z; | |
| 1271 | + int indices[MAX_NORMALS_PER_VERTEX]; | |
| 2171 | 1272 | |
| 2172 | − AxisIndex(int x = -1, int y = -1, int z = -1) : x(x), y(y), z(z) { } | |
| 1273 | + int getClosestIndex(const glm::vec3& normal, QVector<VoxelPoint>& vertices) const; | |
| 2173 | 1274 | }; |
| 2174 | 1275 | |
| 1276 | +int NormalIndex::getClosestIndex(const glm::vec3& normal, QVector<VoxelPoint>& vertices) const { | |
| 1277 | + int firstIndex = indices[0]; | |
| 1278 | + int closestIndex = firstIndex; | |
| 1279 | + const VoxelPoint& firstVertex = vertices.at(firstIndex); | |
| 1280 | + float closest = normal.x * firstVertex.normal[0] + normal.y * firstVertex.normal[1] + normal.z * firstVertex.normal[2]; | |
| 1281 | + for (int i = 1; i < MAX_NORMALS_PER_VERTEX; i++) { | |
| 1282 | + int index = indices[i]; | |
| 1283 | + if (index == firstIndex) { | |
| 1284 | + break; | |
| 1285 | + } | |
| 1286 | + const VoxelPoint& vertex = vertices.at(index); | |
| 1287 | + float product = normal.x * vertex.normal[0] + normal.y * vertex.normal[1] + normal.z * vertex.normal[2]; | |
| 1288 | + if (product > closest) { | |
| 1289 | + closest = product; | |
| 1290 | + closestIndex = index; | |
| 1291 | + } | |
| 1292 | + } | |
| 1293 | + return closestIndex; | |
| 1294 | +} | |
| 1295 | + | |
| 2175 | 1296 | static glm::vec3 safeNormalize(const glm::vec3& vector) { |
| 2176 | 1297 | float length = glm::length(vector); |
| 2177 | 1298 | return (length > 0.0f) ? (vector / length) : vector; |
| @@ -2190,7 +1311,7 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 12 unmodified lines | ||
| 2190 | 1311 | QVector<VoxelPoint> vertices; |
| 2191 | 1312 | QVector<int> indices; |
| 2192 | 1313 | QVector<glm::vec3> hermiteSegments; |
| 2193 | − QMultiHash<QRgb, int> quadIndices; | |
| 1314 | + QMultiHash<VoxelCoord, int> quadIndices; | |
| 2194 | 1315 | |
| 2195 | 1316 | // see http://www.frankpetterson.com/publications/dualcontour/dualcontour.pdf for a description of the |
| 2196 | 1317 | // dual contour algorithm for generating meshes from voxel data using Hermite-tagged edges |
| @@ -2217,10 +1338,10 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 20 unmodified lines | ||
| 2217 | 1338 | // as we scan down the cube generating vertices between grid points, we remember the indices of the last |
| 2218 | 1339 | // (element, line, section--x, y, z) so that we can connect generated vertices as quads |
| 2219 | 1340 | int expanded = size + 1; |
| 2220 | − QVector<AxisIndex> lineIndices(expanded); | |
| 2221 | − QVector<AxisIndex> lastLineIndices(expanded); | |
| 2222 | − QVector<AxisIndex> planeIndices(expanded * expanded); | |
| 2223 | − QVector<AxisIndex> lastPlaneIndices(expanded * expanded); | |
| 1341 | + QVector<NormalIndex> lineIndices(expanded); | |
| 1342 | + QVector<NormalIndex> lastLineIndices(expanded); | |
| 1343 | + QVector<NormalIndex> planeIndices(expanded * expanded); | |
| 1344 | + QVector<NormalIndex> lastPlaneIndices(expanded * expanded); | |
| 2224 | 1345 | |
| 2225 | 1346 | const int EDGES_PER_CUBE = 12; |
| 2226 | 1347 | EdgeCrossing crossings[EDGES_PER_CUBE]; |
| @@ -2232,7 +1353,7 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 5 unmodified lines | ||
| 2232 | 1353 | for (int z = 0; z < expanded; z++) { |
| 2233 | 1354 | const QRgb* colorY = colorZ; |
| 2234 | 1355 | for (int y = 0; y < expanded; y++) { |
| 2235 | − AxisIndex lastIndex; | |
| 1356 | + NormalIndex lastIndex; | |
| 2236 | 1357 | const QRgb* colorX = colorY; |
| 2237 | 1358 | for (int x = 0; x < expanded; x++) { |
| 2238 | 1359 | int alpha0 = colorX[0] >> ALPHA_OFFSET; |
| @@ -2290,9 +1411,9 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 51 unmodified lines | ||
| 2290 | 1411 | // its properties (color, material, normal) if one is present; as before, boundary edges are excluded |
| 2291 | 1412 | int clampedX = qMax(x - 1, 0), clampedY = qMax(y - 1, 0), clampedZ = qMax(z - 1, 0); |
| 2292 | 1413 | const QRgb* hermiteBase = hermiteData + clampedZ * hermiteArea + clampedY * hermiteStride + |
| 2293 | − clampedX * VoxelHermiteData::EDGE_COUNT; | |
| 1414 | + clampedX * VoxelHermiteData::EDGE_COUNT; | |
| 2294 | 1415 | const char* materialBase = materialData ? |
| 2295 | − (materialData + clampedZ * area + clampedY * size + clampedX) : NULL; | |
| 1416 | + (materialData + clampedZ * area + clampedY * size + clampedX) : NULL; | |
| 2296 | 1417 | int crossingCount = 0; |
| 2297 | 1418 | if (middleX) { |
| 2298 | 1419 | if (alpha0 != alpha1) { |
| @@ -2307,7 +1428,6 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 8 unmodified lines | ||
| 2307 | 1428 | crossing.material = materialBase ? materialBase[0] : 0; |
| 2308 | 1429 | } |
| 2309 | 1430 | crossing.point = glm::vec3(qAlpha(hermite) * EIGHT_BIT_MAXIMUM_RECIPROCAL, 0.0f, 0.0f); |
| 2310 | − crossing.axis = 0; | |
| 2311 | 1431 | } |
| 2312 | 1432 | if (middleY) { |
| 2313 | 1433 | if (alpha1 != alpha3) { |
| @@ -2322,7 +1442,6 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 8 unmodified lines | ||
| 2322 | 1442 | crossing.material = materialBase ? materialBase[1] : 0; |
| 2323 | 1443 | } |
| 2324 | 1444 | crossing.point = glm::vec3(1.0f, qAlpha(hermite) * EIGHT_BIT_MAXIMUM_RECIPROCAL, 0.0f); |
| 2325 | − crossing.axis = 1; | |
| 2326 | 1445 | } |
| 2327 | 1446 | if (alpha2 != alpha3) { |
| 2328 | 1447 | QRgb hermite = hermiteBase[hermiteStride]; |
| @@ -2336,7 +1455,6 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 7 unmodified lines | ||
| 2336 | 1455 | crossing.material = materialBase ? materialBase[size] : 0; |
| 2337 | 1456 | } |
| 2338 | 1457 | crossing.point = glm::vec3(qAlpha(hermite) * EIGHT_BIT_MAXIMUM_RECIPROCAL, 1.0f, 0.0f); |
| 2339 | − crossing.axis = 0; | |
| 2340 | 1458 | } |
| 2341 | 1459 | if (middleZ) { |
| 2342 | 1460 | if (alpha3 != alpha7) { |
| @@ -2351,7 +1469,6 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 8 unmodified lines | ||
| 2351 | 1469 | crossing.material = materialBase ? materialBase[offset3] : 0; |
| 2352 | 1470 | } |
| 2353 | 1471 | crossing.point = glm::vec3(1.0f, 1.0f, qAlpha(hermite) * EIGHT_BIT_MAXIMUM_RECIPROCAL); |
| 2354 | − crossing.axis = 2; | |
| 2355 | 1472 | } |
| 2356 | 1473 | if (alpha5 != alpha7) { |
| 2357 | 1474 | QRgb hermite = hermiteBase[hermiteArea + VoxelHermiteData::EDGE_COUNT + 1]; |
| @@ -2365,7 +1482,6 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 7 unmodified lines | ||
| 2365 | 1482 | crossing.material = materialBase ? materialBase[offset5] : 0; |
| 2366 | 1483 | } |
| 2367 | 1484 | crossing.point = glm::vec3(1.0f, qAlpha(hermite) * EIGHT_BIT_MAXIMUM_RECIPROCAL, 1.0f); |
| 2368 | − crossing.axis = 1; | |
| 2369 | 1485 | } |
| 2370 | 1486 | if (alpha6 != alpha7) { |
| 2371 | 1487 | QRgb hermite = hermiteBase[hermiteArea + hermiteStride]; |
| @@ -2379,7 +1495,6 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 7 unmodified lines | ||
| 2379 | 1495 | crossing.material = materialBase ? materialBase[offset6] : 0; |
| 2380 | 1496 | } |
| 2381 | 1497 | crossing.point = glm::vec3(qAlpha(hermite) * EIGHT_BIT_MAXIMUM_RECIPROCAL, 1.0f, 1.0f); |
| 2382 | − crossing.axis = 0; | |
| 2383 | 1498 | } |
| 2384 | 1499 | } |
| 2385 | 1500 | } |
| @@ -2396,7 +1511,6 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 10 unmodified lines | ||
| 2396 | 1511 | crossing.material = materialBase ? materialBase[1] : 0; |
| 2397 | 1512 | } |
| 2398 | 1513 | crossing.point = glm::vec3(1.0f, 0.0f, qAlpha(hermite) * EIGHT_BIT_MAXIMUM_RECIPROCAL); |
| 2399 | − crossing.axis = 2; | |
| 2400 | 1514 | } |
| 2401 | 1515 | if (alpha4 != alpha5) { |
| 2402 | 1516 | QRgb hermite = hermiteBase[hermiteArea]; |
| @@ -2410,7 +1524,6 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 7 unmodified lines | ||
| 2410 | 1524 | crossing.material = materialBase ? materialBase[area] : 0; |
| 2411 | 1525 | } |
| 2412 | 1526 | crossing.point = glm::vec3(qAlpha(hermite) * EIGHT_BIT_MAXIMUM_RECIPROCAL, 0.0f, 1.0f); |
| 2413 | − crossing.axis = 0; | |
| 2414 | 1527 | } |
| 2415 | 1528 | } |
| 2416 | 1529 | } |
| @@ -2427,7 +1540,6 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 10 unmodified lines | ||
| 2427 | 1540 | crossing.material = materialBase ? materialBase[0] : 0; |
| 2428 | 1541 | } |
| 2429 | 1542 | crossing.point = glm::vec3(0.0f, qAlpha(hermite) * EIGHT_BIT_MAXIMUM_RECIPROCAL, 0.0f); |
| 2430 | − crossing.axis = 1; | |
| 2431 | 1543 | } |
| 2432 | 1544 | if (middleZ) { |
| 2433 | 1545 | if (alpha2 != alpha6) { |
| @@ -2442,7 +1554,6 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 8 unmodified lines | ||
| 2442 | 1554 | crossing.material = materialBase ? materialBase[size] : 0; |
| 2443 | 1555 | } |
| 2444 | 1556 | crossing.point = glm::vec3(0.0f, 1.0f, qAlpha(hermite) * EIGHT_BIT_MAXIMUM_RECIPROCAL); |
| 2445 | − crossing.axis = 2; | |
| 2446 | 1557 | } |
| 2447 | 1558 | if (alpha4 != alpha6) { |
| 2448 | 1559 | QRgb hermite = hermiteBase[hermiteArea + 1]; |
| @@ -2456,7 +1567,6 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 7 unmodified lines | ||
| 2456 | 1567 | crossing.material = materialBase ? materialBase[area] : 0; |
| 2457 | 1568 | } |
| 2458 | 1569 | crossing.point = glm::vec3(0.0f, qAlpha(hermite) * EIGHT_BIT_MAXIMUM_RECIPROCAL, 1.0f); |
| 2459 | − crossing.axis = 1; | |
| 2460 | 1570 | } |
| 2461 | 1571 | } |
| 2462 | 1572 | } |
| @@ -2472,12 +1582,13 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 9 unmodified lines | ||
| 2472 | 1582 | crossing.material = materialBase ? materialBase[0] : 0; |
| 2473 | 1583 | } |
| 2474 | 1584 | crossing.point = glm::vec3(0.0f, 0.0f, qAlpha(hermite) * EIGHT_BIT_MAXIMUM_RECIPROCAL); |
| 2475 | − crossing.axis = 2; | |
| 2476 | 1585 | } |
| 2477 | 1586 | // at present, we simply average the properties of each crossing as opposed to finding the vertex that |
| 2478 | 1587 | // minimizes the quadratic error function as described in the reference paper |
| 2479 | 1588 | glm::vec3 center; |
| 2480 | − glm::vec3 axisNormals[3]; | |
| 1589 | + glm::vec3 normals[MAX_NORMALS_PER_VERTEX]; | |
| 1590 | + int normalCount = 0; | |
| 1591 | + const float CREASE_COS_NORMAL = glm::cos(glm::radians(45.0f)); | |
| 2481 | 1592 | const int MAX_MATERIALS_PER_VERTEX = 4; |
| 2482 | 1593 | quint8 materials[] = { 0, 0, 0, 0 }; |
| 2483 | 1594 | glm::vec4 materialWeights; |
| @@ -2486,14 +1597,25 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 2 unmodified lines | ||
| 2486 | 1597 | for (int i = 0; i < crossingCount; i++) { |
| 2487 | 1598 | const EdgeCrossing& crossing = crossings[i]; |
| 2488 | 1599 | center += crossing.point; |
| 2489 | − axisNormals[crossing.axis] += crossing.normal; | |
| 1600 | + | |
| 1601 | + int j = 0; | |
| 1602 | + for (; j < normalCount; j++) { | |
| 1603 | + if (glm::dot(normals[j], crossing.normal) > CREASE_COS_NORMAL) { | |
| 1604 | + normals[j] = safeNormalize(normals[j] + crossing.normal); | |
| 1605 | + break; | |
| 1606 | + } | |
| 1607 | + } | |
| 1608 | + if (j == normalCount) { | |
| 1609 | + normals[normalCount++] = crossing.normal; | |
| 1610 | + } | |
| 1611 | + | |
| 2490 | 1612 | red += qRed(crossing.color); |
| 2491 | 1613 | green += qGreen(crossing.color); |
| 2492 | 1614 | blue += qBlue(crossing.color); |
| 2493 | 1615 | |
| 2494 | 1616 | if (displayHermite) { |
| 2495 | 1617 | glm::vec3 start = info.minimum + (glm::vec3(clampedX, clampedY, clampedZ) + |
| 2496 | − crossing.point) * scale; | |
| 1618 | + crossing.point) * scale; | |
| 2497 | 1619 | hermiteSegments.append(start); |
| 2498 | 1620 | hermiteSegments.append(start + crossing.normal * scale); |
| 2499 | 1621 | } |
| @@ -2501,7 +1623,7 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 1 unmodified line | ||
| 2501 | 1623 | // when assigning a material, search for its presence and, if not found, |
| 2502 | 1624 | // place it in the first empty slot |
| 2503 | 1625 | if (crossing.material != 0) { |
| 2504 | − for (int j = 0; j < MAX_MATERIALS_PER_VERTEX; j++) { | |
| 1626 | + for (j = 0; j < MAX_MATERIALS_PER_VERTEX; j++) { | |
| 2505 | 1627 | if (materials[j] == crossing.material) { |
| 2506 | 1628 | materialWeights[j] += 1.0f; |
| 2507 | 1629 | totalWeight += 1.0f; |
| @@ -2516,18 +1638,15 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 8 unmodified lines | ||
| 2516 | 1638 | } |
| 2517 | 1639 | } |
| 2518 | 1640 | } |
| 2519 | − glm::vec3 normal = safeNormalize(axisNormals[0] + axisNormals[1] + axisNormals[2]); | |
| 2520 | 1641 | center /= crossingCount; |
| 2521 | 1642 | |
| 2522 | 1643 | // use a sequence of Givens rotations to perform a QR decomposition |
| 2523 | 1644 | // see http://www.cs.rice.edu/~jwarren/papers/techreport02408.pdf |
| 2524 | 1645 | glm::mat4 r(0.0f); |
| 2525 | 1646 | glm::vec4 bottom; |
| 2526 | − float smallestCosNormal = 1.0f; | |
| 2527 | 1647 | for (int i = 0; i < crossingCount; i++) { |
| 2528 | 1648 | const EdgeCrossing& crossing = crossings[i]; |
| 2529 | 1649 | bottom = glm::vec4(crossing.normal, glm::dot(crossing.normal, crossing.point - center)); |
| 2530 | − smallestCosNormal = qMin(smallestCosNormal, glm::dot(crossing.normal, normal)); | |
| 2531 | 1650 | |
| 2532 | 1651 | for (int j = 0; j < 4; j++) { |
| 2533 | 1652 | float angle = glm::atan(-bottom[j], r[j][j]); |
| @@ -2556,18 +1675,18 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 22 unmodified lines | ||
| 2556 | 1675 | for (int i = 0; i < MAX_ITERATIONS; i++) { |
| 2557 | 1676 | glm::vec3 offDiagonals = glm::abs(glm::vec3(d[1][0], d[2][0], d[2][1])); |
| 2558 | 1677 | int largestIndex = (offDiagonals[0] > offDiagonals[1]) ? (offDiagonals[0] > offDiagonals[2] ? 0 : 2) : |
| 2559 | − (offDiagonals[1] > offDiagonals[2] ? 1 : 2); | |
| 1678 | + (offDiagonals[1] > offDiagonals[2] ? 1 : 2); | |
| 2560 | 1679 | const float DESIRED_PRECISION = 0.00001f; |
| 2561 | 1680 | if (offDiagonals[largestIndex] < DESIRED_PRECISION) { |
| 2562 | 1681 | break; |
| 2563 | 1682 | } |
| 2564 | 1683 | int largestJ = (largestIndex == 2) ? 1 : 0; |
| 2565 | − int largestI = (largestIndex == 0) ? 1 : 2; | |
| 1684 | + int largestI = (largestIndex == 0) ? 1 : 2; | |
| 2566 | 1685 | float sjj = d[largestJ][largestJ]; |
| 2567 | 1686 | float sii = d[largestI][largestI]; |
| 2568 | 1687 | float angle = glm::atan(2.0f * d[largestJ][largestI], sjj - sii) / 2.0f; |
| 2569 | 1688 | glm::quat rotation = glm::angleAxis(angle, largestIndex == 0 ? glm::vec3(0.0f, 0.0f, -1.0f) : |
| 2570 | − (largestIndex == 1 ? glm::vec3(0.0f, 1.0f, 0.0f) : glm::vec3(-1.0f, 0.0f, 0.0f))); | |
| 1689 | + (largestIndex == 1 ? glm::vec3(0.0f, 1.0f, 0.0f) : glm::vec3(-1.0f, 0.0f, 0.0f))); | |
| 2571 | 1690 | combinedRotation = glm::normalize(rotation * combinedRotation); |
| 2572 | 1691 | glm::mat3 matrix = glm::mat3_cast(combinedRotation); |
| 2573 | 1692 | d = matrix * ata * glm::transpose(matrix); |
| @@ -2581,7 +1700,7 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 7 unmodified lines | ||
| 2581 | 1700 | |
| 2582 | 1701 | // compute the pseudo-inverse, ataplus, and use to find the minimizing solution |
| 2583 | 1702 | glm::mat3 u = glm::mat3_cast(combinedRotation); |
| 2584 | − glm::mat3 ataplus = glm::transpose(u) * d * u; | |
| 1703 | + glm::mat3 ataplus = glm::transpose(u) * d * u; | |
| 2585 | 1704 | glm::vec3 solution = (ataplus * atrans * b) + center; |
| 2586 | 1705 | |
| 2587 | 1706 | // make sure it doesn't fall beyond the cell boundaries |
| @@ -2592,63 +1711,17 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 4 unmodified lines | ||
| 2592 | 1711 | } |
| 2593 | 1712 | VoxelPoint point = { info.minimum + (glm::vec3(clampedX, clampedY, clampedZ) + center) * scale, |
| 2594 | 1713 | { (quint8)(red / crossingCount), (quint8)(green / crossingCount), (quint8)(blue / crossingCount) }, |
| 2595 | − { (char)(normal.x * 127.0f), (char)(normal.y * 127.0f), (char)(normal.z * 127.0f) }, | |
| 1714 | + { (char)(normals[0].x * 127.0f), (char)(normals[0].y * 127.0f), (char)(normals[0].z * 127.0f) }, | |
| 2596 | 1715 | { materials[0], materials[1], materials[2], materials[3] }, |
| 2597 | 1716 | { (quint8)materialWeights[0], (quint8)materialWeights[1], (quint8)materialWeights[2], |
| 2598 | 1717 | (quint8)materialWeights[3] } }; |
| 2599 | 1718 | |
| 2600 | − // determine whether we must "crease" by generating directional normals | |
| 2601 | − const float CREASE_COS_NORMAL = glm::cos(glm::radians(40.0f)); | |
| 2602 | − AxisIndex index(vertices.size(), vertices.size(), vertices.size()); | |
| 2603 | − if (smallestCosNormal > CREASE_COS_NORMAL) { | |
| 1719 | + NormalIndex index = { { vertices.size(), vertices.size(), vertices.size(), vertices.size() } }; | |
| 1720 | + vertices.append(point); | |
| 1721 | + for (int i = 1; i < normalCount; i++) { | |
| 1722 | + index.indices[i] = vertices.size(); | |
| 1723 | + point.setNormal(normals[i]); | |
| 2604 | 1724 | vertices.append(point); |
| 2605 | − | |
| 2606 | − } else { | |
| 2607 | − axisNormals[0] = safeNormalize(axisNormals[0]); | |
| 2608 | − axisNormals[1] = safeNormalize(axisNormals[1]); | |
| 2609 | − axisNormals[2] = safeNormalize(axisNormals[2]); | |
| 2610 | − glm::vec3 normalXY(safeNormalize(axisNormals[0] + axisNormals[1])); | |
| 2611 | − glm::vec3 normalXZ(safeNormalize(axisNormals[0] + axisNormals[2])); | |
| 2612 | − glm::vec3 normalYZ(safeNormalize(axisNormals[1] + axisNormals[2])); | |
| 2613 | − if (glm::dot(axisNormals[0], normalXY) > CREASE_COS_NORMAL && | |
| 2614 | − glm::dot(axisNormals[1], normalXY) > CREASE_COS_NORMAL) { | |
| 2615 | − point.setNormal(normalXY); | |
| 2616 | − vertices.append(point); | |
| 2617 | − | |
| 2618 | − point.setNormal(axisNormals[2]); | |
| 2619 | − index.z = vertices.size(); | |
| 2620 | − vertices.append(point); | |
| 2621 | − | |
| 2622 | − } else if (glm::dot(axisNormals[0], normalXZ) > CREASE_COS_NORMAL && | |
| 2623 | − glm::dot(axisNormals[2], normalXZ) > CREASE_COS_NORMAL) { | |
| 2624 | − point.setNormal(normalXZ); | |
| 2625 | − vertices.append(point); | |
| 2626 | − | |
| 2627 | − point.setNormal(axisNormals[1]); | |
| 2628 | − index.y = vertices.size(); | |
| 2629 | − vertices.append(point); | |
| 2630 | − | |
| 2631 | − } else if (glm::dot(axisNormals[1], normalYZ) > CREASE_COS_NORMAL && | |
| 2632 | − glm::dot(axisNormals[2], normalYZ) > CREASE_COS_NORMAL) { | |
| 2633 | − point.setNormal(normalYZ); | |
| 2634 | − vertices.append(point); | |
| 2635 | − | |
| 2636 | − point.setNormal(axisNormals[0]); | |
| 2637 | − index.x = vertices.size(); | |
| 2638 | − vertices.append(point); | |
| 2639 | − | |
| 2640 | − } else { | |
| 2641 | − point.setNormal(axisNormals[0]); | |
| 2642 | − vertices.append(point); | |
| 2643 | − | |
| 2644 | − point.setNormal(axisNormals[1]); | |
| 2645 | − index.y = vertices.size(); | |
| 2646 | − vertices.append(point); | |
| 2647 | − | |
| 2648 | − point.setNormal(axisNormals[2]); | |
| 2649 | − index.z = vertices.size(); | |
| 2650 | − vertices.append(point); | |
| 2651 | − } | |
| 2652 | 1725 | } |
| 2653 | 1726 | |
| 2654 | 1727 | // the first x, y, and z are repeated for the boundary edge; past that, we consider generating |
| @@ -2659,19 +1732,25 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 4 unmodified lines | ||
| 2659 | 1732 | quadIndices.insert(qRgb(x, y - 1, z), indices.size()); |
| 2660 | 1733 | quadIndices.insert(qRgb(x, y - 1, z - 1), indices.size()); |
| 2661 | 1734 | quadIndices.insert(qRgb(x, y, z - 1), indices.size()); |
| 2662 | − indices.append(index.x); | |
| 2663 | − int index1 = lastLineIndices.at(x).x; | |
| 2664 | − int index2 = lastPlaneIndices.at((y - 1) * expanded + x).x; | |
| 2665 | − int index3 = lastPlaneIndices.at(y * expanded + x).x; | |
| 1735 | + | |
| 1736 | + const NormalIndex& index1 = lastLineIndices.at(x); | |
| 1737 | + const NormalIndex& index2 = lastPlaneIndices.at((y - 1) * expanded + x); | |
| 1738 | + const NormalIndex& index3 = lastPlaneIndices.at(y * expanded + x); | |
| 1739 | + | |
| 1740 | + const glm::vec3& first = vertices.at(index.indices[0]).vertex; | |
| 1741 | + glm::vec3 normal = glm::cross(vertices.at(index1.indices[0]).vertex - first, | |
| 1742 | + vertices.at(index3.indices[0]).vertex - first); | |
| 1743 | + | |
| 2666 | 1744 | if (alpha0 == 0) { // quad faces negative x |
| 2667 | − indices.append(index3); | |
| 2668 | − indices.append(index2); | |
| 2669 | − indices.append(index1); | |
| 1745 | + indices.append(index3.getClosestIndex(normal = -normal, vertices)); | |
| 1746 | + indices.append(index2.getClosestIndex(normal, vertices)); | |
| 1747 | + indices.append(index1.getClosestIndex(normal, vertices)); | |
| 2670 | 1748 | } else { // quad faces positive x |
| 2671 | − indices.append(index1); | |
| 2672 | − indices.append(index2); | |
| 2673 | − indices.append(index3); | |
| 1749 | + indices.append(index1.getClosestIndex(normal, vertices)); | |
| 1750 | + indices.append(index2.getClosestIndex(normal, vertices)); | |
| 1751 | + indices.append(index3.getClosestIndex(normal, vertices)); | |
| 2674 | 1752 | } |
| 1753 | + indices.append(index.getClosestIndex(normal, vertices)); | |
| 2675 | 1754 | } |
| 2676 | 1755 | |
| 2677 | 1756 | if (alpha0 != alpha2) { |
| @@ -2679,19 +1758,25 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 1 unmodified line | ||
| 2679 | 1758 | quadIndices.insert(qRgb(x - 1, y, z), indices.size()); |
| 2680 | 1759 | quadIndices.insert(qRgb(x - 1, y, z - 1), indices.size()); |
| 2681 | 1760 | quadIndices.insert(qRgb(x, y, z - 1), indices.size()); |
| 2682 | − indices.append(index.y); | |
| 2683 | − int index1 = lastIndex.y; | |
| 2684 | − int index2 = lastPlaneIndices.at(y * expanded + x - 1).y; | |
| 2685 | − int index3 = lastPlaneIndices.at(y * expanded + x).y; | |
| 1761 | + | |
| 1762 | + const NormalIndex& index1 = lastIndex; | |
| 1763 | + const NormalIndex& index2 = lastPlaneIndices.at(y * expanded + x - 1); | |
| 1764 | + const NormalIndex& index3 = lastPlaneIndices.at(y * expanded + x); | |
| 1765 | + | |
| 1766 | + const glm::vec3& first = vertices.at(index.indices[0]).vertex; | |
| 1767 | + glm::vec3 normal = glm::cross(vertices.at(index3.indices[0]).vertex - first, | |
| 1768 | + vertices.at(index1.indices[0]).vertex - first); | |
| 1769 | + | |
| 2686 | 1770 | if (alpha0 == 0) { // quad faces negative y |
| 2687 | − indices.append(index1); | |
| 2688 | − indices.append(index2); | |
| 2689 | − indices.append(index3); | |
| 1771 | + indices.append(index1.getClosestIndex(normal = -normal, vertices)); | |
| 1772 | + indices.append(index2.getClosestIndex(normal, vertices)); | |
| 1773 | + indices.append(index3.getClosestIndex(normal, vertices)); | |
| 2690 | 1774 | } else { // quad faces positive y |
| 2691 | − indices.append(index3); | |
| 2692 | − indices.append(index2); | |
| 2693 | − indices.append(index1); | |
| 1775 | + indices.append(index3.getClosestIndex(normal, vertices)); | |
| 1776 | + indices.append(index2.getClosestIndex(normal, vertices)); | |
| 1777 | + indices.append(index1.getClosestIndex(normal, vertices)); | |
| 2694 | 1778 | } |
| 1779 | + indices.append(index.getClosestIndex(normal, vertices)); | |
| 2695 | 1780 | } |
| 2696 | 1781 | |
| 2697 | 1782 | if (alpha0 != alpha4) { |
| @@ -2699,19 +1784,25 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 1 unmodified line | ||
| 2699 | 1784 | quadIndices.insert(qRgb(x - 1, y, z), indices.size()); |
| 2700 | 1785 | quadIndices.insert(qRgb(x - 1, y - 1, z), indices.size()); |
| 2701 | 1786 | quadIndices.insert(qRgb(x, y - 1, z), indices.size()); |
| 2702 | − indices.append(index.z); | |
| 2703 | − int index1 = lastIndex.z; | |
| 2704 | − int index2 = lastLineIndices.at(x - 1).z; | |
| 2705 | − int index3 = lastLineIndices.at(x).z; | |
| 1787 | + | |
| 1788 | + const NormalIndex& index1 = lastIndex; | |
| 1789 | + const NormalIndex& index2 = lastLineIndices.at(x - 1); | |
| 1790 | + const NormalIndex& index3 = lastLineIndices.at(x); | |
| 1791 | + | |
| 1792 | + const glm::vec3& first = vertices.at(index.indices[0]).vertex; | |
| 1793 | + glm::vec3 normal = glm::cross(vertices.at(index1.indices[0]).vertex - first, | |
| 1794 | + vertices.at(index3.indices[0]).vertex - first); | |
| 1795 | + | |
| 2706 | 1796 | if (alpha0 == 0) { // quad faces negative z |
| 2707 | − indices.append(index3); | |
| 2708 | − indices.append(index2); | |
| 2709 | − indices.append(index1); | |
| 1797 | + indices.append(index3.getClosestIndex(normal = -normal, vertices)); | |
| 1798 | + indices.append(index2.getClosestIndex(normal, vertices)); | |
| 1799 | + indices.append(index1.getClosestIndex(normal, vertices)); | |
| 2710 | 1800 | } else { // quad faces positive z |
| 2711 | − indices.append(index1); | |
| 2712 | − indices.append(index2); | |
| 2713 | − indices.append(index3); | |
| 1801 | + indices.append(index1.getClosestIndex(normal, vertices)); | |
| 1802 | + indices.append(index2.getClosestIndex(normal, vertices)); | |
| 1803 | + indices.append(index3.getClosestIndex(normal, vertices)); | |
| 2714 | 1804 | } |
| 1805 | + indices.append(index.getClosestIndex(normal, vertices)); | |
| 2715 | 1806 | } |
| 2716 | 1807 | } |
| 2717 | 1808 | lastIndex = index; |
| @@ -2735,7 +1826,7 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 17 unmodified lines | ||
| 2735 | 1826 | } |
| 2736 | 1827 | } |
| 2737 | 1828 | buffer = new VoxelBuffer(vertices, indices, hermiteSegments, quadIndices, size, |
| 2738 | − material ? material->getMaterials() : QVector<SharedObjectPointer>()); | |
| 1829 | + material ? material->getMaterials() : QVector<SharedObjectPointer>()); | |
| 2739 | 1830 | } |
| 2740 | 1831 | BufferDataPointer pointer(buffer); |
| 2741 | 1832 | info.outputValues[0] = AttributeValue(_outputs.at(0), encodeInline(pointer)); |
| @@ -2743,43 +1834,19 @@ int VoxelAugmentVisitor::visit(MetavoxelInfo& info) { · 1 unmodified line | ||
| 2743 | 1834 | } |
| 2744 | 1835 | |
| 2745 | 1836 | void DefaultMetavoxelRendererImplementation::augment(MetavoxelData& data, const MetavoxelData& previous, |
| 2746 | − MetavoxelInfo& info, const MetavoxelLOD& lod) { | |
| 1837 | + MetavoxelInfo& info, const MetavoxelLOD& lod) { | |
| 2747 | 1838 | // copy the previous buffers |
| 2748 | 1839 | MetavoxelData expandedPrevious = previous; |
| 2749 | 1840 | while (expandedPrevious.getSize() < data.getSize()) { |
| 2750 | 1841 | expandedPrevious.expand(); |
| 2751 | 1842 | } |
| 2752 | − const AttributePointer& pointBufferAttribute = Application::getInstance()->getMetavoxels()->getPointBufferAttribute(); | |
| 2753 | − MetavoxelNode* root = expandedPrevious.getRoot(pointBufferAttribute); | |
| 2754 | − if (root) { | |
| 2755 | − data.setRoot(pointBufferAttribute, root); | |
| 2756 | − root->incrementReferenceCount(); | |
| 2757 | − } | |
| 2758 | − const AttributePointer& heightfieldBufferAttribute = | |
| 2759 | − Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute(); | |
| 2760 | − root = expandedPrevious.getRoot(heightfieldBufferAttribute); | |
| 2761 | − if (root) { | |
| 2762 | − data.setRoot(heightfieldBufferAttribute, root); | |
| 2763 | − root->incrementReferenceCount(); | |
| 2764 | − } | |
| 2765 | 1843 | const AttributePointer& voxelBufferAttribute = |
| 2766 | − Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute(); | |
| 2767 | − root = expandedPrevious.getRoot(voxelBufferAttribute); | |
| 1844 | + Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute(); | |
| 1845 | + MetavoxelNode* root = expandedPrevious.getRoot(voxelBufferAttribute); | |
| 2768 | 1846 | if (root) { |
| 2769 | 1847 | data.setRoot(voxelBufferAttribute, root); |
| 2770 | 1848 | root->incrementReferenceCount(); |
| 2771 | 1849 | } |
| 2772 | − | |
| 2773 | − PointAugmentVisitor pointAugmentVisitor(lod); | |
| 2774 | − data.guideToDifferent(expandedPrevious, pointAugmentVisitor); | |
| 2775 | − | |
| 2776 | − HeightfieldRegionVisitor heightfieldRegionVisitor(lod); | |
| 2777 | − data.guideToDifferent(expandedPrevious, heightfieldRegionVisitor); | |
| 2778 | − | |
| 2779 | − HeightfieldUpdateVisitor heightfieldUpdateVisitor(lod, heightfieldRegionVisitor.regions, | |
| 2780 | − heightfieldRegionVisitor.regionBounds); | |
| 2781 | − data.guide(heightfieldUpdateVisitor); | |
| 2782 | − | |
| 2783 | 1850 | VoxelAugmentVisitor voxelAugmentVisitor(lod); |
| 2784 | 1851 | data.guideToDifferent(expandedPrevious, voxelAugmentVisitor); |
| 2785 | 1852 | } |
| @@ -2789,26 +1856,26 @@ public: · 3 unmodified lines | ||
| 2789 | 1856 | |
| 2790 | 1857 | SpannerSimulateVisitor(float deltaTime, const MetavoxelLOD& lod); |
| 2791 | 1858 | |
| 2792 | − virtual bool visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize); | |
| 2793 | − | |
| 1859 | + virtual bool visit(Spanner* spanner); | |
| 1860 | + | |
| 2794 | 1861 | private: |
| 2795 | 1862 | |
| 2796 | 1863 | float _deltaTime; |
| 2797 | 1864 | }; |
| 2798 | 1865 | |
| 2799 | 1866 | SpannerSimulateVisitor::SpannerSimulateVisitor(float deltaTime, const MetavoxelLOD& lod) : |
| 2800 | − SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(), | |
| 2801 | − QVector<AttributePointer>(), QVector<AttributePointer>(), QVector<AttributePointer>(), lod), | |
| 2802 | − _deltaTime(deltaTime) { | |
| 1867 | +SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(), | |
| 1868 | + QVector<AttributePointer>(), QVector<AttributePointer>(), lod), | |
| 1869 | +_deltaTime(deltaTime) { | |
| 2803 | 1870 | } |
| 2804 | 1871 | |
| 2805 | −bool SpannerSimulateVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) { | |
| 1872 | +bool SpannerSimulateVisitor::visit(Spanner* spanner) { | |
| 2806 | 1873 | spanner->getRenderer()->simulate(_deltaTime); |
| 2807 | 1874 | return true; |
| 2808 | 1875 | } |
| 2809 | 1876 | |
| 2810 | 1877 | void DefaultMetavoxelRendererImplementation::simulate(MetavoxelData& data, float deltaTime, |
| 2811 | − MetavoxelInfo& info, const MetavoxelLOD& lod) { | |
| 1878 | + MetavoxelInfo& info, const MetavoxelLOD& lod) { | |
| 2812 | 1879 | SpannerSimulateVisitor spannerSimulateVisitor(deltaTime, lod); |
| 2813 | 1880 | data.guide(spannerSimulateVisitor); |
| 2814 | 1881 | } |
| @@ -2819,24 +1886,24 @@ public: · 4 unmodified lines | ||
| 2819 | 1886 | SpannerRenderVisitor(const MetavoxelLOD& lod); |
| 2820 | 1887 | |
| 2821 | 1888 | virtual int visit(MetavoxelInfo& info); |
| 2822 | − virtual bool visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize); | |
| 2823 | − | |
| 1889 | + virtual bool visit(Spanner* spanner); | |
| 1890 | + | |
| 2824 | 1891 | private: |
| 2825 | 1892 | |
| 2826 | 1893 | int _containmentDepth; |
| 2827 | 1894 | }; |
| 2828 | 1895 | |
| 2829 | 1896 | SpannerRenderVisitor::SpannerRenderVisitor(const MetavoxelLOD& lod) : |
| 2830 | − SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(), | |
| 2831 | − QVector<AttributePointer>(), QVector<AttributePointer>(), QVector<AttributePointer>(), | |
| 2832 | − lod, encodeOrder(Application::getInstance()->getDisplayViewFrustum()->getDirection())), | |
| 2833 | − _containmentDepth(INT_MAX) { | |
| 1897 | +SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(), | |
| 1898 | + QVector<AttributePointer>(), QVector<AttributePointer>(), lod, | |
| 1899 | + encodeOrder(Application::getInstance()->getDisplayViewFrustum()->getDirection())), | |
| 1900 | +_containmentDepth(INT_MAX) { | |
| 2834 | 1901 | } |
| 2835 | 1902 | |
| 2836 | 1903 | int SpannerRenderVisitor::visit(MetavoxelInfo& info) { |
| 2837 | 1904 | if (_containmentDepth >= _depth) { |
| 2838 | 1905 | Frustum::IntersectionType intersection = Application::getInstance()->getMetavoxels()->getFrustum().getIntersectionType( |
| 2839 | − info.getBounds()); | |
| 1906 | + info.getBounds()); | |
| 2840 | 1907 | if (intersection == Frustum::NO_INTERSECTION) { |
| 2841 | 1908 | return STOP_RECURSION; |
| 2842 | 1909 | } |
| @@ -2845,9 +1912,8 @@ int SpannerRenderVisitor::visit(MetavoxelInfo& info) { · 2 unmodified lines | ||
| 2845 | 1912 | return SpannerVisitor::visit(info); |
| 2846 | 1913 | } |
| 2847 | 1914 | |
| 2848 | −bool SpannerRenderVisitor::visit(Spanner* spanner, const glm::vec3& clipMinimum, float clipSize) { | |
| 2849 | − const glm::vec4 OPAQUE_WHITE(1.0f, 1.0f, 1.0f, 1.0f); | |
| 2850 | − spanner->getRenderer()->render(OPAQUE_WHITE, SpannerRenderer::DEFAULT_MODE, clipMinimum, clipSize); | |
| 1915 | +bool SpannerRenderVisitor::visit(Spanner* spanner) { | |
| 1916 | + spanner->getRenderer()->render(); | |
| 2851 | 1917 | return true; |
| 2852 | 1918 | } |
| 2853 | 1919 | |
| @@ -2857,7 +1923,7 @@ public: · 3 unmodified lines | ||
| 2857 | 1923 | BufferRenderVisitor(const AttributePointer& attribute); |
| 2858 | 1924 | |
| 2859 | 1925 | virtual int visit(MetavoxelInfo& info); |
| 2860 | − | |
| 1926 | + | |
| 2861 | 1927 | private: |
| 2862 | 1928 | |
| 2863 | 1929 | int _order; |
| @@ -2865,15 +1931,15 @@ private: · 1 unmodified line | ||
| 2865 | 1931 | }; |
| 2866 | 1932 | |
| 2867 | 1933 | BufferRenderVisitor::BufferRenderVisitor(const AttributePointer& attribute) : |
| 2868 | − MetavoxelVisitor(QVector<AttributePointer>() << attribute), | |
| 2869 | − _order(encodeOrder(Application::getInstance()->getDisplayViewFrustum()->getDirection())), | |
| 2870 | − _containmentDepth(INT_MAX) { | |
| 1934 | +MetavoxelVisitor(QVector<AttributePointer>() << attribute), | |
| 1935 | +_order(encodeOrder(Application::getInstance()->getDisplayViewFrustum()->getDirection())), | |
| 1936 | +_containmentDepth(INT_MAX) { | |
| 2871 | 1937 | } |
| 2872 | 1938 | |
| 2873 | 1939 | int BufferRenderVisitor::visit(MetavoxelInfo& info) { |
| 2874 | 1940 | if (_containmentDepth >= _depth) { |
| 2875 | 1941 | Frustum::IntersectionType intersection = Application::getInstance()->getMetavoxels()->getFrustum().getIntersectionType( |
| 2876 | − info.getBounds()); | |
| 1942 | + info.getBounds()); | |
| 2877 | 1943 | if (intersection == Frustum::NO_INTERSECTION) { |
| 2878 | 1944 | return STOP_RECURSION; |
| 2879 | 1945 | } |
| @@ -2890,175 +1956,24 @@ int BufferRenderVisitor::visit(MetavoxelInfo& info) { · 10 unmodified lines | ||
| 2890 | 1956 | } |
| 2891 | 1957 | |
| 2892 | 1958 | void DefaultMetavoxelRendererImplementation::render(MetavoxelData& data, MetavoxelInfo& info, const MetavoxelLOD& lod) { |
| 2893 | − SpannerRenderVisitor spannerRenderVisitor(lod); | |
| 2894 | − data.guide(spannerRenderVisitor); | |
| 2895 | − | |
| 2896 | − int viewport[4]; | |
| 2897 | − glGetIntegerv(GL_VIEWPORT, viewport); | |
| 2898 | − const int VIEWPORT_WIDTH_INDEX = 2; | |
| 2899 | − const int VIEWPORT_HEIGHT_INDEX = 3; | |
| 2900 | − float viewportWidth = viewport[VIEWPORT_WIDTH_INDEX]; | |
| 2901 | − float viewportHeight = viewport[VIEWPORT_HEIGHT_INDEX]; | |
| 2902 | − float viewportDiagonal = sqrtf(viewportWidth * viewportWidth + viewportHeight * viewportHeight); | |
| 2903 | − float worldDiagonal = glm::distance(Application::getInstance()->getDisplayViewFrustum()->getNearBottomLeft(), | |
| 2904 | − Application::getInstance()->getDisplayViewFrustum()->getNearTopRight()); | |
| 2905 | − | |
| 2906 | − _pointProgram.bind(); | |
| 2907 | − _pointProgram.setUniformValue(_pointScaleLocation, viewportDiagonal * | |
| 2908 | − Application::getInstance()->getDisplayViewFrustum()->getNearClip() / worldDiagonal); | |
| 2909 | − | |
| 2910 | − glEnableClientState(GL_VERTEX_ARRAY); | |
| 2911 | − glEnableClientState(GL_COLOR_ARRAY); | |
| 2912 | − glEnableClientState(GL_NORMAL_ARRAY); | |
| 2913 | − | |
| 2914 | − glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB); | |
| 2915 | − | |
| 2916 | − glDisable(GL_BLEND); | |
| 2917 | − | |
| 2918 | − BufferRenderVisitor pointRenderVisitor(Application::getInstance()->getMetavoxels()->getPointBufferAttribute()); | |
| 2919 | − data.guide(pointRenderVisitor); | |
| 2920 | − | |
| 2921 | − glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB); | |
| 2922 | − | |
| 2923 | − glDisableClientState(GL_COLOR_ARRAY); | |
| 2924 | − glDisableClientState(GL_NORMAL_ARRAY); | |
| 2925 | − | |
| 2926 | − _pointProgram.release(); | |
| 2927 | − | |
| 2928 | − Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, true); | |
| 2929 | − | |
| 2930 | − glEnable(GL_CULL_FACE); | |
| 2931 | − glEnable(GL_ALPHA_TEST); | |
| 2932 | − glAlphaFunc(GL_EQUAL, 0.0f); | |
| 2933 | − | |
| 2934 | − glColor4f(1.0f, 1.0f, 1.0f, 1.0f); | |
| 2935 | − | |
| 2936 | − if (Menu::getInstance()->isOptionChecked(MenuOption::RenderHeightfields)) { | |
| 2937 | − _baseHeightfieldProgram.bind(); | |
| 2938 | − | |
| 2939 | − glEnableClientState(GL_TEXTURE_COORD_ARRAY); | |
| 2940 | − | |
| 2941 | − BufferRenderVisitor heightfieldRenderVisitor(Application::getInstance()->getMetavoxels()->getHeightfieldBufferAttribute()); | |
| 2942 | − data.guide(heightfieldRenderVisitor); | |
| 2943 | − | |
| 2944 | − glDisableClientState(GL_TEXTURE_COORD_ARRAY); | |
| 2945 | − | |
| 2946 | − _baseHeightfieldProgram.release(); | |
| 1959 | + if (Menu::getInstance()->isOptionChecked(MenuOption::RenderSpanners)) { | |
| 1960 | + SpannerRenderVisitor spannerRenderVisitor(lod); | |
| 1961 | + data.guide(spannerRenderVisitor); | |
| 2947 | 1962 | } |
| 2948 | − | |
| 2949 | 1963 | if (Menu::getInstance()->isOptionChecked(MenuOption::RenderDualContourSurfaces)) { |
| 2950 | − glEnableClientState(GL_COLOR_ARRAY); | |
| 2951 | − glEnableClientState(GL_NORMAL_ARRAY); | |
| 2952 | − | |
| 2953 | − _baseVoxelProgram.bind(); | |
| 2954 | − | |
| 2955 | 1964 | BufferRenderVisitor voxelRenderVisitor(Application::getInstance()->getMetavoxels()->getVoxelBufferAttribute()); |
| 2956 | 1965 | data.guide(voxelRenderVisitor); |
| 2957 | − | |
| 2958 | − _baseVoxelProgram.release(); | |
| 2959 | − | |
| 2960 | − glDisableClientState(GL_COLOR_ARRAY); | |
| 2961 | − glDisableClientState(GL_NORMAL_ARRAY); | |
| 2962 | − } | |
| 2963 | − | |
| 2964 | − glDisable(GL_ALPHA_TEST); | |
| 2965 | − glDisable(GL_CULL_FACE); | |
| 2966 | − glEnable(GL_BLEND); | |
| 2967 | − | |
| 2968 | − glDisableClientState(GL_VERTEX_ARRAY); | |
| 2969 | − | |
| 2970 | − Application::getInstance()->getTextureCache()->setPrimaryDrawBuffers(true, false); | |
| 2971 | −} | |
| 2972 | − | |
| 2973 | −void DefaultMetavoxelRendererImplementation::loadSplatProgram(const char* type, | |
| 2974 | − ProgramObject& program, SplatLocations& locations) { | |
| 2975 | − program.addShaderFromSourceFile(QGLShader::Vertex, Application::resourcesPath() + | |
| 2976 | − "shaders/metavoxel_" + type + "_splat.vert"); | |
| 2977 | − program.addShaderFromSourceFile(QGLShader::Fragment, Application::resourcesPath() + | |
| 2978 | − "shaders/metavoxel_" + type + "_splat.frag"); | |
| 2979 | − program.link(); | |
| 2980 | − | |
| 2981 | − program.bind(); | |
| 2982 | − program.setUniformValue("heightMap", 0); | |
| 2983 | − program.setUniformValue("textureMap", 1); | |
| 2984 | − program.setUniformValueArray("diffuseMaps", SPLAT_TEXTURE_UNITS, SPLAT_COUNT); | |
| 2985 | − locations.heightScale = program.uniformLocation("heightScale"); | |
| 2986 | − locations.textureScale = program.uniformLocation("textureScale"); | |
| 2987 | − locations.splatTextureOffset = program.uniformLocation("splatTextureOffset"); | |
| 2988 | − locations.splatTextureScalesS = program.uniformLocation("splatTextureScalesS"); | |
| 2989 | − locations.splatTextureScalesT = program.uniformLocation("splatTextureScalesT"); | |
| 2990 | − locations.textureValueMinima = program.uniformLocation("textureValueMinima"); | |
| 2991 | − locations.textureValueMaxima = program.uniformLocation("textureValueMaxima"); | |
| 2992 | − locations.materials = program.attributeLocation("materials"); | |
| 2993 | − locations.materialWeights = program.attributeLocation("materialWeights"); | |
| 2994 | − program.release(); | |
| 2995 | −} | |
| 2996 | − | |
| 2997 | −ProgramObject DefaultMetavoxelRendererImplementation::_pointProgram; | |
| 2998 | −int DefaultMetavoxelRendererImplementation::_pointScaleLocation; | |
| 2999 | −ProgramObject DefaultMetavoxelRendererImplementation::_baseHeightfieldProgram; | |
| 3000 | −int DefaultMetavoxelRendererImplementation::_baseHeightScaleLocation; | |
| 3001 | −int DefaultMetavoxelRendererImplementation::_baseColorScaleLocation; | |
| 3002 | −ProgramObject DefaultMetavoxelRendererImplementation::_splatHeightfieldProgram; | |
| 3003 | −DefaultMetavoxelRendererImplementation::SplatLocations DefaultMetavoxelRendererImplementation::_splatHeightfieldLocations; | |
| 3004 | −ProgramObject DefaultMetavoxelRendererImplementation::_heightfieldCursorProgram; | |
| 3005 | −ProgramObject DefaultMetavoxelRendererImplementation::_baseVoxelProgram; | |
| 3006 | −ProgramObject DefaultMetavoxelRendererImplementation::_splatVoxelProgram; | |
| 3007 | −DefaultMetavoxelRendererImplementation::SplatLocations DefaultMetavoxelRendererImplementation::_splatVoxelLocations; | |
| 3008 | −ProgramObject DefaultMetavoxelRendererImplementation::_voxelCursorProgram; | |
| 3009 | − | |
| 3010 | −static void enableClipPlane(GLenum plane, float x, float y, float z, float w) { | |
| 3011 | − GLdouble coefficients[] = { x, y, z, w }; | |
| 3012 | − glClipPlane(plane, coefficients); | |
| 3013 | − glEnable(plane); | |
| 3014 | −} | |
| 3015 | − | |
| 3016 | −void ClippedRenderer::render(const glm::vec4& color, Mode mode, const glm::vec3& clipMinimum, float clipSize) { | |
| 3017 | − if (clipSize == 0.0f) { | |
| 3018 | − renderUnclipped(color, mode); | |
| 3019 | − return; | |
| 3020 | 1966 | } |
| 3021 | − enableClipPlane(GL_CLIP_PLANE0, -1.0f, 0.0f, 0.0f, clipMinimum.x + clipSize); | |
| 3022 | − enableClipPlane(GL_CLIP_PLANE1, 1.0f, 0.0f, 0.0f, -clipMinimum.x); | |
| 3023 | − enableClipPlane(GL_CLIP_PLANE2, 0.0f, -1.0f, 0.0f, clipMinimum.y + clipSize); | |
| 3024 | − enableClipPlane(GL_CLIP_PLANE3, 0.0f, 1.0f, 0.0f, -clipMinimum.y); | |
| 3025 | − enableClipPlane(GL_CLIP_PLANE4, 0.0f, 0.0f, -1.0f, clipMinimum.z + clipSize); | |
| 3026 | − enableClipPlane(GL_CLIP_PLANE5, 0.0f, 0.0f, 1.0f, -clipMinimum.z); | |
| 3027 | − | |
| 3028 | − renderUnclipped(color, mode); | |
| 3029 | − | |
| 3030 | − glDisable(GL_CLIP_PLANE0); | |
| 3031 | − glDisable(GL_CLIP_PLANE1); | |
| 3032 | − glDisable(GL_CLIP_PLANE2); | |
| 3033 | − glDisable(GL_CLIP_PLANE3); | |
| 3034 | − glDisable(GL_CLIP_PLANE4); | |
| 3035 | − glDisable(GL_CLIP_PLANE5); | |
| 3036 | 1967 | } |
| 3037 | 1968 | |
| 3038 | 1969 | SphereRenderer::SphereRenderer() { |
| 3039 | 1970 | } |
| 3040 | 1971 | |
| 3041 | −void SphereRenderer::render(const glm::vec4& color, Mode mode, const glm::vec3& clipMinimum, float clipSize) { | |
| 3042 | − if (clipSize == 0.0f) { | |
| 3043 | − renderUnclipped(color, mode); | |
| 3044 | − return; | |
| 3045 | − } | |
| 3046 | − // slight performance optimization: don't render if clip bounds are entirely within sphere | |
| 3047 | − Sphere* sphere = static_cast<Sphere*>(_spanner); | |
| 3048 | − Box clipBox(clipMinimum, clipMinimum + glm::vec3(clipSize, clipSize, clipSize)); | |
| 3049 | − for (int i = 0; i < Box::VERTEX_COUNT; i++) { | |
| 3050 | − const float CLIP_PROPORTION = 0.95f; | |
| 3051 | − if (glm::distance(sphere->getTranslation(), clipBox.getVertex(i)) >= sphere->getScale() * CLIP_PROPORTION) { | |
| 3052 | − ClippedRenderer::render(color, mode, clipMinimum, clipSize); | |
| 3053 | − return; | |
| 3054 | − } | |
| 3055 | − } | |
| 3056 | −} | |
| 3057 | 1972 | |
| 3058 | −void SphereRenderer::renderUnclipped(const glm::vec4& color, Mode mode) { | |
| 1973 | +void SphereRenderer::render(bool cursor) { | |
| 3059 | 1974 | Sphere* sphere = static_cast<Sphere*>(_spanner); |
| 3060 | − const QColor& ownColor = sphere->getColor(); | |
| 3061 | − glColor4f(ownColor.redF() * color.r, ownColor.greenF() * color.g, ownColor.blueF() * color.b, ownColor.alphaF() * color.a); | |
| 1975 | + const QColor& color = sphere->getColor(); | |
| 1976 | + glColor4f(color.redF(), color.greenF(), color.blueF(), color.alphaF()); | |
| 3062 | 1977 | |
| 3063 | 1978 | glPushMatrix(); |
| 3064 | 1979 | const glm::vec3& translation = sphere->getTranslation(); |
| @@ -3067,7 +1982,7 @@ void SphereRenderer::renderUnclipped(const glm::vec4& color, Mode mode) { · 2 unmodified lines | ||
| 3067 | 1982 | glm::vec3 axis = glm::axis(rotation); |
| 3068 | 1983 | glRotatef(glm::degrees(glm::angle(rotation)), axis.x, axis.y, axis.z); |
| 3069 | 1984 | |
| 3070 | − Application::getInstance()->getGeometryCache()->renderSphere(sphere->getScale(), 10, 10); | |
| 1985 | + Application::getInstance()->getDeferredLightingEffect()->renderSolidSphere(sphere->getScale(), 32, 32); | |
| 3071 | 1986 | |
| 3072 | 1987 | glPopMatrix(); |
| 3073 | 1988 | } |
| @@ -3075,10 +1990,10 @@ void SphereRenderer::renderUnclipped(const glm::vec4& color, Mode mode) { · 1 unmodified line | ||
| 3075 | 1990 | CuboidRenderer::CuboidRenderer() { |
| 3076 | 1991 | } |
| 3077 | 1992 | |
| 3078 | −void CuboidRenderer::renderUnclipped(const glm::vec4& color, Mode mode) { | |
| 1993 | +void CuboidRenderer::render(bool cursor) { | |
| 3079 | 1994 | Cuboid* cuboid = static_cast<Cuboid*>(_spanner); |
| 3080 | − const QColor& ownColor = cuboid->getColor(); | |
| 3081 | − glColor4f(ownColor.redF() * color.r, ownColor.greenF() * color.g, ownColor.blueF() * color.b, ownColor.alphaF() * color.a); | |
| 1995 | + const QColor& color = cuboid->getColor(); | |
| 1996 | + glColor4f(color.redF(), color.greenF(), color.blueF(), color.alphaF()); | |
| 3082 | 1997 | |
| 3083 | 1998 | glPushMatrix(); |
| 3084 | 1999 | const glm::vec3& translation = cuboid->getTranslation(); |
| @@ -3088,18 +2003,18 @@ void CuboidRenderer::renderUnclipped(const glm::vec4& color, Mode mode) { · 3 unmodified lines | ||
| 3088 | 2003 | glRotatef(glm::degrees(glm::angle(rotation)), axis.x, axis.y, axis.z); |
| 3089 | 2004 | glScalef(1.0f, cuboid->getAspectY(), cuboid->getAspectZ()); |
| 3090 | 2005 | |
| 3091 | − glutSolidCube(cuboid->getScale() * 2.0f); | |
| 2006 | + Application::getInstance()->getDeferredLightingEffect()->renderSolidCube(cuboid->getScale() * 2.0f); | |
| 3092 | 2007 | |
| 3093 | 2008 | glPopMatrix(); |
| 3094 | 2009 | } |
| 3095 | 2010 | |
| 3096 | 2011 | StaticModelRenderer::StaticModelRenderer() : |
| 3097 | − _model(new Model(this)) { | |
| 2012 | +_model(new Model(this)) { | |
| 3098 | 2013 | } |
| 3099 | 2014 | |
| 3100 | 2015 | void StaticModelRenderer::init(Spanner* spanner) { |
| 3101 | 2016 | SpannerRenderer::init(spanner); |
| 3102 | − | |
| 2017 | + | |
| 3103 | 2018 | _model->init(); |
| 3104 | 2019 | |
| 3105 | 2020 | StaticModel* staticModel = static_cast<StaticModel*>(spanner); |
| @@ -3122,29 +2037,15 @@ void StaticModelRenderer::simulate(float deltaTime) { · 16 unmodified lines | ||
| 3122 | 2037 | bounds = Box(extents.minimum, extents.maximum); |
| 3123 | 2038 | } |
| 3124 | 2039 | static_cast<StaticModel*>(_spanner)->setBounds(glm::translate(_model->getTranslation()) * |
| 3125 | − glm::mat4_cast(_model->getRotation()) * glm::scale(_model->getScale()) * bounds); | |
| 2040 | + glm::mat4_cast(_model->getRotation()) * glm::scale(_model->getScale()) * bounds); | |
| 3126 | 2041 | _model->simulate(deltaTime); |
| 3127 | 2042 | } |
| 3128 | 2043 | |
| 3129 | −void StaticModelRenderer::renderUnclipped(const glm::vec4& color, Mode mode) { | |
| 3130 | − switch (mode) { | |
| 3131 | − case DIFFUSE_MODE: | |
| 3132 | − _model->render(color.a, Model::DIFFUSE_RENDER_MODE); | |
| 3133 | − break; | |
| 3134 | − | |
| 3135 | − case NORMAL_MODE: | |
| 3136 | − _model->render(color.a, Model::NORMAL_RENDER_MODE); | |
| 3137 | − break; | |
| 3138 | − | |
| 3139 | − default: | |
| 3140 | − _model->render(color.a); | |
| 3141 | − break; | |
| 3142 | − } | |
| 3143 | − _model->render(color.a); | |
| 2044 | +void StaticModelRenderer::render(bool cursor) { | |
| 2045 | + _model->render(); | |
| 3144 | 2046 | } |
| 3145 | 2047 | |
| 3146 | −bool StaticModelRenderer::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, | |
| 3147 | − const glm::vec3& clipMinimum, float clipSize, float& distance) const { | |
| 2048 | +bool StaticModelRenderer::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, float& distance) const { | |
| 3148 | 2049 | RayIntersectionInfo info; |
| 3149 | 2050 | info._rayStart = origin; |
| 3150 | 2051 | info._rayDirection = direction; |
| @@ -3164,10 +2065,261 @@ void StaticModelRenderer::applyRotation(const glm::quat& rotation) { · 13 unmodified lines | ||
| 3164 | 2065 | } |
| 3165 | 2066 | |
| 3166 | 2067 | void StaticModelRenderer::applyScale(float scale) { |
| 3167 | − const float SCALE_MULTIPLIER = 0.0006f; | |
| 3168 | − _model->setScale(glm::vec3(scale, scale, scale) * SCALE_MULTIPLIER); | |
| 2068 | + _model->setScale(glm::vec3(scale, scale, scale)); | |
| 3169 | 2069 | } |
| 3170 | 2070 | |
| 3171 | 2071 | void StaticModelRenderer::applyURL(const QUrl& url) { |
| 3172 | 2072 | _model->setURL(url); |
| 3173 | 2073 | } |
| 2074 | + | |
| 2075 | +HeightfieldRenderer::HeightfieldRenderer() { | |
| 2076 | + glGenTextures(1, &_heightTextureID); | |
| 2077 | + glBindTexture(GL_TEXTURE_2D, _heightTextureID); | |
| 2078 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
| 2079 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
| 2080 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 2081 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 2082 | + | |
| 2083 | + glGenTextures(1, &_colorTextureID); | |
| 2084 | + glBindTexture(GL_TEXTURE_2D, _colorTextureID); | |
| 2085 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 2086 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 2087 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 2088 | + | |
| 2089 | + glGenTextures(1, &_materialTextureID); | |
| 2090 | + glBindTexture(GL_TEXTURE_2D, _materialTextureID); | |
| 2091 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
| 2092 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
| 2093 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 2094 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 2095 | + | |
| 2096 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 2097 | +} | |
| 2098 | + | |
| 2099 | +HeightfieldRenderer::~HeightfieldRenderer() { | |
| 2100 | + glDeleteTextures(1, &_heightTextureID); | |
| 2101 | + glDeleteTextures(1, &_colorTextureID); | |
| 2102 | + glDeleteTextures(1, &_materialTextureID); | |
| 2103 | +} | |
| 2104 | + | |
| 2105 | +void HeightfieldRenderer::init(Spanner* spanner) { | |
| 2106 | + SpannerRenderer::init(spanner); | |
| 2107 | + | |
| 2108 | + Heightfield* heightfield = static_cast<Heightfield*>(spanner); | |
| 2109 | + applyHeight(heightfield->getHeight()); | |
| 2110 | + applyColor(heightfield->getColor()); | |
| 2111 | + applyMaterial(heightfield->getMaterial()); | |
| 2112 | + | |
| 2113 | + connect(heightfield, &Heightfield::heightChanged, this, &HeightfieldRenderer::applyHeight); | |
| 2114 | + connect(heightfield, &Heightfield::colorChanged, this, &HeightfieldRenderer::applyColor); | |
| 2115 | + connect(heightfield, &Heightfield::materialChanged, this, &HeightfieldRenderer::applyMaterial); | |
| 2116 | +} | |
| 2117 | + | |
| 2118 | +void HeightfieldRenderer::render(bool cursor) { | |
| 2119 | + // create the buffer objects lazily | |
| 2120 | + Heightfield* heightfield = static_cast<Heightfield*>(_spanner); | |
| 2121 | + if (!heightfield->getHeight()) { | |
| 2122 | + return; | |
| 2123 | + } | |
| 2124 | + int width = heightfield->getHeight()->getWidth(); | |
| 2125 | + int height = heightfield->getHeight()->getContents().size() / width; | |
| 2126 | + | |
| 2127 | + int innerWidth = width - 2 * HeightfieldHeight::HEIGHT_BORDER; | |
| 2128 | + int innerHeight = height - 2 * HeightfieldHeight::HEIGHT_BORDER; | |
| 2129 | + int vertexCount = width * height; | |
| 2130 | + int rows = height - 1; | |
| 2131 | + int columns = width - 1; | |
| 2132 | + int indexCount = rows * columns * 3 * 2; | |
| 2133 | + BufferPair& bufferPair = _bufferPairs[IntPair(width, height)]; | |
| 2134 | + if (!bufferPair.first.isCreated()) { | |
| 2135 | + QVector<HeightfieldPoint> vertices(vertexCount); | |
| 2136 | + HeightfieldPoint* point = vertices.data(); | |
| 2137 | + | |
| 2138 | + float xStep = 1.0f / (innerWidth - 1); | |
| 2139 | + float zStep = 1.0f / (innerHeight - 1); | |
| 2140 | + float z = -zStep; | |
| 2141 | + float sStep = 1.0f / width; | |
| 2142 | + float tStep = 1.0f / height; | |
| 2143 | + float t = tStep / 2.0f; | |
| 2144 | + for (int i = 0; i < height; i++, z += zStep, t += tStep) { | |
| 2145 | + float x = -xStep; | |
| 2146 | + float s = sStep / 2.0f; | |
| 2147 | + const float SKIRT_LENGTH = 0.25f; | |
| 2148 | + float baseY = (i == 0 || i == height - 1) ? -SKIRT_LENGTH : 0.0f; | |
| 2149 | + for (int j = 0; j < width; j++, point++, x += xStep, s += sStep) { | |
| 2150 | + point->vertex = glm::vec3(x, (j == 0 || j == width - 1) ? -SKIRT_LENGTH : baseY, z); | |
| 2151 | + point->textureCoord = glm::vec2(s, t); | |
| 2152 | + } | |
| 2153 | + } | |
| 2154 | + | |
| 2155 | + bufferPair.first.setUsagePattern(QOpenGLBuffer::StaticDraw); | |
| 2156 | + bufferPair.first.create(); | |
| 2157 | + bufferPair.first.bind(); | |
| 2158 | + bufferPair.first.allocate(vertices.constData(), vertexCount * sizeof(HeightfieldPoint)); | |
| 2159 | + bufferPair.first.release(); | |
| 2160 | + | |
| 2161 | + QVector<int> indices(indexCount); | |
| 2162 | + int* index = indices.data(); | |
| 2163 | + for (int i = 0; i < rows; i++) { | |
| 2164 | + int lineIndex = i * width; | |
| 2165 | + int nextLineIndex = (i + 1) * width; | |
| 2166 | + for (int j = 0; j < columns; j++) { | |
| 2167 | + *index++ = lineIndex + j; | |
| 2168 | + *index++ = nextLineIndex + j; | |
| 2169 | + *index++ = nextLineIndex + j + 1; | |
| 2170 | + | |
| 2171 | + *index++ = nextLineIndex + j + 1; | |
| 2172 | + *index++ = lineIndex + j + 1; | |
| 2173 | + *index++ = lineIndex + j; | |
| 2174 | + } | |
| 2175 | + } | |
| 2176 | + | |
| 2177 | + bufferPair.second = QOpenGLBuffer(QOpenGLBuffer::IndexBuffer); | |
| 2178 | + bufferPair.second.create(); | |
| 2179 | + bufferPair.second.bind(); | |
| 2180 | + bufferPair.second.allocate(indices.constData(), indexCount * sizeof(int)); | |
| 2181 | + bufferPair.second.release(); | |
| 2182 | + } | |
| 2183 | + | |
| 2184 | + float xScale = heightfield->getScale(), zScale = xScale * heightfield->getAspectZ(); | |
| 2185 | + if (cursor) { | |
| 2186 | + bufferPair.first.bind(); | |
| 2187 | + bufferPair.second.bind(); | |
| 2188 | + | |
| 2189 | + glPushMatrix(); | |
| 2190 | + glTranslatef(heightfield->getTranslation().x, heightfield->getTranslation().y, heightfield->getTranslation().z); | |
| 2191 | + glm::vec3 axis = glm::axis(heightfield->getRotation()); | |
| 2192 | + glRotatef(glm::degrees(glm::angle(heightfield->getRotation())), axis.x, axis.y, axis.z); | |
| 2193 | + glScalef(xScale, xScale * heightfield->getAspectY(), zScale); | |
| 2194 | + | |
| 2195 | + HeightfieldPoint* point = 0; | |
| 2196 | + glVertexPointer(3, GL_FLOAT, sizeof(HeightfieldPoint), &point->vertex); | |
| 2197 | + glTexCoordPointer(2, GL_FLOAT, sizeof(HeightfieldPoint), &point->textureCoord); | |
| 2198 | + | |
| 2199 | + glBindTexture(GL_TEXTURE_2D, _heightTextureID); | |
| 2200 | + | |
| 2201 | + glDrawRangeElements(GL_TRIANGLES, 0, vertexCount - 1, indexCount, GL_UNSIGNED_INT, 0); | |
| 2202 | + | |
| 2203 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 2204 | + | |
| 2205 | + glPopMatrix(); | |
| 2206 | + | |
| 2207 | + bufferPair.first.release(); | |
| 2208 | + bufferPair.second.release(); | |
| 2209 | + return; | |
| 2210 | + } | |
| 2211 | + | |
| 2212 | + HeightfieldBaseLayerBatch baseBatch; | |
| 2213 | + baseBatch.vertexBuffer = &bufferPair.first; | |
| 2214 | + baseBatch.indexBuffer = &bufferPair.second; | |
| 2215 | + baseBatch.translation = heightfield->getTranslation(); | |
| 2216 | + baseBatch.rotation = heightfield->getRotation(); | |
| 2217 | + baseBatch.scale = glm::vec3(xScale, xScale * heightfield->getAspectY(), zScale); | |
| 2218 | + baseBatch.vertexCount = vertexCount; | |
| 2219 | + baseBatch.indexCount = indexCount; | |
| 2220 | + baseBatch.heightTextureID = _heightTextureID; | |
| 2221 | + baseBatch.heightScale = glm::vec4(1.0f / width, 1.0f / height, (innerWidth - 1) / -2.0f, (innerHeight - 1) / -2.0f); | |
| 2222 | + baseBatch.colorTextureID = _colorTextureID; | |
| 2223 | + baseBatch.colorScale = glm::vec2((float)width / innerWidth, (float)height / innerHeight); | |
| 2224 | + Application::getInstance()->getMetavoxels()->addHeightfieldBaseBatch(baseBatch); | |
| 2225 | + | |
| 2226 | + if (heightfield->getMaterial() && !_networkTextures.isEmpty()) { | |
| 2227 | + HeightfieldSplatBatch splatBatch; | |
| 2228 | + splatBatch.vertexBuffer = &bufferPair.first; | |
| 2229 | + splatBatch.indexBuffer = &bufferPair.second; | |
| 2230 | + splatBatch.translation = heightfield->getTranslation(); | |
| 2231 | + splatBatch.rotation = heightfield->getRotation(); | |
| 2232 | + splatBatch.scale = glm::vec3(xScale, xScale * heightfield->getAspectY(), zScale); | |
| 2233 | + splatBatch.vertexCount = vertexCount; | |
| 2234 | + splatBatch.indexCount = indexCount; | |
| 2235 | + splatBatch.heightTextureID = _heightTextureID; | |
| 2236 | + splatBatch.heightScale = glm::vec4(1.0f / width, 1.0f / height, 0.0f, 0.0f); | |
| 2237 | + splatBatch.materialTextureID = _materialTextureID; | |
| 2238 | + splatBatch.textureScale = glm::vec2((float)width / innerWidth, (float)height / innerHeight); | |
| 2239 | + splatBatch.splatTextureOffset = glm::vec2( | |
| 2240 | + glm::dot(heightfield->getTranslation(), heightfield->getRotation() * glm::vec3(1.0f, 0.0f, 0.0f)) / xScale, | |
| 2241 | + glm::dot(heightfield->getTranslation(), heightfield->getRotation() * glm::vec3(0.0f, 0.0f, 1.0f)) / zScale); | |
| 2242 | + | |
| 2243 | + const QVector<SharedObjectPointer>& materials = heightfield->getMaterial()->getMaterials(); | |
| 2244 | + for (int i = 0; i < materials.size(); i += SPLAT_COUNT) { | |
| 2245 | + for (int j = 0; j < SPLAT_COUNT; j++) { | |
| 2246 | + int index = i + j; | |
| 2247 | + if (index < _networkTextures.size()) { | |
| 2248 | + const NetworkTexturePointer& texture = _networkTextures.at(index); | |
| 2249 | + if (texture) { | |
| 2250 | + MaterialObject* material = static_cast<MaterialObject*>(materials.at(index).data()); | |
| 2251 | + splatBatch.splatTextureScalesS[j] = xScale / material->getScaleS(); | |
| 2252 | + splatBatch.splatTextureScalesT[j] = zScale / material->getScaleT(); | |
| 2253 | + splatBatch.splatTextureIDs[j] = texture->getID(); | |
| 2254 | + | |
| 2255 | + } else { | |
| 2256 | + splatBatch.splatTextureIDs[j] = 0; | |
| 2257 | + } | |
| 2258 | + } else { | |
| 2259 | + splatBatch.splatTextureIDs[j] = 0; | |
| 2260 | + } | |
| 2261 | + } | |
| 2262 | + splatBatch.materialIndex = i; | |
| 2263 | + Application::getInstance()->getMetavoxels()->addHeightfieldSplatBatch(splatBatch); | |
| 2264 | + } | |
| 2265 | + } | |
| 2266 | +} | |
| 2267 | + | |
| 2268 | +void HeightfieldRenderer::applyHeight(const HeightfieldHeightPointer& height) { | |
| 2269 | + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | |
| 2270 | + glBindTexture(GL_TEXTURE_2D, _heightTextureID); | |
| 2271 | + if (height) { | |
| 2272 | + const QVector<quint16>& contents = height->getContents(); | |
| 2273 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_R16, height->getWidth(), contents.size() / height->getWidth(), 0, | |
| 2274 | + GL_RED, GL_UNSIGNED_SHORT, contents.constData()); | |
| 2275 | + | |
| 2276 | + } else { | |
| 2277 | + const quint16 ZERO_VALUE = 0; | |
| 2278 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_R16, 1, 1, 0, GL_RED, GL_UNSIGNED_SHORT, &ZERO_VALUE); | |
| 2279 | + } | |
| 2280 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 2281 | +} | |
| 2282 | + | |
| 2283 | +void HeightfieldRenderer::applyColor(const HeightfieldColorPointer& color) { | |
| 2284 | + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | |
| 2285 | + glBindTexture(GL_TEXTURE_2D, _colorTextureID); | |
| 2286 | + if (color) { | |
| 2287 | + const QByteArray& contents = color->getContents(); | |
| 2288 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, color->getWidth(), | |
| 2289 | + contents.size() / (color->getWidth() * DataBlock::COLOR_BYTES), 0, GL_RGB, GL_UNSIGNED_BYTE, contents.constData()); | |
| 2290 | + | |
| 2291 | + } else { | |
| 2292 | + const quint8 WHITE_COLOR[] = { 255, 255, 255 }; | |
| 2293 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, WHITE_COLOR); | |
| 2294 | + } | |
| 2295 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 2296 | +} | |
| 2297 | + | |
| 2298 | +void HeightfieldRenderer::applyMaterial(const HeightfieldMaterialPointer& material) { | |
| 2299 | + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); | |
| 2300 | + glBindTexture(GL_TEXTURE_2D, _materialTextureID); | |
| 2301 | + if (material) { | |
| 2302 | + const QByteArray& contents = material->getContents(); | |
| 2303 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, material->getWidth(), contents.size() / material->getWidth(), 0, | |
| 2304 | + GL_RED, GL_UNSIGNED_BYTE, contents.constData()); | |
| 2305 | + | |
| 2306 | + const QVector<SharedObjectPointer>& materials = material->getMaterials(); | |
| 2307 | + _networkTextures.resize(materials.size()); | |
| 2308 | + for (int i = 0; i < materials.size(); i++) { | |
| 2309 | + const SharedObjectPointer& material = materials.at(i); | |
| 2310 | + if (material) { | |
| 2311 | + _networkTextures[i] = Application::getInstance()->getTextureCache()->getTexture( | |
| 2312 | + static_cast<MaterialObject*>(material.data())->getDiffuse(), SPLAT_TEXTURE); | |
| 2313 | + } else { | |
| 2314 | + _networkTextures[i].clear(); | |
| 2315 | + } | |
| 2316 | + } | |
| 2317 | + } else { | |
| 2318 | + const quint8 ZERO_VALUE = 0; | |
| 2319 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 1, 1, 0, GL_RED, GL_UNSIGNED_BYTE, &ZERO_VALUE); | |
| 2320 | + _networkTextures.clear(); | |
| 2321 | + } | |
| 2322 | + glBindTexture(GL_TEXTURE_2D, 0); | |
| 2323 | +} | |
| 2324 | + | |
| 2325 | +QHash<HeightfieldRenderer::IntPair, HeightfieldRenderer::BufferPair> HeightfieldRenderer::_bufferPairs; | |