-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateWindow.cpp
431 lines (383 loc) · 16.9 KB
/
CreateWindow.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
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
425
426
427
428
429
430
431
#include <glad/glad.h>//must in the first
#include <GLFW/glfw3.h>
#include <iostream>
#include "shader.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "camera.h"
#include "mesh.h"
#include "model.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
void Mouse_CallBack(GLFWwindow* window, double xPos, double yPos);
void Scroll_CallBack(GLFWwindow* window, double xOffset, double yOffset);
unsigned int LoadTexture(const char* path);
float mixValue = 0.2f;
float baseSpeed = 2.5f;
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);//摄像机方向
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
float lastTime = 0.0f;
float currentTime = 0.0f;
float deltaTime = 0.0f;
float lastX = 800 / 2;
float lastY = 600 / 2;
bool firstMouse = true;
float fov = 45.0f;
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
int main()
{
glfwInit();//glfw初始化
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);//告诉版本号3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);//使用核心模式
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
//每当改变窗口大小,渲染的视口大小也会改变
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, Mouse_CallBack);
glfwSetScrollCallback(window, Scroll_CallBack);
//GLAD是用来管理OpenGL的函数指针的,所以在调用任何OpenGL的函数之前我们需要初始化GLAD
//给GLAD传入了用来加载系统相关的OpenGL函数指针地址的函数。GLFW给我们的是glfwGetProcAddress,它根据我们编译的系统定义了正确的函数。
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
Shader shader("vertexShader.vs", "fragmentShader.fs");
Shader depthTestShader("DepthTestVertexShader.vs", "DepthTestShader.fs");
Shader lightShader("LightVertexShader.vs", "LightfragmentShader.fs");
Shader stencilShader("StencilVertexShader.vs", "StencilfragmentShader.fs");
shader.use();
unsigned int modelLocation;
unsigned int viewLocation;
unsigned int projLocation;
modelLocation = glGetUniformLocation(shader.ID, "model");
viewLocation = glGetUniformLocation(shader.ID, "view");
projLocation = glGetUniformLocation(shader.ID, "proj");
shader.setFloat("material.shininess", 32.0f);
std::string path = "C:/Users/SunHe/Desktop/Git/ComputerGraphics/ComputerGraphics/model/nanosuit/nanosuit.obj";
Model model(path);
float cubeVertices[] = {
// Back face
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // Bottom-left
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, // bottom-right
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // bottom-left
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left
// Front face
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // bottom-right
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // top-right
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, // top-right
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, // top-left
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left
// Left face
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-right
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-left
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-left
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-left
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-right
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-right
// Right face
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-left
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-right
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // bottom-right
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // top-left
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-left
// Bottom face
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // top-right
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, // top-left
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // bottom-left
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // bottom-left
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, // bottom-right
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, // top-right
// Top face
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // bottom-right
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, // top-right
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, // bottom-right
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, // top-left
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f // bottom-left
};
float planeVertices[] = {
// positions // texture Coords (note we set these higher than 1 (together with GL_REPEAT as texture wrapping mode). this will cause the floor texture to repeat)
5.0f, -0.5f, 5.0f, 2.0f, 0.0f,
-5.0f, -0.5f, 5.0f, 0.0f, 0.0f,
-5.0f, -0.5f, -5.0f, 0.0f, 2.0f,
5.0f, -0.5f, 5.0f, 2.0f, 0.0f,
-5.0f, -0.5f, -5.0f, 0.0f, 2.0f,
5.0f, -0.5f, -5.0f, 2.0f, 2.0f
};
float transparentVertices[] = {
// positions // texture Coords (swapped y coordinates because texture is flipped upside down)
0.0f, 0.5f, 0.0f, 0.0f, 0.0f,
0.0f, -0.5f, 0.0f, 0.0f, 1.0f,
1.0f, -0.5f, 0.0f, 1.0f, 1.0f,
0.0f, 0.5f, 0.0f, 0.0f, 0.0f,
1.0f, -0.5f, 0.0f, 1.0f, 1.0f,
1.0f, 0.5f, 0.0f, 1.0f, 0.0f
};
unsigned int cubeVAO, cubeVBO;
glGenVertexArrays(1, &cubeVAO);
glGenBuffers(1, &cubeVBO);
glBindVertexArray(cubeVAO);
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), &cubeVertices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*) (0));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glBindVertexArray(0);
unsigned int planeVAO, planeVBO;
glGenVertexArrays(1, &planeVAO);
glGenBuffers(1, &planeVBO);
glBindVertexArray(planeVAO);
glBindBuffer(GL_ARRAY_BUFFER, planeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(planeVertices), &planeVertices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(0));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glBindVertexArray(0);
//vegetationVAO
unsigned int transparentVAO, transparentVBO;
glGenVertexArrays(1, &transparentVAO);
glGenBuffers(1, &transparentVBO);
glBindVertexArray(transparentVAO);
glBindBuffer(GL_ARRAY_BUFFER, transparentVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(transparentVertices), transparentVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(0));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glBindVertexArray(0);
unsigned int cubeTexture = LoadTexture("marble.jpg");
unsigned int planeTexture = LoadTexture("metal.png");
unsigned int transparentTexture = LoadTexture("window.png");
//The position of grass
vector<glm::vec3> windows;
windows.push_back(glm::vec3(-1.5f, 0.0f, -0.48f));
windows.push_back(glm::vec3(1.5f, 0.0f, 0.51f));
windows.push_back(glm::vec3(0.0f, 0.0f, 0.7f));
windows.push_back(glm::vec3(-0.3f, 0.0f, -2.3f));
windows.push_back(glm::vec3(0.5f, 0.0f, -0.6f));
depthTestShader.use();
depthTestShader.setInt("texture1", 0);//0代表当前使用哪个着色单元
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
//glEnable(GL_STENCIL_TEST);
//glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
//glStencilFunc(GL_NOTEQUAL,1,0xFF);
while (!glfwWindowShouldClose(window))
{
std::map<float, glm::vec3> sorted;
for (unsigned int i = 0; i < windows.size(); i++)
{
float distance = glm::length(camera.Position - windows[i]);
sorted[distance] = windows[i];
}
processInput(window);
//当调用glClear函数,清除颜色缓冲之后,整个颜色缓冲都会被填充为glClearColor里所设置的颜色。在这里,我们将屏幕设置为了类似黑板的深蓝绿色
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);//设置清空后显示的颜色
glClear(GL_COLOR_BUFFER_BIT);//清空并图上颜色
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
//shader.use();
//stencilShader.use();
depthTestShader.use();
currentTime = glfwGetTime();
deltaTime = currentTime - lastTime;
lastTime = currentTime;
glm::mat4 viewMatrix = glm::mat4(1.0f);
viewMatrix = camera.GetViewMatrix();//cameraPos+cameraFront保持摄像机一直向前看
glm::mat4 projMatrix = glm::mat4(1.0f);
projMatrix = glm::perspective(glm::radians(fov), (float)800 / (float)600, 0.1f, 100.0f);
//先画plane
//glStencilMask(0x00);
glBindVertexArray(planeVAO);
glBindTexture(GL_TEXTURE_2D, planeTexture);
glm::mat4 model = glm::mat4(1.0f);
//model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));
model = glm::mat4(1.0f);
depthTestShader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 6);
//glStencilFunc(GL_ALWAYS, 1, 0xFF);
//glStencilMask(0xFF);
glUniformMatrix4fv(viewLocation, 1, GL_FALSE, glm::value_ptr(viewMatrix));
glUniformMatrix4fv(projLocation, 1, GL_FALSE, glm::value_ptr(projMatrix));
depthTestShader.setMat4("view", viewMatrix);
depthTestShader.setMat4("proj", projMatrix);
glm::mat4 modelMatrix = glm::mat4(1.0f);
modelMatrix = glm::translate(modelMatrix, glm::vec3(0.0f, 0.0f, 0.0f));
modelMatrix = glm::scale(modelMatrix, glm::vec3(0.1f, 0.1f, 0.1f));
glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(modelMatrix));
glBindVertexArray(cubeVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, cubeTexture);
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));
depthTestShader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(2.0f, 0.0f, 0.0f));
depthTestShader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
//float scale = 1.1f;
//stencilShader.use();//没有这句还会使用depthTestShader去画正方形
//glStencilFunc(GL_NOTEQUAL, 1, 0xFF);//模板更新的方法
//glStencilMask(0x00);//设置掩码,在每次写入模板缓冲的值之前与掩码进行与运算
////此时禁止模板缓冲写入
//glBindVertexArray(cubeVAO);
//glDisable(GL_DEPTH_TEST);//让地板不要遮住箱子
//stencilShader.setMat4("view", viewMatrix);
//stencilShader.setMat4("proj", projMatrix);
////在掩码不为一的地方绘制正方体
//model = glm::mat4(1.0f);
//model = glm::translate(model, glm::vec3(-1.0f, 0.0f, -1.0f));
//model = glm::scale(model, glm::vec3(scale));
//stencilShader.setMat4("model", model);
//glDrawArrays(GL_TRIANGLES, 0, 36);
//model = glm::mat4(1.0f);
//model = glm::translate(model, glm::vec3(2.0f, 0.0f, 0.0f));
//model = glm::scale(model, glm::vec3(scale));
//stencilShader.setMat4("model", model);
//glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(transparentVAO);
glBindTexture(GL_TEXTURE_2D, transparentTexture);
for (std::map<float, glm::vec3>::reverse_iterator it = sorted.rbegin(); it != sorted.rend(); ++it)
{
model = glm::mat4(1.0f);
model = glm::translate(model, it->second);
shader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
shader.setvec3("cameraPos", camera.Position);
shader.setvec3("dirLight.direction", glm::vec3(-0.2f, -1.0f, -0.3f));
shader.setvec3("dirLight.ambient", glm::vec3(0.05f, 0.05f, 0.05f));
shader.setvec3("dirLight.diffuse", glm::vec3(0.4f, 0.4f, 0.4f));
shader.setvec3("dirLight.specular", glm::vec3(0.5f, 0.5f, 0.5f));
shader.setvec3("spotLight.position", camera.Position);
shader.setvec3("spotLight.direction", camera.Front);
shader.setvec3("spotLight.ambient", glm::vec3(0.2f, 0.2f, 0.2f));
shader.setvec3("spotLight.diffuse", glm::vec3(1.0f, 1.0f, 1.0f));
shader.setvec3("spotLight.specular", glm::vec3(1.0f, 1.0f, 1.0f));
shader.setFloat("spotLight.constant", 1.0f);
shader.setFloat("spotLight.linear", 0.09);
shader.setFloat("spotLight.quadratic", 0.032);
shader.setFloat("spotLight.cutoff", glm::cos(glm::radians(12.5f)));
shader.setFloat("spotLight.outercutoff", glm::cos(glm::radians(15.0f)));
//用于画复杂模型
//model.Draw(shader);
glBindVertexArray(0);
glStencilMask(0xFF);
glStencilFunc(GL_ALWAYS, 0, 0xFF);
glEnable(GL_DEPTH_TEST);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
//glViewport函数前两个参数控制窗口左下角的位置。第三个和第四个参数控制渲染窗口的宽度和高度(像素),可以比glfw窗口小
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_UP) == GLFW_PRESS) {
mixValue += 0.001f;
if (mixValue >= 1.0f)
mixValue = 1.0f;
}
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
mixValue -= 0.001f;
if (mixValue <= 0.0f)
mixValue = 0.0f;
}
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_D) == GLFW_PRESS) {
camera.ProcessKeyboard(RIGHT, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
camera.ProcessKeyboard(LEFT, deltaTime);
}
}
void Mouse_CallBack(GLFWwindow* window,double xPos,double yPos) {
if (firstMouse) {
lastX = xPos;
lastY = yPos;
firstMouse = false;
}
/*std::cout << xPos <<" "<< lastX << std::endl;
std::cout << lastY <<" "<< yPos << std::endl;*/
float xOffset = xPos - lastX;
float yOffset = lastY - yPos;//y向上减小
lastX = xPos;
lastY = yPos;
camera.ProcessMouseMovement(xOffset, yOffset,true);
}
void Scroll_CallBack(GLFWwindow* window, double xOffset, double yOffset){
if (fov >= 1.0f && fov <= 45.0f) {
fov -= yOffset;
}
if (fov >= 45.0f) {
fov = 45.0f;
}
if (fov <= 1.0f) {
fov = 1.0f;
}
}
unsigned int LoadTexture(const char* path) {
unsigned int textureID;
glGenTextures(1, &textureID);
int width, height, nrChannels;
unsigned char* data = stbi_load(path, &width, &height, &nrChannels, 0);
if (data) {
GLenum format;
if (nrChannels == 1)
format = GL_RED;
else if (nrChannels == 3)
format = GL_RGB;
else if (nrChannels == 4)
format = GL_RGBA;
glActiveTexture(GL_TEXTURE0);
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);
}
else
{
std::cout << "Load Texture Failed at path:" << path <<std::endl;
stbi_image_free(data);
}
return textureID;
}