-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwebcv.js
92 lines (73 loc) · 2.69 KB
/
webcv.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
/*jslint es5: true, browser: true, devel: true */
// Start closure
(function (window, document, undefined) {
"use strict";
var WebCV = {
instanceList: [],
modules: {},
/**
* The core of WebCV, which will be instantiated for each canvas
* Uses a similar pattern to oCanvas in order to have an instance per canvas
*/
core: function (canvas, options) {
var m;
WebCV.instanceList.push(this);
this.textures = [];
// Init modules
for (m in WebCV.modules) {
if (WebCV.modules.hasOwnProperty(m)) {
this[m] = WebCV.modules[m]();
// Set core in modules
this[m].core = this;
}
}
// Reference to canvas element
this.canvas = canvas;
// Set up gl context
this.gl = null;
try {
this.gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
} catch (e) {
alert("WebGL not available");
}
// XXX should only use if needed
this.texture_float = this.gl.getExtension('OES_texture_float');
if (this.texture_float == null) {
alert("No support for float texture. Supported extensions:" + this.gl.getSupportedExtensions());
}
},
/**
* Create a new instance of the core for a specific canvas
*/
create: function (canvas, options) {
return new WebCV.core(canvas, options);
},
registerModule: function (name, func) {
WebCV.modules[name] = func;
}
};
WebCV.prototype = {};
WebCV.prototype.uploadTexture = function (image) {
var texture,
gl = this.gl;
// Create empty texture object
texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
// Upload to GPU
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
// Upscale method
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
// Downscale method
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
// gl.generateMipmap(gl.TEXTURE_2D);
gl.bindTexture(gl.TEXTURE_2D, null);
return texture;
};
WebCV.prototype.setCurrentShader = function (shaderProgram) {
var gl = this.gl;
gl.useProgram(shaderProgram);
gl.webcv_currentShader = shaderProgram;
};
window.WebCV = WebCV;
}(window, document)); // End closure