-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRenderer.cpp
More file actions
173 lines (136 loc) · 5.25 KB
/
Renderer.cpp
File metadata and controls
173 lines (136 loc) · 5.25 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "Renderer.h"
#include "Canvas.h"
static float skyboxVertices[] = {
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f
};
Renderer::Renderer() {
stacktop = 0;
memset(stack, 0, sizeof(stack));
initialize();
}
Renderer::~Renderer() {
}
void Renderer::ClearCurrentFrame() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Renderer::Begin(const glm::mat4& projection, const glm::mat4& view, const glm::mat4& model) {
stack[++stacktop] = DrawState{ projection, view, model };
}
void Renderer::End() {
--stacktop;
}
void Renderer::DrawLines(GLuint VAO, float width, int points) {
_lineShader->Apply();
glLineWidth(width);
_lineShader->SetParameter<glm::mat4>("transform", getCurrentTransform().projection);
glBindVertexArray(VAO);
glDrawArrays(GL_LINES, 0, points);
glBindVertexArray(0);
}
void Renderer::ApplyPhongShader(glm::vec3 lightPos, glm::vec3 phongParameter) {
_phongShader->Apply();
_phongShader->SetParameter<glm::mat4>("projection", getCurrentTransform().projection);
_phongShader->SetParameter<glm::mat4>("view", getCurrentTransform().view);
_phongShader->SetParameter<glm::mat4>("model", getCurrentTransform().model);
_phongShader->SetParameter<glm::vec3>("uLightPos", glm::vec3(getCurrentTransform().view * glm::vec4(lightPos, 1.0f)));
_phongShader->SetParameter<glm::vec3>("uLightParameter", phongParameter);
}
void Renderer::ApplyPRTShader(glm::vec3 color) {
Canvas& canvas = Canvas::GetInstance();
auto R = canvas.GetSkyBoxRotation();
int mode = canvas.GetPRTMode();
_prtShader->Apply();
_prtShader->SetParameter<glm::mat4>("projection", getCurrentTransform().projection);
_prtShader->SetParameter<glm::mat4>("view", getCurrentTransform().view);
_prtShader->SetParameter<glm::mat4>("model", getCurrentTransform().model);
_prtShader->SetParameter<int>("uGammaCorrection", (int)canvas.GetGammaCorrection());
_prtShader->SetParameter<int>("uMode", mode);
_prtShader->SetParameter<glm::vec3>("uColor", color);
if (mode == 0) {
auto M = _curEnvironmentMap->GetUnshadowQuadraticForm(R);
_prtShader->SetParameter<glm::mat4>("uEnvironmentSH.QUAD_R", M[0]);
_prtShader->SetParameter<glm::mat4>("uEnvironmentSH.QUAD_G", M[1]);
_prtShader->SetParameter<glm::mat4>("uEnvironmentSH.QUAD_B", M[2]);
}
else {
auto M = _curEnvironmentMap->GetLightFunction(R);
_prtShader->SetParameter<glm::mat3>("uEnvironmentSH.SH_R", M[0]);
_prtShader->SetParameter<glm::mat3>("uEnvironmentSH.SH_G", M[1]);
_prtShader->SetParameter<glm::mat3>("uEnvironmentSH.SH_B", M[2]);
}
}
void Renderer::SetEnvironment(const std::shared_ptr<EnvironmentMap>& envirMap) {
_curEnvironmentMap = envirMap;
}
void Renderer::DrawSkyBox() {
Canvas& canvas = Canvas::GetInstance();
auto R = canvas.GetSkyBoxRotation();
glDepthMask(GL_FALSE);
_skyBoxShader->Apply();
glBindVertexArray(_vaoBox);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, _curEnvironmentMap->GetTexture());
_skyBoxShader->SetParameter<glm::mat4>("projection", getCurrentTransform().projection);
_skyBoxShader->SetParameter<glm::mat4>("view", getCurrentTransform().view);
_skyBoxShader->SetParameter<glm::mat4>("model", R);
_skyBoxShader->SetParameter<int>("uCubeMap", 0);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glDepthMask(GL_TRUE);
}
DrawState& Renderer::getCurrentTransform() {
return stack[stacktop];
}
void Renderer::initialize() {
_phongShader = std::make_shared<Shader>(loadVertexFragmantShader("../../../resources/shaders/phong.vs",
"../../../resources/shaders/phong.frag"));
_prtShader = std::make_shared<Shader>(loadVertexFragmantShader("../../../resources/shaders/PRT.vs",
"../../../resources/shaders/PRT.frag"));
_skyBoxShader = std::make_shared<Shader>(loadVertexFragmantShader("../../../resources/shaders/skybox.vs",
"../../../resources/shaders/skybox.frag"));
// Skybox VAO
glGenVertexArrays(1, &_vaoBox);
glGenBuffers(1, &_vboBox);
glBindVertexArray(_vaoBox);
glBindBuffer(GL_ARRAY_BUFFER, _vboBox);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), skyboxVertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
}