This repository has been archived by the owner on Aug 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_gloo2.html
102 lines (84 loc) · 2.54 KB
/
test_gloo2.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
<html>
<head>
<meta charset="utf-8">
</head>
<script src="scripts/gloo2.js"></script>
<script>
console.log(window);
function initialize_canvas(canvas_id) {
VERT = 'precision mediump float;\
attribute float a_x;\
attribute float a_y;\
varying vec2 v_texcoord;\
void main() {\
vec2 pos = vec2(a_x, a_y);\
gl_Position = vec4(pos*2.0-1.0, 0.0, 1.0);\
v_texcoord = pos;\
}';
FRAG = 'precision mediump float;\
uniform vec3 u_color[3];\
uniform sampler2D u_tex;\
varying vec2 v_texcoord;\
void main() {\
gl_FragColor = vec4(u_color[2], 1.0);\
gl_FragColor *= texture2D(u_tex, vec2(v_texcoord));\
gl_FragColor.a = 1.0;\
}';
X = new Float32Array([0.1, 0.2, 0.4, 0.6, 0.8, 0.9]);
Y = new Float32Array([0.5, 0.1, 0.8, 0.9, 0.7, 0.3]);
im = new Uint8Array([100, 150, 100, 150, 200, 150, 200, 250, 200]);
var e = document.getElementById(canvas_id);
var gl = e.getContext('webgl');
window.gl = gl;
var prog = new gloo2.Program(gl);
prog.set_shaders(VERT, FRAG);
window.t = prog;
var vbox = new gloo2.VertexBuffer(gl);
var vboy = new gloo2.VertexBuffer(gl);
vbox.set_size(X.length * 4);
vboy.set_size(X.length * 4);
vbox.set_data(0, X);
vboy.set_data(0, Y);
prog.set_attribute('a_x', 'float', vbox);
prog.set_attribute('a_y', 'float', vboy);
var tex = new gloo2.Texture2D(gl);
tex.set_interpolation(gl.LINEAR, gl.LINEAR);
tex.set_wrapping(gl.CLAMP_TO_EDGE, gl.CLAMP_TO_EDGE);
tex.set_size([3, 3], gl.LUMINANCE);
tex.set_data([0, 0], [3, 3], im);
prog.set_texture('u_tex', tex);
var draw = function () {
gl.viewport(0, 0, e.width, e.height);
gl.clearColor(1, 1, 0, 1);
prog.set_uniform('u_color', 'vec3', [1, 0, 0, 0, 1, 0, 0, 0, 1])
gl.clear(gl.COLOR_BUFFER_BIT);
gl.lineWidth(2);
prog.draw(gl.LINE_STRIP, [0, X.length]);
};
return draw;
}
window.onload = function() {
var draw = initialize_canvas("mycanvas");
function render (timestamp) {
draw();
window.requestAnimationFrame(render);
}
window.requestAnimationFrame(render);
};
</script>
<style>
#mycanvas, #mycanvas2 {
/*width: 100%;*/
/*height: 100%;*/
width: 800px;
height: 400px;
margin: 10px 0;
padding: 0;
cursor: default;
}
</style>
</head>
<body>
<canvas width=800, height=400 id="mycanvas"></canvas>
</body>
</html>