-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathmain.cpp
360 lines (289 loc) · 10.6 KB
/
main.cpp
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
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <cmath>
#include <map>
#include <tool/shader.h>
#include <tool/camera.h>
#include <geometry/BoxGeometry.h>
#include <geometry/PlaneGeometry.h>
#include <geometry/SphereGeometry.h>
#define STB_IMAGE_IMPLEMENTATION
#include <tool/stb_image.h>
#include <tool/gui.h>
#include <tool/mesh.h>
#include <tool/model.h>
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void mouse_callback(GLFWwindow *window, double xpos, double ypos);
void processInput(GLFWwindow *window);
unsigned int loadTexture(char const *path);
unsigned int loadCubemap(vector<std::string> faces);
void drawSkyBox(Shader shader, BoxGeometry geometry, unsigned int cubeMap);
std::string Shader::dirName;
int SCREEN_WIDTH = 800;
int SCREEN_HEIGHT = 600;
// int SCREEN_WIDTH = 1600;
// int SCREEN_HEIGHT = 1200;
// delta time
float deltaTime = 0.0f;
float lastTime = 0.0f;
float lastX = SCREEN_WIDTH / 2.0f; // 鼠标上一帧的位置
float lastY = SCREEN_HEIGHT / 2.0f;
Camera camera(glm::vec3(0.0, 1.0, 6.0));
using namespace std;
int main(int argc, char *argv[])
{
Shader::dirName = argv[1];
glfwInit();
// 设置主要和次要版本
const char *glsl_version = "#version 330";
// 片段着色器将作用域每一个采样点(采用4倍抗锯齿,则每个像素有4个片段(四个采样点))
// glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// 窗口对象
GLFWwindow *window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// -----------------------
// 创建imgui上下文
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
(void)io;
// 设置样式
ImGui::StyleColorsDark();
// 设置平台和渲染器
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
// -----------------------
// 设置视口
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
// 深度测试
glEnable(GL_DEPTH_TEST);
// 鼠标键盘事件
// 1.注册窗口变化监听
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// 2.鼠标事件
glfwSetCursorPosCallback(window, mouse_callback);
// 3.将鼠标隐藏
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
Shader skyboxShader("./shader/cube_map_vert.glsl", "./shader/cube_map_frag.glsl");
Shader reflectShader("./shader/reflect_object_vert.glsl", "./shader/reflect_object_frag.glsl");
Shader refractShader("./shader/refract_object_vert.glsl", "./shader/refract_object_frag.glsl");
SphereGeometry sphereGeometry(1.0, 50.0, 50.0); // 圆球
BoxGeometry skyboxGeometry(1.0, 1.0, 1.0); // 天空盒
BoxGeometry containerGeometry(1.0, 1.0, 1.0); // 箱子
PlaneGeometry groundGeometry(10.0, 10.0); // 地面
float fov = 45.0f; // 视锥体的角度
glm::vec3 view_translate = glm::vec3(0.0, 0.0, -5.0);
ImVec4 clear_color = ImVec4(25.0 / 255.0, 25.0 / 255.0, 25.0 / 255.0, 1.0); // 25, 25, 25
// 天空盒贴图
vector<string> faces{
"./static/texture/Park3Med/px.jpg",
"./static/texture/Park3Med/nx.jpg",
"./static/texture/Park3Med/py.jpg",
"./static/texture/Park3Med/ny.jpg",
"./static/texture/Park3Med/pz.jpg",
"./static/texture/Park3Med/nz.jpg"};
unsigned int cubemapTexture = loadCubemap(faces);
Model ourModel("./static/model/walt/WaltHead.obj");
while (!glfwWindowShouldClose(window))
{
processInput(window);
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastTime;
lastTime = currentFrame;
// 在标题中显示帧率信息
// *************************************************************************
int fps_value = (int)round(ImGui::GetIO().Framerate);
int ms_value = (int)round(1000.0f / ImGui::GetIO().Framerate);
std::string FPS = std::to_string(fps_value);
std::string ms = std::to_string(ms_value);
std::string newTitle = "LearnOpenGL - " + ms + " ms/frame " + FPS;
glfwSetWindowTitle(window, newTitle.c_str());
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// *************************************************************************
// 渲染指令
// ...
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 绘制天空盒
drawSkyBox(skyboxShader, skyboxGeometry, cubemapTexture);
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(0.0, 0.0, 0.0));
model = glm::scale(model, glm::vec3(0.03f, 0.03f, 0.03f));
glm::mat4 view = camera.GetViewMatrix();
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT, 0.1f, 100.0f);
refractShader.use();
refractShader.setMat4("model", model);
refractShader.setMat4("view", view);
refractShader.setMat4("projection", projection);
refractShader.setVec3("cameraPos", camera.Position);
refractShader.setVec3("objectColor", glm::vec3(0.0, 0.0, 0.0));
ourModel.Draw(refractShader);
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(-3.0, 0.0, 0.0));
model = glm::scale(model, glm::vec3(0.03f, 0.03f, 0.03f));
refractShader.setMat4("model", model);
refractShader.setVec3("objectColor", glm::vec3(0.1, 0.0, 0.1));
ourModel.Draw(refractShader);
reflectShader.use();
reflectShader.setMat4("view", view);
reflectShader.setMat4("projection", projection);
reflectShader.setVec3("cameraPos", camera.Position);
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(3.0, 0.0, 0.0));
model = glm::scale(model, glm::vec3(0.03f, 0.03f, 0.03f));
reflectShader.setMat4("model", model);
reflectShader.setVec3("objectColor", glm::vec3(0.1, 0.1, 0.0));
ourModel.Draw(reflectShader);
// 渲染 gui
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
glfwPollEvents();
}
containerGeometry.dispose();
sphereGeometry.dispose();
skyboxGeometry.dispose();
glfwTerminate();
return 0;
}
// 窗口变动监听
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
glViewport(0, 0, width, height);
}
// 键盘输入监听
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
// 相机按键控制
// 相机移动
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
{
camera.ProcessKeyboard(FORWARD, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
{
camera.ProcessKeyboard(BACKWARD, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
{
camera.ProcessKeyboard(LEFT, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
{
camera.ProcessKeyboard(RIGHT, deltaTime);
}
}
// 鼠标移动监听
void mouse_callback(GLFWwindow *window, double xpos, double ypos)
{
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
camera.ProcessMouseMovement(xoffset, yoffset);
}
// 加载纹理贴图
unsigned int loadTexture(char const *path)
{
unsigned int textureID;
glGenTextures(1, &textureID);
// 图像y轴翻转
stbi_set_flip_vertically_on_load(true);
int width, height, nrComponents;
unsigned char *data = stbi_load(path, &width, &height, &nrComponents, 0);
if (data)
{
GLenum format;
if (nrComponents == 1)
format = GL_RED;
else if (nrComponents == 3)
format = GL_RGB;
else if (nrComponents == 4)
format = GL_RGBA;
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_image_free(data);
}
else
{
std::cout << "Texture failed to load at path: " << path << std::endl;
stbi_image_free(data);
}
return textureID;
}
// 加载立方体贴图
unsigned int loadCubemap(vector<std::string> faces)
{
unsigned int textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
// 此处需要将y轴旋转关闭,若之前调用过loadTexture
stbi_set_flip_vertically_on_load(false);
int width, height, nrChannels;
for (unsigned int i = 0; i < faces.size(); i++)
{
unsigned char *data = stbi_load(faces[i].c_str(), &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
else
{
std::cout << "Cubemap texture failed to load at path: " << faces[i] << std::endl;
stbi_image_free(data);
}
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
return textureID;
}
// *******************method*********************
void drawSkyBox(Shader shader, BoxGeometry geometry, unsigned int cubeMap)
{
glDepthFunc(GL_LEQUAL);
glDisable(GL_DEPTH_TEST);
glm::mat4 view = camera.GetViewMatrix();
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT, 0.1f, 100.0f);
shader.use();
view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // 移除平移分量
shader.setMat4("view", view);
shader.setMat4("projection", projection);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubeMap);
glBindVertexArray(geometry.VAO);
glDrawElements(GL_TRIANGLES, geometry.indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
view = camera.GetViewMatrix();
}