- Notifications
You must be signed in to change notification settings - Fork 683
/
Copy pathwebgl-2d-geometry-matrix-transform-simpler-functions.html
292 lines (250 loc) · 7.63 KB
/
webgl-2d-geometry-matrix-transform-simpler-functions.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
<!-- 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 Geometry Matrix Transform with simpler functions</title>
<linktype="text/css" href="resources/webgl-tutorials.css" rel="stylesheet" />
</head>
<body>
<divclass="description">
Drag sliders to translate, rotate, and scale.
</div>
<canvasid="canvas"></canvas>
<divid="uiContainer">
<divid="ui">
<divid="x"></div>
<divid="y"></div>
<divid="angle"></div>
<divid="scaleX"></div>
<divid="scaleY"></div>
</div>
</div>
</body>
<!-- vertex shader -->
<scriptid="vertex-shader-2d" type="x-shader/x-vertex">
attributevec2a_position;
uniformmat3u_matrix;
voidmain(){
// Multiply the position by the matrix.
gl_Position=vec4((u_matrix*vec3(a_position,1)).xy,0,1);
}
</script>
<!-- fragment shader -->
<scriptid="fragment-shader-2d" type="x-shader/x-fragment">
precisionmediumpfloat;
uniformvec4u_color;
voidmain(){
gl_FragColor=u_color;
}
</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/webgl-lessons-ui.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,["vertex-shader-2d","fragment-shader-2d"]);
// look up where the vertex data needs to go.
varpositionLocation=gl.getAttribLocation(program,"a_position");
// lookup uniforms
varcolorLocation=gl.getUniformLocation(program,"u_color");
varmatrixLocation=gl.getUniformLocation(program,"u_matrix");
// Create a buffer to put positions in
varpositionBuffer=gl.createBuffer();
// Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)
gl.bindBuffer(gl.ARRAY_BUFFER,positionBuffer);
// Put geometry data into buffer
setGeometry(gl);
vartranslation=[100,150];
varangleInRadians=0;
varscale=[1,1];
varcolor=[Math.random(),Math.random(),Math.random(),1];
drawScene();
// Setup a ui.
webglLessonsUI.setupSlider("#x",{value: translation[0],slide: updatePosition(0),max: gl.canvas.width});
webglLessonsUI.setupSlider("#y",{value: translation[1],slide: updatePosition(1),max: gl.canvas.height});
webglLessonsUI.setupSlider("#angle",{slide: updateAngle,max: 360});
webglLessonsUI.setupSlider("#scaleX",{value: scale[0],slide: updateScale(0),min: -5,max: 5,step: 0.01,precision: 2});
webglLessonsUI.setupSlider("#scaleY",{value: scale[1],slide: updateScale(1),min: -5,max: 5,step: 0.01,precision: 2});
functionupdatePosition(index){
returnfunction(event,ui){
translation[index]=ui.value;
drawScene();
};
}
functionupdateAngle(event,ui){
varangleInDegrees=360-ui.value;
angleInRadians=angleInDegrees*Math.PI/180;
drawScene();
}
functionupdateScale(index){
returnfunction(event,ui){
scale[index]=ui.value;
drawScene();
};
}
// Draw the scene.
functiondrawScene(){
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.clear(gl.COLOR_BUFFER_BIT);
// Tell it to use our program (pair of shaders)
gl.useProgram(program);
// Turn on the attribute
gl.enableVertexAttribArray(positionLocation);
// Bind the position buffer.
gl.bindBuffer(gl.ARRAY_BUFFER,positionBuffer);
// Tell the 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);
// set the color
gl.uniform4fv(colorLocation,color);
// Compute the matrices
varmatrix=m3.projection(gl.canvas.clientWidth,gl.canvas.clientHeight);
matrix=m3.translate(matrix,translation[0],translation[1]);
matrix=m3.rotate(matrix,angleInRadians);
matrix=m3.scale(matrix,scale[0],scale[1]);
// Set the matrix.
gl.uniformMatrix3fv(matrixLocation,false,matrix);
// Draw the geometry.
varprimitiveType=gl.TRIANGLES;
varoffset=0;
varcount=18;// 6 triangles in the 'F', 3 points per triangle
gl.drawArrays(primitiveType,offset,count);
}
}
varm3={
projection: function(width,height){
// Note: This matrix flips the Y axis so that 0 is at the top.
return[
2/width,0,0,
0,-2/height,0,
-1,1,1
];
},
identity: function(){
return[
1,0,0,
0,1,0,
0,0,1,
];
},
translation: function(tx,ty){
return[
1,0,0,
0,1,0,
tx,ty,1,
];
},
rotation: function(angleInRadians){
varc=Math.cos(angleInRadians);
vars=Math.sin(angleInRadians);
return[
c,-s,0,
s,c,0,
0,0,1,
];
},
scaling: function(sx,sy){
return[
sx,0,0,
0,sy,0,
0,0,1,
];
},
multiply: function(a,b){
vara00=a[0*3+0];
vara01=a[0*3+1];
vara02=a[0*3+2];
vara10=a[1*3+0];
vara11=a[1*3+1];
vara12=a[1*3+2];
vara20=a[2*3+0];
vara21=a[2*3+1];
vara22=a[2*3+2];
varb00=b[0*3+0];
varb01=b[0*3+1];
varb02=b[0*3+2];
varb10=b[1*3+0];
varb11=b[1*3+1];
varb12=b[1*3+2];
varb20=b[2*3+0];
varb21=b[2*3+1];
varb22=b[2*3+2];
return[
b00*a00+b01*a10+b02*a20,
b00*a01+b01*a11+b02*a21,
b00*a02+b01*a12+b02*a22,
b10*a00+b11*a10+b12*a20,
b10*a01+b11*a11+b12*a21,
b10*a02+b11*a12+b12*a22,
b20*a00+b21*a10+b22*a20,
b20*a01+b21*a11+b22*a21,
b20*a02+b21*a12+b22*a22,
];
},
translate: function(m,tx,ty){
returnm3.multiply(m,m3.translation(tx,ty));
},
rotate: function(m,angleInRadians){
returnm3.multiply(m,m3.rotation(angleInRadians));
},
scale: function(m,sx,sy){
returnm3.multiply(m,m3.scaling(sx,sy));
},
};
// Fill the buffer with the values that define a letter 'F'.
functionsetGeometry(gl){
gl.bufferData(
gl.ARRAY_BUFFER,
newFloat32Array([
// left column
0,0,
30,0,
0,150,
0,150,
30,0,
30,150,
// top rung
30,0,
100,0,
30,30,
30,30,
100,0,
100,30,
// middle rung
30,60,
67,60,
30,90,
30,90,
67,60,
67,90,
]),
gl.STATIC_DRAW);
}
main();
</script>
</html>