- Notifications
You must be signed in to change notification settings - Fork 683
/
Copy pathwebgl-2d-image-red2blue.html
194 lines (158 loc) · 5.86 KB
/
webgl-2d-image-red2blue.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
<!-- Licensed under a BSD license. See license.html for license -->
<!DOCTYPE html>
<html>
<head>
<metacharset="utf-8">
<metaname="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>WebGL - 2D Image Swap Red and Blue</title>
<linktype="text/css" href="resources/webgl-tutorials.css" rel="stylesheet" />
</head>
<body>
<canvasid="canvas"></canvas>
</body>
<!-- vertex shader -->
<scriptid="vertex-shader-2d" type="x-shader/x-vertex">
attributevec2a_position;
attributevec2a_texCoord;
uniformvec2u_resolution;
varyingvec2v_texCoord;
voidmain(){
// convert the rectangle from pixels to 0.0 to 1.0
vec2 zeroToOne =a_position/u_resolution;
// convert from 0->1 to 0->2
vec2zeroToTwo=zeroToOne*2.0;
// convert from 0->2 to -1->+1 (clipspace)
vec2clipSpace=zeroToTwo-1.0;
gl_Position=vec4(clipSpace*vec2(1,-1),0,1);
// pass the texCoord to the fragment shader
// The GPU will interpolate this value between points.
v_texCoord=a_texCoord;
}
</script>
<!-- fragment shader -->
<scriptid="fragment-shader-2d" type="x-shader/x-fragment">
precisionmediumpfloat;
// our texture
uniformsampler2Du_image;
// the texCoords passed in from the vertex shader.
varyingvec2v_texCoord;
voidmain(){
gl_FragColor=texture2D(u_image,v_texCoord).bgra;
}
</script>
<!--
for most samples webgl-utils only provides shader compiling/linking and
canvas resizing because why clutter the examples with code that's the same in every sample.
See https://webglfundamentals.org/webgl/lessons/webgl-boilerplate.html
and https://webglfundamentals.org/webgl/lessons/webgl-resizing-the-canvas.html
for webgl-utils, m3, m4, and webgl-lessons-ui.
-->
<scriptsrc="resources/webgl-utils.js"></script>
<script>
"use strict";
functionmain(){
varimage=newImage();
image.src="resources/leaves.jpg";
image.onload=function(){
render(image);
};
}
functionrender(image){
// Get A WebGL context
/** @type {HTMLCanvasElement} */
varcanvas=document.querySelector("#canvas");
vargl=canvas.getContext("webgl");
if(!gl){
return;
}
// setup GLSL program
varprogram=webglUtils.createProgramFromScripts(gl,["vertex-shader-2d","fragment-shader-2d"]);
// look up where the vertex data needs to go.
varpositionLocation=gl.getAttribLocation(program,"a_position");
vartexcoordLocation=gl.getAttribLocation(program,"a_texCoord");
// Create a buffer to put three 2d clip space points in
varpositionBuffer=gl.createBuffer();
// Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)
gl.bindBuffer(gl.ARRAY_BUFFER,positionBuffer);
// Set a rectangle the same size as the image.
setRectangle(gl,0,0,image.width,image.height);
// provide texture coordinates for the rectangle.
vartexcoordBuffer=gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER,texcoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER,newFloat32Array([
0.0,0.0,
1.0,0.0,
0.0,1.0,
0.0,1.0,
1.0,0.0,
1.0,1.0,
]),gl.STATIC_DRAW);
// Create a texture.
vartexture=gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D,texture);
// Set the parameters so we can render any size image.
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S,gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T,gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.NEAREST);
// Upload the image into the texture.
gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,gl.RGBA,gl.UNSIGNED_BYTE,image);
// lookup uniforms
varresolutionLocation=gl.getUniformLocation(program,"u_resolution");
webglUtils.resizeCanvasToDisplaySize(gl.canvas);
// Tell WebGL how to convert from clip space to pixels
gl.viewport(0,0,gl.canvas.width,gl.canvas.height);
// Clear the canvas
gl.clearColor(0,0,0,0);
gl.clear(gl.COLOR_BUFFER_BIT);
// Tell it to use our program (pair of shaders)
gl.useProgram(program);
// Turn on the position attribute
gl.enableVertexAttribArray(positionLocation);
// Bind the position buffer.
gl.bindBuffer(gl.ARRAY_BUFFER,positionBuffer);
// Tell the position attribute how to get data out of positionBuffer (ARRAY_BUFFER)
varsize=2;// 2 components per iteration
vartype=gl.FLOAT;// the data is 32bit floats
varnormalize=false;// don't normalize the data
varstride=0;// 0 = move forward size * sizeof(type) each iteration to get the next position
varoffset=0;// start at the beginning of the buffer
gl.vertexAttribPointer(
positionLocation,size,type,normalize,stride,offset);
// Turn on the texcoord attribute
gl.enableVertexAttribArray(texcoordLocation);
// bind the texcoord buffer.
gl.bindBuffer(gl.ARRAY_BUFFER,texcoordBuffer);
// Tell the texcoord attribute how to get data out of texcoordBuffer (ARRAY_BUFFER)
varsize=2;// 2 components per iteration
vartype=gl.FLOAT;// the data is 32bit floats
varnormalize=false;// don't normalize the data
varstride=0;// 0 = move forward size * sizeof(type) each iteration to get the next position
varoffset=0;// start at the beginning of the buffer
gl.vertexAttribPointer(
texcoordLocation,size,type,normalize,stride,offset);
// set the resolution
gl.uniform2f(resolutionLocation,gl.canvas.width,gl.canvas.height);
// Draw the rectangle.
varprimitiveType=gl.TRIANGLES;
varoffset=0;
varcount=6;
gl.drawArrays(primitiveType,offset,count);
}
functionsetRectangle(gl,x,y,width,height){
varx1=x;
varx2=x+width;
vary1=y;
vary2=y+height;
gl.bufferData(gl.ARRAY_BUFFER,newFloat32Array([
x1,y1,
x2,y1,
x1,y2,
x1,y2,
x2,y1,
x2,y2,
]),gl.STATIC_DRAW);
}
main();
</script>
</html>