-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
380 lines (284 loc) · 11.8 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#define _USE_MATH_DEFINES
#include "mesh.hpp"
#include "camera.hpp"
#include "shader.hpp"
#include "object3d.hpp"
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "gl_includes.hpp"
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
#define _MY_OPENGL_IS_33_
// Window parameters
GLFWwindow *g_window {};
// GPU objects
GLuint g_program {}; // A GPU program contains at least a vertex shader and a fragment shader
Camera g_camera {};
std::shared_ptr<Mesh> g_mesh {};
float g_fps = 0.0f;
// Executed each time the window is resized. Adjust the aspect ratio and the rendering viewport to the current window.
void windowSizeCallback(GLFWwindow *window, int width, int height) {
g_camera.setAspectRatio(static_cast<float>(width) / static_cast<float>(height));
glViewport(0, 0, (GLint)width, (GLint)height); // Dimension of the rendering region in the window
}
// Executed each time a key is entered.
void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if(action == GLFW_PRESS) {
if (key == GLFW_KEY_W) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
if (key == GLFW_KEY_F) {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
if ((key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q)) {
glfwSetWindowShouldClose(window, true); // Closes the application if the escape key is pressed
}
}
}
float g_cameraDistance = 5.0f;
float g_cameraAngleX = 0.0f;
// Scroll for zooming
void scrollCallback(GLFWwindow *window, double xoffset, double yoffset) {
g_cameraDistance -= yoffset * 0.1f;
g_cameraDistance = std::max(g_cameraDistance, 0.1f);
g_cameraAngleX -= xoffset * 0.04f;
}
void errorCallback(int error, const char *desc) {
std::cout << "Error " << error << ": " << desc << std::endl;
}
void initGLFW() {
glfwSetErrorCallback(errorCallback);
// Initialize GLFW, the library responsible for window management
if (!glfwInit()) {
std::cerr << "ERROR: Failed to init GLFW" << std::endl;
std::exit(EXIT_FAILURE);
}
// Before creating the window, set some option flags
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
// Create the window
g_window = glfwCreateWindow(
1024, 768,
"Interactive 3D Applications (OpenGL) - Simple Solar System", nullptr, nullptr);
if (!g_window) {
std::cerr << "ERROR: Failed to open window" << std::endl;
glfwTerminate();
std::exit(EXIT_FAILURE);
}
// Load the OpenGL context in the GLFW window using GLAD OpenGL wrangler
glfwMakeContextCurrent(g_window);
glfwSetWindowSizeCallback(g_window, windowSizeCallback);
glfwSetKeyCallback(g_window, keyCallback);
glfwSetScrollCallback(g_window, scrollCallback);
}
void initImGui() {
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(g_window, true); // Second param install_callback=true will install GLFW callbacks and chain to existing ones.
ImGui_ImplOpenGL3_Init();
}
void initOpenGL() {
// Load extensions for modern OpenGL
if (!gladLoadGL()) {
std::cerr << "ERROR: Failed to initialize OpenGL context" << std::endl;
glfwTerminate();
std::exit(EXIT_FAILURE);
}
glEnable(GL_DEBUG_OUTPUT);
glCullFace(GL_BACK); // Specifies the faces to cull (here the ones pointing away from the camera)
glEnable(GL_CULL_FACE); // Enables face culling (based on the orientation defined by the CW/CCW enumeration).
glDepthFunc(GL_LESS); // Specify the depth test for the z-buffer
glEnable(GL_DEPTH_TEST); // Enable the z-buffer test in the rasterization
glClearColor(0.1f, 0.1f, 0.1f, 1.0f); // specify the background color, used any time the framebuffer is cleared
// Disable v-sync
// glfwSwapInterval(0);
}
void initGPUprogram() {
g_program = glCreateProgram(); // Create a GPU program, i.e., two central shaders of the graphics pipeline
loadShader(g_program, GL_VERTEX_SHADER, "resources/vertexShader.glsl");
loadShader(g_program, GL_FRAGMENT_SHADER, "resources/fragmentShader.glsl");
glLinkProgram(g_program); // The main GPU program is ready to be handle streams of polygons
glUseProgram(g_program);
}
void initCPUgeometry() {
//g_mesh = std::make_shared<Object3D>(Mesh::genPlane());
g_mesh = Mesh::genSphere(100);
glBindVertexArray(g_mesh->m_vao);
}
void initCamera() {
int width, height;
glfwGetWindowSize(g_window, &width, &height);
g_camera.setAspectRatio(static_cast<float>(width) / static_cast<float>(height));
g_camera.setPosition(glm::vec3(0.0, 0.0, 3.0));
g_camera.setNear(0.1);
g_camera.setFar(80);
g_camera.setFoV(90);
}
void init() {
initGLFW();
initOpenGL();
initCPUgeometry();
initGPUprogram();
initCamera();
initImGui();
}
void clear() {
glDeleteProgram(g_program);
glfwDestroyWindow(g_window);
glfwTerminate();
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
struct TexturingParams {
glm::vec3 groundColor;
glm::vec3 sandColor;
glm::vec3 waterColor;
glm::vec3 treeColor;
glm::vec3 trunkColor;
glm::vec3 grassColor;
int treeDensity;
int grassDensity;
};
struct GenerationParams {
float heightFactor;
float terrainHeightFactor;
int nOctaves;
float terrainScale;
float terrainHeightOffset;
};
struct GlobalParams {
int nbShells;
TexturingParams texturingParams;
GenerationParams generationParams;
};
GlobalParams g_globalParams = {
128,
{
glm::vec3(0.6f, 0.75f, 0.15f),
glm::vec3(0.9f, 0.8f, 0.2f),
glm::vec3(0.0f, 0.0f, 1.0f),
glm::vec3(0.2f, 0.7f, 0.1f),
glm::vec3(0.5f, 0.3f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f),
100,
2000
},
{
0.3f,
0.2f,
8,
1.0f,
0.1f
}
};
void renderUI() {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// Start drawing here
ImGui::Begin("Parameters", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::SliderInt("Number of shells", &g_globalParams.nbShells, 8, 256);
ImGui::LabelText("Texturing parameters", "");
ImGui::ColorEdit3("Ground color", &g_globalParams.texturingParams.groundColor[0]);
ImGui::ColorEdit3("Sand color", &g_globalParams.texturingParams.sandColor[0]);
ImGui::ColorEdit3("Water color", &g_globalParams.texturingParams.waterColor[0]);
ImGui::NewLine();
ImGui::ColorEdit3("Tree color", &g_globalParams.texturingParams.treeColor[0]);
ImGui::ColorEdit3("Trunk color", &g_globalParams.texturingParams.trunkColor[0]);
ImGui::ColorEdit3("Grass color", &g_globalParams.texturingParams.grassColor[0]);
ImGui::NewLine();
ImGui::SliderInt("Tree density", &g_globalParams.texturingParams.treeDensity, 1, 500);
ImGui::SliderInt("Grass density", &g_globalParams.texturingParams.grassDensity, 1, 5000);
ImGui::End();
ImGui::Begin("Generation parameters", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::SliderFloat("Height factor", &g_globalParams.generationParams.heightFactor, 0.0f, 1.0f);
ImGui::SliderFloat("Terrain height factor", &g_globalParams.generationParams.terrainHeightFactor, 0.0f, 1.0f);
ImGui::SliderInt("Number of octaves", &g_globalParams.generationParams.nOctaves, 1, 16);
ImGui::SliderFloat("Terrain scale", &g_globalParams.generationParams.terrainScale, 0.0f, 10.0f);
ImGui::SliderFloat("Terrain height offset", &g_globalParams.generationParams.terrainHeightOffset, 0.0f, 10.0f);
ImGui::End();
ImGui::Begin("Performance", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::Text("FPS: %.1f", g_fps);
ImGui::End();
// End drawing here
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
void setParamsUniforms() {
// Texturing params
setUniform(g_program, "u_texturingParams.groundColor", g_globalParams.texturingParams.groundColor);
setUniform(g_program, "u_texturingParams.sandColor", g_globalParams.texturingParams.sandColor);
setUniform(g_program, "u_texturingParams.waterColor", g_globalParams.texturingParams.waterColor);
setUniform(g_program, "u_texturingParams.treeColor", g_globalParams.texturingParams.treeColor);
setUniform(g_program, "u_texturingParams.trunkColor", g_globalParams.texturingParams.trunkColor);
setUniform(g_program, "u_texturingParams.grassColor", g_globalParams.texturingParams.grassColor);
setUniform(g_program, "u_texturingParams.treeDensity", g_globalParams.texturingParams.treeDensity);
setUniform(g_program, "u_texturingParams.grassDensity", g_globalParams.texturingParams.grassDensity);
// Generation params
setUniform(g_program, "u_generationParams.heightFactor", g_globalParams.generationParams.heightFactor);
setUniform(g_program, "u_generationParams.terrainHeightFactor", g_globalParams.generationParams.terrainHeightFactor);
setUniform(g_program, "u_generationParams.nOctaves", g_globalParams.generationParams.nOctaves);
setUniform(g_program, "u_generationParams.terrainScale", g_globalParams.generationParams.terrainScale);
setUniform(g_program, "u_generationParams.terrainHeightOffset", g_globalParams.generationParams.terrainHeightOffset);
}
// The main rendering call
void render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Erase the color and z buffers.
const glm::mat4 viewMatrix = g_camera.computeViewMatrix();
const glm::mat4 projMatrix = g_camera.computeProjectionMatrix();
setUniform(g_program, "u_viewMat", viewMatrix);
setUniform(g_program, "u_projMat", projMatrix);
setUniform(g_program, "u_proj_viewMat", projMatrix * viewMatrix);
setUniform(g_program, "u_modelMat", glm::mat4(1.0f));
setUniform(g_program, "u_transposeInverseModelMat", glm::mat4(1.0f));
setUniform(g_program, "u_cameraPosition", g_camera.getPosition());
setUniform(g_program, "u_time", static_cast<float>(glfwGetTime()));
setParamsUniforms();
// Render objects
for (int i = g_globalParams.nbShells - 1; i >= 0; i--) {
float height = (float)i / (float)(g_globalParams.nbShells-1);
setUniform(g_program, "u_height", height);
glDrawElements(GL_TRIANGLES, g_mesh->m_numIndices, GL_UNSIGNED_INT, 0);
}
renderUI();
}
// Update any accessible variable based on the current time
void update(const float currentTimeInSec) {
// Update the FPS computation
static int frameCount = 0;
static float lastTime = glfwGetTime();
frameCount++;
if (currentTimeInSec - lastTime >= 1.0) {
g_fps = static_cast<float>(frameCount) / (currentTimeInSec - lastTime);
frameCount = 0;
lastTime = currentTimeInSec;
}
// Update the camera position
glm::vec3 targetPosition = glm::vec3(0.05f, 0.05f, 0.0f);
g_camera.setTarget(targetPosition);
glm::vec3 cameraOffset = glm::normalize(glm::vec3(cos(g_cameraAngleX), 0.3f, sin(g_cameraAngleX))) * (1.1f + g_cameraDistance);
g_camera.setPosition(targetPosition + cameraOffset);
}
int main(int argc, char **argv) {
init();
while (!glfwWindowShouldClose(g_window)) {
update(static_cast<float>(glfwGetTime()));
render();
glfwSwapBuffers(g_window);
glfwPollEvents();
}
clear();
return EXIT_SUCCESS;
}