-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
309 lines (260 loc) · 10.5 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
//-----------------------------------------------------------------------
// Main file of Vicsek model simulation using Smoothed-particle hydrodynamics (SPH)
// method.
//
// Licensing: This code is distributed under the Apache License 2.0
// Author: Carlos Planelles Alemany, planelles20(at)gmail(dot)com
//-----------------------------------------------------------------------
// GLM Mathematics
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
// our class includes
#include "./common/display.h"
#include "./common/shaderGeometry.h"
#include "./common/shader.h"
#include "systemSPH.h"
//particles = THREADS*BLOCKS
#define THREADS 512 // 2^9
#define BLOCKS 64 // 2^15 16384
//mesh
// dR = min(1/X_MESH, 1/Y_MESH, 1/Z_MESH)
#define X_MESH 100
#define Y_MESH 100
#define Z_MESH 100
// Window dimensions
const GLuint WIDTH = 1800, HEIGHT = 1000;
// Camera
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
GLfloat yaw = -90.0f; // Yaw is initialized to -90.0 degrees since a yaw of 0.0 results in a direction vector pointing to the right (due to how Eular angles work) so we initially rotate a bit to the left.
GLfloat pitch = 0.0f;
GLfloat lastX = WIDTH / 2.0;
GLfloat lastY = HEIGHT / 2.0;
GLfloat fov = -45.0f;
bool keys[1024];
// Deltatime
GLfloat deltaTime = 0.0f; // Time between current frame and last frame
GLfloat lastFrame = 0.0f; // Time of last frame
//openGL functions
void printInstructions(void);
// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void button_mouse_callback(GLFWwindow* window, int button, int action, int mode);
void do_movement();
int main(void){
// Init GLFW
glfwInit();
// create our window
Display display = Display(WIDTH, HEIGHT, "Vicsek 3D simulation, Carlos Planelles",
key_callback, mouse_callback, scroll_callback, button_mouse_callback);
// Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
glewExperimental = GL_TRUE;
// Initialize GLEW to setup the OpenGL Function pointers
glewInit();
// Define the viewport dimensions
glViewport(0, 0, WIDTH, HEIGHT);
// enable z-buffer
glEnable(GL_DEPTH_TEST);
// Build and compile our shader programs
ShaderGeometry ourShader1 = ShaderGeometry("shader1.vs", "shader1.gs", "shader1.fs");
Shader ourShader2 = Shader("shader2.vs", "shader2.fs");
glEnable(GL_PROGRAM_POINT_SIZE);
// create SPH sistem
SystemSPH systemSPH = SystemSPH(BLOCKS, THREADS, X_MESH, Y_MESH, Z_MESH);
int i = 0;
while(i<1000000 && display.State()){
// Calculate deltatime of current frame
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
//start simulation
//sistem.SeedUpdate(i); //update seed each iteration
systemSPH.Calculate();
//sistem.Particle_print();
//sistem.Indices_print();
//sistem.Calc_print();
/*
// save current data
if(i%10000==0){
sistem.Save("save0.csv");
}
*/
display.PollEvents();
do_movement();
systemSPH.BackGround(0.0, 0.0, 0.0, 1.0);
// draw particles
// use our shader 1
ourShader1.Use();
// Camera/View transformation
glm::mat4 view;
view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
glm::mat4 model;
glm::mat4 projection;
model = glm::rotate(model, 90.0f, glm::vec3(1.0f, 0.0f, 0.0f));
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
projection = glm::perspective(45.0f, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f);
// Get their uniform location shader 1
GLint modelLoc1 = glGetUniformLocation(ourShader1.ThisProgram(), "model");
GLint viewLoc1 = glGetUniformLocation(ourShader1.ThisProgram(), "view");
GLint projLoc1 = glGetUniformLocation(ourShader1.ThisProgram(), "projection");
GLint camerajLoc1 = glGetUniformLocation(ourShader1.ThisProgram(), "camera");
// Pass them to the shader 1
glUniformMatrix4fv(modelLoc1, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc1, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc1, 1, GL_FALSE, glm::value_ptr(projection));
glUniform3fv(camerajLoc1, 1, glm::value_ptr(cameraPos));
//draw particles
systemSPH.DrawParticles();
////// draw boundaries
// use our shader 2
ourShader2.Use();
// Get their uniform location shader 2
GLint modelLoc2 = glGetUniformLocation(ourShader2.ThisProgram(), "model");
GLint viewLoc2 = glGetUniformLocation(ourShader2.ThisProgram(), "view");
GLint projLoc2 = glGetUniformLocation(ourShader2.ThisProgram(), "projection");
// Pass them to the shader 2
glUniformMatrix4fv(modelLoc2, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc2, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc2, 1, GL_FALSE, glm::value_ptr(projection));
//draw boundary
systemSPH.DrawBoundary();
//Swap buffers
display.SwapWindows();
i++;
}
glfwTerminate();
return 0;
}
//////////////////////////////////////////////////////////////////////////////
///////////////////////// Function prototypes source /////////////////////////
//////////////////////////////////////////////////////////////////////////////
void printInstructions(void){
std::cout << "w: move forward" << '\n';
std::cout << "s: move backwards" << '\n';
std::cout << "d: move right" << '\n';
std::cout << "a: move left" << '\n';
std::cout << "i: pitch up" << '\n';
std::cout << "k: pitch down" << '\n';
std::cout << "l: yaw right" << '\n';
std::cout << "j: yaw left" << '\n';
std::cout << "use mouse too" << '\n';
}
// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
if (key >= 0 && key < 1024)
{
if (action == GLFW_PRESS)
keys[key] = true;
else if (action == GLFW_RELEASE)
keys[key] = false;
}
}
void button_mouse_callback(GLFWwindow* window, int button, int action, int mode) {
if (button >= 0 && button < 1024)
{
if (action == GLFW_PRESS)
keys[button] = true;
else if (action == GLFW_RELEASE)
keys[button] = false;
}
}
void do_movement() {
// Camera controls
GLfloat cameraSpeed = 3.0f * deltaTime;
GLfloat sensitivity = 0.5;
if (keys[GLFW_KEY_W])
cameraPos += cameraSpeed * cameraFront;
if (keys[GLFW_KEY_S])
cameraPos -= cameraSpeed * cameraFront;
if (keys[GLFW_KEY_A])
cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (keys[GLFW_KEY_D])
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
if (keys[GLFW_KEY_I]) {
pitch += sensitivity;
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);
}
if (keys[GLFW_KEY_K]) {
pitch -= sensitivity;
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);
}
if (keys[GLFW_KEY_L]) {
yaw += sensitivity;
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);
}
if (keys[GLFW_KEY_J]) {
yaw -= sensitivity;
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);
}
// print position
//std::cout << "cameraPos: " << cameraPos.x << "," << cameraPos.y << "," << cameraPos.z << std::endl;
//std::cout << "cameraFront: " << cameraFront.x << "," << cameraFront.y << "," << cameraFront.z << std::endl;
//std::cout << "cameraUp: " << cameraUp.x << "," << cameraUp.y << "," << cameraUp.z << std::endl;
//glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
//glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
//glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
}
bool firstMouse = true;
void mouse_callback(GLFWwindow* window, double xpos, double ypos) {
if (firstMouse || !keys[GLFW_MOUSE_BUTTON_LEFT]) {
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
if(keys[GLFW_MOUSE_BUTTON_LEFT]){
GLfloat xoffset = xpos - lastX;
GLfloat yoffset = lastY - ypos; // Reversed since y-coordinates go from bottom to left
lastX = xpos;
lastY = ypos;
GLfloat sensitivity = 0.05; // Change this value to your liking
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
// Make sure that when pitch is out of bounds, screen doesn't get flipped
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);
}
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
if (fov >= 1.0f && fov <= 45.0f)
fov -= yoffset;
if (fov <= 1.0f)
fov = 1.0f;
if (fov >= 45.0f)
fov = 45.0f;
}