SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLAssetLoader.h
Go to the documentation of this file.
1 /**
2  * \file SLAssetLoader.h
3  * \date May 2024
4  * \authors Marino von Wattenwyl
5  * \copyright http://opensource.org/licenses/GPL-3.0
6  * \remarks Please use clangformat to format the code. See more code style on
7  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
8 */
9 
10 #ifndef SLASSETLOADER_H
11 #define SLASSETLOADER_H
12 
13 #include "SLFileStorage.h"
14 
15 #include <condition_variable>
16 #include <functional>
17 #include <thread>
18 #include <atomic>
19 #include <optional>
20 
21 #include <SL.h>
22 #include <SLGLTexture.h>
23 #include <SLFileStorage.h>
24 #include <SLImporter.h>
25 
26 class SLScene;
27 class SLAssetManager;
28 class SLNode;
29 class SLSkybox;
30 class SLMaterial;
31 class SLDeviceLocation;
32 
33 using std::atomic;
34 using std::condition_variable;
35 using std::function;
36 using std::mutex;
37 using std::optional;
38 using std::thread;
39 
40 //-----------------------------------------------------------------------------
41 typedef function<void()> SLAssetLoadTask;
42 typedef vector<SLAssetLoadTask> SLVAssetLoadTask;
43 //-----------------------------------------------------------------------------
45 {
46 private:
47  enum class State
48  {
49  IDLE,
50  SUBMITTED,
51  WORKING,
52  DONE,
53  STOPPING,
54  STOPPED
55  };
56 
57 public:
61  SLstring fontPath);
63 
64  // Setters
65  void scene(SLScene* scene) { _scene = scene; }
66 
67  // Getters
68  bool isLoading() const { return _state != State::IDLE; }
69  SLstring modelPath() const { return _modelPath; }
70  SLstring shaderPath() const { return _shaderPath; }
71  SLstring texturePath() const { return _texturePath; }
72 
73  void addRawDataToLoad(SLIOBuffer& buffer,
74  SLstring filename,
75  SLIOStreamKind kind);
76 
77  //! Add 2D textures with internal image allocation
78  void addTextureToLoad(SLGLTexture*& texture,
79  const SLstring& path,
80  SLint min_filter = GL_LINEAR_MIPMAP_LINEAR,
81  SLint mag_filter = GL_LINEAR,
83  SLint wrapS = GL_REPEAT,
84  SLint wrapT = GL_REPEAT);
85 
86  //! Add cube map texture with internal image allocation
87  void addTextureToLoad(SLGLTexture*& texture,
88  const SLstring& imageFilenameXPos,
89  const SLstring& imageFilenameXNeg,
90  const SLstring& imageFilenameYPos,
91  const SLstring& imageFilenameYNeg,
92  const SLstring& imageFilenameZPos,
93  const SLstring& imageFilenameZNeg,
94  SLint min_filter = GL_LINEAR,
95  SLint mag_filter = GL_LINEAR,
96  SLTextureType type = TT_unknown);
97 
98  //! Add 3D texture from a single file with depth as 3rd dimension
99  void addTextureToLoad(SLGLTexture*& texture,
100  SLint depth,
101  const SLstring& path,
102  SLint min_filter = GL_LINEAR,
103  SLint mag_filter = GL_LINEAR,
104  SLint wrapS = GL_REPEAT,
105  SLint wrapT = GL_REPEAT,
106  const SLstring& name = "3D-Texture",
107  SLbool loadGrayscaleIntoAlpha = false);
108 
109  //! Add 3D texture from a vector of files
110  void addTextureToLoad(SLGLTexture*& texture,
111  const SLVstring& imagePaths,
112  SLint min_filter,
113  SLint mag_filter,
114  SLint wrapS,
115  SLint wrapT,
116  const SLstring& name,
117  SLbool loadGrayscaleIntoAlpha);
118 
119  //! Add GeoTiff file to load for the SLDevLocation
120  void addGeoTiffToLoad(SLDeviceLocation& devLoc,
121  const SLstring& imageFileWithPath);
122 
123  //! Add mesh from file to load via assimp loader
124  void addNodeToLoad(SLNode*& node,
125  const SLstring& modelPath,
126  SLSkybox* skybox = nullptr,
127  SLbool deleteTexImgAfterBuild = false,
128  SLbool loadMeshesOnly = true,
129  SLMaterial* overrideMat = nullptr,
130  float ambientFactor = 0.5f,
131  SLbool forceCookTorranceRM = false,
132  SLuint flags =
139 
140  //! Add generic GLSL program with shader files to load
141  void addProgramToLoad(SLGLProgram*& program,
142  const SLstring& vertShaderFile,
143  const SLstring& fragShaderFile);
144 
145  //! Add skybox with HDR texture to load
146  void addSkyboxToLoad(SLSkybox*& skybox,
147  const SLstring& path,
148  SLVec2i resolution,
149  SLstring name);
150 
151  //! Add skybox with 6 textures for a cubemap to load
152  void addSkyboxToLoad(SLSkybox*& skybox,
153  const SLstring& cubeMapXPos,
154  const SLstring& cubeMapXNeg,
155  const SLstring& cubeMapYPos,
156  const SLstring& cubeMapYNeg,
157  const SLstring& cubeMapZPos,
158  const SLstring& cubeMapZNeg);
159 
160  //! Add generic task
161  void addLoadTask(SLAssetLoadTask task);
162 
163  void loadAssetsSync();
164  void loadAssetsAsync(function<void()> onDone);
166 
167 public:
175  function<void()> _onDoneLoading; //!< Callback after threaded loading
176 
177  thread _worker; //!< worker thread for parallel loading
178  atomic<State> _state; //!< current state (used for communication between threads)
179  mutex _messageMutex; //!< mutex protecting state between threads
180  condition_variable _messageCondVar; //!< mutex for waiting until state has changed
181 };
182 //-----------------------------------------------------------------------------
183 
184 #endif
unsigned int SLuint
Definition: SL.h:171
bool SLbool
Definition: SL.h:175
vector< SLstring > SLVstring
Definition: SL.h:201
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
function< void()> SLAssetLoadTask
Definition: SLAssetLoader.h:41
vector< SLAssetLoadTask > SLVAssetLoadTask
Definition: SLAssetLoader.h:42
SLIOStreamKind
Enum of file kinds.
Definition: SLFileStorage.h:38
typedef void(SL_STDCALL *cbOnImGuiBuild)(SLScene *s
Callback function typedef for ImGui build function.
SLTextureType
Texture type enumeration & their filename appendix for auto type detection.
Definition: SLGLTexture.h:76
@ TT_unknown
Definition: SLGLTexture.h:77
@ SLProcess_SplitLargeMeshes
Definition: SLImporter.h:40
@ SLProcess_Triangulate
Definition: SLImporter.h:36
@ SLProcess_FindDegenerates
Definition: SLImporter.h:48
@ SLProcess_FindInvalidData
Definition: SLImporter.h:49
@ SLProcess_RemoveRedundantMaterials
Definition: SLImporter.h:45
@ SLProcess_JoinIdenticalVertices
Definition: SLImporter.h:34
SLAssetManager * _am
SLstring _texturePath
void addProgramToLoad(SLGLProgram *&program, const SLstring &vertShaderFile, const SLstring &fragShaderFile)
Add generic GLSL program with shader files to load.
SLstring shaderPath() const
Definition: SLAssetLoader.h:70
SLVAssetLoadTask _loadTasks
SLstring _shaderPath
void scene(SLScene *scene)
Definition: SLAssetLoader.h:65
SLstring _fontPath
SLstring texturePath() const
Definition: SLAssetLoader.h:71
SLScene * _scene
void loadAssetsAsync(function< void()> onDone)
void checkIfAsyncLoadingIsDone()
bool isLoading() const
Definition: SLAssetLoader.h:68
mutex _messageMutex
mutex protecting state between threads
void addGeoTiffToLoad(SLDeviceLocation &devLoc, const SLstring &imageFileWithPath)
Add GeoTiff file to load for the SLDevLocation.
condition_variable _messageCondVar
mutex for waiting until state has changed
atomic< State > _state
current state (used for communication between threads)
void addSkyboxToLoad(SLSkybox *&skybox, const SLstring &path, SLVec2i resolution, SLstring name)
Add skybox with HDR texture to load.
thread _worker
worker thread for parallel loading
SLstring modelPath() const
Definition: SLAssetLoader.h:69
void addLoadTask(SLAssetLoadTask task)
Add generic task.
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.
SLAssetLoader(SLstring modelPath, SLstring texturePath, SLstring shaderPath, SLstring fontPath)
SLstring _modelPath
function< void()> _onDoneLoading
Callback after threaded loading.
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.
void addRawDataToLoad(SLIOBuffer &buffer, SLstring filename, SLIOStreamKind kind)
Toplevel holder of the assets meshes, materials, textures and shaders.
Encapsulation of a mobile device location set by the device's GPS sensor.
Encapsulation of an OpenGL shader program object.
Definition: SLGLProgram.h:56
Texture object for OpenGL texturing.
Definition: SLGLTexture.h:110
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
SLNode represents a node in a hierarchical scene graph.
Definition: SLNode.h:147
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47
Skybox node class with a SLBox mesh.
Definition: SLSkybox.h:29
Utility struct that holds a pointer and its length.
Definition: SLFileStorage.h:28