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
|
define.class("../webgl/devicewebgl", function(require, exports, baseclass){
this.Keyboard = require('./keyboardnodegl')
this.Pointer = require('./pointernodegl')
this.Shader = require('./shadernodegl')
this.Texture = require('./texturenodegl')
this.DrawPass = require('./drawpassnodegl')
var WebGL = require('node-webgl/lib/webgl')
var Image = this.Image = require('node-webgl/lib/image')
var GLFW = require('node-glfw')
this.atConstructor = function(){
this.window =
this.document = this
baseclass.atConstructor.call(this)
}
this.addEventListener = function(name, callback){
if(!callback) throw new Error('invalid listener')
this['on' + name] = callback
}
this.removeEventListener = function (name, callback) {
if (callback && typeof(callback) === "function") {
this['on' + name] = undefined
}
}
this.requestAnimationFrame = function (callback, delay) {
this.req_anim_frame = callback
}
this.Texture.Image = Image
this.createContext = function(){
var width = 800, height = 600
if (process.platform !== 'win32') process.on('SIGINT', function () {
process.exit(0)
})
GLFW.Init()
GLFW.events.on('mousemove', function(event){
event.pageX = event.x, event.pageY = event.y
if(this.onmousemove) this.onmousemove(event)
}.bind(this))
GLFW.events.on('mousedown', function(event){
event.pageX = event.x, event.pageY = event.y
if(this.onmousedown) this.onmousedown(event)
}.bind(this))
GLFW.events.on('mouseup', function(event){
event.pageX = event.x, event.pageY = event.y
if(this.onmouseup) this.onmouseup(event)
}.bind(this))
GLFW.events.on('keydown', function(event){
if(this.onkeydown) this.onkeydown(event)
}.bind(this))
GLFW.events.on('keyup', function(event){
if(this.onkeyup) this.onkeyup(event)
}.bind(this))
GLFW.events.on('quit', function () {
process.exit(0)
})
GLFW.events.on("keydown", function (evt) {
if (evt.keyCode === 'C'.charCodeAt(0) && evt.ctrlKey) { process.exit(0); }
if (evt.keyCode === 27) process.exit(0);
});
GLFW.DefaultWindowHints()
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)
if (!(this.glfwindow = GLFW.CreateWindow(width, height))) {
GLFW.Terminate()
throw "Can't initialize GL surface"
}
GLFW.MakeContextCurrent(this.glfwindow)
GLFW.SetWindowTitle(this.glfwindow,"WebGL")
WebGL.Init()
this.gl = WebGL
this.clear(0,0,0,1.)
GLFW.SwapBuffers(this.glfwindow)
GLFW.SwapInterval(1)
GLFW.events.addListener("framebuffer_resize", function(){
this.doSize()
}.bind(this))
this.getExtension('OES_standard_derivatives')
this.doSize()
var last_anim, last_time
this.redrawCall = function(){
if(last_anim) GLFW.SwapBuffers(this.glfwindow)
GLFW.PollEvents()
var anim_frame = last_anim = this.req_anim_frame
if(anim_frame){
this.req_anim_frame = undefined
var time = GLFW.GetTime()
last_time = time
anim_frame(time*1000)
}
}
setInterval(function(){
this.redrawCall()
}.bind(this), 0)
}
this.doSize = function(width, height){
var sizeWin = GLFW.GetWindowSize(this.glfwindow)
var sizeFB = GLFW.GetFramebufferSize(this.glfwindow)
this.ratio = sizeFB.width / sizeWin.width
this.width = sizeWin.width
this.height = sizeWin.height
this.gl.viewport(0, 0, sizeFB.width, sizeFB.height)
this.main_frame.ratio = this.ratio
this.size = vec2(sizeWin.width, sizeWin.height)
this.main_frame.size = vec2(sizeFB.width, sizeFB.height)
this.atResize ()
}
this.initResize = function(){
}
})
|