-
Notifications
You must be signed in to change notification settings - Fork 99
/
occlusion.html
624 lines (489 loc) · 23.8 KB
/
occlusion.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
<!DOCTYPE html>
<!--
The MIT License (MIT)
Copyright (c) 2017 Tarek Sherif
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<!--
Occlusion culling algorithm based on:
https://developer.nvidia.com/gpugems/GPUGems/gpugems_ch29.html
https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter06.html
-->
<html>
<head>
<title>WebGL 2 Example: Occlusion Culling</title>
<meta charset="utf-8">
<script src="utils/gl-matrix.js"></script>
<script src="utils/utils.js"></script>
<link rel="stylesheet" href="css/webgl2examples.css">
<style>
#occlusion-controls {
position: absolute;
bottom: 20px;
right: 20px;
color: white;
}
</style>
</head>
<body>
<div id="example-title">
<header>WebGL 2 Example: Occlusion Culling</header>
<div id="features">
Features: Vertex Arrays, Uniform Buffers, Immutable Textures, Occlusion Query
</div>
<div>
<a href="https://github.com/tsherif/webgl2examples/blob/master/occlusion.html">Source code</a>
</div>
</div>
<canvas id="gl-canvas"></canvas>
<div id="occlusion-controls">
Spheres: <span id="num-spheres"></span><br>
Culled spheres: <span id="num-invisible-spheres"></span><br>
Enable occlusion culling: <input id="occlusion-toggle" type="checkbox" checked><br>
Top-down HUD: <input id="hud-toggle" type="checkbox" checked>
</div>
<script type="x-shader/vs" id="vertex-draw">
#version 300 es
layout(std140, column_major) uniform;
layout(location=0) in vec4 position;
layout(location=1) in vec2 uv;
layout(location=2) in vec3 normal;
uniform SceneUniforms {
mat4 viewProj;
vec4 eyePosition;
vec4 lightPosition;
} uScene;
uniform mat4 uModel;
out vec3 vPosition;
out vec2 vUV;
out vec3 vNormal;
void main() {
vec4 worldPosition = uModel * position;
vPosition = worldPosition.xyz;
vUV = uv;
vNormal = mat3(uModel) * normal;
gl_Position = uScene.viewProj * worldPosition;
}
</script>
<script type="x-shader/vf" id="fragment-draw">
#version 300 es
precision highp float;
layout(std140, column_major) uniform;
uniform SceneUniforms {
mat4 viewProj;
vec4 eyePosition;
vec4 lightPosition;
} uScene;
uniform sampler2D tex;
in vec3 vPosition;
in vec2 vUV;
in vec3 vNormal;
out vec4 fragColor;
void main() {
vec3 color = texture(tex, vUV).rgb;
vec3 normal = normalize(vNormal);
vec3 eyeVec = normalize(uScene.eyePosition.xyz - vPosition);
vec3 incidentVec = normalize(vPosition - uScene.lightPosition.xyz);
vec3 lightVec = -incidentVec;
float diffuse = max(dot(lightVec, normal), 0.0);
float highlight = pow(max(dot(eyeVec, reflect(incidentVec, normal)), 0.0), 100.0);
float ambient = 0.1;
fragColor = vec4(color * (diffuse + highlight + ambient), 1.0);
}
</script>
<script type="x-shader/vs" id="vertex-boundingBox">
#version 300 es
layout(std140, column_major) uniform;
layout(location=0) in vec4 position;
uniform SceneUniforms {
mat4 viewProj;
vec4 eyePosition;
vec4 lightPosition;
} uScene;
uniform mat4 uModel;
void main() {
gl_Position = uScene.viewProj * uModel * position;
}
</script>
<script type="x-shader/vf" id="fragment-boundingBox">
#version 300 es
precision highp float;
layout(std140, column_major) uniform;
out vec4 fragColor;
void main() {
fragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
</script>
<script type="x-shader/vs" id="vertex-hud">
#version 300 es
layout(std140, column_major) uniform;
layout(location=0) in vec4 position;
uniform mat4 uViewProj;
uniform mat4 uModel;
void main() {
gl_Position = uViewProj * uModel * position;
}
</script>
<script type="x-shader/vf" id="fragment-hud">
#version 300 es
precision highp float;
layout(std140, column_major) uniform;
out vec4 fragColor;
void main() {
fragColor = vec4(1.0, 0.0, 0.0, 0.2);
}
</script>
<script type="text/javascript">
var occlusionCullingEnabled = true;
var showHUD = true;
document.getElementById("occlusion-toggle").addEventListener("change", function() {
occlusionCullingEnabled = this.checked;
for(var i = 0, len = spheres.length; i < len; i++) {
spheres[i].occluded = false;
}
});
document.getElementById("hud-toggle").addEventListener("change", function() {
showHUD = this.checked;
});
var sphereCountElement = document.getElementById("num-spheres");
var occludedSpheresElement = document.getElementById("num-invisible-spheres");
var canvas = document.getElementById("gl-canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var gl = canvas.getContext("webgl2");
if (!gl) {
console.error("WebGL 2 not available");
document.body.innerHTML = "This example requires WebGL 2 which is unavailable on this system."
}
var hudViewport = [
0,
0,
gl.drawingBufferWidth / 5,
gl.drawingBufferHeight / 5
];
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
gl.enable(gl.DEPTH_TEST);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
/////////////////////
// SET UP PROGRAM
/////////////////////
var drawVsSource = document.getElementById("vertex-draw").text.trim();
var drawFsSource = document.getElementById("fragment-draw").text.trim();
var drawVertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(drawVertexShader, drawVsSource);
gl.compileShader(drawVertexShader);
if (!gl.getShaderParameter(drawVertexShader, gl.COMPILE_STATUS)) {
console.error(gl.getShaderInfoLog(drawVertexShader));
}
var drawFragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(drawFragmentShader, drawFsSource);
gl.compileShader(drawFragmentShader);
if (!gl.getShaderParameter(drawFragmentShader, gl.COMPILE_STATUS)) {
console.error(gl.getShaderInfoLog(drawFragmentShader));
}
var program = gl.createProgram();
gl.attachShader(program, drawVertexShader);
gl.attachShader(program, drawFragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
console.error(gl.getProgramInfoLog(program));
}
var boundingBoxVSource = document.getElementById("vertex-boundingBox").text.trim();
var boundingBoxFSource = document.getElementById("fragment-boundingBox").text.trim();
var boundingBoxVertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(boundingBoxVertexShader, boundingBoxVSource);
gl.compileShader(boundingBoxVertexShader);
if (!gl.getShaderParameter(boundingBoxVertexShader, gl.COMPILE_STATUS)) {
console.error(gl.getShaderInfoLog(boundingBoxVertexShader));
}
var boundingBoxFragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(boundingBoxFragmentShader, boundingBoxFSource);
gl.compileShader(boundingBoxFragmentShader);
if (!gl.getShaderParameter(boundingBoxFragmentShader, gl.COMPILE_STATUS)) {
console.error(gl.getShaderInfoLog(boundingBoxFragmentShader));
}
var boundingBoxProgram = gl.createProgram();
gl.attachShader(boundingBoxProgram, boundingBoxVertexShader);
gl.attachShader(boundingBoxProgram, boundingBoxFragmentShader);
gl.linkProgram(boundingBoxProgram);
if (!gl.getProgramParameter(boundingBoxProgram, gl.LINK_STATUS)) {
console.error(gl.getProgramInfoLog(boundingBoxProgram));
}
var hudVSSource = document.getElementById("vertex-hud").text.trim();
var hudFSSoure = document.getElementById("fragment-hud").text.trim();
var hudVertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(hudVertexShader, hudVSSource);
gl.compileShader(hudVertexShader);
if (!gl.getShaderParameter(hudVertexShader, gl.COMPILE_STATUS)) {
console.error(gl.getShaderInfoLog(hudVertexShader));
}
var hudFragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(hudFragmentShader, hudFSSoure);
gl.compileShader(hudFragmentShader);
if (!gl.getShaderParameter(hudFragmentShader, gl.COMPILE_STATUS)) {
console.error(gl.getShaderInfoLog(hudFragmentShader));
}
var hudProgram = gl.createProgram();
gl.attachShader(hudProgram, hudVertexShader);
gl.attachShader(hudProgram, hudFragmentShader);
gl.linkProgram(hudProgram);
if (!gl.getProgramParameter(hudProgram, gl.LINK_STATUS)) {
console.error(gl.getProgramInfoLog(hudProgram));
}
/////////////////////////
// GET UNIFORM LOCATIONS
/////////////////////////
// Main draw program locations
var drawModelMatrixLocation = gl.getUniformLocation(program, "uModel");
var drawTexLocation = gl.getUniformLocation(program, "tex");
var sceneUniformsLocation = gl.getUniformBlockIndex(program, "SceneUniforms");
gl.uniformBlockBinding(program, sceneUniformsLocation, 0);
// Bounding box program locations
var boundingBoxModelMatrixLocation = gl.getUniformLocation(boundingBoxProgram, "uModel");
// Hud program locations
var hudViewProjLocation = gl.getUniformLocation(hudProgram, "uViewProj");
var hudModelMatrixLocation = gl.getUniformLocation(hudProgram, "uModel");
/////////////////////
// SET UP GEOMETRY
/////////////////////
var positionBuffer, uvBuffer, normalBuffer, indices;
// Sphere geometry
var sphere = utils.createSphere({ radius: 0.6 });
var sphereArray = gl.createVertexArray();
gl.bindVertexArray(sphereArray);
positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, sphere.positions, gl.STATIC_DRAW);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(0);
uvBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, uvBuffer);
gl.bufferData(gl.ARRAY_BUFFER, sphere.uvs, gl.STATIC_DRAW);
gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(1);
normalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, sphere.normals, gl.STATIC_DRAW);
gl.vertexAttribPointer(2, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(2);
indices = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indices);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, sphere.indices, gl.STATIC_DRAW);
gl.bindVertexArray(null);
// Bounding box geometry
sphere.boundingBox = utils.computeBoundingBox(sphere.positions, {geo: true});
var boundingBoxArray = gl.createVertexArray();
gl.bindVertexArray(boundingBoxArray);
positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, sphere.boundingBox.geo.positions, gl.STATIC_DRAW);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(0);
gl.bindVertexArray(null);
// Object definitions
var GRID_DIM = 6;
var GRID_OFFSET = GRID_DIM / 2 - 0.5;
var NUM_SPHERES = GRID_DIM * GRID_DIM;
var spheres = new Array(NUM_SPHERES);
for (var i = 0; i < NUM_SPHERES; ++i) {
var x = Math.floor(i / GRID_DIM) - GRID_OFFSET;
var z = i % GRID_DIM - GRID_OFFSET;
spheres[i] = {
rotate: [0, 0, 0], // Will be used for global rotation
translate: [x, 0, z],
modelMatrix: mat4.create(),
vertexArray: sphereArray,
numElements: sphere.indices.length,
boundingBox: sphere.boundingBox,
boundingBoxVertexArray: boundingBoxArray,
boundingBoxNumVertices: sphere.boundingBox.geo.positions.length / 3,
query: gl.createQuery(),
queryInProgress: false,
occluded: false
};
utils.xformMatrix(spheres[i].modelMatrix, spheres[i].translate);
}
sphereCountElement.innerHTML = spheres.length;
//////////////////////////
// UNIFORM DATA
//////////////////////////
// scene main camera
var projMatrix = mat4.create();
mat4.perspective(projMatrix, Math.PI / 2, gl.drawingBufferWidth / gl.drawingBufferHeight, 0.1, 10.0);
var viewMatrix = mat4.create();
var eyePosition = vec3.fromValues(0, 0, 5);
mat4.lookAt(viewMatrix, eyePosition, vec3.fromValues(0, 0, 0), vec3.fromValues(0, 1, 0));
var viewProjMatrix = mat4.create();
mat4.multiply(viewProjMatrix, projMatrix, viewMatrix);
// top down assist camera
var projMatrixAssistCam = mat4.create();
mat4.perspective(projMatrixAssistCam, Math.PI / 2, gl.drawingBufferWidth / gl.drawingBufferHeight, 0.1, 10.0);
var viewMatrixAssistCam = mat4.create();
var eyePositionAssistCam = vec3.fromValues(0, 5, 0);
mat4.lookAt(viewMatrixAssistCam, eyePositionAssistCam, vec3.fromValues(0, 0, 0), vec3.fromValues(0, 0, -1));
var viewProjMatrixAssistCam = mat4.create();
mat4.multiply(viewProjMatrixAssistCam, projMatrixAssistCam, viewMatrixAssistCam);
gl.useProgram(hudProgram);
gl.uniformMatrix4fv(hudViewProjLocation, false, viewProjMatrixAssistCam);
gl.useProgram(null);
var lightPosition = vec3.fromValues(1, 1, 40);
var modelMatrix = mat4.create();
var rotateXMatrix = mat4.create();
var rotateYMatrix = mat4.create();
var sceneUniformData = new Float32Array(24);
sceneUniformData.set(viewProjMatrix);
sceneUniformData.set(eyePosition, 16);
sceneUniformData.set(lightPosition, 20);
var sceneUniformBuffer = gl.createBuffer();
gl.bindBufferBase(gl.UNIFORM_BUFFER, 0, sceneUniformBuffer);
gl.bufferData(gl.UNIFORM_BUFFER, sceneUniformData, gl.STATIC_DRAW);
var assistCamUniformData = new Float32Array(24);
assistCamUniformData.set(viewProjMatrixAssistCam);
assistCamUniformData.set(eyePositionAssistCam, 16);
assistCamUniformData.set(lightPosition, 20);
var assistCamUniformBuffer = gl.createBuffer();
gl.bindBufferBase(gl.UNIFORM_BUFFER, 1, assistCamUniformBuffer);
gl.bufferData(gl.UNIFORM_BUFFER, assistCamUniformData, gl.STATIC_DRAW);
//////////////////////////
// DEPTH SORT FUNCTION
//////////////////////////
var sortPositionA = vec4.create();
var sortPositionB = vec4.create();
var sortModelView = mat4.create();
function depthSort(a, b) {
vec4.set(sortPositionA, a.translate[0], a.translate[1], a.translate[2], 1.0);
vec4.set(sortPositionB, b.translate[0], b.translate[1], b.translate[2], 1.0);
mat4.mul(sortModelView, viewMatrix, a.modelMatrix);
vec4.transformMat4(sortPositionA, sortPositionA, sortModelView);
mat4.mul(sortModelView, viewMatrix, b.modelMatrix);
vec4.transformMat4(sortPositionB, sortPositionB, sortModelView);
return sortPositionB[2] - sortPositionA[2];
}
/////////////////
// LOAD TEXTURE
/////////////////
var image = new Image();
var firstFrame = true;
var occludedSpheres = 0;
image.onload = function() {
var texture = gl.createTexture();
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
var levels = Math.floor(Math.log2(Math.max(this.width, this.height))) + 1;
gl.texStorage2D(gl.TEXTURE_2D, levels, gl.RGBA8, image.width, image.height);
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, image.width, image.height, gl.RGBA, gl.UNSIGNED_BYTE, image);
gl.generateMipmap(gl.TEXTURE_2D);
gl.useProgram(program);
gl.uniform1i(drawTexLocation, 0);
gl.useProgram(null);
var rotationMatrix = mat4.create();
var sphere, boundingBox;
var samplesPassed;
var i;
function draw() {
occludedSpheres = 0;
// Note: Sort based on previous frame's transformations
if (occlusionCullingEnabled) {
spheres.sort(depthSort);
}
gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
gl.clearColor(0, 0, 0, 1);
gl.enable(gl.DEPTH_TEST);
gl.colorMask(true, true, true, true);
gl.depthMask(true);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
for (i = 0; i < NUM_SPHERES; ++i) {
sphere = spheres[i];
boundingBox = sphere.boundingBox;
// Update transforms
sphere.rotate[1] += 0.003;
utils.xformMatrix(sphere.modelMatrix, sphere.translate, null, sphere.scale);
mat4.fromYRotation(rotationMatrix, sphere.rotate[1]);
mat4.multiply(sphere.modelMatrix, rotationMatrix, sphere.modelMatrix);
//////////////////
// OCCLUSION TEST
//////////////////
if (occlusionCullingEnabled) {
gl.colorMask(false, false, false, false);
gl.depthMask(false);
gl.useProgram(boundingBoxProgram);
gl.bindVertexArray(sphere.boundingBoxVertexArray);
gl.uniformMatrix4fv(boundingBoxModelMatrixLocation, false, sphere.modelMatrix);
// Check query results here (will be from previous frame or earlier)
if (sphere.queryInProgress && gl.getQueryParameter(sphere.query, gl.QUERY_RESULT_AVAILABLE)) {
sphere.occluded = !gl.getQueryParameter(sphere.query, gl.QUERY_RESULT);
if (sphere.occluded) {
occludedSpheres++;
}
sphere.queryInProgress = false;
}
// Query is initiated here by drawing the bounding box of the sphere
if (!sphere.queryInProgress) {
gl.beginQuery(gl.ANY_SAMPLES_PASSED_CONSERVATIVE, sphere.query);
gl.drawArrays(gl.TRIANGLES, 0, sphere.boundingBoxNumVertices);
gl.endQuery(gl.ANY_SAMPLES_PASSED_CONSERVATIVE);
sphere.queryInProgress = true;
}
} else {
sphere.occluded = false;
}
if (!sphere.occluded) {
gl.colorMask(true, true, true, true);
gl.depthMask(true);
gl.useProgram(program);
gl.bindVertexArray(sphere.vertexArray);
gl.uniformMatrix4fv(drawModelMatrixLocation, false, sphere.modelMatrix);
gl.drawElements(gl.TRIANGLES, sphere.numElements, gl.UNSIGNED_SHORT, 0);
}
}
// Draw HUD (visualize occlusion results)
if (showHUD) {
gl.viewport(hudViewport[0], hudViewport[1], hudViewport[2], hudViewport[3]);
gl.enable(gl.BLEND);
gl.enable(gl.SCISSOR_TEST);
gl.scissor(hudViewport[0], hudViewport[1], hudViewport[2], hudViewport[3]);
gl.colorMask(true, true, true, true);
gl.depthMask(true);
gl.disable(gl.DEPTH_TEST);
gl.clearColor(0.3, 0.3, 0.3, 1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.useProgram(hudProgram);
for (i = 0; i < NUM_SPHERES; ++i) {
sphere = spheres[i];
if (!sphere.occluded) {
gl.bindVertexArray(sphere.vertexArray);
gl.uniformMatrix4fv(hudModelMatrixLocation, false, sphere.modelMatrix);
gl.drawElements(gl.TRIANGLES, sphere.numElements, gl.UNSIGNED_SHORT, 0);
}
}
gl.disable(gl.SCISSOR_TEST);
gl.disable(gl.BLEND);
}
occludedSpheresElement.innerHTML = occludedSpheres;
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
}
image.src = "img/khronos_webgl.png";
</script>
<a href="https://github.com/tsherif/webgl2examples" id="github-ribbon"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"></a>
</body>
</html>