-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceManager.cpp
117 lines (97 loc) · 2.8 KB
/
ResourceManager.cpp
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
#include "ResourceManager.h"
#include <iostream>
#include <sstream>>
#include <fstream>
#include "stb_image.h"
std::map<std::string, Texture> ResourceManager::Textures;
std::map<std::string, Shader> ResourceManager::Shaders;
Shader ResourceManager::LoadShader(const char* vShaderFile, const char* fShaderFile, const char* gShaderFile, std::string name)
{
Shaders[name] = loadShaderFromFile(vShaderFile, fShaderFile, gShaderFile);
return Shaders[name];
}
Shader &ResourceManager::GetShader(std::string name)
{
return Shaders[name];
}
Texture ResourceManager::LoadTexture(const char* file, bool alpha, std::string name)
{
Textures[name] = loadTextureFromFile(file, alpha);
return Textures[name];
}
Texture &ResourceManager::GetTexture(std::string name)
{
return Textures[name];
}
void ResourceManager::Clear()
{
// Delete All shaders and texture
for (auto iter : Shaders) {
glDeleteProgram(iter.second.ID);
}
for (auto iter : Textures) {
glDeleteTextures(1, &iter.second.ID);
}
}
Shader ResourceManager::loadShaderFromFile(const char* vShaderFile, const char* fShaderFile, const char* gShaderFile)
{
std::string vertexCode;
std::string fragmentCode;
std::string geometryCode;
try
{
// Open files
std::ifstream vertexShaderFile(vShaderFile);
std::ifstream fragmentShaderFile(fShaderFile);
std::stringstream vShaderStream, fShaderStream;
// Read File
vShaderStream << vertexShaderFile.rdbuf();
fShaderStream << fragmentShaderFile.rdbuf();
// Close files
vertexShaderFile.close();
fragmentShaderFile.close();
//Convert Stream into string.
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
if (gShaderFile != nullptr)
{
std::ifstream geometryShaderFile(gShaderFile);
std::stringstream gShaderStream;
gShaderStream << geometryShaderFile.rdbuf();
geometryShaderFile.close();
geometryCode = gShaderStream.str();
}
}
catch (std::exception e)
{
std::cout << "ERROR Shader file not found." << std::endl;
}
const char *vShaderCode = vertexCode.c_str();
const char *fShaderCode = fragmentCode.c_str();
const char* gShaderCode = geometryCode.c_str();
Shader shader;
shader.Compile(vShaderCode, fShaderCode, gShaderFile != nullptr ? gShaderCode : nullptr);
return shader;
}
Texture ResourceManager::loadTextureFromFile(const char *file, bool alpha)
{
Texture texture;
if (alpha)
{
texture.Internal_Format = GL_RGBA;
texture.Image_Format = GL_RGBA;
}
//Load Image
int width, height, nrChannels;
auto* data = stbi_load(file, &width, &height, &nrChannels, 0);
std::cout << file << std::endl;
if (stbi_failure_reason())
std::cout << stbi_failure_reason() << std::endl;
if (data == NULL)
std::cout << "NULL TEXTURE DETECTED.\n";
// Generate Texture
texture.Generate(width, height, data);
// Free Image data
stbi_image_free(data);
return texture;
}