-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.html
333 lines (273 loc) · 14.4 KB
/
index.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
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Radial lens undistortion filter</title>
<script type="text/javascript" src="utils/glMatrix-0.9.5.min.js"></script>
<script type="text/javascript" src="utils/webgl-utils.js"></script>
<script type="text/javascript" src="utils/webgl-debug.js"></script>
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D texture_diffuse;
uniform vec2 image_dimensions;
uniform float alphax;
uniform float alphay;
varying vec4 pass_Color;
varying vec2 pass_TextureCoord;
void main(void) {
// Normalize the u,v coordinates in the range [-1;+1]
float x = (2.0 * pass_TextureCoord.x - 1.0) / 1.0;
float y = (2.0 * pass_TextureCoord.y - 1.0) / 1.0;
// Calculate l2 norm
float r = x*x + y*y;
// Calculate the deflated or inflated new coordinate (reverse transform)
float x3 = x / (1.0 - alphax * r);
float y3 = y / (1.0 - alphay * r);
float x2 = x / (1.0 - alphax * (x3 * x3 + y3 * y3));
float y2 = y / (1.0 - alphay * (x3 * x3 + y3 * y3));
// Forward transform
// float x2 = x * (1.0 - alphax * r);
// float y2 = y * (1.0 - alphay * r);
// De-normalize to the original range
float i2 = (x2 + 1.0) * 1.0 / 2.0;
float j2 = (y2 + 1.0) * 1.0 / 2.0;
if(i2 >= 0.0 && i2 <= 1.0 && j2 >= 0.0 && j2 <= 1.0)
gl_FragColor = texture2D(texture_diffuse, vec2(i2, j2));
else
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec4 in_Position;
attribute vec4 in_Color;
attribute vec2 in_TextureCoord;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
varying vec4 pass_Color;
varying vec2 pass_TextureCoord;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * in_Position;
pass_Color = in_Color;
pass_TextureCoord = in_TextureCoord;
}
</script>
<script type="text/javascript">
var gl; // gl instance
function webGLStart() {
var canvas = document.getElementById("filter-canvas");
try {
gl = canvas.getContext("experimental-webgl");
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
} catch (e) {}
if (!gl) {
alert("Could not initialize WebGL");
}
setupQuad();
initShaders();
initTexture();
ctx = WebGLDebugUtils.makeDebugContext(gl.clearColor(0.0, 0.0, 0.0, 1.0));
ctx = WebGLDebugUtils.makeDebugContext(gl.enable(gl.DEPTH_TEST));
paintLoop();
}
var vboId; // Quad data
var vboiId; // Quad indices
var indices_count;
var textureId;
var imageDimensions = [0.0, 0.0];
var alphax = 0.0;
var alphay = 0.0;
var maximum_alpha = 0.25;
var shaderProgram;
var textureIsSafeToRender = false;
var mvMatrix = mat4.create(); // Warning: does not default to identity
var pMatrix = mat4.create(); // Warning: does not default to identity
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) { // Check for TEXT_NODE
str += k.textContent;
}
k = k.nextSibling;
}
var shader;
if (shaderScript.type == "x-shader/x-fragment") {
ctx = WebGLDebugUtils.makeDebugContext(shader = gl.createShader(gl.FRAGMENT_SHADER));
} else if (shaderScript.type == "x-shader/x-vertex") {
ctx = WebGLDebugUtils.makeDebugContext(shader = gl.createShader(gl.VERTEX_SHADER));
} else {
return null;
}
ctx = WebGLDebugUtils.makeDebugContext(gl.shaderSource(shader, str));
ctx = WebGLDebugUtils.makeDebugContext(gl.compileShader(shader));
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
function initShaders() {
var fragmentShader = getShader(gl, "shader-fs");
var vertexShader = getShader(gl, "shader-vs");
ctx = WebGLDebugUtils.makeDebugContext(shaderProgram = gl.createProgram());
ctx = WebGLDebugUtils.makeDebugContext(gl.attachShader(shaderProgram, vertexShader));
ctx = WebGLDebugUtils.makeDebugContext(gl.attachShader(shaderProgram, fragmentShader));
ctx = WebGLDebugUtils.makeDebugContext(gl.linkProgram(shaderProgram));
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialize shaders");
}
ctx = WebGLDebugUtils.makeDebugContext(gl.useProgram(shaderProgram));
// Enable vertex attribute arrays for position, color, uvs
ctx = WebGLDebugUtils.makeDebugContext(shaderProgram.inPosition = gl.getAttribLocation(shaderProgram, "in_Position"));
ctx = WebGLDebugUtils.makeDebugContext(gl.enableVertexAttribArray(shaderProgram.inPosition));
shaderProgram.inColor = gl.getAttribLocation(shaderProgram, "in_Color");
ctx = WebGLDebugUtils.makeDebugContext(gl.enableVertexAttribArray(shaderProgram.inColor));
shaderProgram.inTextureCoord = gl.getAttribLocation(shaderProgram, "in_TextureCoord");
ctx = WebGLDebugUtils.makeDebugContext(gl.enableVertexAttribArray(shaderProgram.inTextureCoord));
// Bind uniforms for matrices
ctx = WebGLDebugUtils.makeDebugContext(shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix"));
ctx = WebGLDebugUtils.makeDebugContext(shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix"));
// Bind uniform for image dimensions and alpha factors
ctx = WebGLDebugUtils.makeDebugContext(shaderProgram.imageDimensionsUniform = gl.getUniformLocation(shaderProgram, "image_dimensions"));
ctx = WebGLDebugUtils.makeDebugContext(shaderProgram.alphax = gl.getUniformLocation(shaderProgram, "alphax"));
ctx = WebGLDebugUtils.makeDebugContext(shaderProgram.alphay = gl.getUniformLocation(shaderProgram, "alphay"));
}
function setupQuad() {
// Set up VBO with quad data
ctx = WebGLDebugUtils.makeDebugContext(vboId = gl.createBuffer());
ctx = WebGLDebugUtils.makeDebugContext(gl.bindBuffer(gl.ARRAY_BUFFER, vboId));
var vbo_data = [
-0.5, 0.5, 0.0, 1.0, // Vertex (xyzw)
1.0, 0.0, 0.0, 1.0, // RGBA
0.0, 1.0, // UV coords
-0.5, -0.5, 0.0, 1.0, // Vertex (xyzw)
0.0, 1.0, 0.0, 1.0, // RGBA
0.0, 0.0, // UV coords
0.5, -0.5, 0.0, 1.0, // Vertex (xyzw)
0.0, 0.0, 1.0, 1.0, // RGBA
1.0, 0.0, // UV coords
0.5, 0.5, 0.0, 1.0, // Vertex (xyzw)
1.0, 1.0, 1.0, 1.0, // RGBA
1.0, 1.0 // UV coords
];
ctx = WebGLDebugUtils.makeDebugContext(gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vbo_data), gl.STATIC_DRAW));
// Set up VBO with indices data
ctx = WebGLDebugUtils.makeDebugContext(vboiId = gl.createBuffer());
ctx = WebGLDebugUtils.makeDebugContext(gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vboiId));
var indices = [
0, 1, 2,
2, 3, 0
];
ctx = WebGLDebugUtils.makeDebugContext(gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW));
indices_count = indices.length;
}
function initTexture() {
ctx = WebGLDebugUtils.makeDebugContext(textureId = gl.createTexture());
textureId.image = new Image();
textureId.image.onload = function () {
ctx = WebGLDebugUtils.makeDebugContext(gl.bindTexture(gl.TEXTURE_2D, textureId));
ctx = WebGLDebugUtils.makeDebugContext(gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true));
ctx = WebGLDebugUtils.makeDebugContext(gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textureId.image));
ctx = WebGLDebugUtils.makeDebugContext(gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR));
ctx = WebGLDebugUtils.makeDebugContext(gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR));
ctx = WebGLDebugUtils.makeDebugContext(gl.bindTexture(gl.TEXTURE_2D, null));
textureIsSafeToRender = true;
}
textureId.image.src = "assets/pincushion_distortion.png";
imageDimensions = new Float32Array([textureId.image.width, textureId.image.height]);
}
function drawScene() {
ctx = WebGLDebugUtils.makeDebugContext(gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight));
ctx = WebGLDebugUtils.makeDebugContext(gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT));
if(textureIsSafeToRender == false)
return; // Still can't render stuff if the texture hasn't been loaded yet
// Set matrix uniforms
mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);
mat4.identity(mvMatrix);
mat4.translate(mvMatrix, [0.0, 0.0, -1.2]);
ctx = WebGLDebugUtils.makeDebugContext(gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix));
ctx = WebGLDebugUtils.makeDebugContext(gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix));
// Set image dimensions uniform
ctx = WebGLDebugUtils.makeDebugContext(gl.uniform2fv(shaderProgram.imageDimensionsUniform, imageDimensions));
// Set alpha factors uniforms
ctx = WebGLDebugUtils.makeDebugContext(gl.uniform1fv(shaderProgram.alphax, new Float32Array([alphax])));
ctx = WebGLDebugUtils.makeDebugContext(gl.uniform1fv(shaderProgram.alphay, new Float32Array([alphay])));
// Bind texture to texture unit 0
ctx = WebGLDebugUtils.makeDebugContext(gl.activeTexture(gl.TEXTURE0));
ctx = WebGLDebugUtils.makeDebugContext(gl.bindTexture(gl.TEXTURE_2D, textureId));
var sampler2D_loc = gl.getUniformLocation(shaderProgram, "texture_diffuse");
ctx = WebGLDebugUtils.makeDebugContext(gl.uniform1i(sampler2D_loc, 0));
// Bind vertex data and set vertex attributes (as from a VAO)
ctx = WebGLDebugUtils.makeDebugContext(gl.bindBuffer(gl.ARRAY_BUFFER, vboId));
ctx = WebGLDebugUtils.makeDebugContext(gl.vertexAttribPointer(shaderProgram.inPosition, 4, gl.FLOAT, false, 10 * 4 /* 10 floats */, 0));
ctx = WebGLDebugUtils.makeDebugContext(gl.vertexAttribPointer(shaderProgram.inColor, 4, gl.FLOAT, false, 10 * 4, 4 * 4));
ctx = WebGLDebugUtils.makeDebugContext(gl.vertexAttribPointer(shaderProgram.inTextureCoord, 2, gl.FLOAT, false, 10 * 4, 8 * 4));
// Bind vertex index info
ctx = WebGLDebugUtils.makeDebugContext(gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vboiId));
// ------------ Filtering-specific data ------------
// Bind
ctx = WebGLDebugUtils.makeDebugContext(gl.vertexAttribPointer(shaderProgram.inTextureCoord, 2, gl.FLOAT, false, 10 * 4, 8 * 4));
// Draw the quad
ctx = WebGLDebugUtils.makeDebugContext(gl.drawElements(gl.TRIANGLES, indices_count, gl.UNSIGNED_SHORT, 0));
// Cleanup
ctx = WebGLDebugUtils.makeDebugContext(gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null));
ctx = WebGLDebugUtils.makeDebugContext(gl.bindBuffer(gl.ARRAY_BUFFER, null));
ctx = WebGLDebugUtils.makeDebugContext(gl.bindTexture(gl.TEXTURE_2D, null));
}
function paintLoop() {
requestAnimFrame(paintLoop);
drawScene();
}
// ~-~-~-~-~-~-~-~-~- UI related handling routines ~-~-~-~-~-~-~-~-~-
var same_alpha_factors = true; // Whether alphax and alphay are forced to be equal
function adjustAlphaFactor(direction, value) { // value should be in range [0;100]
switch(direction) {
case 'x': {
alphax = (value * 0.01) * maximum_alpha;
document.getElementById("alphax_value").value = alphax;
if(same_alpha_factors) {
alphay = alphax;
document.getElementById("alphay_slider").value = value;
document.getElementById("alphay_value").value = alphay;
}
} break;
case 'y': {
alphay = (value * 0.01) * maximum_alpha;
document.getElementById("alphay_value").value = alphay;
if(same_alpha_factors) {
alphax = alphay;
document.getElementById("alphax_slider").value = value;
document.getElementById("alphax_value").value = alphax;
}
} break;
}
}
// ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
</script>
</head>
<body onload="webGLStart();">
<div style="padding: 10px;">
<div style="float: left;">
<canvas id="filter-canvas" style="border: none;" width="500" height="500"></canvas>
</div>
<div style="float: left; padding: 10px;">
<input type="checkbox" onchange="same_alpha_factors = this.checked;" checked/>Force square pixels<br/><br/>
Adjust horizontal distortion parameter<br/>
<input id="alphax_slider" type="range" style="vertical-align:middle;" min="0" max="100" value="0" onchange="adjustAlphaFactor('x', this.value)"
oninput="adjustAlphaFactor('x', this.value)"/>
<input type="text" id="alphax_value" style="vertical-align:middle; width: 50px; text-align: center;" value="0.0" readonly><br/>
Adjust vertical distortion parameter<br/>
<input id="alphay_slider" type="range" style="vertical-align:middle;" min="0" max="100" value="0" onchange="adjustAlphaFactor('y', this.value)"
oninput="adjustAlphaFactor('y', this.value)"/>
<input type="text" id="alphay_value" style="vertical-align:middle; width: 50px; text-align: center;" value="0.0" readonly><br/>
<div style="float: left; padding: 20px">
<img style="vertical-align:middle;" src="http://marcodiiga.github.io/images/webicon-github-m.png"/>
<a href="https://github.com/marcodiiga/lens_distortion_filtering">Github Repo</a>
</div>
</div>
</div>
</body>
</html>