-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresourcemanager.h
136 lines (100 loc) · 4.42 KB
/
resourcemanager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#ifndef RESOURCEMANAGER_H
#define RESOURCEMANAGER_H
#include <map>
#include <string>
#include <memory>
#include "texture.h"
#include "vertex.h"
#include "meshdata.h"
#ifdef _WIN32
#include <al.h>
#include <alc.h>
#elif __APPLE__
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#endif
#include "wavfilehandler.h"
class SoundSource;
/**
* @brief The resource manager is responsible for handling all resources, making only a single copy whenever possible to keep memory usage low.
*/
class ResourceManager : protected QOpenGLFunctions_4_1_Core
{
friend class App;
private:
static ResourceManager* inst;
ResourceManager();
public:
static ResourceManager& instance()
{
assert(inst != nullptr);
return *inst;
}
virtual ~ResourceManager();
/**
* @brief Helper function that iteratively goes through all folders in gsl::assetFilePath and loads the files.
* NOTE: Does not include shaders. Only meshes, textures and sounds.
*/
void LoadAssetFiles();
// Shaders
void addShader(const std::string& name, std::shared_ptr<Shader> shader);
std::shared_ptr<Shader> getShader(const std::string& name);
// Textures
void addTexture(const std::string& name, const std::string& path, GLenum type = GL_TEXTURE_2D);
std::shared_ptr<Texture> getTexture(const std::string& name);
// Meshes
std::shared_ptr<MeshData> addMesh(const std::string& name, const std::string& path, GLenum renderType = GL_TRIANGLES);
std::shared_ptr<MeshData> getMesh(const std::string& name);
/**
* @brief Adds the LOD meshdata to the baseMesh meshdata at the given level and removes the LOD mesh from the mesharray.
* @param baseMesh - The receiver of the LOD
* @param LOD - The meshdata of the LOD
* @param level - At what level the LOD shall be added; 0, 1 or 2. Defaults to 1. (0 is the basemesh)
*/
void setupLOD(std::shared_ptr<MeshData> baseMesh, std::shared_ptr<MeshData> LOD, unsigned int level = 1);
// Sound
void loadWav(const std::string& name, const std::string& path);
wave_t* getWav(const std::string& name);
int getSourceBuffer(const std::string& name);
// Helper functions for UI
std::vector<std::string> getAllMeshNames();
std::vector<std::string> getAllShaderNames();
std::vector<std::string> getAllTextureNames();
std::vector<std::string> getAllWavFileNames();
private:
/**
* @brief Reads an obj file at the specified fileName. Relative means gsl::assetFilePath/Meshes if true, working directory if false
*/
std::pair<std::vector<Vertex>, std::vector<GLuint>> readObjFile(std::string filename, bool relative = true);
/**
* @brief Reads a txt file written like the Vertex << overload is setup. Can only be read by the Vertex >> overload.
*/
std::pair<std::vector<Vertex>, std::vector<GLuint>> readTxtFile(std::string filename);
/**
* @brief Sets up LOD meshes for a .obj. It looks for same name suffixed with '_L01/_L02'. Hardcoded to create 2 LODs
*/
std::pair<std::shared_ptr<MeshData>, std::shared_ptr<MeshData> > initializeLODs(const std::string& name, const std::string& path, GLenum renderType);
/**
* @brief Sets up the base mesh data vao, vertexcount and indexcount arrays with the provided values from LOD1 and LOD2 respectively.
*/
void setupLODs(std::shared_ptr<MeshData> baseMeshData, std::shared_ptr<MeshData> LOD1 = nullptr, std::shared_ptr<MeshData> LOD2 = nullptr);
/**
* @brief Creates the MeshData. Does the OpenGL initialization of buffers using the provided vertex and indices data.
*/
std::shared_ptr<MeshData> initializeMeshData(const std::string& name, GLenum renderType, std::pair<std::vector<Vertex>, std::vector<GLuint>> data);
/**
* @brief Calculates bounds for the mesh based on vertices
* @param vertices - list of vertices for mesh
* @return A bounding sphere in local space
*/
static MeshData::Bounds CalculateBounds(const std::vector<Vertex>& vertices);
// Data
std::map<std::string, std::shared_ptr<Shader>> mShaders;
std::map<std::string, std::shared_ptr<Texture>> mTextures;
std::map<std::string, std::shared_ptr<MeshData>> mMeshes;
std::map<std::string, wave_t*> mWavFiles;
std::map<std::string, unsigned int> mSourceBuffers;
// Used to check if it's needed to call initializeOpenGLFunctions().
bool mIsInitialized = false;
};
#endif // RESOURCEMANAGER_H