-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathwebgl-2d-matrixstack-04.html
448 lines (368 loc) · 13.1 KB
/
webgl-2d-matrixstack-04.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<!-- Licensed under a BSD license. See license.html for license -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>WebGL2 - 2D - DrawImage with MatrixStack images at 4 corners</title>
<link type="text/css" href="resources/webgl-tutorials.css" rel="stylesheet" />
</head>
<body>
<canvas id="canvas" width="400" height="300"></canvas>
</body>
<script src="resources/webgl-utils.js"></script>
<script src="resources/webgl-lessons-helper.js"></script>
<script src="resources/3d-math.js"></script>
<script>
"use strict";
var vertexShaderSource = `#version 300 es
in vec4 a_position;
in vec2 a_texcoord;
uniform mat4 u_matrix;
uniform mat4 u_textureMatrix;
out vec2 v_texcoord;
void main() {
gl_Position = u_matrix * a_position;
v_texcoord = (u_textureMatrix * vec4(a_texcoord, 0, 1)).xy;
}
`;
var fragmentShaderSource = `#version 300 es
precision mediump float;
in vec2 v_texcoord;
uniform sampler2D u_texture;
out vec4 outColor;
void main() {
outColor = texture(u_texture, v_texcoord);
}
`;
function MatrixStack() {
this.stack = [];
// since the stack is empty this will put an initial matrix in it
this.restore();
}
// Pops the top of the stack restoring the previously saved matrix
MatrixStack.prototype.restore = function() {
this.stack.pop();
// Never let the stack be totally empty
if (this.stack.length < 1) {
this.stack[0] = m4.identity();
}
};
// Pushes a copy of the current matrix on the stack
MatrixStack.prototype.save = function() {
this.stack.push(this.getCurrentMatrix());
};
// Gets a copy of the current matrix (top of the stack)
MatrixStack.prototype.getCurrentMatrix = function() {
return this.stack[this.stack.length - 1].slice(); // makes a copy
};
// Lets us set the current matrix
MatrixStack.prototype.setCurrentMatrix = function(m) {
return this.stack[this.stack.length - 1] = m;
};
// Translates the current matrix
MatrixStack.prototype.translate = function(x, y, z) {
if (z === undefined) {
z = 0;
}
var m = this.getCurrentMatrix();
this.setCurrentMatrix(m4.translate(m, x, y, z));
};
// Rotates the current matrix around Z
MatrixStack.prototype.rotateZ = function(angleInRadians) {
var m = this.getCurrentMatrix();
this.setCurrentMatrix(m4.zRotate(m, angleInRadians));
};
// Scales the current matrix
MatrixStack.prototype.scale = function(x, y, z) {
if (z === undefined) {
z = 1;
}
var m = this.getCurrentMatrix();
this.setCurrentMatrix(m4.scale(m, x, y, z));
};
function main() {
// Get A WebGL context
var canvas = document.getElementById("canvas");
webglLessonsHelper.setupLesson(canvas);
/** @type {WebGLRenderingContext} */
var gl = canvas.getContext("webgl2");
if (!gl) {
webglLessonsHelper.showNeedWebGL2(canvas);
return;
}
var matrixStack = new MatrixStack();
// Use our boilerplate utils to compile the shaders and link into a program
var program = webglUtils.createProgramFromSources(gl,
[vertexShaderSource, fragmentShaderSource]);
// look up where the vertex data needs to go.
var positionAttributeLocation = gl.getAttribLocation(program, "a_position");
var texcoordAttributeLocation = gl.getAttribLocation(program, "a_texcoord");
// lookup uniforms
var matrixLocation = gl.getUniformLocation(program, "u_matrix");
var textureLocation = gl.getUniformLocation(program, "u_texture");
var textureMatrixLocation = gl.getUniformLocation(program, "u_textureMatrix");
// Create a vertex array object (attribute state)
var vao = gl.createVertexArray();
// and make it the one we're currently working with
gl.bindVertexArray(vao);
// create the position buffer, make it the current ARRAY_BUFFER
// and copy in the color values
var positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
// Put a unit quad in the buffer
var positions = [
0, 0,
0, 1,
1, 0,
1, 0,
0, 1,
1, 1,
]
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
// Turn on the attribute
gl.enableVertexAttribArray(positionAttributeLocation);
// Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER)
var size = 2; // 2 components per iteration
var type = gl.FLOAT; // the data is 32bit floats
var normalize = false; // don't normalize the data
var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next position
var offset = 0; // start at the beginning of the buffer
gl.vertexAttribPointer(
positionAttributeLocation, size, type, normalize, stride, offset);
// create the texcoord buffer, make it the current ARRAY_BUFFER
// and copy in the texcoord values
var texcoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, texcoordBuffer);
// Put texcoords in the buffer
var texcoords = [
0, 0,
0, 1,
1, 0,
1, 0,
0, 1,
1, 1,
]
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(texcoords), gl.STATIC_DRAW);
// Turn on the attribute
gl.enableVertexAttribArray(texcoordAttributeLocation);
// Tell the attribute how to get data out of colorBuffer (ARRAY_BUFFER)
var size = 2; // 3 components per iteration
var type = gl.FLOAT; // the data is 32bit floats
var normalize = true; // convert from 0-255 to 0.0-1.0
var stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next color
var offset = 0; // start at the beginning of the buffer
gl.vertexAttribPointer(
texcoordAttributeLocation, size, type, normalize, stride, offset);
// 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
function loadImageAndCreateTextureInfo(url) {
var tex = 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,
new Uint8Array([0, 0, 255, 255]));
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);
var textureInfo = {
width: 1, // we don't know the size until it loads
height: 1,
texture: tex,
};
var img = new Image();
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);
gl.generateMipmap(gl.TEXTURE_2D);
});
img.src = url;
return textureInfo;
}
var textureInfo = loadImageAndCreateTextureInfo('resources/star.jpg');
var scale = 0.5;
function drawLevel(time, depth) {
--depth;
if (depth > 0) {
time *= 1.4;
matrixStack.save();
{
// We're at the center of the center image so go to the top/left corner
matrixStack.translate(textureInfo.width / -2, textureInfo.height / -2);
matrixStack.rotateZ(Math.sin(time * 2.2));
matrixStack.scale(scale, scale);
matrixStack.save();
{
// Now we want the bottom/right corner of the image we're about to draw
matrixStack.translate(-textureInfo.width, -textureInfo.height);
drawImage(
textureInfo.texture,
textureInfo.width,
textureInfo.height,
0, 0);
}
matrixStack.restore();
matrixStack.translate(-textureInfo.width / 2, -textureInfo.height / 2);
drawLevel(time, depth);
}
matrixStack.restore();
matrixStack.save();
{
// We're at the center of the center image so go to the top/right corner
matrixStack.translate(textureInfo.width / 2, textureInfo.height / -2);
matrixStack.rotateZ(Math.sin(time * 2.3));
matrixStack.scale(scale, scale);
matrixStack.save();
{
// Now we want the bottom/right corner of the image we're about to draw
matrixStack.translate(0, -textureInfo.height);
drawImage(
textureInfo.texture,
textureInfo.width,
textureInfo.height,
0, 0);
}
matrixStack.restore();
matrixStack.translate(textureInfo.width / 2, -textureInfo.height / 2);
drawLevel(time, depth);
}
matrixStack.restore();
matrixStack.save();
{
// We're at the center of the center image so go to the bottom/left corner
matrixStack.translate(textureInfo.width / -2, textureInfo.height / 2);
matrixStack.rotateZ(Math.sin(time * 2.4));
matrixStack.scale(scale, scale);
matrixStack.save();
{
// Now we want the top/right corner of the image we're about to draw
matrixStack.translate(-textureInfo.width, 0);
drawImage(
textureInfo.texture,
textureInfo.width,
textureInfo.height,
0, 0);
}
matrixStack.restore();
matrixStack.translate(-textureInfo.width / 2, textureInfo.height / 2);
drawLevel(time, depth);
}
matrixStack.restore();
matrixStack.save();
{
// We're at the center of the center image so go to the bottom/right corner
matrixStack.translate(textureInfo.width / 2, textureInfo.height / 2);
matrixStack.rotateZ(Math.sin(time * 2.5));
matrixStack.scale(scale, scale);
matrixStack.save();
{
// Now we want the top/left corner of the image we're about to draw
matrixStack.translate(0, 0); // 0,0 means this line is not really doing anything
drawImage(
textureInfo.texture,
textureInfo.width,
textureInfo.height,
0, 0);
}
matrixStack.restore();
matrixStack.translate(textureInfo.width / 2, textureInfo.height / 2);
drawLevel(time, depth);
}
matrixStack.restore();
}
}
function draw(time) {
// 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 | gl.DEPTH_BUFFER_BIT);
matrixStack.save();
matrixStack.translate(gl.canvas.width / 2, gl.canvas.height / 2);
matrixStack.rotateZ(time);
matrixStack.scale(0.5, 0.5);
matrixStack.save();
{
matrixStack.translate(textureInfo.width / -2, textureInfo.height / -2);
drawImage(
textureInfo.texture,
textureInfo.width,
textureInfo.height,
0, 0);
}
matrixStack.restore();
drawLevel(time, 6);
matrixStack.restore();
}
var then = 0;
function render(time) {
time *= 0.001;
draw(time);
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
function drawImage(
tex, texWidth, texHeight,
srcX, srcY, srcWidth, srcHeight,
dstX, dstY, dstWidth, dstHeight) {
if (dstX === undefined) {
dstX = srcX;
}
if (dstY === undefined) {
dstY = srcY;
}
if (srcWidth === undefined) {
srcWidth = texWidth;
}
if (srcHeight === undefined) {
srcHeight = texHeight;
}
if (dstWidth === undefined) {
dstWidth = srcWidth;
}
if (dstHeight === undefined) {
dstHeight = srcHeight;
}
gl.useProgram(program);
// Setup the attributes for the quad
gl.bindVertexArray(vao);
var textureUnit = 0;
// The the shader we're putting the texture on texture unit 0
gl.uniform1i(textureLocation, textureUnit);
// Bind the texture to texture unit 0
gl.activeTexture(gl.TEXTURE0 + textureUnit);
gl.bindTexture(gl.TEXTURE_2D, tex);
// this matirx will convert from pixels to clip space
var matrix = m4.orthographic(
0, gl.canvas.clientWidth, gl.canvas.clientHeight, 0, -1, 1);
// The matrix stack is in pixels so it goes after the projection
// above which converted our space from clip space to pixel space
matrix = m4.multiply(matrix, matrixStack.getCurrentMatrix());
// translate our quad to dstX, dstY
matrix = m4.translate(matrix, dstX, dstY, 0);
// scale our 1 unit quad
// from 1 unit to dstWidth, dstHeight units
matrix = m4.scale(matrix, dstWidth, dstHeight, 1);
// Set the matrix.
gl.uniformMatrix4fv(matrixLocation, false, matrix);
// Because texture coordinates go from 0 to 1
// and because our texture coordinates are already a unit quad
// we can select an area of the texture by scaling the unit quad
// down
var texMatrix = m4.translation(srcX / texWidth, srcY / texHeight, 0);
texMatrix = m4.scale(texMatrix, srcWidth / texWidth, srcHeight / texHeight, 1);
// Set the texture matrix.
gl.uniformMatrix4fv(textureMatrixLocation, false, texMatrix);
// draw the quad (2 triangles, 6 vertices)
var offset = 0;
var count = 6;
gl.drawArrays(gl.TRIANGLES, offset, count);
}
}
main();
</script>
</html>