-
Notifications
You must be signed in to change notification settings - Fork 0
/
colored-cube.js
98 lines (80 loc) · 3.35 KB
/
colored-cube.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
const glmatrix = require('gl-matrix')
const vec3 = glmatrix.vec3
const mat4 = glmatrix.mat4
const geometry = require('./geometry')
const glutils = require('./glutils')
function ColoredCube(camera, environment, size, position) {
const cube = geometry.createCube(size)
const r = [1.0, 0.0, 0.0, 1.0],
g = [0.0, 1.0, 0.0, 1.0],
b = [0.0, 0.0, 1.0, 1.0],
w = [1.0, 1.0, 1.0, 1.0]
const colors = [].concat(r, g, b, w, b, g,
b, w, g, r, g, w,
g, r, w, b, w, r,
w, b, r, g, r, b,
g, b, w, r, w, b,
w, b, g, b, w, r)
let positionBuffer = null
let colorBuffer = null
let normalsBuffer = null
let program = null
let attributes = null
let xRot = Math.PI
let yRot = Math.PI
this.initialize = (context, content) => {
program = content.programs['colored-cube']
positionBuffer = glutils.createArrayBuffer(context,
program,
new Float32Array(cube.vertices),
(context, program) => context.getAttribLocation(program, 'a_position'),
3)
colorBuffer = glutils.createArrayBuffer(context,
program,
new Float32Array(colors),
(context, program) => context.getAttribLocation(program, 'a_color'),
4)
normalsBuffer = glutils.createArrayBuffer(context,
program,
new Float32Array(cube.normals),
(context, program) => context.getAttribLocation(program, 'a_normal'),
3)
attributes = {
'u_world': context.getUniformLocation(program, 'u_world'),
'u_worldInverseTranspose': context.getUniformLocation(program, 'u_worldInverseTranspose'),
'u_worldViewProjection': context.getUniformLocation(program, 'u_worldViewProjection'),
'u_lightWorldPosition': context.getUniformLocation(program, 'u_lightWorldPosition'),
}
}
this.update = (context, time) => {
context.useProgram(program)
xRot += time.delta
yRot += time.delta/3
const rx = mat4.create()
mat4.fromXRotation(rx, xRot)
const ry = mat4.create()
mat4.fromYRotation(ry, yRot)
const rxry = mat4.create()
mat4.multiply(rxry, rx, ry)
const translation = mat4.create()
mat4.translate(translation, translation, position)
const world = mat4.create()
mat4.multiply(world, translation, rxry)
const worldInverse = mat4.invert(mat4.create(), world)
const worldInverseTranspose = mat4.transpose(mat4.create(), worldInverse);
context.uniformMatrix4fv(attributes['u_world'], false, world)
context.uniformMatrix4fv(attributes['u_worldInverseTranspose'], false, world)
context.uniformMatrix4fv(attributes['u_worldViewProjection'], false, camera.getWorldViewProjection(context, world))
context.uniform3fv(attributes['u_lightWorldPosition'], environment.getLightPosition())
}
this.draw = (context, time) => {
context.useProgram(program)
positionBuffer.bind(context)
colorBuffer.bind(context)
normalsBuffer.bind(context)
context.drawArrays(context.TRIANGLES,
0,
cube.vertices.length/3)
}
}
module.exports = ColoredCube