-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexample-fourWindows.js
114 lines (90 loc) · 2.99 KB
/
example-fourWindows.js
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
const glfw = require("./glfw3.js")
const { vec2, vec3, vec4, quat, mat2, mat2d, mat3, mat4} = require("gl-matrix")
const gl = require('./gles3.js')
const glutils = require('./glutils.js');
if (!glfw.init()) {
console.log("Failed to initialize GLFW");
process.exit(-1);
}
let version = glfw.getVersion();
console.log('glfw ' + version.major + '.' + version.minor + '.' + version.rev);
console.log('glfw version-string: ' + glfw.getVersionString());
let monitors = glfw.getMonitors()
const requestAnimationFrame = function(callback, delay=1000/60) {
let t0 = process.hrtime();
let timer = ()=>{
let dt = process.hrtime(t0)
// dt (in ms):
let ms = (dt[0]*1e9 + dt[1]) * 1e-6;
if (ms > delay) {
callback();
} else {
setImmediate(timer);
}
}
timer();
}
function createWindow(title="", width=640, height=480, x=30, y=30) {
// Open OpenGL window
// Open OpenGL window
glfw.defaultWindowHints();
glfw.windowHint(glfw.CONTEXT_VERSION_MAJOR, 3);
glfw.windowHint(glfw.CONTEXT_VERSION_MINOR, 3);
glfw.windowHint(glfw.OPENGL_FORWARD_COMPAT, 1);
glfw.windowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE);
glfw.windowHint(glfw.RESIZABLE, 1);
glfw.windowHint(glfw.VISIBLE, 1);
glfw.windowHint(glfw.DECORATED, 1);
glfw.windowHint(glfw.RED_BITS, 8);
glfw.windowHint(glfw.GREEN_BITS, 8);
glfw.windowHint(glfw.BLUE_BITS, 8);
glfw.windowHint(glfw.DEPTH_BITS, 24);
glfw.windowHint(glfw.REFRESH_RATE, 0);
let window=glfw.createWindow(width, height, title); //, monitors.length-1);
if (!window) {
console.log("Failed to open glfw window");
glfw.terminate();
process.exit(-1);
}
glfw.makeContextCurrent(window);
console.log(gl.glewInit());
glfw.setWindowTitle(window, title);
glfw.setWindowPos(window, x, y);
glfw.swapInterval(0); // Disable VSync (we want to get as high FPS as possible!)
//can only be called after window creation!
console.log('GL ' + glfw.getWindowAttrib(window, glfw.CONTEXT_VERSION_MAJOR) + '.' + glfw.getWindowAttrib(window, glfw.CONTEXT_VERSION_MINOR) + '.' + glfw.getWindowAttrib(window, glfw.CONTEXT_REVISION) + " Profile: " + glfw.getWindowAttrib(window, glfw.OPENGL_PROFILE));
return window;
}
let windows = [
createWindow('win0', 720, 480, 30, 30),
createWindow('win1', 720, 480, 750, 30),
createWindow('win2', 720, 480, 30, 550),
createWindow('win3', 720, 480, 750, 550),
];
let t0 = process.hrtime();
let framecount = 0;
function update() {
glfw.pollEvents();
for (let win of windows) {
if (glfw.windowShouldClose(win)) {
return;
}
}
let dt = process.hrtime(t0);
framecount++;
let elapsed_ms = (dt[0]*1e9 + dt[1]) * 1e-6;
let fps = framecount*1000/(elapsed_ms);
if (Math.random() < 0.01) console.log("fps", fps);
requestAnimationFrame(update, 1);
for (let i in windows) {
let win = windows[i]
let f = i/(windows.length-1)
glfw.makeContextCurrent(win);
//let wsize = glfw.GetFramebufferSize(win);
gl.clearColor(f, 1-f, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Swap buffers
glfw.swapBuffers(win);
}
}
update();