-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
306 lines (248 loc) · 8.54 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
/*
* OpenGL version 4.6 project.
*/
#include <ctime>
#include <iostream>
#include <filesystem>
#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 <SOIL2/SOIL2.h>
#include "shader.hpp"
#include "camera.hpp"
#include "terrain_engine.h"
namespace fs = std::filesystem;
using namespace cg;
// input image files
constexpr auto HEIGHTMAP_FILE = "assets/heightmap.bmp";
constexpr auto TEXTURE_FILE = "assets/terrain-texture3.bmp";
constexpr auto DETAIL_FILE = "assets/detail.bmp";
constexpr const char* SKYBOX_FILES[5] = {
"assets/SkyBox/SkyBox0.bmp",
"assets/SkyBox/SkyBox1.bmp",
"assets/SkyBox/SkyBox2.bmp",
"assets/SkyBox/SkyBox3.bmp",
"assets/SkyBox/SkyBox4.bmp",
};
constexpr auto WATER_FILE = "assets/SkyBox/SkyBox5.bmp";
constexpr auto SKYBOX_VERT_SHADER = "shaders/skybox.vert";
constexpr auto SKYBOX_FRAG_SHADER = "shaders/skybox.frag";
constexpr auto WATER_VERT_SHADER = "shaders/water.vert";
constexpr auto WATER_FRAG_SHADER = "shaders/water.frag";
constexpr auto TERRAIN_VERT_SHADER = "shaders/terrain.vert";
constexpr auto TERRAIN_FRAG_SHADER = "shaders/terrain.frag";
constexpr auto LAMP_VERT_SHADER = "shaders/lamp.vert";
constexpr auto LAMP_FRAG_SHADER = "shaders/lamp.frag";
// --------------------------------------
// window settings
int screenWidth = 1902;
int screenHeight = 1080;
bool keys[1024]{false};
Camera camera(glm::vec3(0.0f, 1.5f, 15.0f), glm::vec3(0.0f, 0.0f, -1.0f), 2.0f);
// -----------------------------------------------------------
// helper functions
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mode);
void framebufferSizeCallback(GLFWwindow* window, int width, int height);
void mouseCallback(GLFWwindow* window, double xpos, double ypos);
void scrollCallback(GLFWwindow* window, double xoffset, double yoffset);
void moveCamera(GLfloat deltaTime);
void saveScreenshot();
int main()
{
// Setup a GLFW window
// init GLFW, set GL version & pipeline info
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// create a window
GLFWwindow* window = glfwCreateWindow(screenWidth, screenHeight, "Yifei Li - Terrain Engine", nullptr, nullptr);
if (window == nullptr) {
std::cerr << "Error creating window" << std::endl;
glfwTerminate();
return -1;
}
// use newly created window as context
glfwMakeContextCurrent(window);
// register callbacks
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
glfwSetKeyCallback(window, keyCallback);
glfwSetCursorPosCallback(window, mouseCallback);
glfwSetScrollCallback(window, scrollCallback);
// ---------------------------------------------------------------
// Connect GLAD to GLFW by registerring glfwGetProcAddress() as GLAD loader function,
// this must be done after setting current context
if (gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) == 0) {
std::cerr << "Error registerring gladLoadGLLoader" << std::endl;
glfwTerminate();
return -2;
}
// Setup OpenGL options
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
// ---------------------------------------------------------------
// Load terrain engine resources
TerrainEngine engine;
/* load an image as a heightmap, forcing greyscale (so channels should be 1) */
if (!engine.LoadHeightmap(HEIGHTMAP_FILE)) {
std::cerr << "Error loading heightmap '" << HEIGHTMAP_FILE << "'" << std::endl;
glfwTerminate();
return -3;
}
if (!engine.LoadTerrainTexture(TEXTURE_FILE, DETAIL_FILE)) {
std::cerr << "Error loading land texture '" << TEXTURE_FILE << "'" << std::endl;
glfwTerminate();
return -3;
}
if (!engine.LoadWaterTexture(WATER_FILE)) {
std::cerr << "Error loading water texture '" << WATER_FILE << "'" << std::endl;
glfwTerminate();
return -3;
}
if (!engine.LoadSkybox(SKYBOX_FILES)) {
std::cerr << "Error loading skybox images" << std::endl;
glfwTerminate();
return -3;
}
// -----------------------------------------
// Install GLSL Shader programs
if (!engine.InstallSkyboxShaders(SKYBOX_VERT_SHADER, SKYBOX_FRAG_SHADER)) {
std::cerr << "Error creating Shader Program for skybox" << std::endl;
glfwTerminate();
return -4;
}
if (!engine.InstallWaterShaders(WATER_VERT_SHADER, WATER_FRAG_SHADER)) {
std::cerr << "Error creating Shader Program for water" << std::endl;
glfwTerminate();
return -4;
}
if (!engine.InstallTerrainShaders(TERRAIN_VERT_SHADER, TERRAIN_FRAG_SHADER)) {
std::cerr << "Error creating Shader Program for terrain" << std::endl;
glfwTerminate();
return -4;
}
if (!engine.InstallLampShaders(LAMP_VERT_SHADER, LAMP_FRAG_SHADER)) {
std::cerr << "Error creating Shader Program for lamp" << std::endl;
glfwTerminate();
return -4;
}
// -----------------------------------------
// Define the viewport dimensions
glViewport(0, 0, screenWidth, screenHeight);
// Update loop
GLfloat deltaTime = 0.0f; // Time between current frame and last frame
GLfloat lastFrame = 0.0f; // Time of last frame
while (glfwWindowShouldClose(window) == 0) {
// Calculate deltatime of current frame
GLfloat currentFrame = GLfloat(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// check event queue
glfwPollEvents();
/* your update code here */
moveCamera(deltaTime);
// draw background
GLfloat red = 0.2f;
GLfloat green = 0.3f;
GLfloat blue = 0.3f;
glClearColor(red, green, blue, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Camera/View transformation
glm::mat4 view = camera.ViewMatrix();
// Projection
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom()), (GLfloat)screenWidth / (GLfloat)screenHeight, 0.1f, 10000.0f);
// draw sky & water
engine.DrawSkybox(view, projection);
engine.DrawTerrain(view, projection, camera.Position());
engine.DrawWater(view, projection, deltaTime, camera.Position());
//engine.DrawLamp(view, projection);
// swap buffer
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
/* ======================== helper functions ======================== */
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
// exit when pressing ESC
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
else if (key == GLFW_KEY_PRINT_SCREEN && action == GLFW_PRESS) {
saveScreenshot();
}
else if (key >= 0 && key < 1024) {
if (action == GLFW_PRESS) {
keys[key] = true;
} else if (action == GLFW_RELEASE) {
keys[key] = false;
}
}
}
void moveCamera(GLfloat deltaTime)
{
// Camera controls
if (keys[GLFW_KEY_W]) {
camera.ProcessKeyboard(Camera::Movement::FORWARD, deltaTime);
}
if (keys[GLFW_KEY_S]) {
camera.ProcessKeyboard(Camera::Movement::BACKWARD, deltaTime);
}
if (keys[GLFW_KEY_A]) {
camera.ProcessKeyboard(Camera::Movement::LEFT, deltaTime);
}
if (keys[GLFW_KEY_D]) {
camera.ProcessKeyboard(Camera::Movement::RIGHT, deltaTime);
}
}
void mouseCallback(GLFWwindow* window, double xpos, double ypos)
{
static GLfloat lastX = GLfloat(screenWidth) / 2;
static GLfloat lastY = GLfloat(screenHeight) / 2;
GLfloat xoffset = GLfloat(xpos) - lastX;
GLfloat yoffset = lastY - GLfloat(ypos); // Reversed since y-coordinates go from bottom to left
lastX = GLfloat(xpos);
lastY = GLfloat(ypos);
camera.ProcessMouseMovement(xoffset, yoffset);
}
void scrollCallback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.ProcessMouseScroll(GLfloat(yoffset));
}
void framebufferSizeCallback(GLFWwindow* window, int width, int height)
{
screenWidth = width;
screenHeight= height;
// resize window
glViewport(0, 0, screenWidth, screenHeight);
}
void saveScreenshot()
{
auto dir = fs::current_path() / "screenshots";
if (!(fs::exists(dir) && fs::is_directory(dir))) {
if (!fs::create_directories(dir)) {
std::cerr << "Cannot create screenshot directory '" << dir << "'" << std::endl;
return;
}
}
std::time_t t = std::time(nullptr);
#ifdef __STDC_LIB_EXT1__
struct tm buf;
localtime_s(&t, &buf);
#else
struct tm buf;
localtime_s(&buf, &t);
#endif
char mbstr[32];
std::strftime(mbstr, sizeof(mbstr), "%Y%m%d-%H%M%S", &buf);
auto filename = dir / (std::string("Terrain_Engine-") + mbstr + ".png");
auto filenameStr = filename.string();
if (SOIL_save_screenshot(filenameStr.c_str(), SOIL_SAVE_TYPE_PNG, 0, 0, screenWidth, screenHeight) == 0) {
std::cerr << "Saving screenshot '" << filename <<"' failed" << std::endl;
} else {
std::cout << "Screenshot saved to '" << filenameStr << "'" << std::endl;
}
}