ofThreadedModelLoader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ThreadedModelLoader.h" | |
#include <sstream> | |
ThreadedModelLoader::ThreadedModelLoader(){ | |
nextID = 0; | |
ofAddListener(ofEvents().update, this, &ThreadedModelLoader::update); | |
startThread(); | |
lastUpdate = 0; | |
loading = false; | |
} | |
ThreadedModelLoader::~ThreadedModelLoader(){ | |
models_to_load_from_disk.close(); | |
models_to_update.close(); | |
waitForThread(true); | |
ofRemoveListener(ofEvents().update, this, &ThreadedModelLoader::update); | |
} | |
// Load a model from disk. | |
//-------------------------------------------------------------- | |
void ThreadedModelLoader::load(ofxAssimpModelLoader& model, string filename, bool _useCustomShader) { | |
nextID++; | |
ModelLoaderEntry entry(model); | |
entry.filename = filename; | |
entry.name = filename; | |
useCustomShader = _useCustomShader; | |
models_to_load_from_disk.send(entry); | |
loading = true; | |
} | |
// Reads from the queue and loads new models. | |
//-------------------------------------------------------------- | |
void ThreadedModelLoader::threadedFunction() { | |
ModelLoaderEntry entry; | |
while( models_to_load_from_disk.receive(entry) ) { | |
if(entry.model->loadModel(entry.filename, false) ) { | |
models_to_update.send(entry); | |
}else{ | |
ofLogError("ThreadedModelLoader") << "couldn't load file: \"" << entry.filename << "\""; | |
} | |
} | |
ofLogVerbose("ThreadedModelLoader") << "finishing thread on closed queue"; | |
} | |
// Check the update queue and update the texture | |
//-------------------------------------------------------------- | |
void ThreadedModelLoader::update(ofEventArgs & a){ | |
// Load 1 model per update so we don't block the gl thread for too long | |
ModelLoaderEntry entry; | |
if (models_to_update.tryReceive(entry)) { | |
if (!useCustomShader) { | |
entry.model->disableTextures(); | |
entry.model->disableMaterials(); | |
} | |
loading = false; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofThread.h" | |
#include "ofxAssimpModelLoader.h" | |
#include "ofThreadChannel.h" | |
using namespace std; | |
class ThreadedModelLoader : public ofThread { | |
public: | |
ThreadedModelLoader(); | |
~ThreadedModelLoader(); | |
void load(ofxAssimpModelLoader& model, string file, bool _useCustomShader); | |
bool loading; | |
private: | |
void update(ofEventArgs & a); | |
virtual void threadedFunction(); | |
struct ModelLoaderEntry { | |
ModelLoaderEntry() { | |
model = NULL; | |
} | |
ModelLoaderEntry(ofxAssimpModelLoader & pModel) { | |
model = &pModel; | |
} | |
ofxAssimpModelLoader* model; | |
string filename; | |
string name; | |
}; | |
typedef map<string, ModelLoaderEntry>::iterator models_entry_iterator; | |
map<string,ModelLoaderEntry> models_async_loading; // keeps track of models which are loading async | |
ofThreadChannel<ModelLoaderEntry> models_to_load_from_disk; | |
ofThreadChannel<ModelLoaderEntry> models_to_update; | |
bool useCustomShader; | |
int nextID; | |
int lastUpdate; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment