/
smtest.nt
349 lines (326 loc) · 10.2 KB
/
smtest.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
module smtest;
import glsetup, opengl, libgd, simplex, std.file, std.math, std.time, std.thread, sdl;
int fps;
long last_time;
float calcsum = 0;
float lightsum = 0;
GLuint tex1, tex2;
struct Block {
bool active;
int lightlevel;
}
alias BlockType = Block;
alias SectorSize = 16;
class Sector {
vec3i base;
bool contains(vec3i v) {
v -= base;
return eval v.(
x >= 0 && y >= 0 && z >= 0 &&
x < SectorSize && y < SectorSize && z < SectorSize
);
}
BlockType x SectorSize x SectorSize x SectorSize cache;
void init() { }
void init(Sector s) { base = s.base; cache = s.cache; }
}
class SectorCache {
Sector[auto~] sectors, backup;
bool backupIsInited;
Sector[] getBackup(bool initIt = false) {
if backup.length != sectors.length {
backup.free;
backup = new Sector[] (sectors.length);
backupIsInited = false;
}
if (initIt || !backupIsInited) {
for int i <- 0..sectors.length {
if (!backup[i]) backup[i] = new Sector;
backup[i].init sectors[i];
}
backupIsInited = true;
}
return backup[];
}
void swapBackup() { (sectors, backup) = (backup, sectors); }
BlockType delegate(vec3i) dg;
Mutex m;
void init(type-of dg dg2) {
dg = dg2;
m = new Mutex;
}
Sector calcSector(vec3i base) {
auto sec = new Sector;
sec.base = base;
writeln "calc $(base)";
int i;
for (int x, int y, int z) <- cross((0..SectorSize) x 3) {
sec.cache[x][y][z] = dg(base + vec3i(x, y, z));
i++;
}
writeln "done $i";
return sec;
}
int lastMatched;
BlockType lookup(vec3i v) {
alias last = sectors[lastMatched];
if sectors.length && last.contains v {
auto sec = last;
return (v - sec.base).(sec.cache[x][y][z]);
}
for (int id, Sector sec) <- zip(ints, sectors) {
if (sec.contains v) {
lastMatched = id;
return (v - sec.base).(sec.cache[x][y][z]);
}
}
vec3i low = (v & 0x7fff_ffff) % SectorSize;
auto sec = calcSector (v - low);
sectors ~= sec;
return low.(sec.cache[x][y][z]);
}
// does not cause calcs
BlockType weakLookup(vec3i v, BlockType deflt, bool inBackup = false) {
auto list = sectors;
if (inBackup) list = backup;
if !list.length return deflt;
if lastMatched < list.length && (auto sec = list[lastMatched]).contains v {
return (v - sec.base).(sec.cache[x][y][z]);
}
for (int id, Sector sec) <- zip(ints, list) if id != lastMatched && sec.contains v {
lastMatched = id;
return (v - sec.base).(sec.cache[x][y][z]);
}
return deflt;
}
}
void initLight(SectorCache sc) {
Sector[] list;
using LockedMutex:sc.m list = sc.getBackup(initIt => true);
Sector[auto~] tops;
onSuccess tops.free;
for (auto sector <- list) {
bool replaced;
for (int i <- 0..tops.length) {
if (!replaced
&& tops[i].base.xz == sector.base.xz
&& tops[i].base.y < sector.base.y) { // found a higher one
tops[i] = sector;
replaced = true;
}
}
if (!replaced) tops ~= sector;
}
for (auto sector <- tops) { // fill top with light
for (int x, int z) <- cross(0..SectorSize, 0..SectorSize) {
sector.cache[x][SectorSize - 1][z].lightlevel = 128;
}
}
}
void stepLight(SectorCache sc) {
BlockType nothing;
Sector[] list;
using LockedMutex:sc.m list = sc.getBackup();
for (auto sector <- list) {
for auto vec <- [for tup <- cross((0..SectorSize) x 3): vec3i(tup)] {
alias myBlock = vec.(sector.cache[x][y][z]);
if !myBlock.active {
int sum;
auto mybase = sector.base, fullvec = mybase + vec;
for auto vec2 <- [for tup <- cross([-1, 0, 1], [1], [-1, 0, 1]): vec3i(tup)] {
auto nuvec = fullvec + vec2;
BlockType bt = void;
if sector.contains nuvec {
bt = (vec + vec2).(sector.cache[x][y][z]);
} else bt = sc.weakLookup(nuvec, nothing, inBackup => true);
if bt.active bt.lightlevel = 0; // shadows
sum += bt.lightlevel;
}
sum /= 9;
myBlock.lightlevel = sum;
}
}
}
using LockedMutex:sc.m sc.swapBackup;
}
import camera;
Camera cam;
void drawScene(SectorCache* scp) {
alias sc = *scp;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity;
cam.gl-setup();
BlockType mkBlock(bool b) {
BlockType res; res.active = b; return res;
}
BlockType genFun(vec3i vi) {
float max(float a, float b) { if (a > b) return a; else return b; }
float abs(float f) { if (f < 0) return -f; return f; }
// int sum = vi.((eval abs x < 2) + (eval abs y < 2) + (eval abs z < 2));
// return mkBlock eval sum > 1; // axis cross test shape
auto dist = max(max(abs(vi.x), abs(vi.y)), abs(vi.z));
dist -= noise3(vi * 0.1) * 5;
if dist > 7 return mkBlock(false);
// if dist < 4 return false;
return mkBlock(true);
}
if (!sc) sc = new SectorCache null;
sc.m.lock;
onExit sc.m.unlock;
sc.dg = &genFun;
auto fun = &sc.lookup;
bool occluded(vec3i vi) {
auto fun = fun;
if (!fun(vi).active) return false;
return eval
fun vec3i(vi.(x+1, y, z)).active && fun vec3i(vi.(x-1, y, z)).active &&
fun vec3i(vi.(x, y+1, z)).active && fun vec3i(vi.(x, y-1, z)).active &&
fun vec3i(vi.(x, y, z+1)).active && fun vec3i(vi.(x, y, z-1)).active;
}
vec3f x 4 [auto~] x 2 colorCache;
vec3f x 4 [auto~] x 2 vertexCache;
vec2f x 4 [auto~] x 2 texcoordCache;
void flushCache(int tex = -1) {
int from, to;
if (tex == -1) (from, to) = (0, 2);
else (from, to) = (tex, tex + 1);
for (int tid <- from .. to) {
if (tid == 0) glBindTexture (GL_TEXTURE_2D, tex1);
else glBindTexture (GL_TEXTURE_2D, tex2);
glEnableClientState GL_VERTEX_ARRAY;
glEnableClientState GL_COLOR_ARRAY;
glEnableClientState GL_TEXTURE_COORD_ARRAY;
glColorPointer(3, GL_FLOAT, size-of vec3f, void*:colorCache[tid].ptr);
glVertexPointer(3, GL_FLOAT, size-of vec3f, void*:vertexCache[tid].ptr);
glTexCoordPointer(2, GL_FLOAT, size-of vec2f, void*:texcoordCache[tid].ptr);
glDrawArrays(GL_QUADS, 0, 4 * vertexCache[tid].length);
glDisableClientState GL_VERTEX_ARRAY;
glDisableClientState GL_COLOR_ARRAY;
glDisableClientState GL_TEXTURE_COORD_ARRAY;
colorCache[tid].free;
vertexCache[tid].free;
texcoordCache[tid].free;
}
}
void queueQuad(int tex, vec3f color, vec3f x 4 corners) {
colorCache[tex] ~= [color, color, color, color];
vertexCache[tex] ~= corners;
texcoordCache[tex] ~= [vec2f(0, 0), vec2f(0, 1), vec2f(1, 1), vec2f(1, 0)];
if (vertexCache[tex].length == 4096) flushCache(tex);
}
auto start = sec();
while auto vec <- [for x <- cross (-12 .. 12) x 3: vec3i(x)] if fun(vec).active && !occluded(vec) {
int tex;
if (noise3(vec3f(vec.(x, y, z)) * 0.3) > 0.5) tex = 0;
else tex = 1;
alias corners = [for tup <- cross([0, 1], [0, 1], [0, 1]): vec3i(tup)];
auto sides = [
([0, 1, 3, 2], -vec3i.X),
([4, 6, 7, 5], vec3i.X),
([1, 5, 7, 3], vec3i.Z),
([0, 2, 6, 4], -vec3i.Z),
([2, 3, 7, 6], vec3i.Y),
([1, 0, 4, 5], -vec3i.Y)
];
for (int x 4 points, vec3i dir) <- sides {
auto bt = fun(vec + dir);
alias map = [for point <- points: vec3f(corners[point]) + vec];
if !bt.active
queueQuad (
tex,
vec3f(bt.lightlevel / 64f),
[map[0], map[1], map[2], map[3]]
);
}
}
flushCache();
swap();
calcsum += float:(sec() - start);
fps ++;
auto ct = µsec();
if ct > last_time + 1_000_000 {
last_time = ct;
writeln "FPS: $fps, $(calcsum)s for calc, $(lightsum)s for light";
fps = 0;
calcsum = 0;
lightsum = 0;
}
}
int loadTexture(string pngdata) {
GLuint tex;
auto img = gdImageCreateFromPngPtr (pngdata.length, pngdata.ptr);
writeln "Read $(pngdata.length), is $((img.sx, img.sy)). Truecolor? $(img.trueColor). ";
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
int[auto~] data;
while auto pt <- cross img.(0..sy, 0..sx) {
data ~= gdImageGetPixel (img, pt);
}
// glTexImage2D(GL_TEXTURE_2D, 0, 3, img.sx, img.sy, 0, GL_RGBA, GL_UNSIGNED_BYTE, img.tpixels);
gluBuild2DMipmaps (GL_TEXTURE_2D, 3, img.sx, img.sy, GL_RGBA, GL_UNSIGNED_BYTE, data.ptr);
data.free;
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
return tex;
}
int main(int argc, char** argv) {
auto ec = new EgoCam!PerspectiveCam (vec3f(0, 0, -32), 0, 0);
ec.fov = 60f;
cam = ec;
gl-context-callbacks ~= delegate void() {
writeln "load textures";
tex1 = loadTexture import("letter-a.png");
tex2 = loadTexture import("letter-b.png");
// tex1 = loadTexture import("smooth-rock-tex0-32.png");
// tex2 = loadTexture import("vivid-rock-tex-32.png");
writeln "loaded";
};
auto surf = setup-gl();
gl-context-callbacks ~= delegate void() {
ec.aspect = surf.w * 1f / surf.h;
};
bool active;
void toggleActive() {
SDL_ShowCursor active;
if !active
// SDL_WM_GrabInput(SDL_GRAB_ON);
SDL_WarpMouse(320, 240);
active = eval !active;
}
auto lastTime = sec();
auto tp = new ThreadPool 1;
SectorCache sc;
drawScene ≻ // init sector cache
tp.addTask delegate void() {
while true {
initLight sc;
stepLight sc;
}
};
while true {
drawScene ≻
/*auto start = sec();
initLight sc;
stepLight sc;
lightsum += float:(sec() - start);*/
if update(surf) quit(0);
// copied from pyramid.cr
if (active) {
auto idelta = mousepos - vec2i(320, 240);
auto delta = vec2f((0.001 * idelta).(x, y));
using (ec, delta) { turn-left-x; turn-up-y; } // *MWAHAHAHAHAAAAAHAHA*
if idelta.x || idelta.y
SDL_WarpMouse(320, 240);
}
if (mouseClicked) toggleActive;
auto factor = float:(sec() - lastTime);
auto wsfactor = factor * 16, adfactor = factor * 64;
lastTime = sec();
using ec {
if (keyPressed[SDLK_w]) pos += dir.normalize3f() * wsfactor;
if (keyPressed[SDLK_s]) pos -= dir.normalize3f() * wsfactor;
if (keyPressed[SDLK_a]) pos += left.normalize3f() * adfactor;
if (keyPressed[SDLK_d]) pos -= left.normalize3f() * adfactor;
}
}
}