-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstractsimulation.cc
235 lines (210 loc) · 8.53 KB
/
abstractsimulation.cc
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
#include "abstractsimulation.hpp"
#include <fstream>
#include <sstream>
#include <vector>
glm::vec2 AbstractSimulation::pMouse = glm::vec2(0.0f, 0.0f);
glm::vec3 AbstractSimulation::cameraPos = glm::vec3(1200.0f, M_PI_2, 0.0f); // in spherical coordinates (r, theta, phi)
glm::vec3 AbstractSimulation::centerOfMass = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 AbstractSimulation::upVector = glm::vec3(0.0f, 0.0f, 1.0f);
unsigned int AbstractSimulation::N = 0;
int AbstractSimulation::width = 0;
int AbstractSimulation::height = 0;
char* loadFile(const char *filename) {
char* data;
int len;
std::ifstream ifs(filename, std::ifstream::in);
if(ifs.is_open() == false) {
printf("File not open!\n");
}
ifs.seekg(0, std::ios::end);
len = (int)ifs.tellg();
ifs.seekg(0, std::ios::beg);
data = new char[len + 1];
ifs.read(data, len);
data[len] = 0;
ifs.close();
return data;
}
glm::vec3 sphericalToCartesian(glm::vec3 vec)
{
return glm::vec3(
vec.x * sin(vec.y) * cos(vec.z),
vec.x * sin(vec.y) * sin(vec.z),
vec.x * cos(vec.y)
);
}
AbstractSimulation::AbstractSimulation()
{
potentialFieldRendering = false;
}
AbstractSimulation::~AbstractSimulation()
{
delete bodies;
delete velocities;
}
void AbstractSimulation::setPotentialFieldRendering(bool enable)
{
potentialFieldRendering = enable;
}
void AbstractSimulation::init(int dimensions)
{
#ifdef _DEBUG_THRUST_
AbstractSimulation::N = 8;
#else
if(potentialFieldRendering == true) {
AbstractSimulation::N = 4*1024;
} else {
AbstractSimulation::N = 11*1024;
}
#endif
initNBodyPositions(_2D_SIMULATION_);
size_t num_bytes = AbstractSimulation::N * sizeof(glm::vec4);
// Create vertex array
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// Create a buffer of bodies' positions and allocate memory
glGenBuffers(1, &posBodiesBuffer);
glBindBuffer(GL_ARRAY_BUFFER, posBodiesBuffer);
glBufferData(GL_ARRAY_BUFFER, num_bytes, 0, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
if(potentialFieldRendering == true) {
// Fragmentation of a potential field
potential_Hx = 125; // x-axis
potential_Hy = 125; // y-axis
// Create a buffer of potential field positions
createPotentialPosition_VBO(&potentialFieldPositionBuffer, potential_Hx, potential_Hy);
// Create a buffer of potential field colors
createPotentialColor_VBO(&potentialFieldColorBuffer, potential_Hx, potential_Hy);
// Create a buffer of potential field indeces
createPotential_IBO(&potentialFieldIndexBuffer, potential_Hx, potential_Hy);
}
glBindVertexArray(0);
{ // shaders for rendering bodies
bodiesVertexShader = glCreateShader(GL_VERTEX_SHADER);
bodiesFragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar* vShaderSource = loadFile("./shaders/nbody.vert.glsl");
const GLchar* fShaderSource = loadFile("./shaders/nbody.frag.glsl");
glShaderSource(bodiesVertexShader, 1, &vShaderSource, NULL);
glShaderSource(bodiesFragmentShader, 1, &fShaderSource, NULL);
delete [] vShaderSource;
delete [] fShaderSource;
glCompileShader(bodiesVertexShader);
glCompileShader(bodiesFragmentShader);
bodiesShaderProgram = glCreateProgram();
glAttachShader(bodiesShaderProgram, bodiesVertexShader);
glAttachShader(bodiesShaderProgram, bodiesFragmentShader);
glLinkProgram(bodiesShaderProgram);
}
if(potentialFieldRendering == true) { // shaders for rendering potential field
potentialVertexShader = glCreateShader(GL_VERTEX_SHADER);
potentialFragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar* vShaderSource = loadFile("./shaders/potential.vert.glsl");
const GLchar* fShaderSource = loadFile("./shaders/potential.frag.glsl");
glShaderSource(potentialVertexShader, 1, &vShaderSource, NULL);
glShaderSource(potentialFragmentShader, 1, &fShaderSource, NULL);
delete [] vShaderSource;
delete [] fShaderSource;
glCompileShader(potentialVertexShader);
glCompileShader(potentialFragmentShader);
potentialShaderProgram = glCreateProgram();
glAttachShader(potentialShaderProgram, potentialVertexShader);
glAttachShader(potentialShaderProgram, potentialFragmentShader);
glLinkProgram(potentialShaderProgram);
}
}
void AbstractSimulation::initNBodyPositions(int dimensions)
{
bodies = new glm::vec4[AbstractSimulation::N];
velocities = new glm::vec3[AbstractSimulation::N];
for(int i = 1; i < AbstractSimulation::N; ++i) {
glm::vec3 pos = glm::ballRand(1000.0f);
float mass;
if(potentialFieldRendering == true) {
mass = 1000 + fabs(glm::ballRand(1000.0f).x);
} else {
mass = 100 + fabs(glm::ballRand(100.0f).x);
}
if(dimensions == _2D_SIMULATION_) {
if(sqrtf(pos.y*pos.y+pos.z*pos.z) < 10) {
bodies[i] = glm::vec4(0.0f, 5*pos.y, 5*pos.z, mass);
velocities[i] = glm::vec3(0.0f, -pos.z, pos.y);
} else if(sqrtf(pos.y*pos.y+pos.z*pos.z) < 100) {
bodies[i] = glm::vec4(0.0f, 5*pos.y, 5*pos.z, mass);
velocities[i] = glm::vec3(0.0f, -pos.z, pos.y);
} else if(sqrtf(pos.y*pos.y+pos.z*pos.z) < 200) {
bodies[i] = glm::vec4(0.0f, 3.4*pos.y, 3.4*pos.z, mass);
velocities[i] = glm::vec3(0.0f, -pos.z, pos.y);
} else if(sqrtf(pos.y*pos.y+pos.z*pos.z) < 300) {
bodies[i] = glm::vec4(0.0f, 2.3*pos.y, 2.3*pos.z, mass);
velocities[i] = glm::vec3(0.0f, -pos.z/2, pos.y/2);
} else if(sqrtf(pos.y*pos.y+pos.z*pos.z) < 400) {
bodies[i] = glm::vec4(0.0f, 1.7*pos.y, 1.7*pos.z, mass);
velocities[i] = glm::vec3(0.0f, -pos.z/2.2, pos.y/2.2);
} else if(sqrtf(pos.y*pos.y+pos.z*pos.z) < 500) {
bodies[i] = glm::vec4(0.0f, 1.2*pos.y, 1.2*pos.z, mass);
velocities[i] = glm::vec3(0.0f, -pos.z/2.2, pos.y/3);
} else {
bodies[i] = glm::vec4(0.0f, pos.y, pos.z, mass);
velocities[i] = glm::vec3(0.0f, -pos.z/5, pos.y/5);
}
} else if(dimensions == _3D_SIMULATION_) {
glm::vec3 vel = glm::ballRand(50.0f);
bodies[i] = glm::vec4(pos.x, pos.y, pos.z, mass);
velocities[i] = glm::vec3(vel.x, vel.y, vel.z);
}
}
if(potentialFieldRendering == true) {
bodies[0] = glm::vec4(0.0f,0.0f,0.0f,400000.0f); velocities[0] = glm::vec3(0.0f,0.0f,0.0f);
} else {
bodies[0] = glm::vec4(0.0f,0.0f,0.0f,2000000.0f); velocities[0] = glm::vec3(0.0f,0.0f,0.0f);
}
#ifdef _DEBUG_THRUST_
for(int i = 0; i < AbstractSimulation::N; ++i)
printf("%f %f %f %f\n", bodies[i].x, bodies[i].y, bodies[i].z, bodies[i].w);
printf("\n");
#endif
}
void AbstractSimulation::createPotentialPosition_VBO(GLuint *id, int w, int h)
{
float radius = 1250.0f;
potentialFieldPositions = new glm::vec4[w * h];
glm::vec3* pos = new glm::vec3[w * h];
glGenBuffers(1, id);
glBindBuffer(GL_ARRAY_BUFFER, *id);
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
pos[y*w+x] = glm::vec3(-20.0f, -radius + 2*radius / w * x, -radius + 2*radius / h * y);
potentialFieldPositions[y*w+x] =
glm::vec4(0.0f, -radius + 2*radius / w * x, -radius + 2*radius / h * y, 1.0f);
}
}
glBufferData(GL_ARRAY_BUFFER, w * h * sizeof(glm::vec3), pos, GL_STATIC_DRAW);
if (!pos) {
printf("Error: createMeshPositionVBO\n");
return;
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
delete pos;
}
void AbstractSimulation::createPotentialColor_VBO(GLuint *id, int w, int h)
{
glGenBuffers(1, id);
glBindBuffer(GL_ARRAY_BUFFER, *id);
glBufferData(GL_ARRAY_BUFFER, w * h * sizeof(glm::vec3), 0, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void AbstractSimulation::createPotential_IBO(GLuint *id, int w, int h)
{
std::vector<GLuint> ids;
for (int y = 0; y < h - 1; ++y) {
for (int x = 0; x < w - 1; ++x) {
ids.push_back(y * w + x); ids.push_back(y * w + x+1); ids.push_back((y+1) * w + x);
ids.push_back(y * w + x+1); ids.push_back((y+1) * w + x); ids.push_back((y+1) * w + x+1);
}
}
// create index buffer
glGenBuffers(1, id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ids.size() * sizeof(GLuint), ids.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}