SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLAssetManager.cpp
Go to the documentation of this file.
1 /**
2  * \file SLAssetManager.cpp
3  * \authors Michael Goettlicher, Marcus Hudritsch
4  * \date Feb 2020
5  * \authors Marcus Hudritsch
6  * \copyright http://opensource.org/licenses/GPL-3.0
7  * \remarks Please use clangformat to format the code. See more code style on
8  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
9 */
10 
11 #include <SLAssimpImporter.h>
12 #include <SLAssetManager.h>
13 #include <SLTexFont.h>
14 
15 #define SL_LOAD_ASSETS_IN_PARALLEL 1
16 
17 //-----------------------------------------------------------------------------
18 // Initialize static font pointers
30 //-----------------------------------------------------------------------------
32 {
33  clear();
34 }
35 //-----------------------------------------------------------------------------
37 {
38  // font and video texture are not added to the _textures vector
41 }
42 //-----------------------------------------------------------------------------
43 //! for all assets, clear gpu data
45 {
46  // delete materials
47  for (auto m : _materials)
48  delete m;
49  _materials.clear();
50 
51  // delete textures
52  for (auto t : _textures)
53  delete t;
54  _textures.clear();
55 
56  // delete meshes
57  for (auto m : _meshes)
58  delete m;
59  _meshes.clear();
60 
61  // delete shader programs
62  for (auto p : _programs)
63  delete p;
64  _programs.clear();
65 }
66 //-----------------------------------------------------------------------------
67 //! for all assets, clear gpu data
69 {
70  /*
71  SLGLState* stateGL = SLGLState::instance();
72 
73  // check if current
74  for (auto m : _materials)
75  {
76  if (stateGL->currentMaterial() == m)
77  stateGL->currentMaterial(nullptr);
78  }
79  */
80 
81  // delete textures
82  for (auto t : _textures)
83  t->deleteDataGpu();
84 
85  // delete meshes
86  for (auto m : _meshes)
87  m->deleteDataGpu();
88 
89  // delete shader programs
90  for (auto p : _programs)
91  ; // p->deleteDataGpu();
92 }
93 //-----------------------------------------------------------------------------
94 //! Removes the specified mesh from the meshes resource vector.
96 {
97  assert(mesh);
98  for (SLulong i = 0; i < _meshes.size(); ++i)
99  {
100  if (_meshes[i] == mesh)
101  {
102  _meshes.erase(_meshes.begin() + i);
103  return true;
104  }
105  }
106  return false;
107 }
108 //-----------------------------------------------------------------------------
109 //! Removes the specified program from the meshes resource vector.
111 {
112  assert(program);
113  for (SLulong i = 0; i < _programs.size(); ++i)
114  {
115  if (_programs[i] == program)
116  {
117  _programs.erase(_programs.begin() + i);
118  return true;
119  }
120  }
121  return false;
122 }
123 //-----------------------------------------------------------------------------
124 //! Returns the pointer to shader program if found by name
126 {
127  for (auto sp : _programs)
128  if (sp->name() == programName)
129  return sp;
130  return nullptr;
131 }
132 
133 //-----------------------------------------------------------------------------
134 //! merge other asset manager into this
136 {
137  // update the assetmanager pointer for automatic program assignment
138  for (SLMaterial* m : other.materials())
139  m->assetManager(this);
140  // transfer assets from other to this
141  _meshes.insert(_meshes.end(), other.meshes().begin(), other.meshes().end());
142  _materials.insert(_materials.end(), other.materials().begin(), other.materials().end());
143  _textures.insert(_textures.end(), other.textures().begin(), other.textures().end());
144  _programs.insert(_programs.end(), other.programs().begin(), other.programs().end());
145  // clear ownership of other
146  other.meshes().clear();
147  other.materials().clear();
148  other.textures().clear();
149  other.programs().clear();
150 }
151 //-----------------------------------------------------------------------------
152 //! Generates all static fonts
153 void SLAssetManager::generateFonts(SLstring fontPath, SLGLProgram& fontTexProgram)
154 {
155  font07 = new SLTexFont(fontPath + "Font07.png", &fontTexProgram);
156  assert(font07);
157  font08 = new SLTexFont(fontPath + "Font08.png", &fontTexProgram);
158  assert(font08);
159  font09 = new SLTexFont(fontPath + "Font09.png", &fontTexProgram);
160  assert(font09);
161  font10 = new SLTexFont(fontPath + "Font10.png", &fontTexProgram);
162  assert(font10);
163  font12 = new SLTexFont(fontPath + "Font12.png", &fontTexProgram);
164  assert(font12);
165  font14 = new SLTexFont(fontPath + "Font14.png", &fontTexProgram);
166  assert(font14);
167  font16 = new SLTexFont(fontPath + "Font16.png", &fontTexProgram);
168  assert(font16);
169  font18 = new SLTexFont(fontPath + "Font18.png", &fontTexProgram);
170  assert(font18);
171  font20 = new SLTexFont(fontPath + "Font20.png", &fontTexProgram);
172  assert(font20);
173  font22 = new SLTexFont(fontPath + "Font22.png", &fontTexProgram);
174  assert(font22);
175  font24 = new SLTexFont(fontPath + "Font24.png", &fontTexProgram);
176  assert(font24);
177 }
178 //-----------------------------------------------------------------------------
179 //! Deletes all static fonts
181 {
182  delete font07;
183  font07 = nullptr;
184  delete font08;
185  font08 = nullptr;
186  delete font09;
187  font09 = nullptr;
188  delete font10;
189  font10 = nullptr;
190  delete font12;
191  font12 = nullptr;
192  delete font14;
193  font14 = nullptr;
194  delete font16;
195  font16 = nullptr;
196  delete font18;
197  font18 = nullptr;
198  delete font20;
199  font20 = nullptr;
200  delete font22;
201  font22 = nullptr;
202  delete font24;
203  font24 = nullptr;
204 }
205 //-----------------------------------------------------------------------------
206 //! returns nearest font for a given height in mm
208 {
209  SLfloat dpmm = (SLfloat)dpi / 25.4f;
210  SLfloat targetH_PX = dpmm * heightMM;
211 
212  if (targetH_PX < 7) return font07;
213  if (targetH_PX < 8) return font08;
214  if (targetH_PX < 9) return font09;
215  if (targetH_PX < 10) return font10;
216  if (targetH_PX < 12) return font12;
217  if (targetH_PX < 14) return font14;
218  if (targetH_PX < 16) return font16;
219  if (targetH_PX < 18) return font18;
220  if (targetH_PX < 20) return font20;
221  if (targetH_PX < 24) return font22;
222  return font24;
223 }
224 //-----------------------------------------------------------------------------
static SLint dpi
Dot per inch resolution of screen.
Definition: AppGLFW.cpp:41
float SLfloat
Definition: SL.h:173
unsigned long SLulong
Definition: SL.h:165
string SLstring
Definition: SL.h:158
int SLint
Definition: SL.h:170
@ SP_fontTex
Toplevel holder of the assets meshes, materials, textures and shaders.
SLVGLTexture _textures
Vector of all texture pointers.
SLVMesh & meshes()
bool removeMesh(SLMesh *mesh)
Removes the specified mesh from the meshes resource vector.
void clear()
for all assets, clear gpu data
static SLTexFont * font24
24 pixel high fixed size font
static void deleteFonts()
Deletes all static fonts.
static SLTexFont * font10
10 pixel high fixed size font
static SLTexFont * getFont(SLfloat heightMM, SLint dpi)
returns nearest font for a given height in mm
static SLTexFont * font20
20 pixel high fixed size font
static SLTexFont * font09
9 pixel high fixed size font
static SLTexFont * font22
22 pixel high fixed size font
static SLTexFont * font14
14 pixel high fixed size font
static SLTexFont * font07
7 pixel high fixed size font
static SLTexFont * font08
8 pixel high fixed size font
SLVGLProgram & programs()
static SLTexFont * font18
18 pixel high fixed size font
SLGLProgram * getProgramByName(const string &programName)
Returns the pointer to shader program if found by name.
void merge(SLAssetManager &other)
merge other asset manager into this
SLVMaterial _materials
Vector of all materials pointers.
static SLTexFont * font12
12 pixel high fixed size font
SLVGLProgram _programs
Vector of all shader program pointers.
void deleteDataGpu()
for all assets, clear gpu data
static void generateFonts(SLstring fontPath, SLGLProgram &fontTexProgram)
Generates all static fonts.
SLVMesh _meshes
Vector of all meshes.
void generateStaticFonts(SLstring fontPath)
bool removeProgram(SLGLProgram *program)
Removes the specified mesh from the meshes resource vector.
SLVMaterial & materials()
static SLTexFont * font16
16 pixel high fixed size font
SLVGLTexture & textures()
Encapsulation of an OpenGL shader program object.
Definition: SLGLProgram.h:56
static SLGLProgramGeneric * get(SLStdShaderProg id)
Get program reference for given id.
Defines a standard CG material with textures and a shader program.
Definition: SLMaterial.h:56
An SLMesh object is a triangulated mesh, drawn with one draw call.
Definition: SLMesh.h:134
Texture Font class inherits SLGLTexture for alpha blended font rendering.
Definition: SLTexFont.h:40