SimpleGL  1.1.0
A framework for platform independent rendering
manager_gl.cpp
Go to the documentation of this file.
1 
9 
10 #include <fstream>
11 #include <map>
12 
13 #define STB_IMAGE_IMPLEMENTATION
14 #include <stb_image.h>
15 
16 #include <SimpleGL/logging.hpp>
17 
18 using namespace std;
19 
20 namespace sgl {
21 
22  void TextureManager::remove(std::string name) {
23  /* Dispose Texture */
24  Texture* texture = get(name);
25  delete texture;
26 
27  /* Call base method */
28  Manager::remove(name);
29  }
30 
31  void TextureManager::clear() {
32  /* Dispose all textures */
33  for (map<string, Texture*>::iterator it = objects.begin(); it != objects.end(); it++) {
34  Texture* texture = it->second;
35  delete texture;
36  }
37 
38  /* Call base method */
39  Manager::clear();
40  }
41 
42  Texture* TextureManager::load(std::string path, std::string name) {
43  /* Load bitmap file and create image */
44  stbi_set_flip_vertically_on_load(true);
45  TextureData image;
46  image.pixels = stbi_load(path.c_str(), &image.width, &image.height, &image.components, 4);
47 
48  /* Generate Texture */
49  Texture* texture = new Texture();
50  texture->bind();
51 
52  /* Set texture parameters */
53  texture->setWrapS(TextureWrap::CLAMP_TO_BORDER);
54  texture->setWrapT(TextureWrap::CLAMP_TO_BORDER);
55  texture->setMinFilter(TextureFilter::LINEAR);
56  texture->setMagFilter(TextureFilter::LINEAR);
57 
58  /* Upload texture data */
59  texture->uploadImage(image);
60  stbi_image_free(image.pixels);
61 
62  /* Put texture in storage and return it */
63  put(name, texture);
64  return texture;
65  }
66 
67  void ShaderManager::remove(std::string name) {
68  /* Dispose Shader */
69  Shader* shader = get(name);
70  delete shader;
71 
72  /* Call base method */
73  Manager::remove(name);
74  }
75 
76  void ShaderManager::clear() {
77  /* Dispose all shaders */
78  for (map<string, Shader*>::iterator it = objects.begin(); it != objects.end(); it++) {
79  Shader* shader = it->second;
80  delete shader;
81  }
82 
83  /* Call base method */
84  Manager::clear();
85  }
86 
87  Shader* ShaderManager::loadShader(std::string path, std::string name, ShaderType type) {
88  /* Opening file stream */
89  ifstream fileStream(path);
90  if (fileStream.is_open()) {
91  /* Reading line by line */
92  string source;
93  while (fileStream.good()) {
94  string line;
95  getline(fileStream, line);
96  source.append(line + "\n");
97  }
98  fileStream.close();
99 
100  /* Generate Shader */
101  Shader* shader = new Shader(type);
102 
103  /* Compile Shader */
104  shader->source(source);
105  shader->compile();
106  if (!shader->getCompileStatus()) {
107  Logger::logError(shader->getInfoLog());
108  }
109 
110  /* Store shader and return it */
111  put(name, shader);
112  return shader;
113  } else {
114  Logger::logError("Could not open file " + path);
115  return new Shader(type);
116  }
117  }
118 
119  ShaderProgram* ShaderManager::loadShaderProgram(std::string vertexPath, std::string fragmentPath, std::string vertexName, std::string fragmentName) {
120  /* Load shaders */
121  Shader* vertexShader = loadShader(vertexPath, vertexName, ShaderType::VERTEX_SHADER);
122  Shader* fragmentShader = loadShader(fragmentPath, fragmentName, ShaderType::FRAGMENT_SHADER);
123 
124  /* Link shaders */
125  ShaderProgram* shaderProgram = new ShaderProgram();
126  shaderProgram->attach(vertexShader);
127  shaderProgram->attach(fragmentShader);
128  shaderProgram->link();
129 
130  /* Check status */
131  if (!shaderProgram->getLinkStatus()) {
132  Logger::logError(shaderProgram->getInfoLog());
133  }
134 
135  /* Return program */
136  return shaderProgram;
137  }
138 
139  ShaderProgram* ShaderManager::createShaderProgram(std::string vertexName, std::string fragmentName) {
140  /* Get shaders */
141  Shader* vertexShader = get(vertexName);
142  Shader* fragmentShader = get(fragmentName);
143 
144  /* Link shaders */
145  ShaderProgram* shaderProgram = new ShaderProgram();
146  shaderProgram->attach(vertexShader);
147  shaderProgram->attach(fragmentShader);
148  shaderProgram->link();
149 
150  /* Check status */
151  if (!shaderProgram->getLinkStatus()) {
152  Logger::logError(shaderProgram->getInfoLog());
153  }
154 
155  /* Return program */
156  return shaderProgram;
157  }
158 }
This structure stores texture image data.
This class wraps an OpenGL shader object.
unsigned char * pixels
The pixel data of the image.
ShaderType
This enum wraps the shader types.
int height
The height of the image.
void compile()
Compiles the shader object.
void source(std::string source)
Sets the source code of the shader object.
This class wraps an OpenGL shader program.
int components
The number of color channels.
Generic namespace for the SimpleGL framework.
Definition: application.hpp:18
This class wraps an OpenGL texture object.
void attach(Shader *shader)
Attaches a shader object to the shader program.
bool getLinkStatus()
Checks the link status of the shader program.
std::string getInfoLog()
Gets the info log of the shader program.
bool getCompileStatus()
Checks the compile status of the shader object.
std::string getInfoLog()
Gets the info log of the shader object.
void link()
Links the shader program.
int width
The width of the image.