SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLAssetLoader Class Reference

#include <SLAssetLoader.h>

Public Member Functions

 SLAssetLoader (SLstring modelPath, SLstring texturePath, SLstring shaderPath, SLstring fontPath)
 
 ~SLAssetLoader ()
 
void scene (SLScene *scene)
 
bool isLoading () const
 
SLstring modelPath () const
 
SLstring shaderPath () const
 
SLstring texturePath () const
 
void addRawDataToLoad (SLIOBuffer &buffer, SLstring filename, SLIOStreamKind kind)
 
void addTextureToLoad (SLGLTexture *&texture, const SLstring &path, SLint min_filter=GL_LINEAR_MIPMAP_LINEAR, SLint mag_filter=GL_LINEAR, SLTextureType type=TT_unknown, SLint wrapS=GL_REPEAT, SLint wrapT=GL_REPEAT)
 Add 2D textures with internal image allocation. More...
 
void addTextureToLoad (SLGLTexture *&texture, const SLstring &imageFilenameXPos, const SLstring &imageFilenameXNeg, const SLstring &imageFilenameYPos, const SLstring &imageFilenameYNeg, const SLstring &imageFilenameZPos, const SLstring &imageFilenameZNeg, SLint min_filter=GL_LINEAR, SLint mag_filter=GL_LINEAR, SLTextureType type=TT_unknown)
 Add cube map texture with internal image allocation. More...
 
void addTextureToLoad (SLGLTexture *&texture, SLint depth, const SLstring &path, SLint min_filter=GL_LINEAR, SLint mag_filter=GL_LINEAR, SLint wrapS=GL_REPEAT, SLint wrapT=GL_REPEAT, const SLstring &name="3D-Texture", SLbool loadGrayscaleIntoAlpha=false)
 Add 3D texture from a single file with depth as 3rd dimension. More...
 
void addTextureToLoad (SLGLTexture *&texture, const SLVstring &imagePaths, SLint min_filter, SLint mag_filter, SLint wrapS, SLint wrapT, const SLstring &name, SLbool loadGrayscaleIntoAlpha)
 Add 3D texture from a vector of files. More...
 
void addGeoTiffToLoad (SLDeviceLocation &devLoc, const SLstring &imageFileWithPath)
 Add GeoTiff file to load for the SLDevLocation. More...
 
void addNodeToLoad (SLNode *&node, const SLstring &modelPath, SLSkybox *skybox=nullptr, SLbool deleteTexImgAfterBuild=false, SLbool loadMeshesOnly=true, SLMaterial *overrideMat=nullptr, float ambientFactor=0.5f, SLbool forceCookTorranceRM=false, SLuint flags=SLProcess_Triangulate|SLProcess_JoinIdenticalVertices|SLProcess_RemoveRedundantMaterials|SLProcess_FindDegenerates|SLProcess_FindInvalidData|SLProcess_SplitLargeMeshes)
 Add mesh from file to load via assimp loader. More...
 
void addProgramToLoad (SLGLProgram *&program, const SLstring &vertShaderFile, const SLstring &fragShaderFile)
 Add generic GLSL program with shader files to load. More...
 
void addSkyboxToLoad (SLSkybox *&skybox, const SLstring &path, SLVec2i resolution, SLstring name)
 Add skybox with HDR texture to load. More...
 
void addSkyboxToLoad (SLSkybox *&skybox, const SLstring &cubeMapXPos, const SLstring &cubeMapXNeg, const SLstring &cubeMapYPos, const SLstring &cubeMapYNeg, const SLstring &cubeMapZPos, const SLstring &cubeMapZNeg)
 Add skybox with 6 textures for a cubemap to load. More...
 
void addLoadTask (SLAssetLoadTask task)
 Add generic task. More...
 
void loadAssetsSync ()
 
void loadAssetsAsync (function< void()> onDone)
 
void checkIfAsyncLoadingIsDone ()
 

Public Attributes

SLScene_scene
 
SLAssetManager_am
 
SLstring _modelPath
 
SLstring _texturePath
 
SLstring _shaderPath
 
SLstring _fontPath
 
SLVAssetLoadTask _loadTasks
 
function< void()> _onDoneLoading
 Callback after threaded loading. More...
 
thread _worker
 worker thread for parallel loading More...
 
atomic< State_state
 current state (used for communication between threads) More...
 
mutex _messageMutex
 mutex protecting state between threads More...
 
condition_variable _messageCondVar
 mutex for waiting until state has changed More...
 

Private Types

enum class  State { IDLE , SUBMITTED , WORKING , DONE , STOPPING , STOPPED }
 

Detailed Description

Definition at line 44 of file SLAssetLoader.h.

Member Enumeration Documentation

◆ State

enum SLAssetLoader::State
strongprivate
Enumerator
IDLE 
SUBMITTED 
WORKING 
DONE 
STOPPING 
STOPPED 

Definition at line 47 of file SLAssetLoader.h.

48  {
49  IDLE,
50  SUBMITTED,
51  WORKING,
52  DONE,
53  STOPPING,
54  STOPPED
55  };

Constructor & Destructor Documentation

◆ SLAssetLoader()

SLAssetLoader::SLAssetLoader ( SLstring  modelPath,
SLstring  texturePath,
SLstring  shaderPath,
SLstring  fontPath 
)

Definition at line 27 of file SLAssetLoader.cpp.

34  _fontPath(fontPath),
36 {
37  auto workerFunc = [this]()
38  {
39  while (true)
40  {
41  // Wait until the main thread sends a message.
42  // 'Sending a message' means updating the state to a value that instructs
43  // the worker thread to do something.
44  unique_lock lock(_messageMutex);
45  _messageCondVar.wait(lock, [this]()
46  { return _state == State::SUBMITTED || _state == State::STOPPING; });
47 
48  // Do something depending on the current state.
49  if (_state == State::SUBMITTED)
50  {
51  // Process the tasks defined by the main thread.
52  for (SLAssetLoadTask& task : _loadTasks)
53  task();
54 
55  // Notify the main thread that the worker thread is done.
56  // The main thread checks this in checkIfAsyncLoadingIsDone.
58  }
59  else if (_state == State::STOPPING)
60  {
61  // Stops the worker thread by jumping...
62  break;
63  }
64  }
65 
66  // ...here and notifying the main thread that it can now join the worker thread.
68  _messageCondVar.notify_one();
69  };
70 
71  _worker = std::thread(workerFunc);
72 }
function< void()> SLAssetLoadTask
Definition: SLAssetLoader.h:41
SLstring _texturePath
SLstring shaderPath() const
Definition: SLAssetLoader.h:70
SLVAssetLoadTask _loadTasks
SLstring _shaderPath
SLstring _fontPath
SLstring texturePath() const
Definition: SLAssetLoader.h:71
mutex _messageMutex
mutex protecting state between threads
condition_variable _messageCondVar
mutex for waiting until state has changed
atomic< State > _state
current state (used for communication between threads)
thread _worker
worker thread for parallel loading
SLstring modelPath() const
Definition: SLAssetLoader.h:69
SLstring _modelPath

◆ ~SLAssetLoader()

SLAssetLoader::~SLAssetLoader ( )

Definition at line 74 of file SLAssetLoader.cpp.

75 {
76  // Instruct the worker thread to stop.
77  unique_lock lock(_messageMutex);
79  lock.unlock();
80  _messageCondVar.notify_one();
81 
82  // Wait until the worker thread has stopped so we can join it.
83  lock.lock();
84  _messageCondVar.wait(lock, [this]()
85  { return _state == State::STOPPED; });
86  _worker.join();
87 }

Member Function Documentation

◆ addGeoTiffToLoad()

void SLAssetLoader::addGeoTiffToLoad ( SLDeviceLocation devLoc,
const SLstring imageFileWithPath 
)

Add GeoTiff file to load for the SLDevLocation.

Definition at line 229 of file SLAssetLoader.cpp.

231 {
232  _loadTasks.push_back([this,
233  &devLoc,
234  imageFileWithPath]
235  { devLoc.loadGeoTiff(imageFileWithPath); });
236 }
void loadGeoTiff(const SLstring &geoTiffFile)
Loads a GeoTiff DEM (Digital Elevation Model) Image.

◆ addLoadTask()

void SLAssetLoader::addLoadTask ( SLAssetLoadTask  task)

Add generic task.

Definition at line 304 of file SLAssetLoader.cpp.

305 {
306  _loadTasks.push_back(task);
307 }

◆ addNodeToLoad()

void SLAssetLoader::addNodeToLoad ( SLNode *&  node,
const SLstring modelPath,
SLSkybox skybox = nullptr,
SLbool  deleteTexImgAfterBuild = false,
SLbool  loadMeshesOnly = true,
SLMaterial overrideMat = nullptr,
float  ambientFactor = 0.5f,
SLbool  forceCookTorranceRM = false,
SLuint  flags = SLProcess_Triangulate | SLProcess_JoinIdenticalVertices | SLProcess_RemoveRedundantMaterials | SLProcess_FindDegenerates | SLProcess_FindInvalidData | SLProcess_SplitLargeMeshes 
)

Add mesh from file to load via assimp loader.

Definition at line 251 of file SLAssetLoader.cpp.

260 {
261  _loadTasks.push_back([this,
262  &node,
263  modelPath,
264  skybox,
265  deleteTexImgAfterBuild,
266  loadMeshesOnly,
267  overrideMat,
268  ambientFactor,
269  forceCookTorranceRM,
270  flags]
271  {
272  SLAssimpImporter importer;
273  node = importer.load(_scene->animManager(),
274  _scene->assetManager(),
275  modelPath,
276  _texturePath,
277  skybox,
278  deleteTexImgAfterBuild,
279  loadMeshesOnly,
280  overrideMat,
281  ambientFactor,
282  forceCookTorranceRM,
283  nullptr,
284  flags); });
285 }
SLScene * _scene
SLAssetManager * assetManager()
Definition: SLScene.h:98
SLAnimManager & animManager()
Definition: SLScene.h:97

◆ addProgramToLoad()

void SLAssetLoader::addProgramToLoad ( SLGLProgram *&  program,
const SLstring vertShaderFile,
const SLstring fragShaderFile 
)

Add generic GLSL program with shader files to load.

Definition at line 238 of file SLAssetLoader.cpp.

241 {
242  _loadTasks.push_back([this,
243  &program,
244  vertShaderPath,
245  fragShaderPath]
246  { program = new SLGLProgramGeneric(_scene->assetManager(),
247  vertShaderPath,
248  fragShaderPath); });
249 }
Generic Shader Program class inherited from SLGLProgram.

◆ addRawDataToLoad()

void SLAssetLoader::addRawDataToLoad ( SLIOBuffer buffer,
SLstring  filename,
SLIOStreamKind  kind 
)

Definition at line 89 of file SLAssetLoader.cpp.

92 {
93  _loadTasks.push_back([this, &buffer, filename, kind]
94  { buffer = SLFileStorage::readIntoBuffer(filename, kind); });
95 }
SLIOBuffer readIntoBuffer(std::string path, SLIOStreamKind kind)
Reads an entire file into memory.

◆ addSkyboxToLoad() [1/2]

void SLAssetLoader::addSkyboxToLoad ( SLSkybox *&  skybox,
const SLstring cubeMapXPos,
const SLstring cubeMapXNeg,
const SLstring cubeMapYPos,
const SLstring cubeMapYNeg,
const SLstring cubeMapZPos,
const SLstring cubeMapZNeg 
)

Add skybox with 6 textures for a cubemap to load.

Definition at line 309 of file SLAssetLoader.cpp.

316 {
317  _loadTasks.push_back([this,
318  &skybox,
319  cubeMapXPos,
320  cubeMapXNeg,
321  cubeMapYPos,
322  cubeMapYNeg,
323  cubeMapZPos,
324  cubeMapZNeg]
325  { skybox = new SLSkybox(_scene->assetManager(),
326  _shaderPath,
327  _texturePath + cubeMapXPos,
328  _texturePath + cubeMapXNeg,
329  _texturePath + cubeMapYPos,
330  _texturePath + cubeMapYNeg,
331  _texturePath + cubeMapZPos,
332  _texturePath + cubeMapZNeg); });
333 }
Skybox node class with a SLBox mesh.
Definition: SLSkybox.h:29

◆ addSkyboxToLoad() [2/2]

void SLAssetLoader::addSkyboxToLoad ( SLSkybox *&  skybox,
const SLstring path,
SLVec2i  resolution,
SLstring  name 
)

Add skybox with HDR texture to load.

Definition at line 287 of file SLAssetLoader.cpp.

291 {
292  _loadTasks.push_back([this,
293  &skybox,
294  hdrImageWithFullPath,
295  resolution,
296  name]
297  { skybox = new SLSkybox(_scene->assetManager(),
298  _shaderPath,
299  hdrImageWithFullPath,
300  resolution,
301  name); });
302 }

◆ addTextureToLoad() [1/4]

void SLAssetLoader::addTextureToLoad ( SLGLTexture *&  texture,
const SLstring imageFilenameXPos,
const SLstring imageFilenameXNeg,
const SLstring imageFilenameYPos,
const SLstring imageFilenameYNeg,
const SLstring imageFilenameZPos,
const SLstring imageFilenameZNeg,
SLint  min_filter = GL_LINEAR,
SLint  mag_filter = GL_LINEAR,
SLTextureType  type = TT_unknown 
)

Add cube map texture with internal image allocation.

Definition at line 122 of file SLAssetLoader.cpp.

132 {
133  _loadTasks.push_back([this,
134  &texture,
135  filenameXPos,
136  filenameXNeg,
137  filenameYPos,
138  filenameYNeg,
139  filenameZPos,
140  filenameZNeg,
141  min_filter,
142  mag_filter,
143  type]
144  { texture = new SLGLTexture(_scene->assetManager(),
145  filenameXPos,
146  filenameXNeg,
147  filenameYPos,
148  filenameYNeg,
149  filenameZPos,
150  filenameZNeg,
151  min_filter,
152  mag_filter,
153  type); });
154 }
Texture object for OpenGL texturing.
Definition: SLGLTexture.h:110

◆ addTextureToLoad() [2/4]

void SLAssetLoader::addTextureToLoad ( SLGLTexture *&  texture,
const SLstring path,
SLint  min_filter = GL_LINEAR_MIPMAP_LINEAR,
SLint  mag_filter = GL_LINEAR,
SLTextureType  type = TT_unknown,
SLint  wrapS = GL_REPEAT,
SLint  wrapT = GL_REPEAT 
)

Add 2D textures with internal image allocation.

Definition at line 97 of file SLAssetLoader.cpp.

104 {
105  _loadTasks.push_back([this,
106  &texture,
107  path,
108  min_filter,
109  mag_filter,
110  type,
111  wrapS,
112  wrapT]
113  { texture = new SLGLTexture(_scene->assetManager(),
114  path,
115  min_filter,
116  mag_filter,
117  type,
118  wrapS,
119  wrapT); });
120 }

◆ addTextureToLoad() [3/4]

void SLAssetLoader::addTextureToLoad ( SLGLTexture *&  texture,
const SLVstring imagePaths,
SLint  min_filter,
SLint  mag_filter,
SLint  wrapS,
SLint  wrapT,
const SLstring name,
SLbool  loadGrayscaleIntoAlpha 
)

Add 3D texture from a vector of files.

Method for adding a 3D texture from a vector of images to load in parallel thread.

Parameters
texturePointer to SLGLTexture to return
imageFilenamesVector of texture image files. If only filenames are passed they will be searched on the SLGLTexture::defaultPath.
min_filterMinification filter constant from OpenGL
mag_filterMagnification filter constant from OpenGL
wrapSTexture wrapping in S direction (OpenGL constant)
wrapTTexture wrapping in T direction (OpenGL constant)
nameName of the 3D texture
loadGrayscaleIntoAlphaFlag if grayscale image should be loaded into alpha channel.

Definition at line 201 of file SLAssetLoader.cpp.

209 {
210  _loadTasks.push_back([this,
211  &texture,
212  imagePaths,
213  min_filter,
214  mag_filter,
215  wrapS,
216  wrapT,
217  name,
218  loadGrayscaleIntoAlpha]
219  { texture = new SLGLTexture(_scene->assetManager(),
220  imagePaths,
221  min_filter,
222  mag_filter,
223  wrapS,
224  wrapT,
225  name,
226  loadGrayscaleIntoAlpha); });
227 }

◆ addTextureToLoad() [4/4]

void SLAssetLoader::addTextureToLoad ( SLGLTexture *&  texture,
SLint  depth,
const SLstring path,
SLint  min_filter = GL_LINEAR,
SLint  mag_filter = GL_LINEAR,
SLint  wrapS = GL_REPEAT,
SLint  wrapT = GL_REPEAT,
const SLstring name = "3D-Texture",
SLbool  loadGrayscaleIntoAlpha = false 
)

Add 3D texture from a single file with depth as 3rd dimension.

Definition at line 156 of file SLAssetLoader.cpp.

166 {
167  _loadTasks.push_back([this,
168  &texture,
169  depth,
170  imagePath,
171  min_filter,
172  mag_filter,
173  wrapS,
174  wrapT,
175  name,
176  loadGrayscaleIntoAlpha]
177  { texture = new SLGLTexture(_scene->assetManager(),
178  depth,
179  imagePath,
180  min_filter,
181  mag_filter,
182  wrapS,
183  wrapT,
184  name,
185  loadGrayscaleIntoAlpha); });
186 }

◆ checkIfAsyncLoadingIsDone()

void SLAssetLoader::checkIfAsyncLoadingIsDone ( )

This method is called from the main thread to check if the async loading is is finished. If so, the assembly can be done in _onDoneLoading.

Definition at line 357 of file SLAssetLoader.cpp.

358 {
359  if (_state == State::DONE)
360  {
362  _loadTasks.clear();
363 
364  _onDoneLoading();
365  }
366 }
function< void()> _onDoneLoading
Callback after threaded loading.

◆ isLoading()

bool SLAssetLoader::isLoading ( ) const
inline

Definition at line 68 of file SLAssetLoader.h.

68 { return _state != State::IDLE; }

◆ loadAssetsAsync()

void SLAssetLoader::loadAssetsAsync ( function< void()>  onDone)

Definition at line 343 of file SLAssetLoader.cpp.

344 {
345  _onDoneLoading = onDoneLoading;
346 
347  // Instruct the worker thread to start processing tasks.
348  unique_lock lock(_messageMutex);
350  lock.unlock();
351  _messageCondVar.notify_one();
352 }

◆ loadAssetsSync()

void SLAssetLoader::loadAssetsSync ( )

Definition at line 335 of file SLAssetLoader.cpp.

336 {
337  for (const SLAssetLoadTask& task : _loadTasks)
338  task();
339 
340  _loadTasks.clear();
341 }

◆ modelPath()

SLstring SLAssetLoader::modelPath ( ) const
inline

Definition at line 69 of file SLAssetLoader.h.

69 { return _modelPath; }

◆ scene()

void SLAssetLoader::scene ( SLScene scene)
inline

Definition at line 65 of file SLAssetLoader.h.

65 { _scene = scene; }
void scene(SLScene *scene)
Definition: SLAssetLoader.h:65

◆ shaderPath()

SLstring SLAssetLoader::shaderPath ( ) const
inline

Definition at line 70 of file SLAssetLoader.h.

70 { return _shaderPath; }

◆ texturePath()

SLstring SLAssetLoader::texturePath ( ) const
inline

Definition at line 71 of file SLAssetLoader.h.

71 { return _texturePath; }

Member Data Documentation

◆ _am

SLAssetManager* SLAssetLoader::_am

Definition at line 169 of file SLAssetLoader.h.

◆ _fontPath

SLstring SLAssetLoader::_fontPath

Definition at line 173 of file SLAssetLoader.h.

◆ _loadTasks

SLVAssetLoadTask SLAssetLoader::_loadTasks

Definition at line 174 of file SLAssetLoader.h.

◆ _messageCondVar

condition_variable SLAssetLoader::_messageCondVar

mutex for waiting until state has changed

Definition at line 180 of file SLAssetLoader.h.

◆ _messageMutex

mutex SLAssetLoader::_messageMutex

mutex protecting state between threads

Definition at line 179 of file SLAssetLoader.h.

◆ _modelPath

SLstring SLAssetLoader::_modelPath

Definition at line 170 of file SLAssetLoader.h.

◆ _onDoneLoading

function<void()> SLAssetLoader::_onDoneLoading

Callback after threaded loading.

Definition at line 175 of file SLAssetLoader.h.

◆ _scene

SLScene* SLAssetLoader::_scene

Definition at line 168 of file SLAssetLoader.h.

◆ _shaderPath

SLstring SLAssetLoader::_shaderPath

Definition at line 172 of file SLAssetLoader.h.

◆ _state

atomic<State> SLAssetLoader::_state

current state (used for communication between threads)

Definition at line 178 of file SLAssetLoader.h.

◆ _texturePath

SLstring SLAssetLoader::_texturePath

Definition at line 171 of file SLAssetLoader.h.

◆ _worker

thread SLAssetLoader::_worker

worker thread for parallel loading

Definition at line 177 of file SLAssetLoader.h.


The documentation for this class was generated from the following files: