@@ -13,19 +13,24 @@ type HTML struct {
13
13
}
14
14
15
15
// NewHTML returns new HTML handler.
16
- func NewHTML (bind string , width , height float64 ) * HTML {
16
+ func NewHTML (bind string , width , height float64 , gl bool ) * HTML {
17
17
h := & HTML {}
18
18
19
19
b := strings .Split (bind , ":" )
20
20
if b [0 ] == "" {
21
21
bind = "127.0.0.1" + bind
22
22
}
23
23
24
- html = strings .Replace (html , "{BIND}" , bind , - 1 )
25
- html = strings .Replace (html , "{WIDTH}" , fmt .Sprintf ("%.0f" , width ), - 1 )
26
- html = strings .Replace (html , "{HEIGHT}" , fmt .Sprintf ("%.0f" , height ), - 1 )
24
+ tpl := html
25
+ if gl {
26
+ tpl = htmlWebGL
27
+ }
28
+
29
+ tpl = strings .Replace (tpl , "{BIND}" , bind , - 1 )
30
+ tpl = strings .Replace (tpl , "{WIDTH}" , fmt .Sprintf ("%.0f" , width ), - 1 )
31
+ tpl = strings .Replace (tpl , "{HEIGHT}" , fmt .Sprintf ("%.0f" , height ), - 1 )
27
32
28
- h .Template = []byte (html )
33
+ h .Template = []byte (tpl )
29
34
return h
30
35
}
31
36
@@ -51,7 +56,7 @@ var html = `<html>
51
56
var image = new Image();
52
57
53
58
ws.onopen = function() {
54
- var context = document.getElementById("canvas").getContext("2d");
59
+ var context = document.getElementById("canvas").getContext("2d", {alpha: false} );
55
60
image.onload = function() {
56
61
context.drawImage(image, 0, 0);
57
62
}
@@ -72,3 +77,101 @@ var html = `<html>
72
77
</table>
73
78
</body>
74
79
</html>`
80
+
81
+ var htmlWebGL = `<html>
82
+ <head>
83
+ <meta charset="utf-8"/>
84
+ <title>cam2ip</title>
85
+ <script>
86
+ var texture, vloc, tloc, vertexBuff, textureBuff;
87
+
88
+ ws = new WebSocket("ws://{BIND}/socket");
89
+ var image = new Image();
90
+
91
+ ws.onopen = function() {
92
+ var gl = document.getElementById('canvas').getContext('webgl');
93
+
94
+ var vertexShaderSrc =
95
+ "attribute vec2 aVertex;" +
96
+ "attribute vec2 aUV;" +
97
+ "varying vec2 vTex;" +
98
+ "void main(void) {" +
99
+ " gl_Position = vec4(aVertex, 0.0, 1.0);" +
100
+ " vTex = aUV;" +
101
+ "}";
102
+
103
+ var fragmentShaderSrc =
104
+ "precision mediump float;" +
105
+ "varying vec2 vTex;" +
106
+ "uniform sampler2D sampler0;" +
107
+ "void main(void){" +
108
+ " gl_FragColor = texture2D(sampler0, vTex);"+
109
+ "}";
110
+
111
+ var vertShaderObj = gl.createShader(gl.VERTEX_SHADER);
112
+ var fragShaderObj = gl.createShader(gl.FRAGMENT_SHADER);
113
+ gl.shaderSource(vertShaderObj, vertexShaderSrc);
114
+ gl.shaderSource(fragShaderObj, fragmentShaderSrc);
115
+ gl.compileShader(vertShaderObj);
116
+ gl.compileShader(fragShaderObj);
117
+
118
+ var program = gl.createProgram();
119
+ gl.attachShader(program, vertShaderObj);
120
+ gl.attachShader(program, fragShaderObj);
121
+
122
+ gl.linkProgram(program);
123
+ gl.useProgram(program);
124
+
125
+ gl.viewport(0, 0, {WIDTH}, {HEIGHT});
126
+
127
+ vertexBuff = gl.createBuffer();
128
+ gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuff);
129
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, 1, -1, -1, 1, -1, 1, 1]), gl.STATIC_DRAW);
130
+
131
+ textureBuff = gl.createBuffer();
132
+ gl.bindBuffer(gl.ARRAY_BUFFER, textureBuff);
133
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([0, 1, 0, 0, 1, 0, 1, 1]), gl.STATIC_DRAW);
134
+
135
+ vloc = gl.getAttribLocation(program, "aVertex");
136
+ tloc = gl.getAttribLocation(program, "aUV");
137
+
138
+ texture = gl.createTexture();
139
+
140
+ image.onload = function() {
141
+ gl.bindTexture(gl.TEXTURE_2D, texture);
142
+
143
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
144
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
145
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
146
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
147
+
148
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
149
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
150
+
151
+ gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuff);
152
+ gl.enableVertexAttribArray(vloc);
153
+ gl.vertexAttribPointer(vloc, 2, gl.FLOAT, false, 0, 0);
154
+
155
+ gl.bindBuffer(gl.ARRAY_BUFFER, textureBuff);
156
+ gl.enableVertexAttribArray(tloc);
157
+ gl.vertexAttribPointer(tloc, 2, gl.FLOAT, false, 0, 0);
158
+
159
+ gl.drawArrays(gl.TRIANGLE_FAN, 0, 4);
160
+ }
161
+ }
162
+
163
+ ws.onmessage = function(e) {
164
+ image.setAttribute("src", "data:image/jpeg;base64," + e.data);
165
+ }
166
+ </script>
167
+ </head>
168
+ <body style="background-color: #000000">
169
+ <table style="width:100%; height:100%">
170
+ <tr style="height:100%">
171
+ <td style="height:100%; text-align:center">
172
+ <canvas id="canvas" width="{WIDTH}" height="{HEIGHT}"></canvas>
173
+ </td>
174
+ </tr>
175
+ </table>
176
+ </body>
177
+ </html>`
0 commit comments