-
Notifications
You must be signed in to change notification settings - Fork 5
/
Neat.nt
290 lines (246 loc) · 6.53 KB
/
Neat.nt
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
module Neat;
import std.math, std.fun, std.lib.(opengl, glfw3);
import c.math;
alias
PRINT_FRAMES = true,
TITLE = "ParticleBench",
WIDTH = 800,
HEIGHT = 600,
MIN_X = -80,
MAX_X = 80,
MIN_Y = -90,
MAX_Y = 50,
MIN_DEPTH = 50,
MAX_DEPTH = 250,
START_RANGE = 15,
START_X = (MIN_X + (MIN_X + MAX_X) / 2),
START_Y = MAX_Y,
START_DEPTH = (MIN_DEPTH + (MIN_DEPTH + MAX_DEPTH) / 2),
POINTS_PER_SEC = 2000,
MAX_INIT_VEL = 7,
SPAWN_INTERVAL = 0.01,
MAX_LIFE = 5000,
MAX_SCALE = 4,
WIND_CHANGE = 2000,
MAX_WIND = 3,
RUNNING_TIME = ((MAX_LIFE / 1000) * 4);
alias ambient = vec4f(0.8, 0.05, 0.1, 1);
alias diffuse = vec4f(1, 1, 1, 1);
float[] lightPos;
void init() { lightPos = [float: MIN_X + (MAX_X - MIN_X) / 2, MAX_Y, MIN_DEPTH, 0].dup; }
GLuint gVBO = 0;
vec3d wind;
double
grav = 50,
initT = 0,
endT = 0,
gpuInitT = 0,
gpuEndT = 0,
frameDur = 0,
spwnTmr = 0,
cleanupTmr = 0,
runTmr = 0;
double[auto~] frames, gpuTimes;
struct Pt {
vec3d pos; alias implicit-cast = pos;
vec3d motion; double R, Life;
bool alive;
}
Pt[auto~] Pts;
int minPt = 0;
uint seed = 1234569;
(vec3f pos, vec3f normal)[auto~] Vertices;
bool loadCubeToGPU(){
alias vertices = [for i <- 0..8: ([-1,1][i&1 != 0], [-1,1][i&2 != 0], [-1,1][i&4 != 0])];
// imagine a cube like so
/* 6 7
2 3
4 5
0 1
*/
alias faces = [
(( 0, 0, 1), (4, 5, 7, 6)),
(( 0, 0,-1), (0, 2, 3, 1)),
(( 0, 1, 0), (2, 6, 7, 3)),
(( 0,-1, 0), (0, 1, 5, 4)),
(( 1, 0, 0), (1, 3, 7, 5)),
((-1, 0, 0), (0, 4, 6, 2))];
for auto (normal, face) <- faces {
static for int i <- 0..4 {
Vertices ~= (vec3f vertices[face[i]], vec3f normal);
}
}
using mode GL {
GenBuffers( 1, &gVBO);
ARRAY_BUFFER.BindBuffer gVBO;
ARRAY_BUFFER.BufferData((ubyte[]: Vertices[]).(length, ptr), STATIC_DRAW);
EnableClientState VERTEX_ARRAY;
EnableClientState NORMAL_ARRAY;
auto size = size-of type-of Vertices[0];
VertexPointer (3, FLOAT, size, null);
NormalPointer (FLOAT, size, void*: 16);
MatrixMode MODELVIEW;
}
return true;
}
uint xorRand() {
seed xor= seed << 13;
seed xor= seed >> 17;
seed xor= seed << 5;
return seed;
}
void movPts(double secs) {
for ref pt <- Pts[minPt .. $] using pt if alive {
pos += motion * secs;
motion += wind * 1 / R;
motion.y -= grav * secs;
Life -= secs;
if (Life <= 0) alive = false;
}
}
void spwnPts(double secs) {
uint num = int:(secs * POINTS_PER_SEC);
for auto i <- 0..num {
using Pt pt {
pos = vec3d(
0 + double:(xorRand() % START_RANGE) - START_RANGE / 2,
START_Y,
START_DEPTH + double:(xorRand() % START_RANGE) - START_RANGE / 2
);
motion = vec3d((xorRand() % MAX_INIT_VEL) x 3);
R = double:(xorRand() % (MAX_SCALE * 100)) / 200;
Life = double:(xorRand() % MAX_LIFE) / 1000;
alive = true;
Pts ~= that;
}
}
}
void doWind() {
alias windComponent = (double:(xorRand() % WIND_CHANGE)/WIND_CHANGE - WIND_CHANGE / 2000) * frameDur;
wind += vec3d(windComponent x 3);
for ref d <- wind {
if (abs d > MAX_WIND) d *= -0.5;
}
}
void checkColls() {
alias
MIN = (MIN_X, MIN_Y, MIN_DEPTH),
MAX = (MAX_X, MAX_Y, MAX_DEPTH);
for ref pt <- Pts[minPt .. $] using pt if alive {
static for int i <- 0..3 {
ref d = pos[i], v = motion[i];
alias min = MIN[i], max = MAX[i];
if (d < min) {
d = min + R;
v *= -1.1; // These particles are magic; they accelerate by 10% at every bounce off the bounding box
}
if (d > max) {
d = max - R;
v *= -1.1;
}
}
}
}
void cleanupPtPool() {
// ignore all dead points at the start
while (minPt < Pts.length && !Pts[minPt].alive)
minPt ++;
}
void main() using mode GLFW using mode GL {
Init();
WindowHint(SAMPLES, 2);
WindowHint(CONTEXT_VERSION_MAJOR, 2);
WindowHint(CONTEXT_VERSION_MINOR, 1);
auto window = CreateWindow(WIDTH, HEIGHT, TITLE, null, null);
MakeContextCurrent(window);
SwapInterval(0);
initScene();
loadCubeToGPU();
while (!WindowShouldClose(window)){
initT = GetTime();
if (spwnTmr >= SPAWN_INTERVAL) {
spwnPts(SPAWN_INTERVAL);
spwnTmr -= SPAWN_INTERVAL;
}
if (cleanupTmr >= double:(MAX_LIFE) / 1000) {
cleanupPtPool();
cleanupTmr = 0;
}
doWind();
checkColls();
movPts(frameDur);
Clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT);
gpuInitT = GetTime();
renderPts();
SwapBuffers(window);
gpuEndT = GetTime();
PollEvents();
endT = GetTime();
frameDur = endT-initT;
spwnTmr += frameDur;
cleanupTmr += frameDur;
runTmr += frameDur;
if (runTmr > MAX_LIFE / 1000) {
frames ~ = frameDur;
gpuTimes ~= gpuEndT - gpuInitT;
}
if (runTmr >= RUNNING_TIME) {
template sum(T) { double sum(T t) {
double s = 0;
for auto v <- t s += v;
return s;
}}
double frameTimeMean = sum frames / frames.length;
printf("Average framerate was: %f frames per second.\n", 1 / frameTimeMean);
double gpuTimeMean = sum gpuTimes / gpuTimes.length;
printf("Average cpu time was- %f seconds per frame.\n", frameTimeMean - gpuTimeMean);
double sumDiffs = sum for x in frames map ((1 / x)-(1 / frameTimeMean))^2;
double variance = sumDiffs / frames.length;
double sd = sqrt variance;
printf("The standard deviation was: %f frames per second.\n", sd);
if (PRINT_FRAMES == 1){
printf("--:");
for auto frame <- frames {
printf("%f",1 / frame);
printf(",");
}
printf(".--");
}
break;
}
}
DestroyWindow(window);
Terminate();
}
void initScene() using mode GL {
Enable DEPTH_TEST;
Enable LIGHTING;
ClearColor (0.1, 0.1, 0.6, 1.0);
ClearDepth 1;
DepthFunc LEQUAL;
using LIGHT0 {
// cast to float* (take address of (declare anonymous variable initialized with (ambient)))
Lightfv (AMBIENT, float*: & auto=ambient);
Lightfv (DIFFUSE, float*: & auto=diffuse);
Lightfv (POSITION, lightPos.ptr);
Enable;
}
Viewport (0, 0, WIDTH, HEIGHT);
MatrixMode PROJECTION;
LoadIdentity;
Frustum (-1, 1, -1, 1, 1.0, 1000.0);
Rotatef (20, 1, 0, 0);
MatrixMode MODELVIEW;
LoadIdentity;
PushMatrix;
return;
}
void renderPts() using mode GL {
for ref pt <- Pts if pt.alive {
PopMatrix;
PushMatrix;
Translated pt.pos.(x, y, -z);
Scaled pt.(R * 2, R * 2, R * 2);
DrawArrays (QUADS, 0, 24);
}
}