- Notifications
You must be signed in to change notification settings - Fork 683
/
Copy pathwebgl-2d-drawimage-05.html
312 lines (266 loc) · 9.41 KB
/
webgl-2d-drawimage-05.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
<!-- 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 - DrawImage with src rotation</title>
<linktype="text/css" href="resources/webgl-tutorials.css" rel="stylesheet" />
</head>
<body>
<canvasid="canvas"></canvas>
</body>
<!-- vertex shader -->
<scriptid="drawImage-vertex-shader" type="x-shader/x-vertex">
attributevec4a_position;
attributevec2a_texcoord;
uniformmat4u_matrix;
uniformmat4u_textureMatrix;
varyingvec2v_texcoord;
voidmain(){
gl_Position=u_matrix*a_position;
v_texcoord=(u_textureMatrix*vec4(a_texcoord,0,1)).xy;
}
</script>
<scriptid="drawImage-fragment-shader" type="x-shader/x-fragment">
precisionmediumpfloat;
varyingvec2v_texcoord;
uniformsampler2Du_texture;
voidmain(){
gl_FragColor=texture2D(u_texture,v_texcoord);
}
</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>
<scriptsrc="resources/m4.js"></script>
<script>
"use strict";
functionmain(){
// Get A WebGL context
/** @type {HTMLCanvasElement} */
varcanvas=document.querySelector("#canvas");
vargl=canvas.getContext("webgl");
if(!gl){
return;
}
// setup GLSL program
varprogram=webglUtils.createProgramFromScripts(gl,["drawImage-vertex-shader","drawImage-fragment-shader"]);
// look up where the vertex data needs to go.
varpositionLocation=gl.getAttribLocation(program,"a_position");
vartexcoordLocation=gl.getAttribLocation(program,"a_texcoord");
// lookup uniforms
varmatrixLocation=gl.getUniformLocation(program,"u_matrix");
vartextureMatrixLocation=gl.getUniformLocation(program,"u_textureMatrix");
vartextureLocation=gl.getUniformLocation(program,"u_texture");
// Create a buffer.
varpositionBuffer=gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER,positionBuffer);
// Put a unit quad in the buffer
varpositions=[
0,0,
0,1,
1,0,
1,0,
0,1,
1,1,
];
gl.bufferData(gl.ARRAY_BUFFER,newFloat32Array(positions),gl.STATIC_DRAW);
// Create a buffer for texture coords
vartexcoordBuffer=gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER,texcoordBuffer);
// Put texcoords in the buffer
vartexcoords=[
0,0,
0,1,
1,0,
1,0,
0,1,
1,1,
];
gl.bufferData(gl.ARRAY_BUFFER,newFloat32Array(texcoords),gl.STATIC_DRAW);
// creates a texture info { width: w, height: h, texture: tex }
// The texture will start with 1x1 pixels and be updated
// when the image has loaded
functionloadImageAndCreateTextureInfo(url){
vartex=gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D,tex);
// Fill the texture with a 1x1 blue pixel.
gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,1,1,0,gl.RGBA,gl.UNSIGNED_BYTE,
newUint8Array([0,0,255,255]));
// let's assume all images are not a power of 2
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.LINEAR);
vartextureInfo={
width: 1,// we don't know the size until it loads
height: 1,
texture: tex,
};
varimg=newImage();
img.addEventListener('load',function(){
textureInfo.width=img.width;
textureInfo.height=img.height;
gl.bindTexture(gl.TEXTURE_2D,textureInfo.texture);
gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,gl.RGBA,gl.UNSIGNED_BYTE,img);
});
img.src=url;
returntextureInfo;
}
vartextureInfos=[
loadImageAndCreateTextureInfo('resources/star.jpg'),
loadImageAndCreateTextureInfo('resources/leaves.jpg'),
loadImageAndCreateTextureInfo('resources/keyboard.jpg'),
];
vardrawInfos=[];
varnumToDraw=9;
varspeed=60;
for(varii=0;ii<numToDraw;++ii){
vardrawInfo={
x: Math.random()*gl.canvas.width,
y: Math.random()*gl.canvas.height,
dx: Math.random()>0.5 ? -1 : 1,
dy: Math.random()>0.5 ? -1 : 1,
xScale: Math.random()*0.25+0.25,
yScale: Math.random()*0.25+0.25,
offX: 0,
offY: 0,
rotation: Math.random()*Math.PI*2,
deltaRotation: (0.5+Math.random()*0.5)*(Math.random()>0.5 ? -1 : 1),
width: 1,
height: 1,
textureInfo: textureInfos[Math.random()*textureInfos.length|0],
};
drawInfos.push(drawInfo);
}
functionupdate(deltaTime){
drawInfos.forEach(function(drawInfo){
drawInfo.x+=drawInfo.dx*speed*deltaTime;
drawInfo.y+=drawInfo.dy*speed*deltaTime;
if(drawInfo.x<0){
drawInfo.dx=1;
}
if(drawInfo.x>=gl.canvas.width){
drawInfo.dx=-1;
}
if(drawInfo.y<0){
drawInfo.dy=1;
}
if(drawInfo.y>=gl.canvas.height){
drawInfo.dy=-1;
}
drawInfo.rotation+=drawInfo.deltaRotation*deltaTime;
});
}
functiondraw(){
webglUtils.resizeCanvasToDisplaySize(gl.canvas);
// Tell WebGL how to convert from clip space to pixels
gl.viewport(0,0,gl.canvas.width,gl.canvas.height);
gl.clear(gl.COLOR_BUFFER_BIT);
drawInfos.forEach(function(drawInfo){
vardstX=drawInfo.x;
vardstY=drawInfo.y;
vardstWidth=drawInfo.textureInfo.width*drawInfo.xScale;
vardstHeight=drawInfo.textureInfo.height*drawInfo.yScale;
varsrcX=drawInfo.textureInfo.width*drawInfo.offX;
varsrcY=drawInfo.textureInfo.height*drawInfo.offY;
varsrcWidth=drawInfo.textureInfo.width*drawInfo.width;
varsrcHeight=drawInfo.textureInfo.height*drawInfo.height;
drawImage(
drawInfo.textureInfo.texture,
drawInfo.textureInfo.width,
drawInfo.textureInfo.height,
srcX,srcY,srcWidth,srcHeight,
dstX,dstY,dstWidth,dstHeight,
drawInfo.rotation);
});
}
varthen=0;
functionrender(time){
varnow=time*0.001;
vardeltaTime=Math.min(0.1,now-then);
then=now;
update(deltaTime);
draw();
requestAnimationFrame(render);
}
requestAnimationFrame(render);
// Unlike images, textures do not have a width and height associated
// with them so we'll pass in the width and height of the texture
functiondrawImage(
tex,texWidth,texHeight,
srcX,srcY,srcWidth,srcHeight,
dstX,dstY,dstWidth,dstHeight,
srcRotation){
if(dstX===undefined){
dstX=srcX;
srcX=0;
}
if(dstY===undefined){
dstY=srcY;
srcY=0;
}
if(srcWidth===undefined){
srcWidth=texWidth;
}
if(srcHeight===undefined){
srcHeight=texHeight;
}
if(dstWidth===undefined){
dstWidth=srcWidth;
srcWidth=texWidth;
}
if(dstHeight===undefined){
dstHeight=srcHeight;
srcHeight=texHeight;
}
if(srcRotation===undefined){
srcRotation=0;
}
gl.bindTexture(gl.TEXTURE_2D,tex);
// Tell WebGL to use our shader program pair
gl.useProgram(program);
// Setup the attributes to pull data from our buffers
gl.bindBuffer(gl.ARRAY_BUFFER,positionBuffer);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation,2,gl.FLOAT,false,0,0);
gl.bindBuffer(gl.ARRAY_BUFFER,texcoordBuffer);
gl.enableVertexAttribArray(texcoordLocation);
gl.vertexAttribPointer(texcoordLocation,2,gl.FLOAT,false,0,0);
// this matrix will convert from pixels to clip space
varmatrix=m4.orthographic(0,gl.canvas.width,gl.canvas.height,0,-1,1);
// this matrix will translate our quad to dstX, dstY
matrix=m4.translate(matrix,dstX,dstY,0);
// this matrix will scale our 1 unit quad
// from 1 unit to texWidth, texHeight units
matrix=m4.scale(matrix,dstWidth,dstHeight,1);
// Set the matrix.
gl.uniformMatrix4fv(matrixLocation,false,matrix);
// just like a 2d projection matrix except in texture space (0 to 1)
// instead of clip space. This matrix puts us in pixel space.
vartexMatrix=m4.scaling(1/texWidth,1/texHeight,1);
// We need to pick a place to rotate around
// We'll move to the middle, rotate, then move back
vartexMatrix=m4.translate(texMatrix,texWidth*0.5,texHeight*0.5,0);
vartexMatrix=m4.zRotate(texMatrix,srcRotation);
vartexMatrix=m4.translate(texMatrix,texWidth*-0.5,texHeight*-0.5,0);
// because were in pixel space
// the scale and translation are now in pixels
vartexMatrix=m4.translate(texMatrix,srcX,srcY,0);
vartexMatrix=m4.scale(texMatrix,srcWidth,srcHeight,1);
// Set the texture matrix.
gl.uniformMatrix4fv(textureMatrixLocation,false,texMatrix);
// Tell the shader to get the texture from texture unit 0
gl.uniform1i(textureLocation,0);
// draw the quad (2 triangles, 6 vertices)
gl.drawArrays(gl.TRIANGLES,0,6);
}
}
main();
</script>
</html>