The small3d framework
Renderer.hpp
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #define GLEW_NO_GLU
13 #include <GL/glew.h>
14 #include <GLFW/glfw3.h>
15 
16 #include "Logger.hpp"
17 #include "Image.hpp"
18 #include "Model.hpp"
19 #include "SceneObject.hpp"
20 
21 #include <unordered_map>
22 #include <vector>
23 
24 #define GLM_FORCE_RADIANS
25 #include <glm/glm.hpp>
26 #include <ft2build.h>
27 #include FT_FREETYPE_H
28 
29 namespace small3d
30 {
31 
36  class Renderer
37  {
38 
39  private:
40 
41  GLFWwindow* window;
42 
43  static int realScreenWidth, realScreenHeight;
44 
45  uint32_t perspectiveProgram = 0;
46  uint32_t orthographicProgram = 0;
47  uint32_t vao = 0;
48 
49  uint32_t renderOrientation = 0;
50  uint32_t cameraOrientation = 0;
51  uint32_t worldDetails = 0;
52  uint32_t lightUboId = 0;
53  uint32_t perspColourUboId = 0;
54  uint32_t orthoColourUboId = 0;
55 
56  bool noShaders;
57 
58  float frustumScale = 0.0f;
59  float zNear = 0.0f;
60  float zFar = 0.0f;
61  float zOffsetFromCamera = 0.0f;
62 
63  std::unordered_map<std::string, uint32_t> textures;
64 
65  FT_Library library = 0;
66  std::vector<float> textMemory;
67  std::unordered_map<std::string, FT_Face> fontFaces;
68 
69  static void framebufferSizeCallback(GLFWwindow* window, int width,
70  int height);
71 
72  std::string loadShaderFromFile(const std::string& fileLocation) const;
73  uint32_t compileShader(const std::string& shaderSourceFile,
74  const uint32_t shaderType) const;
75  std::string getProgramInfoLog(const uint32_t linkedProgram) const;
76  std::string getShaderInfoLog(const uint32_t shader) const;
77  void initOpenGL();
78  void checkForOpenGLErrors(const std::string& when, const bool abort) const;
79 
80  void positionNextObject(const glm::vec3& offset,
81  const glm::vec3& rotation) const;
82  void positionCamera() const;
83  uint32_t getTextureHandle(const std::string& name) const;
84  uint32_t generateTexture(const std::string& name, const float* data,
85  const unsigned long width,
86  const unsigned long height,
87  const bool replace);
88 
89  void init(const int width, const int height, const std::string& windowTitle,
90  const std::string& shadersPath);
91  void initWindow(int& width, int& height,
92  const std::string& windowTitle = "");
93 
94  void setPerspectiveAndLight();
95 
96  void bindTexture(const std::string& name, bool perspective);
97 
98  Renderer(const std::string& windowTitle, const int width, const int height,
99  const float frustumScale, const float zNear, const float zFar,
100  const float zOffsetFromCamera, const std::string& shadersPath,
101  const uint32_t maxObjectsPerPass);
102 
103  Renderer();
104 
105  public:
109  glm::vec3 lightDirection;
110 
114  glm::vec3 cameraPosition;
115 
119  glm::vec3 cameraRotation;
120 
124  float lightIntensity = 1.0f;
125 
160  static Renderer& getInstance(const std::string& windowTitle = "",
161  const int width = 0,
162  const int height = 0,
163  const float frustumScale = 1.0f,
164  const float zNear = 1.0f,
165  const float zFar = 24.0f,
166  const float zOffsetFromCamera = -1.0f,
167  const std::string& shadersPath =
168  "resources/shaders/",
169  const uint32_t maxObjectsPerPass = 20);
170 
174  ~Renderer();
175 
179  GLFWwindow* getWindow() const;
180 
186  void generateTexture(const std::string& name, const Image& image);
187 
199  void generateTexture(const std::string& name, const std::string& text,
200  const glm::vec3& colour,
201  const int fontSize = 48,
202  const std::string& fontPath =
203  "resources/fonts/CrusoeText/CrusoeText-Regular.ttf",
204  const bool replace = true);
205 
211  void deleteTexture(const std::string& name);
212 
220  void createRectangle(Model& rect,
221  const glm::vec3& topLeft,
222  const glm::vec3& bottomRight);
223 
239  void render(Model& model, const glm::vec3& offset, const glm::vec3& rotation,
240  const glm::vec4& colour, const std::string& textureName = "",
241  const bool perspective = true);
242 
251  void render(Model& model, const glm::vec3& offset, const glm::vec3& rotation,
252  const std::string& textureName);
253 
261  void render(Model& model, const std::string& textureName,
262  const bool perspective = true);
263 
270  void render(Model& model, const glm::vec4& colour,
271  const bool perspective = true);
272 
278  void render(SceneObject& sceneObject, const glm::vec4& colour);
279 
286  void render(SceneObject& sceneObject, const std::string& textureName);
287 
293  void clearBuffers(Model& model) const;
294 
300  void clearBuffers(SceneObject& sceneObject) const;
301 
305  void clearScreen() const;
306 
311  void clearScreen(const glm::vec4& colour) const;
312 
317  void swapBuffers() const;
318 
319  Renderer(Renderer const&) = delete;
320  void operator=(Renderer const&) = delete;
321  Renderer(Renderer&&) = delete;
322  void operator=(Renderer&&) = delete;
323 
324  };
325 
326 }
void deleteTexture(const std::string &name)
Deletes the texture indicated by the given name.
Definition: Renderer.cpp:662
A 3D model, loaded from a Wavefront file.
Definition: Model.hpp:21
void clearScreen() const
Clears the screen.
Definition: Renderer.cpp:877
Header of the Image class.
void clearBuffers(Model &model) const
Clear a Model from the GPU buffers (the Model itself remains intact).
Definition: Renderer.cpp:850
Header of the SceneObject class.
An image, loaded from a png file, which can be used for generating textures.
Definition: Image.hpp:29
glm::vec3 cameraPosition
The camera position in world space.
Definition: Renderer.hpp:114
glm::vec3 lightDirection
Vector, indicating the direction of the light in the scene.
Definition: Renderer.hpp:109
An object that appears on the 3D scene. It is made up of one or more models (the latter for animation...
Definition: SceneObject.hpp:35
void render(Model &model, const glm::vec3 &offset, const glm::vec3 &rotation, const glm::vec4 &colour, const std::string &textureName="", const bool perspective=true)
Render a Model.
Definition: Renderer.cpp:703
~Renderer()
Destructor.
Definition: Renderer.cpp:528
Renderer class (OpenGL 3.3)
Definition: Renderer.hpp:36
void swapBuffers() const
This is a double buffered system and this command swaps the buffers.
Definition: Renderer.cpp:892
void createRectangle(Model &rect, const glm::vec3 &topLeft, const glm::vec3 &bottomRight)
Populates a Model object with a rectangle stretching between the given coordinates.
Definition: Renderer.cpp:671
glm::vec3 cameraRotation
The camera rotation (around the x, y and z axes)
Definition: Renderer.hpp:119
Header of the Model class.
Header of the Logger class.
static Renderer & getInstance(const std::string &windowTitle="", const int width=0, const int height=0, const float frustumScale=1.0f, const float zNear=1.0f, const float zFar=24.0f, const float zOffsetFromCamera=-1.0f, const std::string &shadersPath="resources/shaders/", const uint32_t maxObjectsPerPass=20)
Get the instance of the Renderer (the Renderer is a singleton).
Definition: Renderer.cpp:514
GLFWwindow * getWindow() const
Get the GLFW window object, associated with the Renderer.
Definition: Renderer.cpp:561
float lightIntensity
The light intensity (set to -1.0f if no lighting is to be used).
Definition: Renderer.hpp:124