forked from regl-project/regl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webgl_cube.html
executable file
Β·379 lines (310 loc) Β· 8.12 KB
/
webgl_cube.html
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<!doctype html>
<html>
<head>
<title>WebGL Textured Cube Demo</title>
<script type="text/javascript">
/* global Image, alert, requestAnimationFrame */
var canvas
var gl
var cubePositionBuffer
var cubeUvBuffer
var cubeElementsBuffers
var cubeTexture
var shaderProgram
var cubePositionAttribute
var cubeUvAttribute
var projectionUniformLocation
var viewUniformLocation
var tick
function start () {
canvas = document.getElementById('glcanvas')
tick = 0
initWebGL(canvas)
if (gl) {
gl.clearColor(0.0, 0.0, 0.0, 1.0)
gl.enable(gl.DEPTH_TEST)
gl.enable(gl.CULL_FACE)
initShaders()
initBuffers()
initTextures()
// start RAF
requestAnimationFrame(drawScene)
}
}
function initWebGL () {
try {
gl = canvas.getContext('experimental-webgl')
} catch (e) {
}
if (!gl) {
alert('Unable to initialize WebGL. Your browser may not support it.')
}
}
function initBuffers () {
cubePositionBuffer = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, cubePositionBuffer)
var cubePosition = [
// positive z face.
-0.5, +0.5, +0.5,
+0.5, +0.5, +0.5,
+0.5, -0.5, +0.5,
-0.5, -0.5, +0.5,
// positive x face
+0.5, +0.5, +0.5,
+0.5, +0.5, -0.5,
+0.5, -0.5, -0.5,
+0.5, -0.5, +0.5,
// negative z face
+0.5, +0.5, -0.5,
-0.5, +0.5, -0.5,
-0.5, -0.5, -0.5,
+0.5, -0.5, -0.5,
// negative x face.
-0.5, +0.5, -0.5,
-0.5, +0.5, +0.5,
-0.5, -0.5, +0.5,
-0.5, -0.5, -0.5,
// top face
-0.5, +0.5, -0.5,
+0.5, +0.5, -0.5,
+0.5, +0.5, +0.5,
-0.5, +0.5, +0.5,
// bottom face
-0.5, -0.5, -0.5,
+0.5, -0.5, -0.5,
+0.5, -0.5, +0.5,
-0.5, -0.5, +0.5
]
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(cubePosition), gl.STATIC_DRAW)
cubeUvBuffer = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, cubeUvBuffer)
var cubeUv = [
// positive z face.
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// positive x face.
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// negative z face.
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// negative x face.
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// top face
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0,
// bottom face
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0
]
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(cubeUv), gl.STATIC_DRAW)
cubeElementsBuffers = gl.createBuffer()
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeElementsBuffers)
var cubeElements = [
// positive z face.
2, 1, 0,
2, 0, 3,
// positive x face.
6, 5, 4,
6, 4, 7,
// negative z face.
10, 9, 8,
10, 8, 11,
// negative x face.
14, 13, 12,
14, 12, 15,
// top face.
18, 17, 16,
18, 16, 19,
// bottom face
20, 21, 22,
23, 20, 22
]
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(cubeElements), gl.STATIC_DRAW)
}
function initTextures () {
cubeTexture = gl.createTexture()
var cubeImage = new Image()
cubeImage.onload = function () {
gl.bindTexture(gl.TEXTURE_2D, cubeTexture)
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, cubeImage)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
}
cubeImage.src = 'assets/peppers.png'
}
function drawScene () {
tick++
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
// bind buffers.
gl.bindBuffer(gl.ARRAY_BUFFER, cubePositionBuffer)
gl.vertexAttribPointer(cubePositionAttribute, 3, gl.FLOAT, false, 0, 0)
gl.bindBuffer(gl.ARRAY_BUFFER, cubeUvBuffer)
gl.vertexAttribPointer(cubeUvAttribute, 2, gl.FLOAT, false, 0, 0)
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeElementsBuffers)
// set texture.
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_2D, cubeTexture)
// set uniforms
gl.uniform1i(gl.getUniformLocation(shaderProgram, 'tex'), 0)
const t = 0.01 * tick
var perspectiveMatrix = perspective(45, 640.0 / 480.0, 0.1, 100.0)
gl.uniformMatrix4fv(projectionUniformLocation, false, new Float32Array(perspectiveMatrix))
gl.uniformMatrix4fv(viewUniformLocation, false, new Float32Array(
lookAt(
[5 * Math.cos(t), 2.5 * Math.sin(t), 5 * Math.sin(t)],
[0, 0.0, 0],
[0, 1, 0])))
gl.drawElements(gl.TRIANGLES, 36, gl.UNSIGNED_SHORT, 0)
requestAnimationFrame(drawScene)
}
function initShaders () {
var fragmentShader = getShader(gl, 'shader-fs')
var vertexShader = getShader(gl, 'shader-vs')
shaderProgram = gl.createProgram()
gl.attachShader(shaderProgram, vertexShader)
gl.attachShader(shaderProgram, fragmentShader)
gl.linkProgram(shaderProgram)
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert('Unable to initialize the shader program: ' + gl.getProgramInfoLog(shaderProgram))
}
gl.useProgram(shaderProgram)
cubePositionAttribute = gl.getAttribLocation(shaderProgram, 'position')
gl.enableVertexAttribArray(cubePositionAttribute)
cubeUvAttribute = gl.getAttribLocation(shaderProgram, 'uv')
gl.enableVertexAttribArray(cubeUvAttribute)
projectionUniformLocation = gl.getUniformLocation(shaderProgram, 'projection')
viewUniformLocation = gl.getUniformLocation(shaderProgram, 'view')
}
function getShader (gl, id) {
var e = document.getElementById(id)
if (!e) {
return null
}
var sourceCode = e.firstChild.textContent
var shader
if (e.type === 'x-shader/x-fragment') {
shader = gl.createShader(gl.FRAGMENT_SHADER)
} else if (e.type === 'x-shader/x-vertex') {
shader = gl.createShader(gl.VERTEX_SHADER)
} else {
return null
}
gl.shaderSource(shader, sourceCode)
gl.compileShader(shader)
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader))
return null
}
return shader
}
// Taken from gl-mat4
// https://github.com/stackgl/gl-mat4/blob/master/lookAt.js
function lookAt (eye, center, up) {
var x0, x1, x2, y0, y1, y2, z0, z1, z2, len
var eyex = eye[0]
var eyey = eye[1]
var eyez = eye[2]
var upx = up[0]
var upy = up[1]
var upz = up[2]
var centerx = center[0]
var centery = center[1]
var centerz = center[2]
z0 = eyex - centerx
z1 = eyey - centery
z2 = eyez - centerz
len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2)
z0 *= len
z1 *= len
z2 *= len
x0 = upy * z2 - upz * z1
x1 = upz * z0 - upx * z2
x2 = upx * z1 - upy * z0
len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2)
if (!len) {
x0 = 0
x1 = 0
x2 = 0
} else {
len = 1 / len
x0 *= len
x1 *= len
x2 *= len
}
y0 = z1 * x2 - z2 * x1
y1 = z2 * x0 - z0 * x2
y2 = z0 * x1 - z1 * x0
len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2)
if (!len) {
y0 = 0
y1 = 0
y2 = 0
} else {
len = 1 / len
y0 *= len
y1 *= len
y2 *= len
}
return [
x0, y0, z0, 0,
x1, y1, z1, 0,
x2, y2, z2, 0,
-(x0 * eyex + x1 * eyey + x2 * eyez),
-(y0 * eyex + y1 * eyey + y2 * eyez),
-(z0 * eyex + z1 * eyey + z2 * eyez),
1
]
}
// Taken from gl-mat4
// https://github.com/stackgl/gl-mat4/blob/master/perspective.js
function perspective (fovy, aspect, near, far) {
var f = 1.0 / Math.tan(fovy / 2)
var nf = 1 / (near - far)
return [
f / aspect, 0.0, 0.0, 0.0,
0.0, f, 0.0, 0.0,
0.0, 0.0, (far + near) * nf, -1.0,
0.0, 0.0, (2 * far * near) * nf, 0.0
]
}
</script>
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
varying vec2 vUv;
uniform sampler2D tex;
void main () {
gl_FragColor = texture2D(tex,vUv);
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
precision mediump float;
attribute vec3 position;
attribute vec2 uv;
varying vec2 vUv;
uniform mat4 projection, view;
void main() {
vUv = uv;
gl_Position = projection * view * vec4(position, 1);
}
</script>
</head>
<body onload="start()">
<canvas id="glcanvas" width="640" height="480">
Your browser doesn't appear to support the <code><canvas></code> element.
</canvas>
</body>
</html>