-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
114 lines (93 loc) · 3.36 KB
/
main.cpp
File metadata and controls
114 lines (93 loc) · 3.36 KB
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
#include "cuda_runtime.h"
#include "glad/glad.h"
#include "GLFW/glfw3.h"
#include <chrono>
#include <cmath>
#include <mutex>
#include <random>
#include "computation/Behavior.h"
#include "computation/types.h"
#include "Config.hpp"
#include "graphics/Aquarium.hpp"
#include "graphics/Shoal.hpp"
#include "graphics/UI.hpp"
void quit(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
}
int main()
{
Config config = Config();
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
GLFWwindow* window = glfwCreateWindow(config.WIDTH, config.HEIGHT, "Fishmation - Loading...", config.FULLSCREEN ? glfwGetPrimaryMonitor() : NULL, NULL);
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
if (config.FULLSCREEN) {
glfwSetKeyCallback(window, quit);
}
glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE);
// Generate random fish positions from a normal distribution
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<float> distribution(0.0f, 0.5f);
float* shoalData = new float[config.FISH_COUNT * 3];
for (int i = 0; i < config.FISH_COUNT * 3; i++) {
float random_number;
do { random_number = distribution(gen); } while (random_number < -0.9f || random_number > 0.9f);
shoalData[i] = random_number;
}
// Generate perspective matrices
glm::mat4 view = glm::lookAt(
glm::vec3(3.0f, 0.0f, 0.5f),
glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 0.0f, 1.0f)
);
glm::mat4 proj = glm::perspective(glm::radians(70.0f), (float)config.WIDTH / (float)config.HEIGHT, 1.0f, 10.0f);
// Set initial fish properties
computation::FishProperties properties;
properties.mass = 1.0f;
properties.maxForce = 0.0001f;
properties.maxSpeed = 0.001f;
properties.fieldOfViewCos = (float)std::cos(270.0f / 2 * 3.14159 / 180);
properties.viewDistance = config.REGION_SIZE;
properties.predatorViewDistance = 0.6f;
properties.containmentWeight = 50.0f;
properties.alignmentWeight = 50.0f;
properties.cohesionWeight = 50.0f;
properties.separationWeight = 50.0f;
properties.predatorAvoidanceWeight = 5.0f;
graphics::UI ui = graphics::UI(window, properties, config);
graphics::Aquarium aquarium = graphics::Aquarium(view, proj);
graphics::Shoal shoal = graphics::Shoal(config, view, proj, shoalData);
computation::Behavior behavior = computation::Behavior(config, shoal.GetShoalBuffer(), properties);
delete[] shoalData;
glfwSetWindowTitle(window, "Fishmation");
cudaError_t cudaStatus;
auto t_start = std::chrono::high_resolution_clock::now();
while (!glfwWindowShouldClose(window)) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
auto t_now = std::chrono::high_resolution_clock::now();
float time = std::chrono::duration_cast<std::chrono::duration<float>>(t_now - t_start).count();
aquarium.Draw(time);
shoal.Draw(time);
ui.Draw();
glfwSwapBuffers(window);
glfwPollEvents();
cudaStatus = behavior.ComputeMove();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "ComputeMove failed!");
return 1;
}
}
return 0;
}