-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglfw-window.js
More file actions
165 lines (140 loc) · 3.61 KB
/
glfw-window.js
File metadata and controls
165 lines (140 loc) · 3.61 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
// Copyright (c) 2017 Intel Corporation. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license
// that can be found in the LICENSE file.
'use strict';
const glfw = require('node-glfw-3');
const rs2 = require('node-librealsense');
const now = require('performance-now');
class FPSCounter {
// fps is reported to console for each interval frames
constructor(interval = 10) {
this.cnt = 0;
this.interval = interval;
this.timestamps = [];
}
count() {
this.timestamps[this.cnt] = now();
this.cnt++;
if (this.cnt === this.interval) {
this._report();
this.reset();
}
}
reset() {
this.cnt = 0;
}
_report() {
let duration = this.timestamps[this.cnt - 1] - this.timestamps[0];
let fps = Math.round((this.cnt - 1) / (duration / 1000));
console.log('FPS: ', fps);
}
}
/* eslint-disable new-cap */
class GLFWWindow {
// TODO(kenny-y): Check disabled jshint errors
constructor(width = 1280, height = 720, title = 'GLFW Window') {
this.width_ = width;
this.height_ = height;
glfw.Init();
glfw.DefaultWindowHints();
this.window_ = glfw.CreateGLFWWindow(width, height, title);
glfw.MakeContextCurrent(this.window_);
// Enable vertical sync (on cards that support it)
glfw.SwapInterval(1); // 0 for vsync off
this.enableFps = false;
this.fpsCounter = new FPSCounter();
}
// Call this method to show or not to show fps to console
showFPS(showOrNot) {
this.enableFps = showOrNot;
this.fpsCounter.reset();
}
get width() {
return this.width_;
}
get height() {
return this.height_;
}
get window() {
return this.window_;
}
shouldWindowClose() {
let result = glfw.WindowShouldClose(this.window_);
if (! result) {
result = glfw.GetKey(this.window_, glfw.KEY_ESCAPE);
}
return result;
}
beginPaint() {
// We don't clear buffer for now, because
// there is currently sync issue between depth & color stream
//
// glfw.ClearColorBuffer();
}
endPaint() {
glfw.PopMatrix();
glfw.SwapBuffers(this.window);
glfw.PollEvents();
glfw.PushMatrix();
glfw.Ortho(0, this.width_, this.height_, 0, -1, +1);
if (this.enableFps) {
this.fpsCounter.count();
}
}
destroy() {
glfw.DestroyWindow(this.window_);
glfw.Terminate();
this.window_ = undefined;
}
setKeyCallback(callback) {
glfw.setKeyCallback(this.window_, function(key, scancode, action, modes) {
callback(key, scancode, action, modes);
});
}
}
class Rect {
constructor(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
adjustRatio(size) {
let newH = this.h;
let newW = this.h * size.x / size.y;
if (newW > this.w) {
let scale = this.w / newW;
newW *= scale;
newH *= scale;
}
return new Rect(this.x + (this.w-newW) / 2, this.y + (this.h - newH) / 2, newW, newH);
}
}
class Texture {
constructor() {
this.glHandle = 0;
}
render(videoFrame, rect) {
this.upload(videoFrame);
this.show(rect);
}
upload(videoFrame) {
if (!this.glHandle) {
this.glHandle = glfw.genTexture();
}
glfw.uploadAsTexture(
this.glHandle,
new Uint8Array(videoFrame.getData().buffer),
videoFrame.width,
videoFrame.height,
rs2.format.formatToString(videoFrame.format));
this.stream = videoFrame.stream;
}
show(rect) {
glfw.showInRect(this.glHandle, rect.x, rect.y, rect.w, rect.h);
}
}
module.exports.GLFWWindow = GLFWWindow;
module.exports.glfw = glfw;
module.exports.Rect = Rect;
module.exports.Texture = Texture;