-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_PBR_Test.cpp
More file actions
424 lines (424 loc) · 15.1 KB
/
Copy pathmain_PBR_Test.cpp
File metadata and controls
424 lines (424 loc) · 15.1 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//#define GLEW_STATIC
//#include <iostream>
//#include <stdlib.h>
//#include <glad/glad.h>
//#include <GLFW/glfw3.h>
//
//#include <glm/glm.hpp>
//#include <glm/gtc/matrix_transform.hpp>
//#include <glm/gtc/type_ptr.hpp>
//
//#include <math.h>
//#include <vector>
//
//#define _USE_MATH_DEFINES
//#include <math.h>
//
//#include "Shader_PBR.h"
//#include "Camera_PBR.h"
//#include "Material.h"
//#include "LightDirectional.h"
////#include "LightPoint.h"
////#include "LightSpot.h"
//#include "Mesh_PBR.h"
//#include "Model_PBR.h"
//
//
//#define STB_IMAGE_IMPLEMENTATION
//#include "stb_image.h"
//
//
//#pragma region Model Data
//
//// the vertices of the ground
//float vertices_ground[] = {
// // positions // normals // texture coords
// -0.5f, -0.0f, -0.5f, 0.0f, 1.0f, 0.0f, 100.0f, 100.0f, // top right
// 0.5f, -0.0f, -0.5f, 0.0f, 1.0f, 0.0f, 100.0f, 0.0f, // bottom right
// 0.5f, -0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, // bottom left
// -0.5f, -0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 100.0f // top left
//};
//
//// the indices of the vertices of the ground, passed to the EBO
//unsigned int indices_ground[] = {
// 0, 1, 2,
// 2, 3, 0
//};
//#pragma endregion
//
//#pragma region Light Declaration
//// declare a light object
//LightDirectional lightD(glm::vec3(10.0f, 30.0f, 20.0f), glm::vec3(glm::radians(110.0f), glm::radians(30.0f), 0), glm::vec3(5.0f, 5.0f, 5.0f));
//#pragma endregion
//
//#pragma region Camera Declaration
//// Initialize camera class
//Camera camera(glm::vec3(0, 3.0f, 20.0f), glm::radians(0.0f), glm::radians(-90.0f), glm::vec3(0, 1.0f, 0));
//#pragma endregion
//
//#pragma region Texture Loading Function
//// here you should pass RGB when loading .jpg, while pass RGBA when loading .png
//unsigned int LoadTextureImageToGPU(const char* filename, GLint internalFormat, GLenum format, int textureSlot)
//{
// unsigned int TexBuffer;
// glGenTextures(1, &TexBuffer);
//
// glActiveTexture(GL_TEXTURE0 + textureSlot);
// glBindTexture(GL_TEXTURE_2D, TexBuffer);
//
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// // set texture filtering parameters
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//
// int width, height, nrChannel;
// unsigned char* data = stbi_load(filename, &width, &height, &nrChannel, 0);
// if (data)
// {
// glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_UNSIGNED_BYTE, data);
// glGenerateMipmap(GL_TEXTURE_2D);
// }
// else
// {
// cout << "Texture image " << filename << " load failed." << endl;
// }
// stbi_image_free(data);
//
// //cout << filename << "Slot: " << TexBuffer << endl;
//
// return TexBuffer;
//}
//#pragma endregion
//
//#pragma region Camera Operation Funtions
//// moving mouse and press some keys will change the position and the forward direction of the camera
//double previous_xPos, previous_yPos;
//bool first_initialise_mouse = true;
//
//// press keys as input
//void processInput_camera(GLFWwindow* window)
//{
//
// if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
// {
// glfwSetWindowShouldClose(window, true);
// }
//
// if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
// {
// camera.SpeedZ = 1.0;
// }
// else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
// {
// camera.SpeedZ = -1.0;
// }
// else
// {
// camera.SpeedZ = 0;
// }
//
// if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
// {
// camera.SpeedX = 1.0;
// }
// else if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
// {
// camera.SpeedX = -1.0;
// }
// else
// {
// camera.SpeedX = 0;
// }
//
// if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
// {
// camera.SpeedY = 1.0;
// }
// else if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
// {
// camera.SpeedY = -1.0;
// }
// else
// {
// camera.SpeedY = 0;
// }
//}
//
//// move mouse to change the orientation of the camera
//void mouse_callback_camera(GLFWwindow* window, double xPos, double yPos)
//{
// if (first_initialise_mouse == true)
// {
// previous_xPos = xPos;
// previous_yPos = yPos;
// first_initialise_mouse = false;
// }
//
// double Delta_x = xPos - previous_xPos;
// double Delta_y = yPos - previous_yPos;
//
// previous_xPos = xPos;
// previous_yPos = yPos;
//
// //cout << Delta_x << " " << Delta_y << endl; // this line is for debug
// camera.ProcessMouseMovement(Delta_x, Delta_y);
//}
//#pragma endregion
//
//int main(int argc, char* argv[])
//{
// std::string exePath = argv[0];
//
//#pragma region Open a Window
// glfwInit();
// glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
// glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//
// GLFWwindow* window = glfwCreateWindow(1400, 1000, "My openGL game", NULL, NULL);
//
// if (window == NULL)
// {
// cout << "Open window failed." << endl;
// glfwTerminate();
// return -1;
// }
// glfwMakeContextCurrent(window);
// glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
//
// //glewExperimental = true;
// //if (glewInit() != GLEW_OK)
// //{
// // cout << "Init GlEW failed." << endl;
// // glfwTerminate();
// // return -1;
// //}
//
// glfwSetCursorPosCallback(window, mouse_callback_camera);
// //glfwSetCursorPosCallback(window, mouse_callback_camera_ThirdPersonView);
// //glfwSetScrollCallback(window, scroll_callback_camera_ThirdPersonView);
//
// //glViewport(0, 0, 800, 600);
// glEnable(GL_DEPTH_TEST);
// //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//#pragma endregion
//
//#pragma region Init Shader Program
// Shader* shader_ground = new Shader("VertexShader_ground.vert", "FragmentShader_ground.frag");
// Shader* shader_blinnPhong = new Shader("VertexShader_blinnPhong.vert", "FragmentShader_blinnPhong.frag");
// Shader* shader_cookTorrance = new Shader("VertexShader_cookTorrance.vert", "FragmentShader_cookTorrance.frag");
// Shader* shader_nonPhysical = new Shader("VertexShader_nonPhysical.vert", "FragmentShader_nonPhysical.frag");
// Shader* shader_silhouette = new Shader("VertexShader_silhouette.vert", "FragmentShader_silhouette.frag");
//#pragma endregion
//
//#pragma region Pass light to shaders;
// shader_blinnPhong->use();
// shader_blinnPhong->SetUniform3f("lightD.pos", glm::vec3(lightD.position.x, lightD.position.y, lightD.position.z));
// shader_blinnPhong->SetUniform3f("lightD.color", glm::vec3(lightD.color.x, lightD.color.y, lightD.color.z));
// shader_blinnPhong->SetUniform3f("lightD.dirToLight", glm::vec3(lightD.direction.x, lightD.direction.y, lightD.direction.z));
//
// shader_cookTorrance->use();
// shader_cookTorrance->SetUniform3f("lightD.pos", glm::vec3(lightD.position.x, lightD.position.y, lightD.position.z));
// shader_cookTorrance->SetUniform3f("lightD.color", glm::vec3(lightD.color.x, lightD.color.y, lightD.color.z));
// shader_cookTorrance->SetUniform3f("lightD.dirToLight", glm::vec3(lightD.direction.x, lightD.direction.y, lightD.direction.z));
//
// shader_nonPhysical->use();
// shader_nonPhysical->SetUniform3f("lightD.pos", glm::vec3(lightD.position.x, lightD.position.y, lightD.position.z));
// shader_nonPhysical->SetUniform3f("lightD.color", glm::vec3(lightD.color.x, lightD.color.y, lightD.color.z));
// shader_nonPhysical->SetUniform3f("lightD.dirToLight", glm::vec3(lightD.direction.x, lightD.direction.y, lightD.direction.z));
//#pragma endregion
//
//#pragma region Load Models
//
// // load a .obj model
// Model bunny(exePath.substr(0, exePath.find_last_of('\\')) + "\\bunny\\bunny.obj");
//
// // manually load the data of the ground
// unsigned int VBO_ground;
// glGenBuffers(1, &VBO_ground);
// glBindBuffer(GL_ARRAY_BUFFER, VBO_ground);
// glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_ground), vertices_ground, GL_STATIC_DRAW);
//
// unsigned int VAO_ground;
// glGenVertexArrays(1, &VAO_ground);
// glBindVertexArray(VAO_ground);
//
// unsigned int EBO_ground;
// glGenBuffers(1, &EBO_ground);
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO_ground);
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices_ground), indices_ground, GL_STATIC_DRAW);
//
// glEnableVertexAttribArray(0);
// glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
// glEnableVertexAttribArray(1);
// glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
// glEnableVertexAttribArray(2);
// glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
//#pragma endregion
//
//#pragma region Init and Load Texture;
// stbi_set_flip_vertically_on_load(true);
// unsigned int TexBuffer_ground;
// TexBuffer_ground = LoadTextureImageToGPU("ground.jpg", GL_RGB, GL_RGB, 0);
// //unsigned int TexBufferB;
// //TexBufferB = LoadTextureImageToGPU("awesomeface.png", GL_RGBA, GL_RGBA, 1);
//#pragma endregion
//
//#pragma region Prepare MVP matrices
// //glm::mat4 scale; glm::scale(transMatrix, glm::vec3(100.0f, 100.0f, 100.0f));
// glm::mat4 trans;
// glm::mat4 modelMat;
// //modelMat = glm::rotate(modelMat, glm::radians(0.0f), glm::vec3(1.0f, 0, 0));
// glm::mat4 viewMat;
// glm::mat4 projMat;
// projMat = glm::perspective(glm::radians(45.0f), 1400.0f / 1000.0f, 0.1f, 300.0f);
//#pragma endregion
//
// glEnable(GL_CULL_FACE);
// //glCullFace(GL_FRONT);
//
// // Start drawing
// while (!glfwWindowShouldClose(window))
// {
// // get current time
// float currentTime = glfwGetTime();
//
// // Mouse Action
// processInput_camera(window);
//
// // Clear Screen
// glClearColor(0.2f, 0.5f, 0.5f, 1.0f);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//
// glViewport(0, 0, 1400, 1000);
// viewMat = camera.GetViewMatrix();
//
//#pragma region Draw ground
// // in this demonstration, the ground is not used
// // so commented out
//
// // Ground
// /*shader_blinnPhong->use();
// trans = glm::scale(glm::mat4(1.0f), glm::vec3(1000.0f, 1000.0f, 1000.0f));
// modelMat = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f));
// shader_blinnPhong->SetMatrix("transform", trans);
// shader_blinnPhong->SetMatrix("modelMat", modelMat);
// shader_blinnPhong->SetMatrix("viewMat", viewMat);
// shader_blinnPhong->SetMatrix("projMat", projMat);
//
// glActiveTexture(GL_TEXTURE0);
// glBindTexture(GL_TEXTURE_2D, TexBuffer_ground);
// shader_blinnPhong->SetUniform1i("groundTexture_diffuse", 0);
//
// glBindVertexArray(VAO_ground);
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO_ground);
// glDrawElements(GL_TRIANGLES, sizeof(indices_ground), GL_UNSIGNED_INT, 0);*/
//#pragma endregion
//
//#pragma region Draw Bunny BlinnPhong
//
// // in the shader of blinn-phong, two parameters were manuplated
// // one is the effect of the ambinent colour (change with i)
// // another one is the shininess (change with j)
// for (int i = 0; i < 5; i++)
// {
// for (int j = 0; j < 5; j++)
// {
// shader_blinnPhong->use();
// trans = glm::scale(glm::mat4(1.0f), glm::vec3(1.0f, 1.0f, 1.0f));
// trans = glm::rotate(trans, currentTime, glm::vec3(0.0f, 1.0f, 0.0f));
// modelMat = glm::translate(glm::mat4(1.0f), glm::vec3(-40.0f + i * 4.0f, 8.0f - j * 4.0f, -15.0f));
// shader_blinnPhong->SetMatrix("transform", trans);
// shader_blinnPhong->SetMatrix("modelMat", modelMat);
// shader_blinnPhong->SetMatrix("viewMat", viewMat);
// shader_blinnPhong->SetMatrix("projMat", projMat);
// shader_blinnPhong->SetUniform3f("viewPos", camera.Position);
// shader_blinnPhong->SetUniform1f("ambinent_weight", 0.1 * i);
// shader_blinnPhong->SetUniform1f("shininess", std::pow(2, j + 2));
//
// bunny.Draw(shader_blinnPhong);
// }
// }
//#pragma endregion
//
//#pragma region Draw Bunny CookTorrance
//
// // in the shader of PBR with cook-torrance BRDF, two parameters were manuplated
// // one is the property of metallic (change with i)
// // another one is the property of roughness (change with j)
//
// shader_cookTorrance->use();
//
// for (int i = 0; i < 5; i++)
// {
// for (int j = 0; j < 5; j++)
// {
// trans = glm::scale(glm::mat4(1.0f), glm::vec3(1.0f, 1.0f, 1.0f));
// trans = glm::rotate(trans, currentTime, glm::vec3(0.0f, 1.0f, 0.0f));
// modelMat = glm::translate(glm::mat4(1.0f), glm::vec3(-8.0f + i * 4.0f, 8.0f - j * 4.0f, -15.0f));
// shader_cookTorrance->SetMatrix("transform", trans);
// shader_cookTorrance->SetMatrix("modelMat", modelMat);
// shader_cookTorrance->SetMatrix("viewMat", viewMat);
// shader_cookTorrance->SetMatrix("projMat", projMat);
// shader_cookTorrance->SetUniform3f("viewPos", camera.Position);
// shader_cookTorrance->SetUniform3f("albedo", glm::vec3(0.5f, 0.5f, 0.5f));
// shader_cookTorrance->SetUniform1f("metallic", 0.01f + i * 0.2f);
// shader_cookTorrance->SetUniform1f("roughness", 0.1f + j * 0.2f);
//
// bunny.Draw(shader_cookTorrance);
// }
// }
//#pragma endregion
//
//#pragma region Draw Bunny NonPhysical
//
// // in the shader of non-photorealistic rendering, two parameters were manuplated
// // one is the value of the upper threshold of diffuse reflection (change with i)
// // another one is the threshold of specular reflection (change with j)
//
// for (int i = 0; i < 5; i++)
// {
// for (int j = 0; j < 5; j++)
// {
// shader_nonPhysical->use();
// trans = glm::scale(glm::mat4(1.0f), glm::vec3(1.0f, 1.0f, 1.0f));
// trans = glm::rotate(trans, currentTime, glm::vec3(0.0f, 1.0f, 0.0f));
// modelMat = glm::translate(glm::mat4(1.0f), glm::vec3(24.0f + i * 4.0f, 8.0f - j * 4.0f, -15.0f));
// shader_nonPhysical->SetMatrix("transform", trans);
// shader_nonPhysical->SetMatrix("modelMat", modelMat);
// shader_nonPhysical->SetMatrix("viewMat", viewMat);
// shader_nonPhysical->SetMatrix("projMat", projMat);
// shader_nonPhysical->SetUniform3f("viewPos", camera.Position);
// shader_nonPhysical->SetUniform1f("diffuse_upper_bound", 0.25f + 0.05f * i);
// shader_nonPhysical->SetUniform1f("diffuse_lower_bound", 0.20f);
// shader_nonPhysical->SetUniform1f("specular_bound", 0.5f + 0.1f * j);
//
// bunny.Draw(shader_nonPhysical);
//
// glCullFace(GL_FRONT);
// shader_silhouette->use();
// trans = glm::scale(glm::mat4(1.0f), glm::vec3(1.0f, 1.0f, 1.0f));
// trans = glm::rotate(trans, currentTime, glm::vec3(0.0f, 1.0f, 0.0f));
// modelMat = glm::translate(glm::mat4(1.0f), glm::vec3(24.0f + i * 4.0f, 8.0f - j * 4.0f, -15.0f));
// shader_silhouette->SetMatrix("transform", trans);
// shader_silhouette->SetMatrix("modelMat", modelMat);
// shader_silhouette->SetMatrix("viewMat", viewMat);
// shader_silhouette->SetMatrix("projMat", projMat);
// shader_silhouette->SetUniform3f("viewPos", camera.Position);
//
//
// bunny.Draw(shader_silhouette);
// glCullFace(GL_BACK);
// }
// }
//
//#pragma endregion
//
// // Clean up, prepare for next render loop
// glfwSwapBuffers(window);
// glfwPollEvents();
// camera.UpdateCameraPos();
// }
//
//}