fix for crash when plain format file downloaded to screenspacecygnet and texturecygnet

This commit is contained in:
Michael Nilsson
2016-06-17 16:22:21 -04:00
parent c5b1a5e53a
commit 2d47eb7a6d
5 changed files with 38 additions and 19 deletions

View File

@@ -47,6 +47,7 @@ ScreenSpaceImage::ScreenSpaceImage(const ghoul::Dictionary& dictionary)
: ScreenSpaceRenderable(dictionary)
, _texturePath("texturePath", "Texture path", "")
, _downloadImage(false)
, _textureDirty(false)
{
std::string name;
if (dictionary.getValue(KeyName, name)) {
@@ -109,10 +110,13 @@ void ScreenSpaceImage::render() {
}
void ScreenSpaceImage::update() {
if(!_downloadImage) return;
if (_futureImage.valid() && DownloadManager::futureReady(_futureImage)) {
if (!_downloadImage) {
if (_futureImage.valid() && DownloadManager::futureReady(_futureImage))
_textureDirty = true;
}
if (_textureDirty) {
loadTexture();
_textureDirty = false;
}
}
@@ -128,7 +132,7 @@ void ScreenSpaceImage::loadTexture() {
texture = std::move(loadFromMemory());
if (texture) {
// LDEBUG("Loaded texture from '" << absPath(_texturePath) << "'");
LDEBUG("Loaded texture for '" << name() << "'");
texture->uploadTexture();
// Textures of planets looks much smoother with AnisotropicMipMap rather than linear
@@ -140,7 +144,7 @@ void ScreenSpaceImage::loadTexture() {
void ScreenSpaceImage::updateTexture() {
if (!_downloadImage) {
loadTexture();
_textureDirty = true;
} else {
if (_futureImage.valid())
return;

View File

@@ -52,6 +52,7 @@ protected:
std::string _url;
bool _downloadImage;
std::future<DownloadManager::MemoryFile> _futureImage;
bool _textureDirty;
private:
std::future<DownloadManager::MemoryFile> downloadImageToMemory(std::string url);

View File

@@ -45,13 +45,13 @@ ScreenSpaceCygnet::ScreenSpaceCygnet(const ghoul::Dictionary& dictionary)
float interval;
dictionary.getValue("UpdateInterval", interval);
_updateTime = (int) interval;
_updateInterval = (int) interval;
_downloadImage = true;
_url = IswaManager::ref().iswaUrl(_cygnetId);
_openSpaceTime = Time::ref().currentTime();
_lastUpdateOpenSpaceTime = _openSpaceTime;
_lastUpdateOpenSpaceTime = 0.0f;
_realTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
_lastUpdateRealTime = _realTime;
@@ -69,21 +69,35 @@ ScreenSpaceCygnet::ScreenSpaceCygnet(const ghoul::Dictionary& dictionary)
ScreenSpaceCygnet::~ScreenSpaceCygnet(){}
void ScreenSpaceCygnet::update(){
if (!_enabled)
return;
// the image is downloaded ahead of time, so we need to
// know if we are going backwards or forwards in time
double clockwiseSign = (Time::ref().deltaTime()>0) ? 1.0 : -1.0;
_openSpaceTime = Time::ref().currentTime();
_realTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
bool timeToUpdate = (fabs(_openSpaceTime-_lastUpdateOpenSpaceTime) >= _updateTime &&
bool timeToUpdate = (fabs(_openSpaceTime-_lastUpdateOpenSpaceTime) >= _updateInterval &&
(_realTime.count()-_lastUpdateRealTime.count()) > _minRealTimeUpdateInterval);
if((Time::ref().timeJumped() || timeToUpdate )){
_url = IswaManager::ref().iswaUrl(_cygnetId);
updateTexture();
_lastUpdateRealTime = _realTime;
_lastUpdateOpenSpaceTime = _openSpaceTime;
if(_futureImage.valid() && DownloadManager::futureReady(_futureImage)) {
_textureDirty = true;
}
if(_futureImage.valid() && DownloadManager::futureReady(_futureImage)) {
loadTexture();
if(_textureDirty && _updateInterval != 0 && timeToUpdate) {
try{ // in case the loaded file format is not supported, catch exception.
loadTexture();
} catch (const std::runtime_error& e) {
LWARNING("Error loading texture for " + name() + ": " + e.what());
}
_textureDirty = false;
_url = IswaManager::ref().iswaUrl(_cygnetId, _openSpaceTime + clockwiseSign*_updateInterval);
updateTexture();
_lastUpdateRealTime = _realTime;
_lastUpdateOpenSpaceTime =_openSpaceTime;
}
}
}

View File

@@ -45,7 +45,7 @@ public:
private:
int _cygnetId;
int _updateTime;
int _updateInterval;
double _openSpaceTime;
double _lastUpdateOpenSpaceTime;

View File

@@ -43,13 +43,13 @@ TextureCygnet::~TextureCygnet(){}
bool TextureCygnet::updateTexture() {
std::unique_ptr<ghoul::opengl::Texture> texture;
try{ // in case the loaded file is not supported, catch exception.
try{ // in case the loaded file format is not supported, catch exception.
texture = ghoul::io::TextureReader::ref().loadTexture(
(void*) _imageFile.buffer,
_imageFile.size,
_imageFile.format);
} catch (std::exception& e) {
LWARNING(e.what());
} catch (const std::runtime_error& e) {
LWARNING("Error loading texture for " + name() + ": " + e.what());
}
if (texture) {
LDEBUG("Loaded texture from image iswa cygnet with id: '" << _data->id << "'");