-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderer.cpp
More file actions
160 lines (120 loc) · 4.41 KB
/
Renderer.cpp
File metadata and controls
160 lines (120 loc) · 4.41 KB
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
#include "Renderer.h"
#include "logger.h"
#include "ShaderManager.hpp"
namespace Engine
{
int m_window_size_X = 800*2, m_window_size_Y = 450*2;
bool render2fullWindow = true;
//Resize viewport
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
m_window_size_X = width;
m_window_size_Y = height;
glViewport(0, 0, m_window_size_X, m_window_size_Y);
}
int Renderer::Init(const char* window_title,bool fullscreen) {
log_message(log_level_e::LOG_DEBUG, " Initializing Renderer");
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
m_fullscreen_mode = fullscreen;
m_window = glfwCreateWindow(m_window_size_X, m_window_size_Y, window_title, (m_fullscreen_mode) ? m_primaryMonitor : NULL, NULL);
if (m_window == NULL)
{
log_error("ERROR::RENDERER::GLFW::WINDOW::FAILED_TO_CREATE_GLFW_WINDOW\n");
glfwTerminate();
return -1;
}
//Seting window
glfwMakeContextCurrent(m_window);
glfwSetFramebufferSizeCallback(m_window, framebuffer_size_callback);//Telling glad to call framebuffer_size_callback on resize window
glfwSetWindowAspectRatio(m_window, 800, 450);
log_message(log_level_e::LOG_INFO, " Renderer Initialized");
return 0;
}
int Renderer::getWindowSizeX()const {
return m_window_size_X;
}
int Renderer::getWindowSizeY()const {
return m_window_size_Y;
}
void Renderer::setWindowSizeX(int value) {
m_window_size_X = value;
}
void Renderer::setWindowSizeY(int value) {
m_window_size_Y = value;
}
float Renderer::getWindowAspectRatio()const {
return m_window_size_X / (1 + m_window_size_Y);
}
bool renderPassSortPred(const IRenderPass* a, const IRenderPass* b) {
return a->getRenderOrder() < b->getRenderOrder();
}
void Renderer::Render()
{
const FrameBuffer* aux = nullptr;
for (int i = 0; i < renderPasses.size(); i++)
{
aux = renderPasses[i]->RenderPass(aux);
}
for (int i = 0; i < renderPasses.size(); i++)
{
renderPasses[i]->RenderPassDebugGUI();
}
}
void Renderer::addRenderPass(IRenderPass* renderPass)
{
renderPasses.push_back(renderPass);
//Sort in ascending order
std::sort(renderPasses.begin(), renderPasses.end(), renderPassSortPred);
}
void Renderer::drawFullScreenQuad(unsigned int textureID, unsigned int textureAtachment, bool useFullScreenQuadShader) {
static float vertices[] = {
//Position
1, 1, 0.0f, 1, 1, // top right
1, -1, 0.0f, 1, 0, // bottom right
-1, -1, 0.0f, 0, 0, // bottom left
-1, 1, 0.0f, 0, 1 // top left
};
static unsigned int indices[] = { // note that we start from 0!
2,0,3,
2,1,0
};
static unsigned int VBO, VAO, EBO;
static bool initialized = false;
if (!initialized) {
//create buffers
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// uv attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
if (useFullScreenQuadShader) {
Shader* sh = ShaderManager::Instance()->getShader("FullScreenQuadShader");
sh->bind();
}
//bind texture
glActiveTexture(GL_TEXTURE0 + textureAtachment); // activate the texture unit first before binding texture
glBindTexture(GL_TEXTURE_2D, textureID);
glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
//glDeleteVertexArrays(1, &VAO);
//glDeleteBuffers(1, &VBO);
//glDeleteBuffers(1, &EBO);
}
}