-
Notifications
You must be signed in to change notification settings - Fork 2
/
teapot-sobel-normals.html
527 lines (394 loc) · 16.7 KB
/
teapot-sobel-normals.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
<html>
<head>
<title>Normal edge detection teapot</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="sylvester.js"></script>
<script type="text/javascript" src="glUtils.js"></script>
<script type="text/javascript" src="webgl.debug.js"></script>
<script id="normals-fs" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
varying vec4 vTransformedNormal;
void main(void) {
gl_FragColor = vec4(normalize(vTransformedNormal.xyz), 1.0);
}
</script>
<script id="matrix-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat4 uNMatrix;
varying vec4 vTransformedNormal;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
vTransformedNormal = uNMatrix * vec4(aVertexNormal, 1.0);
}
</script>
<script id="direct-texture-fs" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
void main(void) {
gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
}
</script>
<script id="flat-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec2 aTextureCoord;
varying vec2 vTextureCoord;
void main(void) {
gl_Position = vec4(aVertexPosition, 1.0);
vTextureCoord = aTextureCoord;
}
</script>
<script id="sobel-fs" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
varying vec2 vTextureCoord;
uniform sampler2D uSampler;
void main(void) {
mat3 xKernel = mat3(
-1.0, 0, 1.0,
-2.0, 0, 2.0,
-1.0, 0, 1.0
);
mat3 yKernel = mat3(
-1.0, -2.0, -1.0,
0.0, 0.0, 0.0,
1.0, 2.0, 1.0
);
float xSlope = 0.0;
float ySlope = 0.0;
for (float i=-1.0; i<=1.0; i++) {
for (float j=-1.0; j<=1.0; j++) {
xSlope += texture2D(uSampler, vec2(vTextureCoord.s + i/512.0, vTextureCoord.t + j/512.0)).r * xKernel[int(i)+1][int(j)+1];
xSlope += texture2D(uSampler, vec2(vTextureCoord.s + i/512.0, vTextureCoord.t + j/512.0)).g * xKernel[int(i)+1][int(j)+1];
xSlope += texture2D(uSampler, vec2(vTextureCoord.s + i/512.0, vTextureCoord.t + j/512.0)).b * xKernel[int(i)+1][int(j)+1];
ySlope += texture2D(uSampler, vec2(vTextureCoord.s + i/512.0, vTextureCoord.t + j/512.0)).r * yKernel[int(i)+1][int(j)+1];
ySlope += texture2D(uSampler, vec2(vTextureCoord.s + i/512.0, vTextureCoord.t + j/512.0)).g * yKernel[int(i)+1][int(j)+1];
ySlope += texture2D(uSampler, vec2(vTextureCoord.s + i/512.0, vTextureCoord.t + j/512.0)).b * yKernel[int(i)+1][int(j)+1];
}
}
float magnitude = length(vec2(xSlope, ySlope));
if (magnitude > 0.6) {
gl_FragColor += vec4(0.0, 0.0, 0.0, 1.0);
} else {
gl_FragColor += vec4(1.0, 1.0, 1.0, 1.0);
}
}
</script>
<script type="text/javascript">
var gl;
function initGL(canvas) {
try {
gl = canvas.getContext("experimental-webgl");
gl = WebGLDebugUtils.makeDebugContext(gl);
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
} catch(e) {
}
if (!gl) {
alert("Could not initialise WebGL, sorry :-(");
}
}
function getShader(gl, id) {
var shaderScript = document.getElementById(id);
if (!shaderScript) {
return null;
}
var str = "";
var k = shaderScript.firstChild;
while (k) {
if (k.nodeType == 3) {
str += k.textContent;
}
k = k.nextSibling;
}
var shader;
if (shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
function createProgram(fsId, vsId) {
var normalsFragmentShader = getShader(gl, fsId);
var vertexShader = getShader(gl, vsId);
var program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, normalsFragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
alert("Could not initialise shaders " + vsId + ", " + fsId + gl.getProgramInfoLog(program));
}
gl.useProgram(program);
program.vertexPositionAttribute = gl.getAttribLocation(program, "aVertexPosition");
if (program.vertexPositionAttribute != -1) {
gl.enableVertexAttribArray(program.vertexPositionAttribute);
}
program.vertexNormalAttribute = gl.getAttribLocation(program, "aVertexNormal");
if (program.vertexNormalAttribute != -1) {
gl.enableVertexAttribArray(program.vertexNormalAttribute);
}
program.textureCoordAttribute = gl.getAttribLocation(program, "aTextureCoord");
if (program.textureCoordAttribute != -1) {
gl.enableVertexAttribArray(program.textureCoordAttribute);
}
program.pMatrixUniform = gl.getUniformLocation(program, "uPMatrix");
program.mvMatrixUniform = gl.getUniformLocation(program, "uMVMatrix");
program.nMatrixUniform = gl.getUniformLocation(program, "uNMatrix");
program.samplerUniform = gl.getUniformLocation(program, "uSampler");
program.materialShininessUniform = gl.getUniformLocation(program, "uMaterialShininess");
program.ambientColorUniform = gl.getUniformLocation(program, "uAmbientColor");
program.pointLightingLocationUniform = gl.getUniformLocation(program, "uPointLightingLocation");
program.pointLightingSpecularColorUniform = gl.getUniformLocation(program, "uPointLightingSpecularColor");
program.pointLightingDiffuseColorUniform = gl.getUniformLocation(program, "uPointLightingDiffuseColor");
return program;
}
var normalsProgram;
var sobelProgram;
var directTextureProgram;
function initShaders() {
normalsProgram = createProgram("normals-fs", "matrix-vs");
sobelProgram = createProgram("sobel-fs", "flat-vs");
directTextureProgram = createProgram("direct-texture-fs", "flat-vs");
}
var mvMatrix;
var mvMatrixStack = [];
function mvPushMatrix(m) {
if (m) {
mvMatrixStack.push(m.dup());
mvMatrix = m.dup();
} else {
mvMatrixStack.push(mvMatrix.dup());
}
}
function mvPopMatrix() {
if (mvMatrixStack.length == 0) {
throw "Invalid popMatrix!";
}
mvMatrix = mvMatrixStack.pop();
return mvMatrix;
}
function loadIdentity() {
mvMatrix = Matrix.I(4);
}
function multMatrix(m) {
mvMatrix = mvMatrix.x(m);
}
function mvTranslate(v) {
var m = Matrix.Translation($V([v[0], v[1], v[2]])).ensure4x4();
multMatrix(m);
}
function createRotationMatrix(angle, v) {
var arad = angle * Math.PI / 180.0;
return Matrix.Rotation(arad, $V([v[0], v[1], v[2]])).ensure4x4();
}
function mvRotate(angle, v) {
multMatrix(createRotationMatrix(angle, v));
}
var pMatrix;
function perspective(fovy, aspect, znear, zfar) {
pMatrix = makePerspective(fovy, aspect, znear, zfar);
}
function setMatrixUniforms(program) {
gl.uniformMatrix4fv(program.pMatrixUniform, false, new Float32Array(pMatrix.flatten()));
gl.uniformMatrix4fv(program.mvMatrixUniform, false, new Float32Array(mvMatrix.flatten()));
var normalMatrix = mvMatrix.inverse();
normalMatrix = normalMatrix.transpose();
gl.uniformMatrix4fv(program.nMatrixUniform, false, new Float32Array(normalMatrix.flatten()));
}
var teapotVertexPositionBuffer;
var teapotVertexNormalBuffer;
var teapotVertexTextureCoordBuffer;
var teapotVertexIndexBuffer;
function handleLoadedTeapot(teapotData) {
teapotVertexNormalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, teapotVertexNormalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(teapotData.vertexNormals), gl.STATIC_DRAW);
teapotVertexNormalBuffer.itemSize = 3;
teapotVertexNormalBuffer.numItems = teapotData.vertexNormals.length / 3;
teapotVertexTextureCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, teapotVertexTextureCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(teapotData.vertexTextureCoords), gl.STATIC_DRAW);
teapotVertexTextureCoordBuffer.itemSize = 2;
teapotVertexTextureCoordBuffer.numItems = teapotData.vertexTextureCoords.length / 2;
teapotVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, teapotVertexPositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(teapotData.vertexPositions), gl.STATIC_DRAW);
teapotVertexPositionBuffer.itemSize = 3;
teapotVertexPositionBuffer.numItems = teapotData.vertexPositions.length / 3;
teapotVertexIndexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, teapotVertexIndexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(teapotData.indices), gl.STREAM_DRAW);
teapotVertexIndexBuffer.itemSize = 1;
teapotVertexIndexBuffer.numItems = teapotData.indices.length;
document.getElementById("loadingtext").textContent = "";
}
function loadTeapot() {
var request = new XMLHttpRequest();
request.open("GET", "Teapot.json");
request.onreadystatechange = function() {
if (request.readyState == 4) {
handleLoadedTeapot(JSON.parse(request.responseText));
}
}
request.send();
}
var directTextureVertexPositionBuffer;
var directTextureVertexTextureCoordBuffer;
function initBuffers() {
directTextureVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, directTextureVertexPositionBuffer);
var vertices = [
1.0, 1.0,
-1.0, 1.0,
1.0, -1.0,
-1.0, -1.0,
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
directTextureVertexPositionBuffer.itemSize = 2;
directTextureVertexPositionBuffer.numItems = 4;
directTextureVertexTextureCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, directTextureVertexTextureCoordBuffer);
var textureCoords = [
1.0, 1.0,
0.0, 1.0,
1.0, 0.0,
0.0, 0.0,
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);
directTextureVertexTextureCoordBuffer.itemSize = 2;
directTextureVertexTextureCoordBuffer.numItems = 4;
}
function createRTTFrameBuffer() {
framebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
framebuffer.width = 512;
framebuffer.height = 512;
framebuffer.texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, framebuffer.texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
gl.generateMipmap(gl.TEXTURE_2D);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, framebuffer.width, framebuffer.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
var renderbuffer = gl.createRenderbuffer();
gl.bindRenderbuffer(gl.RENDERBUFFER, renderbuffer);
gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, framebuffer.width, framebuffer.height);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, framebuffer.texture, 0);
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, renderbuffer);
gl.bindTexture(gl.TEXTURE_2D, null);
gl.bindRenderbuffer(gl.RENDERBUFFER, null);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
return framebuffer;
}
var normalsFramebuffer;
function initRTTFramebuffers() {
normalsFramebuffer = createRTTFrameBuffer();
}
var teapotAngle = 180;
function drawScene() {
// Render the spinning teapot scene's normals to the texture.
gl.bindFramebuffer(gl.FRAMEBUFFER, normalsFramebuffer);
gl.useProgram(normalsProgram);
gl.viewport(0, 0, normalsFramebuffer.width, normalsFramebuffer.height);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
if (teapotVertexPositionBuffer == null || teapotVertexNormalBuffer == null || teapotVertexTextureCoordBuffer == null || teapotVertexIndexBuffer == null) {
return;
}
perspective(45, normalsFramebuffer.width / normalsFramebuffer.height, 0.1, 100.0);
loadIdentity();
mvTranslate([0, 0, -40]);
mvRotate(23.4, [1, 0, -1]);
mvRotate(teapotAngle, [0, 1, 0]);
gl.bindBuffer(gl.ARRAY_BUFFER, teapotVertexPositionBuffer);
gl.vertexAttribPointer(normalsProgram.vertexPositionAttribute, teapotVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, teapotVertexNormalBuffer);
gl.vertexAttribPointer(normalsProgram.vertexNormalAttribute, teapotVertexNormalBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, teapotVertexIndexBuffer);
setMatrixUniforms(normalsProgram);
gl.drawElements(gl.TRIANGLES, teapotVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
var program;
if (document.getElementById("id_render_type").value == "normals") {
// The viewer wants to see the normals as a colour map, so just blat the texture onto the canvas.
program = directTextureProgram;
} else {
// The viewer wants to see the edges, so use the a program that does nothing in the vertex shader
// and runs a sobel-filter program in the fragment shader
program = sobelProgram;
}
gl.useProgram(program);
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, normalsFramebuffer.texture);
gl.uniform1i(program.samplerUniform, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, directTextureVertexPositionBuffer);
gl.vertexAttribPointer(program.vertexPositionAttribute, directTextureVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, directTextureVertexTextureCoordBuffer);
gl.vertexAttribPointer(program.textureCoordAttribute, directTextureVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);
setMatrixUniforms(program);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, directTextureVertexPositionBuffer.numItems);
}
var lastTime = 0;
function animate() {
var timeNow = new Date().getTime();
if (lastTime != 0) {
var elapsed = timeNow - lastTime;
teapotAngle += 0.05 * elapsed;
}
lastTime = timeNow;
}
function tick() {
drawScene();
animate();
}
function webGLStart() {
var canvas = document.getElementById("teapot-canvas");
initGL(canvas);
initShaders();
initBuffers();
initRTTFramebuffers();
loadTeapot();
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clearDepth(1.0);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
setInterval(tick, 15);
}
</script>
<style type="text/css">
#loadingtext {
position:absolute;
top:250px;
left:150px;
font-size:2em;
color: white;
}
</style>
</head>
<body onload="webGLStart();">
<canvas id="teapot-canvas" style="border: none;" width="500" height="500"></canvas>
<div id="loadingtext">Loading world...</div>
<br/>
Render:
<select id="id_render_type">
<option value="normals">Normals</option>
<option selected value="edges">Edges (Sobel-filtered)</option>
</select>
</body>
</html>