This repository was archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.cpp
More file actions
194 lines (155 loc) · 5.92 KB
/
main.cpp
File metadata and controls
194 lines (155 loc) · 5.92 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
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
#include <cstdio>
#include <iostream>
#include <base/appinstance.h>
#include <base/surfacewindows.h>
#include <base/device.h>
#include <renderer/particles_element.h>
#include <renderer/scene.h>
#include <base/buffer.h>
#include <psim/model.h>
#include <psim/point_generator.h>
#define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
#define GLM_FORCE_LEFT_HANDED
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
constexpr int SCR_WIDTH = 1280;
constexpr int SCR_HEIGHT = 960;
constexpr float WORLD_SIZE = 500;
namespace {
std::size_t MAX_PARTICLES = 1000000;
float RATE_NUM = 200.f;
float RATE_DEN = 0.1f;
uint32_t TTL = 600;
};
psim::Model::GeneratorList initGeneratorsList()
{
psim::Model::GeneratorList genList;
psim::generators::PointGenerator* g = new psim::generators::PointGenerator(
{ 0.0f, 250.0f, 0.0f },
psim::generators::PointGenerator::defTheta(90, 5),
psim::generators::PointGenerator::defPhi(-90, 5),
psim::generators::PointGenerator::defTTL(TTL, 0),
psim::generators::PointGenerator::defMass(5.0e+10f, 0),
psim::generators::PointGenerator::defSpeed(95, 1),
psim::generators::PointGenerator::defSpawningRate(RATE_NUM, RATE_DEN)
);
genList.push_back(g);
g = nullptr;
return genList;
}
psim::interactors::Setup initInteractors()
{
//GPU_TP48
psim::interactors::Setup ret;
ret.pointGravity[0] = {{ -60.0f, 100.0f, 0.0f}, 8.0e+15f, psim::interactors::G};
ret.pointGravity[1] = {{ 60.0f, 150.0f, 0.0f}, 4.0e+15f, psim::interactors::G};
ret.planarGravity[0] = {{0.0f, 300.0f, 0.0f},{ 0.0f, -1.0f, 0.0f}, 2.5e+15f, psim::interactors::G, 1};
// ret.constForce[0] = {{0.0f, 1.0f, 0.0f}, 10.0e2f};
ret.pointGravityCount = 2;
ret.planarGravityCount = 1;
ret.constForceCount = 0;
return ret;
}
glm::mat4 initMVP()
{
glm::mat4 model = glm::mat4();
glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
return proj * view * model;
}
bool getSettings(int argc, char* argv[])
{
for(int i = 1; i< argc; ++i) {
if(strcmp("-np", argv[i]) == 0) {
MAX_PARTICLES = atoi(argv[++i]);
} else if(strcmp("-rn", argv[i]) == 0) {
RATE_NUM = atof(argv[++i]);
} else if(strcmp("-rd", argv[i]) == 0) {
RATE_DEN = atof(argv[++i]);
} else if(strcmp("-ttl", argv[i]) == 0) {
TTL = atoi(argv[++i]);
} else {
return false;
}
}
return true;
}
int main(int argc, char* argv[])
{
bool success = getSettings(argc, argv);
if(!success) {
std::clog << "Incorrect arguments\n"
<<"\t-np <UINT>: number of particles (default: 10000)\n"
<<"\t-rn <FLOAT>: spawning rate #particles (default 200)\n"
<<"\t-rd <FLOAT>: spawning rate period in seconds(default 0.1)\n"
<<"\t-ttl <UINT>: particle lifetime in seconds(default 600)\n";
return 0;
}
GLFWwindow *wnd;
glfwInit();
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
wnd = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Particle Vulkan CPU", nullptr, nullptr);
try {
//GPU_TP41
base::AppInstance appInstance;
base::SurfaceWindows appSurface(appInstance, GetModuleHandle(nullptr), glfwGetWin32Window(wnd));
base::Device appDevice(appInstance, appSurface.surface(), base::Device::INTEL_VENDOR_ID);
base::Buffer buffer(MAX_PARTICLES, appDevice);
auto genList = initGeneratorsList();
auto interactors = initInteractors();
auto mvp = initMVP();
psim::Model::size worldSize;
worldSize[base::axis::X] = WORLD_SIZE;
worldSize[base::axis::Y] = WORLD_SIZE;
worldSize[base::axis::Z] = WORLD_SIZE;
//GPU_TP44
psim::Model world(appDevice, buffer, worldSize, genList, interactors);
for (psim::generators::BaseGenerator* g : genList) {
delete g;
g = nullptr;
}
genList.clear();
renderer::Scene scene(appDevice);
//GPU_TP45
renderer::ParticlesElement partElem(appDevice, scene.renderPass(), buffer.dataSizeTotal());
scene.addToScene(&partElem);
partElem.setVertexBuffer(buffer.dataBuffer());
partElem.setMVP(mvp);
std::chrono::system_clock::time_point prev = std::chrono::system_clock::now();
std::chrono::system_clock::time_point now;
std::chrono::system_clock::time_point fpsBegin = prev;
uint32_t framecount = 0;
std::chrono::microseconds dt;
while (!glfwWindowShouldClose(wnd)) {
now = std::chrono::system_clock::now();
dt = std::chrono::duration_cast<std::chrono::microseconds>(now - prev);
prev = now;
world.progress(dt);
partElem.setCurrentCount(buffer.activeCount());
scene.render();
++framecount;
uint32_t fpsdt = std::chrono::duration_cast<std::chrono::seconds>(now - fpsBegin).count();
if (fpsdt >= 1) {
std::clog << "FPS: " << framecount << " " << "\n";
framecount = 0;
fpsBegin = now;
}
glfwPollEvents();
}
}
catch (const std::runtime_error& e) {
std::clog << "Exception raised: " << e.what() << std::endl;
}
catch (...) {
std::clog << "Unexpected exception raised\n";
}
glfwDestroyWindow(wnd);
glfwTerminate();
system("pause");
return 0;
}