-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceManager.cpp
More file actions
113 lines (101 loc) · 3.01 KB
/
ResourceManager.cpp
File metadata and controls
113 lines (101 loc) · 3.01 KB
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
#include "ResourceManager.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <SOIL/soil.h>
ResourceManager& ResourceManager::getInstance()
{
static ResourceManager instance;
return instance;
}
void ResourceManager::init()
{
getInstance();
}
void ResourceManager::clear()
{
for (auto& var : shaders_)
glDeleteProgram(var.second.id_);
for (auto& var : textures_)
glDeleteTextures(1, &var.second.id_);
}
Shader& ResourceManager::loadShader(const GLchar* vShaderFile, const GLchar* fShaderFile, const GLchar* gShaderFile, std::string name)
{
shaders_[name] = loadShaderFromFile(vShaderFile, fShaderFile, gShaderFile);
return shaders_[name];
}
Shader& ResourceManager::getShader(std::string name)
{
return shaders_[name];
}
Texture2D& ResourceManager::loadTexture(const GLchar* file, GLboolean alpha, std::string name)
{
textures_[name] = loadTextureFromFile(file, alpha);
return textures_[name];
}
Texture2D& ResourceManager::getTexture(std::string name)
{
return textures_[name];
}
Shader ResourceManager::loadShaderFromFile(const GLchar* vShaderFile, const GLchar* fShaderFile, const GLchar* gShaderFile)
{
// 1. Retrieve the vertex/fragment source code from filePath
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's buffer contents into streams
vShaderStream << vertexShaderFile.rdbuf();
fShaderStream << fragmentShaderFile.rdbuf();
// close file handlers
vertexShaderFile.close();
fragmentShaderFile.close();
// Convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
// If geometry shader path is present, also load a geometry shader
if (gShaderFile != nullptr)
{
std::ifstream geometryShaderFile(gShaderFile);
std::stringstream gShaderStream;
gShaderStream << geometryShaderFile.rdbuf();
geometryShaderFile.close();
geometryCode = gShaderStream.str();
}
}
catch (std::exception e)
{
std::cerr << "ERROR::SHADER: Failed to read shader files" << std::endl;
}
const GLchar *vShaderCode = vertexCode.c_str();
const GLchar *fShaderCode = fragmentCode.c_str();
const GLchar *gShaderCode = geometryCode.c_str();
// 2. Now create shader object from source code
Shader shader;
shader.compile(vShaderCode, fShaderCode, gShaderFile != nullptr ? gShaderCode : nullptr);
return shader;
}
Texture2D ResourceManager::loadTextureFromFile(const GLchar* file, GLboolean alpha)
{
// Create Texture object
Texture2D texture;
if (alpha)
{
texture.internalFormat_ = GL_RGBA;
texture.imageFormat_ = GL_RGBA;
}
// Load image
int width;
int height;
unsigned char* image = SOIL_load_image(file, &width, &height, nullptr, texture.imageFormat_ == GL_RGBA ? SOIL_LOAD_RGBA : SOIL_LOAD_RGB);
// Now generate texture
texture.generate(width, height, image);
// And finally free image data
SOIL_free_image_data(image);
return texture;
}