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
/* DreemGL is a collaboration between Teeming Society & Samsung Electronics, sponsored by Samsung and others.
   Copyright 2015-2016 Teeming Society. Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except in compliance with the License.
   You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and limitations under the License.*/

define.class("../webgl/devicewebgl", function(require, exports, baseclass){

        this.Keyboard = require('./keyboardnodegl')
        this.Pointer = require('./pointernodegl')

        // require embedded classes
        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)
        }

        // make a fake window/document interface
        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

        // create nodegl context
        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); }// Control+C
                        if (evt.keyCode === 27) process.exit(0);  // ESC
                });

                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")

                // make sure GLEW is initialized
                WebGL.Init()
                this.gl = WebGL

                this.clear(0,0,0,1.)
                  GLFW.SwapBuffers(this.glfwindow)

                GLFW.SwapInterval(1)

                //for (var l = 0, ln = resizeListeners.length; l < ln; ++l)
                GLFW.events.addListener("framebuffer_resize", function(){
                        this.doSize()
                        //this.redrawCall()
                }.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()
                        // renderloop                
                        var anim_frame = last_anim = this.req_anim_frame
                        if(anim_frame){
                                this.req_anim_frame = undefined
                                var time = GLFW.GetTime()
                                //console.log((last_time - time)*1000)
                                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
                //var sw = width
                //var sh = height
                this.gl.viewport(0, 0, sizeFB.width, sizeFB.height)
                // store our w/h and pixelratio on our frame
                this.main_frame.ratio = this.ratio
                this.size = vec2(sizeWin.width, sizeWin.height)
                this.main_frame.size = vec2(sizeFB.width, sizeFB.height) // actual size
                //this.ratio = this.main_frame.ratio
                this.atResize ()
        }

        this.initResize = function(){
        }

})