forked from stackgl/glsl-lighting-walkthrough
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (51 loc) · 1.47 KB
/
app.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
var context = require('webgl-context')
var loop = require('canvas-loop')
var assign = require('object-assign')
var createCamera = require('perspective-camera')
var createScene = require('./scene')
module.exports = function (images) {
// get a retina-scaled WebGL canvas
var gl = context()
var canvas = gl.canvas
var app = loop(canvas, {
scale: window.devicePixelRatio
}).on('tick', render)
// create a simple perspective camera
// contains our projection & view matrices
var camera = createCamera({
fov: Math.PI / 4,
near: 0.01,
far: 100
})
// create our custom scene
var drawScene = createScene(gl, images)
var time = 0
app.start()
return assign(app, {
canvas,
gl
})
function render (dt) {
// our screen-space viewport
var [ width, height ] = app.shape
time += dt / 1000
// set WebGL viewport to device size
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight)
gl.enable(gl.DEPTH_TEST)
gl.enable(gl.CULL_FACE)
gl.clearColor(0.04, 0.04, 0.04, 1)
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
// rotate the camera around origin
var rotation = Math.PI / 4 + time * 0.2
var radius = 4
var x = Math.cos(rotation) * radius
var z = Math.sin(rotation) * radius
camera.identity()
camera.translate([ x, 0, z ])
camera.lookAt([ 0, 0, 0 ])
camera.viewport = [ 0, 0, width, height ]
camera.update()
// draw our scene
drawScene(time, camera)
}
}