-
Notifications
You must be signed in to change notification settings - Fork 0
/
textured-cube.js
108 lines (88 loc) · 3.73 KB
/
textured-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
99
100
101
102
103
104
105
106
107
108
const glmatrix = require('gl-matrix')
const vec3 = glmatrix.vec3
const mat4 = glmatrix.mat4
const geometry = require('./geometry')
const glutils = require('./glutils')
function TexturedCube(camera, environment, size, position, assetName) {
const cube = geometry.createCube(size)
let positionBuffer = null
let textureBuffer = null
let normalsBuffer = null
let program = null
let attributes = null
let xRot = Math.PI
let yRot = Math.PI
let texture = null
this.initialize = (context, content) => {
program = content.programs['textured-cube']
positionBuffer = glutils.createArrayBuffer(context,
program,
new Float32Array(cube.vertices),
(context, program) => context.getAttribLocation(program, 'a_position'),
3)
textureBuffer = glutils.createArrayBuffer(context,
program,
new Float32Array(cube.textureCoords),
(context, program) => context.getAttribLocation(program, 'a_texcoord'),
2)
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'),
'u_texture': context.getUniformLocation(program, 'u_texture')
}
texture = createAndBindTexture(context, content, assetName)
}
const createAndBindTexture = (context, content, assetName) => {
const texture = context.createTexture()
context.bindTexture(context.TEXTURE_2D, texture)
context.texImage2D(context.TEXTURE_2D,
0,
context.RGBA,
context.RGBA,
context.UNSIGNED_BYTE,
content.resources[assetName].content)
context.generateMipmap(context.TEXTURE_2D)
return texture
}
this.update = (context, time) => {
context.useProgram(program)
xRot += time.delta/2
yRot += time.delta/6
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)
textureBuffer.bind(context)
normalsBuffer.bind(context)
context.uniform1i(attributes['u_texture'], 0)
context.activeTexture(context.TEXTURE0)
context.bindTexture(context.TEXTURE_2D, texture)
context.drawArrays(context.TRIANGLES,
0,
cube.vertices.length/3)
}
}
module.exports = TexturedCube