forked from hehao98/WaterRendering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Water2.cpp
330 lines (284 loc) · 11.8 KB
/
Water2.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
//
// Created by 何昊 on 2018/03/05.
//
#include <iostream>
#include <algorithm>
// GLM Math Library
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// GLAD: A library that wraps OpenGL functions to make things easier
// Note that GLAD MUST be included before GLFW
#include "glad/glad.h"
// GLFW: A library that helps us manage windows
#include <GLFW/glfw3.h>
// Wrapper classes to make things a little easier
#include "Shader.h"
#include "Camera.h"
#include "Skybox.h"
#include "TextRenderer.h"
// Water related header file
#include "VertexBufferOcean.h"
// **********GLFW window related functions**********
// Returns pointer to a initialized window with OpenGL context set up
GLFWwindow *init();
// Sometimes user might resize the window. so the OpenGL viewport should be adjusted as well.
void frameBufferSizeCallback(GLFWwindow *window, int width, int height);
// User input is handled in this function
void processInput(GLFWwindow *window);
// Mouse input is handled in this function
void mouseCallback(GLFWwindow *window, double xpos, double ypos);
void scrollCallback(GLFWwindow *window, double offsetX, double offsetY);
// **********GLFW window related attributes**********
int gScreenWidth = 800;
int gScreenHeight = 600;
float gDeltaTime = 0.0f;
float gLastFrame = 0.0f;
// Global vaiables and flags
Camera gCamera;
bool gDrawNormals = false;
int main()
{
GLFWwindow *window = init();
if (window == nullptr) {
std::cout << "Failed to initialize GLFW and OpenGL!" << std::endl;
return -1;
}
// Load shaders
Shader shader("shaders/SingleColor.vert", "shaders/Water.frag");
Shader textShader("shaders/TextShader.vert", "shaders/TextShader.frag");
Shader normalShader("shaders/DrawNormal.vert", "shaders/DrawNormal.frag",
"shaders/DrawNormal.geom");
// Initialize fonts
TextRenderer textRenderer;
// Initialize skybox
std::vector<std::string> skyboxPaths = {
"textures/TropicalSunnyDay/TropicalSunnyDayLeft2048.png",
"textures/TropicalSunnyDay/TropicalSunnyDayRight2048.png",
"textures/TropicalSunnyDay/TropicalSunnyDayUp2048.png",
"textures/TropicalSunnyDay/TropicalSunnyDayDown2048.png",
"textures/TropicalSunnyDay/TropicalSunnyDayFront2048.png",
"textures/TropicalSunnyDay/TropicalSunnyDayBack2048.png",
};
std::vector<std::string> skyboxPaths2 = {
"textures/SunSet/SunSetLeft2048.png",
"textures/SunSet/SunSetRight2048.png",
"textures/SunSet/SunSetUp2048.png",
"textures/SunSet/SunSetDown2048.png",
"textures/SunSet/SunSetFront2048.png",
"textures/SunSet/SunSetBack2048.png",
};
Shader skyboxShader("shaders/SkyboxShader.vert", "shaders/SkyboxShader.frag");
Skybox skybox(skyboxPaths2);
// Necessary OpenGL Parameters
glEnable(GL_DEPTH_TEST);
// Enable gamma correction
glEnable(GL_FRAMEBUFFER_SRGB);
// Enable anti-aliasing
glEnable(GL_MULTISAMPLE);
// Enable blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gCamera.Position = glm::vec3(0.0f, 10.0f, 20.0f);
VertexBufferOcean ocean(glm::vec2(2.0f, 2.0f), 128, 0.02f);
ocean.generateWave((float)glfwGetTime());
// Pass the vertex data to GPU
unsigned int VBO, VBO2, EBO, VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * ocean.vertexCount, ocean.vertices, GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glGenBuffers(1, &VBO2);
glBindBuffer(GL_ARRAY_BUFFER, VBO2);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * ocean.normalCount, ocean.normals, GL_DYNAMIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * ocean.indexCount, ocean.indices, GL_STATIC_DRAW);
std::cout << "Total vertices: " << ocean.vertexCount << std::endl;
for (int i = 0; i < 50; ++i) {
using namespace std;
cout << "(" << ocean.vertices[3 * i] << ","
<< ocean.vertices[3 * i + 1] << ","
<< ocean.vertices[3 * i + 2] << ")";
cout << "(" << ocean.indices[3 * i] << ","
<< ocean.indices[3 * i + 1] << ","
<< ocean.indices[3 * i + 2] << ")";
cout << "(" << ocean.normals[3 * i] << ","
<< ocean.normals[3 * i + 1] << ","
<< ocean.normals[3 * i + 2] << ")" << endl;
}
// Game loop
while (!glfwWindowShouldClose(window)) {
// Calculate how much time since last frame
auto currentFrame = (float)glfwGetTime();
gDeltaTime = currentFrame - gLastFrame;
gLastFrame = currentFrame;
auto currentFPS = (int)(1.0f / gDeltaTime);
// Handle user input
processInput(window);
// Update wave data
ocean.generateWave((float)glfwGetTime());
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * ocean.vertexCount, ocean.vertices, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, VBO2);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * ocean.normalCount, ocean.normals, GL_DYNAMIC_DRAW);
// All the rendering starts from here
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set up view and projection matrix
glm::mat4 view = gCamera.GetViewMatrix();
glm::mat4 projection = glm::perspective(glm::radians(gCamera.Zoom),
(float)gScreenWidth / gScreenHeight, 0.1f, 100.0f);
skybox.Draw(skyboxShader, view, projection);
shader.use();
// Set vertex shader data
shader.setMat4("view", view);
shader.setMat4("projection", projection);
shader.setMat4("model", glm::mat4(1.0f));
shader.setFloat("time", (float)glfwGetTime());
// Set fragment shader data
shader.setVec3("viewPos", gCamera.Position);
shader.setVec3("deepWaterColor", glm::vec3(0.1f, 0.2f, 0.35f));
shader.setVec3("shallowWaterColor", glm::vec3(0.45f, 0.55f, 0.7f));
shader.setVec4("color", glm::vec4(1.0f, 0.0f, 0.0f, 1.0f));
shader.setInt("skybox", 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.getCubeMap());
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, ocean.indexCount, GL_UNSIGNED_INT, nullptr);
// Draw normals for debugging
if (gDrawNormals) {
normalShader.use();
normalShader.setMat4("view", view);
normalShader.setMat4("projection", projection);
normalShader.setMat4("model", glm::mat4(1.0f));
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, ocean.indexCount, GL_UNSIGNED_INT, nullptr);
}
// Start to render texts
textRenderer.projection = glm::ortho(0.0f, (float)gScreenWidth,
0.0f, (float)gScreenHeight);
textRenderer.renderText(textShader, "FPS: " + std::to_string(currentFPS),
0.0f, gScreenHeight - 48.0f*0.3f, 0.3f,
glm::vec3(0.0, 1.0f, 1.0f));
textRenderer.renderText(textShader, "Use WSAD to move, mouse to look around",
0.0f, 2.0f, 0.3f,
glm::vec3(0.0, 1.0f, 1.0f));
textRenderer.renderText(textShader, "Use Q to switch between polygon and fill mode",
0.0f, 18.0f, 0.3f,
glm::vec3(0.0, 1.0f, 1.0f));
textRenderer.renderText(textShader, "Use E to decide whether to draw normals",
0.0f, 34.0f, 0.3f,
glm::vec3(0.0, 1.0f, 1.0f));
// Rendering Ends here
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
GLFWwindow *init()
{
// Initialization of GLFW context
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Something needed for Mac OS X
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// For anti-aliasing effects
glfwWindowHint(GLFW_SAMPLES, 4);
// Create a window object
GLFWwindow *window = glfwCreateWindow(gScreenWidth, gScreenHeight, "Window Title", nullptr, nullptr);
if (window == nullptr) {
std::cout << "Failed to create GLFW window!" << std::endl;
glfwTerminate();
return nullptr;
}
glfwMakeContextCurrent(window);
// Initialize GLAD before calling OpenGL functions
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
return nullptr;
}
// Tell OpenGL the size of rendering window
glViewport(0, 0, gScreenWidth * 2, gScreenHeight * 2);
// Set the windows resize callback function
glfwSetFramebufferSizeCallback(window, frameBufferSizeCallback);
// Set up mouse input
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, mouseCallback);
glfwSetScrollCallback(window, scrollCallback);
return window;
}
void frameBufferSizeCallback(GLFWwindow *window, int width, int height)
{
gScreenWidth = width;
gScreenHeight = height;
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow *window)
{
// Exit
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
// Handle camera movement
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
gCamera.ProcessKeyboard(FORWARD, gDeltaTime);
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
gCamera.ProcessKeyboard(BACKWARD, gDeltaTime);
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
gCamera.ProcessKeyboard(LEFT, gDeltaTime);
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
gCamera.ProcessKeyboard(RIGHT, gDeltaTime);
}
// Some mode switches
static bool isPolygon = false;
static double lastPressedTime = 0.0;
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS
&& glfwGetTime() - lastPressedTime > 0.2) {
if (isPolygon) {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
isPolygon = false;
} else {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
isPolygon = true;
}
lastPressedTime = glfwGetTime();
}
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS
&& glfwGetTime() - lastPressedTime > 0.2) {
gDrawNormals = !gDrawNormals;
lastPressedTime = glfwGetTime();
}
}
void mouseCallback(GLFWwindow *window, double xpos, double ypos)
{
// Variables needed to handle mouse input
static float lastMouseX = 400.0f;
static float lastMouseY = 300.0f;
static bool firstMouse = true;
if (firstMouse) {
lastMouseX = (float)xpos;
lastMouseY = (float)ypos;
firstMouse = false;
}
// Calculate mouse movement since last frame
float offsetX = (float)xpos - lastMouseX;
float offsetY = (float)ypos - lastMouseY;
lastMouseX = (float)xpos;
lastMouseY = (float)ypos;
gCamera.ProcessMouseMovement(offsetX, offsetY);
}
void scrollCallback(GLFWwindow *window, double offsetX, double offsetY)
{
gCamera.ProcessMouseScroll((float)offsetY);
}