Wait until each LOD level is actually requested before we start loading;

before a level is loaded, try to use the closest already-loaded level.
This commit is contained in:
Andrzej Kapolka 2014-02-13 14:25:01 -08:00
parent 684a43da64
commit ff01470850
2 changed files with 36 additions and 3 deletions

View file

@ -308,13 +308,18 @@ NetworkGeometry::NetworkGeometry(const QUrl& url, const QSharedPointer<NetworkGe
_textureBase(textureBase.isValid() ? textureBase : url),
_fallback(fallback),
_attempts(0),
_startedLoading(false),
_failedToLoad(false) {
if (!url.isValid()) {
return;
}
_request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
makeRequest();
// if we already have a mapping (because we're an LOD), hold off on loading until we're requested
if (mapping.isEmpty()) {
makeRequest();
}
}
NetworkGeometry::~NetworkGeometry() {
@ -331,7 +336,27 @@ QSharedPointer<NetworkGeometry> NetworkGeometry::getLODOrFallback(float distance
return _fallback;
}
QMap<float, QSharedPointer<NetworkGeometry> >::const_iterator it = _lods.upperBound(distance);
return (it == _lods.constBegin()) ? _lodParent.toStrongRef() : *(it - 1);
QSharedPointer<NetworkGeometry> lod = (it == _lods.constBegin()) ? _lodParent.toStrongRef() : *(it - 1);
if (lod->isLoaded()) {
return lod;
}
// if the ideal LOD isn't loaded, we need to make sure it's started to load, and possibly return the closest loaded one
if (!lod->_startedLoading) {
lod->makeRequest();
}
float closestDistance = FLT_MAX;
if (isLoaded()) {
lod = _lodParent;
closestDistance = distance;
}
for (it = _lods.constBegin(); it != _lods.constEnd(); it++) {
float distanceToLOD = glm::abs(distance - it.key());
if (it.value()->isLoaded() && distanceToLOD < closestDistance) {
lod = it.value();
closestDistance = distanceToLOD;
}
}
return lod;
}
glm::vec4 NetworkGeometry::computeAverageColor() const {
@ -359,6 +384,7 @@ glm::vec4 NetworkGeometry::computeAverageColor() const {
}
void NetworkGeometry::makeRequest() {
_startedLoading = true;
_reply = Application::getInstance()->getNetworkAccessManager()->get(_request);
connect(_reply, SIGNAL(downloadProgress(qint64,qint64)), SLOT(handleDownloadProgress(qint64,qint64)));
@ -400,7 +426,12 @@ void NetworkGeometry::handleDownloadProgress(qint64 bytesReceived, qint64 bytesT
_lods.insert(it.value().toFloat(), geometry);
}
_request.setUrl(url.resolved(filename));
makeRequest();
// make the request immediately only if we have no LODs to switch between
_startedLoading = false;
if (_lods.isEmpty()) {
makeRequest();
}
}
return;
}

View file

@ -66,6 +66,7 @@ public:
const QVariantHash& mapping = QVariantHash(), const QUrl& textureBase = QUrl());
~NetworkGeometry();
/// Checks whether the geometry is fulled loaded.
bool isLoaded() const { return !_geometry.joints.isEmpty(); }
/// Returns a pointer to the geometry appropriate for the specified distance.
@ -94,6 +95,7 @@ private:
QVariantHash _mapping;
QUrl _textureBase;
QSharedPointer<NetworkGeometry> _fallback;
bool _startedLoading;
bool _failedToLoad;
int _attempts;