The small3d library
Tiny C++ 3D game development library for Win/MacOS/Linux/FreeBSD
Loading...
Searching...
No Matches
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
16#include "Windowing.hpp"
17#include "Logger.hpp"
18#include "Image.hpp"
19#include "Model.hpp"
20#include "SceneObject.hpp"
21#include <unordered_map>
22#include "Math.hpp"
23#include <ft2build.h>
24#include FT_FREETYPE_H
25
26namespace small3d
27{
28
34 {
35
36 private:
37
38 Windowing windowing;
39
40 uint32_t shaderProgram = 0;
41
42 uint32_t vao = 0;
43
44 uint32_t renderOrientation = 0;
45 uint32_t cameraOrientation = 0;
46 uint32_t worldDetails = 0;
47 uint32_t lightUboId = 0;
48 uint32_t perspColourUboId = 0;
49 uint32_t orthoColourUboId = 0;
50
51 bool noShaders;
52
53 float fieldOfView = 0.0f;
54 float zNear = 0.0f;
55 float zFar = 0.0f;
56
57 Vec4 clearColour = Vec4(0.0f, 0.0f, 0.0f, 1.0f);
58
59 std::unordered_map<std::string, uint32_t> textures;
60
61 FT_Library library = 0;
62 std::vector<uint8_t> textMemory;
63 std::unordered_map<std::string, FT_Face> fontFaces;
64
65 Mat4 cameraTransformation = Mat4(1.0f);
66 Vec3 cameraRotationXYZ = Vec3(0.0f);
67 bool cameraRotationByMatrix = false;
68
69 std::string loadShaderFromFile(const std::string& fileLocation) const;
70 uint32_t compileShader(const std::string& shaderSourceFile,
71 const uint32_t shaderType) const;
72 std::string getProgramInfoLog(const uint32_t linkedProgram) const;
73 std::string getShaderInfoLog(const uint32_t shader) const;
74 void initOpenGL();
75 void checkForOpenGLErrors(const std::string& when, const bool abort) const;
76
77 void transform(Model& model, Vec3& offset,
78 const Mat4& rotation, uint64_t currentPose) const;
79
80 uint32_t getTextureHandle(const std::string& name) const;
81 uint32_t generateTexture(const std::string& name, const uint8_t* data,
82 const unsigned long width,
83 const unsigned long height,
84 const bool replace);
85
86 void init(const int width, const int height, const std::string& windowTitle,
87 const std::string& shadersPath);
88
89 void setWorldDetails(bool perspective);
90
91 void bindTexture(const std::string& name);
92
93 void clearScreen() const;
94
95 Renderer(const std::string& windowTitle, const int width, const int height,
96 const float fieldOfView, const float zNear, const float zFar,
97 const std::string& shadersPath,
98 const uint32_t objectsPerFrame,
99 const uint32_t objectsPerFrameInc);
100
101 Renderer();
102
103 std::vector<std::tuple< Model*, Vec3, Mat4, Vec4, std::string, bool, uint64_t>> renderList;
104
105 void renderTuple(std::tuple< Model*, Vec3, Mat4, Vec4, std::string, bool, uint64_t> tuple);
106
107 GLuint depthMapFramebuffer = 0;
108 GLuint depthMapTexture = 0;
109
110 const uint32_t depthMapTextureWidth = 2048;
111 const uint32_t depthMapTextureHeight = 2048;
112 Mat4 lightSpaceMatrix = Mat4(0);
113 bool renderingDepthMap = false;
114
115
116#ifdef _WIN32
117 void captureScreen();
118#endif
119
120 public:
121
127
133
137 bool shadowsActive = false;
138
143 void start(const std::string& windowTitle, const int width, const int height,
144 const float fieldOfView, const float zNear, const float zFar,
145 const std::string& shadersPath,
146 const uint32_t objectsPerFrame,
147 const uint32_t objectsPerFrameInc);
148
153 void stop();
154
159 Vec3 lightDirection = Vec3(0.0f, 0.7f, 0.3f);
160
165 float shadowSpaceSize = 20.0f;
166
171 rotate(Mat4(1.0f), 1.57f, Vec3(1.0f, 0.0f, 0.0f)) *
172 translate(Mat4(1.0f), Vec3(0.0f, -10.0f, 0.0f));
173
179
185 void setCameraRotation(const Vec3& rotation);
186
192 void rotateCamera(const Vec3& rotation);
193
199 void setCameraTransformation(const Mat4& transformation);
200
206 const Vec3 getCameraOrientation() const;
207
215 const Mat4 getCameraRotation() const;
216
225 const Vec3 getCameraRotationXYZ() const;
226
230 float lightIntensity = 1.0f;
231
266 static Renderer& getInstance(const std::string& windowTitle = "",
267 const int width = 0,
268 const int height = 0,
269 const float fieldOfView = 0.785,
270 const float zNear = 1.0f,
271 const float zFar = 24.0f,
272 const std::string& shadersPath =
273
274 "resources/shaders/",
275
276 const uint32_t objectsPerFrame = 200,
277 const uint32_t objectsPerFrameInc = 1000);
278
282 ~Renderer();
283
289 void generateTexture(const std::string& name, const Image& image);
290
302 void generateTexture(const std::string& name, const std::string& text,
303 const Vec3& colour,
304 const int fontSize = 48,
305 const std::string& fontPath =
306
307 "resources/fonts/CrusoeText/CrusoeText-Regular.ttf",
308
309 const bool replace = true);
310
316 void deleteTexture(const std::string& name);
317
325 void createRectangle(Model& rect,
326 const Vec3& topLeft,
327 const Vec3& bottomRight);
328
346 void render(Model& model, const Vec3& position, const Vec3& rotation,
347 const Vec4& colour, const std::string& textureName = "",
348 const uint64_t currentPose = 0,
349 const bool perspective = true);
350
360 void render(Model& model, const Vec3& position, const Vec3& rotation,
361 const std::string& textureName, const uint64_t currentPose = 0);
362
380 void render(Model& model, const Vec3& position, const Mat4& rotation,
381 const Vec4& colour, const std::string& textureName = "",
382 const uint64_t currentPose = 0,
383 const bool perspective = true);
384
394 void render(Model& model, const Vec3& position, const Mat4& rotation,
395 const std::string& textureName, const uint64_t currentPose = 0);
396
409 void render(Model& model, const std::string& textureName, const uint64_t currentPose = 0,
410 const bool perspective = true);
411
423 void render(Model& model, const Vec4& colour, const uint64_t currentPose = 0,
424 const bool perspective = true);
425
431 void render(SceneObject& sceneObject, const Vec4& colour);
432
439 void render(SceneObject& sceneObject, const std::string& textureName);
440
446 void clearBuffers(Model& model) const;
447
453 void clearBuffers(SceneObject& sceneObject) const;
454
459 void setBackgroundColour(const Vec4& colour);
460
465 void swapBuffers();
466
471 GLFWwindow* getWindow();
472
473#ifdef _WIN32
482 bool screenCapture = false;
483#endif
484
485 Renderer(Renderer const&) = delete;
486 void operator=(Renderer const&) = delete;
487 Renderer(Renderer&&) = delete;
488 void operator=(Renderer&&) = delete;
489
490 };
491
492}
Image loading and manipulation.
Logger used in small3d.
Vectors, matrices and other math things.
A 3D model class.
An object on the small3d scene.
Window management class.
An image, loaded from a .png file, which can be used for generating textures.
Definition Image.hpp:29
A 3D model. It can be loaded from a WavefrontFile or GlbFile. It can also be constructed procedurally...
Definition Model.hpp:34
Renderer class (OpenGL 3.3 / OpenGL ES 3.0)
Definition Renderer.hpp:34
Vec3 cameraPosition
The camera position in world space. Ignored for orthographic rendering.
Definition Renderer.hpp:178
Mat4 shadowCamTransformation
Shadow camera transformation.
Definition Renderer.hpp:170
void stop()
Used to shutdown the renderer. When Android was supported it was used in apps when they lost focus.
Definition Renderer.cpp:576
float lightIntensity
The light intensity (set to -1.0f if no lighting is to be used).
Definition Renderer.hpp:230
const Mat4 getCameraRotation() const
: Get the rotation of the camera by transformation matrix
Definition Renderer.cpp:534
~Renderer()
Destructor.
Definition Renderer.cpp:560
const Vec3 getCameraOrientation() const
: Get the orientation of the camera
Definition Renderer.cpp:529
void setCameraRotation(const Vec3 &rotation)
: Set the rotation of the camera
Definition Renderer.cpp:503
float shadowSpaceSize
Size of the shadows space (half-edge of the orthographic projection cube)
Definition Renderer.hpp:165
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:726
void setCameraTransformation(const Mat4 &transformation)
: Set the camera by transformation matrix
Definition Renderer.cpp:523
void setBackgroundColour(const Vec4 &colour)
Set the background colour of the screen.
Definition Renderer.cpp:1027
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:758
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:538
GLint origFramebuffer
Set to the id of the original framebuffer, in case it is temporarily replaced during shadow mapping.
Definition Renderer.hpp:132
void deleteTexture(const std::string &name)
Deletes the texture indicated by the given name.
Definition Renderer.cpp:716
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 ba...
Definition Renderer.cpp:471
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:545
void clearBuffers(Model &model) const
Clear a Model from the GPU buffers (the Model itself remains intact).
Definition Renderer.cpp:1000
void swapBuffers()
This is a double buffered system and this command swaps the buffers.
Definition Renderer.cpp:1033
void rotateCamera(const Vec3 &rotation)
: Modify the rotation of the camera
Definition Renderer.cpp:511
Vec3 lightDirection
Vector, indicating the direction of the light in the scene. It points towards a directional light sou...
Definition Renderer.hpp:159
GLint origRenderbuffer
Set to the id of the original renderbuffer, in case it is temporarily replaced during shadow mapping.
Definition Renderer.hpp:126
GLFWwindow * getWindow()
Get the GLFW window used by the renderer.
Definition Renderer.cpp:1101
bool shadowsActive
Render shadows?
Definition Renderer.hpp:137
An object that appears on the 3D scene. It is made up of a Model, together with information for posit...
Definition SceneObject.hpp:38
Window management class.
Definition Windowing.hpp:27
Definition BinaryFile.hpp:15
4x4 float matrix
Definition Math.hpp:112
3 component float vector
Definition Math.hpp:21
4 component vector
Definition Math.hpp:68