-
Notifications
You must be signed in to change notification settings - Fork 5
/
Renderer.hpp
249 lines (198 loc) · 7.46 KB
/
Renderer.hpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Renderer.hpp
*
* Created on: 2014/10/19
* Author: Dimitri Kourkoulis
* http://dimitros.be
* License: BSD 3-Clause License (see LICENSE file)
*/
// bii://dimitrikourk/small3d/resources/shaders/OpenGL33/perspectiveMatrixLightedShader.vert
// bii://dimitrikourk/small3d/resources/shaders/OpenGL33/textureShader.frag
// bii://dimitrikourk/small3d/resources/shaders/OpenGL33/simpleShader.vert
// bii://dimitrikourk/small3d/resources/shaders/OpenGL33/simpleShader.frag
// bii://dimitrikourk/small3d/resources/shaders/OpenGL21/perspectiveMatrixLightedShader.vert
// bii://dimitrikourk/small3d/resources/shaders/OpenGL21/textureShader.frag
// bii://dimitrikourk/small3d/resources/shaders/OpenGL21/simpleShader.vert
// bii://dimitrikourk/small3d/resources/shaders/OpenGL21/simpleShader.frag
#pragma once
#ifndef SDLANDOPENGL
#define SDLANDOPENGL
#include <dimitrikourk/glew/include/GL/glew.h>
#include <miguel/sdl2/include/SDL_opengl.h>
#include <miguel/sdl2/include/SDL.h>
#endif //SDLANDOPENGL
#include <memory>
#include "SceneObject.hpp"
#include <vector>
#include "Logger.hpp"
#include <unordered_map>
#include <dimitrikourk/glm/glm/glm.hpp>
using namespace std;
namespace small3d
{
/**
* @class Renderer
*
* @brief Renderer class, which can render using either OpenGL v3.3 or v2.1
*
*/
class Renderer
{
private:
SDL_Window* sdlWindow;
GLuint perspectiveProgram;
GLuint orthographicProgram;
bool isOpenGL33Supported;
bool noShaders;
float frustumScale;
float zNear;
float zFar;
float zOffsetFromCamera;
/**
* Load a shader's source code from a file into a string
* @param fileLocation The file's location, relative to the game path
* @return String containing the shader's source code
*/
string loadShaderFromFile(const string &fileLocation);
/**
* Compile a shader's source code
* @param shaderSource String containing the shader's source code
* @param shaderType Type of shader (GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, GL_GEOMETRY_SHADER - the latter for OpenGL 3.3)
* @return OpenGL shader reference
*/
GLuint compileShader(const string &shaderSource, const GLenum shaderType);
/**
* Initialise SDL
*/
void initSDL(int width, int height, bool fullScreen);
/**
* Retrieve the information of what went wrong when linking a shader program
*/
string getProgramInfoLog(const GLuint linkedProgram) const;
/**
* Detect if OpenGL 3.3 is supported. If not, fall back to OpenGL 2.1.
* If neither of the two is supported, an exception is raised.
*/
void detectOpenGLVersion();
void checkForOpenGLErrors(string when, bool abort);
/**
* Textures used in the scene, each corresponding to the name of one of
* the rendered models
*/
unordered_map<string, GLuint> *textures;
public:
/**
* Constructor
*/
Renderer();
/**
* Initialise renderer (OpenGL, GLEW, etc)
* @param width The width of the window
* @param height The height of the window
* @param fullScreen Will the Renderer run in full screen mode? If set to true,
* the width and height have to correspond to a resolution supported
* by the user's screen in order for the scene to be displayed properly.
* @param frustumScale How much the frustum scales the items rendered
* @param zNear Projection plane z coordinate (use positive value)
* @param zFar Far end of frustum z coordinate (use positive value)
* @param zOffsetFromCamera The position of the projection plane with regard to the camera.
* @param shadersPath The path where the shaders will be stored, relative
* to the application's executing directory. It
* defaults to the path provided by the engine, but
* it can be changed, so as to accommodate for executables
* which are going to be using it. Even though the path to
* the folder can be changed, the folder structure within
* it and the names of the shaders must remain as provided.
* The shader code can be changed, provided that their inputs
* and outputs are maintained the same.
*/
void init(const int width, const int height, const bool fullScreen,
const float &frustumScale = 1.0f, const float &zNear = 1.0f,
const float &zFar = 24.0f, const float &zOffsetFromCamera = -1.0f,
const string &shadersPath = "dimitrikourk/small3d/resources/shaders/");
/**
* @brief Vector indicating the direction of the light in the scene.
*/
glm::vec3 lightDirection;
/**
* @brief The camera position in world space.
*/
glm::vec3 cameraPosition;
/**
* @brief The camera rotation (around the x, y and z axes)
*/
glm::vec3 cameraRotation;
/**
* @brief The light intensity (set to -1.0f if no lighting is to be used).
*/
float lightIntensity;
/**
* Generate a texture in OpenGL, using the given data
* @param name The name by which the texture will be known
* @param texture The texture data
* @param width The width of the texture, in pixels
* @param height The height of the texture, in pixels
* @return The texture handle
*/
GLuint generateTexture(const string &name, const float *texture, const int width, const int height);
/**
* @fn void Renderer::deleteTexture(const string &name);
*
* @brief Deletes the texture indicated by the given name.
*
* @param name The name of the texture.
*/
void deleteTexture(const string &name);
/**
* Get the handle of a texture which has already been generated (see generateTexture)
* @param name The name of the texture
* @return The texture handle (0 if not found)
*/
GLuint getTextureHandle(const string &name);
/**
* @fn void Renderer::positionSceneObject(const glm::vec3 &offset, const glm::vec3 &rotation);
*
* @brief Positions the scene object (sets offset and rotation)
*
* @param offset The offset (x, y, z values).
* @param rotation The rotation (around the x, y and z axes).
*/
void positionSceneObject(const glm::vec3 &offset, const glm::vec3 &rotation);
/**
* @fn void Renderer::positionCamera();
*
* @brief Position the camera.
*
*/
void positionCamera();
/**
* Render an image. The image is in effect a textured quad, since 4 vertex positions are
* passed to this method, in order to define its position and size.
* @param vertices The vertices
* @param textureName The name of the texture, containing the image (must have been loaded with
* generateTexture())
* @param perspective If set to true, use perspective rendering, otherwise use simple (orthographic) rendering.
* @param offset The offset (position) at which the quad of the image will be drawn.
*/
void renderImage(const float *vertices, const string &textureName, const bool &perspective = false,
const glm::vec3 &offset = glm::vec3(0.0f, 0.0f, 0.0f));
/**
* Render a scene object
* @param sceneObject The scene object
*/
void renderSceneObject(shared_ptr<SceneObject> sceneObject);
/**
* Clears the screen.
*/
void clearScreen();
/**
* This is a double buffered system and this commands swaps
* the buffers
*/
void swapBuffers();
/**
* Destructor
*/
~Renderer();
};
}