The small3d library
Renderer.hpp
Go to the documentation of this file.
1 
10 #pragma once
11 #include <vector>
12 
13 #define GLEW_NO_GLU
14 #include <GL/glew.h>
15 #include <GLFW/glfw3.h>
16 #ifdef _WIN32
17 #define GLFW_EXPOSE_NATIVE_WIN32
18 #include <GLFW/glfw3native.h>
19 #endif
20 
21 #include "Logger.hpp"
22 #include "Image.hpp"
23 #include "Model.hpp"
24 #include "SceneObject.hpp"
25 #include <unordered_map>
26 #include "Math.h"
27 #include <ft2build.h>
28 #include FT_FREETYPE_H
29 
30 namespace small3d
31 {
32 
37  class Renderer
38  {
39 
40  private:
41 
42  GLFWwindow* window;
43 
44  static int realScreenWidth, realScreenHeight;
45 
46  uint32_t shaderProgram = 0;
47 
48  uint32_t vao = 0;
49 
50  uint32_t renderOrientation = 0;
51  uint32_t cameraOrientation = 0;
52  uint32_t worldDetails = 0;
53  uint32_t lightUboId = 0;
54  uint32_t perspColourUboId = 0;
55  uint32_t orthoColourUboId = 0;
56 
57  bool noShaders;
58 
59  float fieldOfView = 0.0f;
60  float zNear = 0.0f;
61  float zFar = 0.0f;
62 
63  Vec4 clearColour = Vec4(0.0f, 0.0f, 0.0f, 1.0f);
64 
65  std::unordered_map<std::string, uint32_t> textures;
66 
67  FT_Library library = 0;
68  std::vector<uint8_t> textMemory;
69  std::unordered_map<std::string, FT_Face> fontFaces;
70 
71  Mat4 cameraTransformation = Mat4(1.0f);
72  Vec3 cameraRotationXYZ = Vec3(0.0f);
73  bool cameraRotationByMatrix = false;
74 
75  static void framebufferSizeCallback(GLFWwindow* window, int width,
76  int height);
77 
78  std::string loadShaderFromFile(const std::string& fileLocation) const;
79  uint32_t compileShader(const std::string& shaderSourceFile,
80  const uint32_t shaderType) const;
81  std::string getProgramInfoLog(const uint32_t linkedProgram) const;
82  std::string getShaderInfoLog(const uint32_t shader) const;
83  void initOpenGL();
84  void checkForOpenGLErrors(const std::string& when, const bool abort) const;
85 
86  void transform(Model& model, Vec3& offset,
87  const Mat4& rotation, uint64_t currentPose) const;
88 
89  uint32_t getTextureHandle(const std::string& name) const;
90  uint32_t generateTexture(const std::string& name, const uint8_t* data,
91  const unsigned long width,
92  const unsigned long height,
93  const bool replace);
94 
95  void init(const int width, const int height, const std::string& windowTitle,
96  const std::string& shadersPath);
97  void initWindow(int& width, int& height,
98  const std::string& windowTitle = "");
99 
100  void setWorldDetails(bool perspective);
101 
102  void bindTexture(const std::string& name);
103 
104  void clearScreen() const;
105 
106  Renderer(const std::string& windowTitle, const int width, const int height,
107  const float fieldOfView, const float zNear, const float zFar,
108  const std::string& shadersPath,
109  const uint32_t objectsPerFrame,
110  const uint32_t objectsPerFrameInc);
111 
112  Renderer();
113 
114  std::vector<std::tuple< Model*, Vec3, Mat4, Vec4, std::string, bool, uint64_t>> renderList;
115 
116  void renderTuple(std::tuple< Model*, Vec3, Mat4, Vec4, std::string, bool, uint64_t> tuple);
117 
118  GLuint depthMapFramebuffer = 0;
119  GLuint depthMapTexture = 0;
120 
121  const uint32_t depthMapTextureWidth = 2048;
122  const uint32_t depthMapTextureHeight = 2048;
123  Mat4 lightSpaceMatrix = Mat4(0);
124  bool renderingDepthMap = false;
125 
126 
127 #ifdef _WIN32
128  void captureScreen();
129 #endif
130 
131  public:
136  GLint origRenderbuffer = 0;
137 
142  GLint origFramebuffer = 0;
143 
147  bool shadowsActive = false;
148 
153  void start(const std::string& windowTitle, const int width, const int height,
154  const float fieldOfView, const float zNear, const float zFar,
155  const std::string& shadersPath,
156  const uint32_t objectsPerFrame,
157  const uint32_t objectsPerFrameInc);
158 
163  void stop();
164 
169  Vec3 lightDirection = Vec3(0.0f, 0.7f, 0.3f);
170 
175  float shadowSpaceSize = 20.0f;
176 
181  rotate(Mat4(1.0f), 1.57f, Vec3(1.0f, 0.0f, 0.0f)) *
182  translate(Mat4(1.0f), Vec3(0.0f, -10.0f, 0.0f));
183 
189 
195  void setCameraRotation(const Vec3& rotation);
196 
202  void rotateCamera(const Vec3& rotation);
203 
209  void setCameraTransformation(const Mat4& transformation);
210 
216  const Vec3 getCameraOrientation() const;
217 
225  const Mat4 getCameraRotation() const;
226 
235  const Vec3 getCameraRotationXYZ() const;
236 
241  int getScreenWidth();
242 
247  int getScreenHeight();
248 
252  float lightIntensity = 1.0f;
253 
288  static Renderer& getInstance(const std::string& windowTitle = "",
289  const int width = 0,
290  const int height = 0,
291  const float fieldOfView = 0.785,
292  const float zNear = 1.0f,
293  const float zFar = 24.0f,
294  const std::string& shadersPath =
295 
296  "resources/shaders/",
297 
298  const uint32_t objectsPerFrame = 200,
299  const uint32_t objectsPerFrameInc = 1000);
300 
304  ~Renderer();
305 
309  GLFWwindow* getWindow() const;
310 
316  void generateTexture(const std::string& name, const Image& image);
317 
329  void generateTexture(const std::string& name, const std::string& text,
330  const Vec3& colour,
331  const int fontSize = 48,
332  const std::string& fontPath =
333 
334  "resources/fonts/CrusoeText/CrusoeText-Regular.ttf",
335 
336  const bool replace = true);
337 
343  void deleteTexture(const std::string& name);
344 
352  void createRectangle(Model& rect,
353  const Vec3& topLeft,
354  const Vec3& bottomRight);
355 
373  void render(Model& model, const Vec3& position, const Vec3& rotation,
374  const Vec4& colour, const std::string& textureName = "",
375  const uint64_t currentPose = 0,
376  const bool perspective = true);
377 
387  void render(Model& model, const Vec3& position, const Vec3& rotation,
388  const std::string& textureName, const uint64_t currentPose = 0);
389 
407  void render(Model& model, const Vec3& position, const Mat4& rotation,
408  const Vec4& colour, const std::string& textureName = "",
409  const uint64_t currentPose = 0,
410  const bool perspective = true);
411 
421  void render(Model& model, const Vec3& position, const Mat4& rotation,
422  const std::string& textureName, const uint64_t currentPose = 0);
423 
436  void render(Model& model, const std::string& textureName, const uint64_t currentPose = 0,
437  const bool perspective = true);
438 
450  void render(Model& model, const Vec4& colour, const uint64_t currentPose = 0,
451  const bool perspective = true);
452 
458  void render(SceneObject& sceneObject, const Vec4& colour);
459 
466  void render(SceneObject& sceneObject, const std::string& textureName);
467 
473  void clearBuffers(Model& model) const;
474 
480  void clearBuffers(SceneObject& sceneObject) const;
481 
486  void setBackgroundColour(const Vec4& colour);
487 
492  void swapBuffers();
493 
494 #ifdef _WIN32
495 
503  bool screenCapture = false;
504 #endif
505 
506  Renderer(Renderer const&) = delete;
507  void operator=(Renderer const&) = delete;
508  Renderer(Renderer&&) = delete;
509  void operator=(Renderer&&) = delete;
510 
511  };
512 
513 }
small3d::Renderer::getCameraRotation
const Mat4 getCameraRotation() const
: Get the rotation of the camera by transformation matrix
Definition: Renderer.cpp:637
small3d::Vec4
4 component vector
Definition: Math.hpp:56
small3d::Renderer::~Renderer
~Renderer()
Destructor.
Definition: Renderer.cpp:663
small3d::Renderer::swapBuffers
void swapBuffers()
This is a double buffered system and this command swaps the buffers.
Definition: Renderer.cpp:1141
small3d::Renderer::shadowSpaceSize
float shadowSpaceSize
Size of the shadows space (half-edge of the orthographic projection cube)
Definition: Renderer.hpp:175
Model.hpp
A 3D model class.
small3d::Renderer::setCameraTransformation
void setCameraTransformation(const Mat4 &transformation)
: Set the camera by transformation matrix
Definition: Renderer.cpp:626
small3d::Renderer::start
void start(const std::string &windowTitle, const int width, const int height, const float fieldOfView, const float zNear, const float zFar, const std::string &shadersPath, const uint32_t objectsPerFrame, const uint32_t objectsPerFrameInc)
: Used to re-initialise the Renderer. When Android was supported it was used in apps after they came ...
Definition: Renderer.cpp:574
small3d::Renderer::rotateCamera
void rotateCamera(const Vec3 &rotation)
: Modify the rotation of the camera
Definition: Renderer.cpp:614
small3d::Renderer::getCameraOrientation
const Vec3 getCameraOrientation() const
: Get the orientation of the camera
Definition: Renderer.cpp:632
small3d::Renderer::render
void render(Model &model, const Vec3 &position, const Vec3 &rotation, const Vec4 &colour, const std::string &textureName="", const uint64_t currentPose=0, const bool perspective=true)
Render a Model.
Definition: Renderer.cpp:866
small3d::Renderer::getScreenHeight
int getScreenHeight()
Get the real screen height.
Definition: Renderer.cpp:55
small3d::Renderer::shadowsActive
bool shadowsActive
: Render shadows?
Definition: Renderer.hpp:147
small3d::Renderer::stop
void stop()
: Used to shutdown the renderer. When Android was supported it was used in apps when they lost focus.
Definition: Renderer.cpp:680
small3d::Model
A 3D model. It can be loaded from a WavefrontFile or GlbFile. It can also be constructed procedurally...
Definition: Model.hpp:34
small3d::Renderer::lightDirection
Vec3 lightDirection
Vector, indicating the direction of the light in the scene. It points towards a directional light sou...
Definition: Renderer.hpp:169
small3d::Renderer::shadowCamTransformation
Mat4 shadowCamTransformation
Shadow camera transformation.
Definition: Renderer.hpp:180
small3d::Renderer::deleteTexture
void deleteTexture(const std::string &name)
Deletes the texture indicated by the given name.
Definition: Renderer.cpp:824
small3d::Renderer::origRenderbuffer
GLint origRenderbuffer
: Set to the id of the original renderbuffer, in case it is temporarily replaced during shadow mappin...
Definition: Renderer.hpp:136
Image.hpp
Image loading and manipulation.
small3d::Renderer::lightIntensity
float lightIntensity
The light intensity (set to -1.0f if no lighting is to be used).
Definition: Renderer.hpp:252
small3d::Renderer::clearBuffers
void clearBuffers(Model &model) const
Clear a Model from the GPU buffers (the Model itself remains intact).
Definition: Renderer.cpp:1108
small3d::Renderer
Renderer class (OpenGL 3.3 / OpenGL ES 3.0)
Definition: Renderer.hpp:38
small3d::Renderer::setBackgroundColour
void setBackgroundColour(const Vec4 &colour)
Set the background colour of the screen.
Definition: Renderer.cpp:1135
small3d::Renderer::getWindow
GLFWwindow * getWindow() const
Get the GLFW window object, associated with the Renderer.
Definition: Renderer.cpp:717
small3d::Renderer::getInstance
static Renderer & getInstance(const std::string &windowTitle="", const int width=0, const int height=0, const float fieldOfView=0.785, const float zNear=1.0f, const float zFar=24.0f, const std::string &shadersPath="resources/shaders/", const uint32_t objectsPerFrame=200, const uint32_t objectsPerFrameInc=1000)
Get the instance of the Renderer (the Renderer is a singleton).
Definition: Renderer.cpp:648
small3d
Definition: BinaryFile.hpp:15
small3d::Renderer::getCameraRotationXYZ
const Vec3 getCameraRotationXYZ() const
: Get the rotation of the camera in x, y, z representation. This will NOT work if the rotation was se...
Definition: Renderer.cpp:641
small3d::Renderer::origFramebuffer
GLint origFramebuffer
: Set to the id of the original framebuffer, in case it is temporarily replaced during shadow mapping...
Definition: Renderer.hpp:142
small3d::Renderer::setCameraRotation
void setCameraRotation(const Vec3 &rotation)
: Set the rotation of the camera
Definition: Renderer.cpp:606
small3d::Image
An image, loaded from a .png file, which can be used for generating textures.
Definition: Image.hpp:29
small3d::Renderer::cameraPosition
Vec3 cameraPosition
The camera position in world space. Ignored for orthographic rendering.
Definition: Renderer.hpp:188
small3d::Vec3
3 component float vector
Definition: Math.hpp:19
small3d::Renderer::createRectangle
void createRectangle(Model &rect, const Vec3 &topLeft, const Vec3 &bottomRight)
Populates a Model object with a rectangle stretching between the given coordinates.
Definition: Renderer.cpp:834
SceneObject.hpp
An object on the small3d scene.
small3d::Renderer::getScreenWidth
int getScreenWidth()
Get the real screen width.
Definition: Renderer.cpp:51
small3d::SceneObject
An object that appears on the 3D scene. It is made up of a Model, together with information for posit...
Definition: SceneObject.hpp:38
small3d::Mat4
4x4 float matrix
Definition: Math.hpp:99
Logger.hpp
Logger used in small3d.