/
tri2_script.js
executable file
·339 lines (262 loc) · 8.82 KB
/
tri2_script.js
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
/*
ORDER OF OPERATIONS:
1) Get rendering context for webGL
2) Initialize shaders
3) Set positions of vertices
4) Set the color for clearing canvas
5) Clear canvas
6) Draw
*/
var canvas = document.getElementById('webgl');
var ANGLE = parseFloat($('#transform').val());
var radian = Math.PI * ANGLE/180.0; // convert to rads
var cosB = Math.cos(radian);
var sinB = Math.sin(radian);
var vertices;
var n;
var move = false;
var xformMatrix;
var Tx = 0.0, Ty =0.0, Tz = 0.0;
var Sx = cosB;
var Sy = sinB;
var Sz = 1.0;
var xformMatrix = new Float32Array([
cosB, sinB, 0.0, 0.0,
sinB, cosB, 0.0, 0.0,
0.0,0.0, 1.0,0.0,
Tx, Ty, Tz, Sz
]);
// DEFINE SHADERS
var VSHADER_SOURCE =
'attribute vec4 a_Position;\n' +
'uniform mat4 u_xformMatrix;\n' +
'void main() {\n' +
' gl_Position = u_xformMatrix * a_Position;\n' +
'}\n';
// Fragment shader program
var FSHADER_SOURCE =
'void main() {\n' +
' gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' +
'}\n';
// event handlers
$('#scaleup').on('click', function(e){
e.preventDefault();
Sz-= 0.05;
if (Sz < 0.5){
Sz = 1.0;
}
radian = 0;
xformMatrix = new Float32Array([
cosB, sinB, 0.0, 0.0,
sinB,cosB, 0.0, 0.0,
0.0,0.0,Sz, 0.0,
Tx, Ty, Tz, Sz
]);
render($('#shape').val(), ANGLE);
});
$('#scaledown').on('click', function(e){
e.preventDefault();
Sz+= 0.05;
xformMatrix = new Float32Array([
cosB, sinB, 0.0, 0.0,
sinB,cosB, 0.0, 0.0,
0.0,0.0,Sz, 0.0,
Tx, Ty, Tz, Sz
]);
render($('#shape').val(), ANGLE);
});
$('#transform').on('change', function(e){
e.preventDefault();
ANGLE = parseFloat($('#transform').val());
radian = Math.PI * ANGLE/180.0; // convert to rads
cosB = Math.cos(radian);
sinB = Math.sin(radian);
var Sx = cosB;
var Sy = sinB;
xformMatrix = new Float32Array([
cosB, sinB, 0.0, 0.0,
sinB, cosB, 0.0, 0.0,
0.0,0.0, Sz,0.0,
Tx, Ty, Tz, Sz
]);
render($('#shape').val(), ANGLE);
});
$('#move').on('click', function(e){
e.preventDefault();
Tx +=0.05;
Ty +=0.05;
move = true;
xformMatrix = new Float32Array([
cosB, sinB, 0.0, 0.0,
sinB, cosB, 0.0, 0.0,
0.0,0.0, Sz,0.0,
Tx, Ty, Tz, Sz
]);
render($('#shape').val(), ANGLE);
});
// main rendering function
function render(shape, degree){
console.log("Angle: %s", ANGLE);
console.log("Radian: %s", radian );
console.log("cosB: %s", cosB );
console.log("sinB: %s", sinB );
console.log("sx: %s", Sx);
console.log("sy: %s", Sy);
console.log("sz: %s", Sz);
// radian = Math.PI * ANGLE/180.0; // convert to rads
// cosB = Math.cos(radian);
// sinB = Math.sin(radian);
// if (cosB<0.1 || cosB >2){
// cosB = 1.0;
// }
console.log("Angle: %s", ANGLE);
console.log("Radian: %s", radian );
console.log("cosB: %s", cosB );
console.log("sinB: %s", sinB );
console.log("sx: %s", Sx);
console.log("sy: %s", Sy);
console.log("sz: %s", Sz);
if (shape == 'tri'){
vertices = new Float32Array([
// 4 bytes, 32-bit floating point number
// empty typed array = var vertices = new Float32Array(4); // specifies 4 slots
-0.25, 0.0, -0.5, 0.0, -0.5, -0.5,
0.25, 0.0, 0.5, 0.5, 0.5, 0.0
]);
n = 6; // Num of vertices being drawn - draw 9 points
} else if (shape =='trifan'){
vertices = new Float32Array([
// 4 bytes, 32-bit floating point number
// empty typed array = var vertices = new Float32Array(4); // specifies 4 slots
0.0, -0.5, -0.5, 0.0, -0.25, 0.5,
0.0, -0.5, -0.25, 0.5, 0.25, 0.5,
0.0, -0.5, 0.25, 0.5, 0.5, 0.0
]);
n = 9; // Num of vertices being drawn - draw 9 points
} else if (shape == 'tristrip'){
console.log ('tristrip');
vertices = new Float32Array([
// 4 bytes, 32-bit floating point number
// empty typed array = var vertices = new Float32Array(4); // specifies 4 slots
-0.5,0.5,
-0.5,-0.5,
0.5,0.5,
0.5,-0.5
]);
//triangles (v0, v1, v2) and (v2, v1, v3)
n = 4;
} else {
vertices = new Float32Array([
// 4 bytes, 32-bit floating point number
// empty typed array = var vertices = new Float32Array(4); // specifies 4 slots
0.0, 0.5, -0.5, -0.5, 0.5, -0.5
]);
n = 3; // Num of vertices being drawn - draw 3 points
}
/* 1) Get rendering context for webGL */
var gl = getWebGLContext(canvas);
if (!gl) {
console.log('Failed to get rendering context for WebGL');
return;
}
/*2) Initialize shaders*/
if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
console.log('Failed to intialize shaders.');
return;
}
/*3) Set positions of vertices */
var n = initVertexBuffers(gl);
u_xformMatrix=gl.getUniformLocation(gl.program, 'u_xformMatrix');
gl.uniformMatrix4fv(u_xformMatrix, false, xformMatrix);
if (n <0){
console.log("Cannot set positions of vertices.");
return;
}
/* 4) Set the color for clearing canvas */
gl.clearColor(1.0, 1.0, 1.0, 1);
/* 5) Clear canvas */
gl.clear(gl.COLOR_BUFFER_BIT);
/* 6) Draw */
// (mode, first, count)
// draw shapes based on mode parameter (type of shape)
// gl.POINTS, gl.LINES, gl.LINE_STRIP, gl.LINE_LOOP, gl.TRIANGLES, gl.TRIANGLE_STRIP, gl.TRIANGLE_FAN
// first specifies number-th vertex is used to draw from
// count n is set by initVertexBuffers()
// draw triangle using n=3 vertices from buffer starting with first vertex coords
var mode;
switch (shape){
case 'tri':
mode = gl.TRIANGLES;
break;
case 'lines':
mode = gl.LINES;
break;
case 'strip':
mode = gl.LINE_STRIP;
break;
case 'loop':
mode = gl.LINE_LOOP;
break;
case 'trifan':
mode = gl.TRIANGLE_FAN;
break;
case 'tristrip' :
mode = gl.TRIANGLE_STRIP;
break;
}
gl.drawArrays(mode, 0, n);
// delete the buffer objected created by gl.createBuffer()
// gl.deleteBuffer();
// Initialize the vertices and create buffer
// stores multiple vertices in buffer object then completes preparations
// for passing it to a vertex shader
// use buffer object to pass multiple vertices to vertex shader
// 5 steps for passing multiple data values to a vertex shader through a buffer object:
// 1) Create a buffer object - gl.createBuffer()
// 2) Bind buffer object to target - gl.bindBuffer()
// 3) Write data into the buffer object - gl.bufferData()
// 4) Assign buffer object to an attribute variable - gl.vertexAttribPointer()
// 5) Enable the assignment- gl.enableVertexAttribArray()
function initVertexBuffers(gl){
// 1) Create a buff object - gl.createBuffer()
var vertexBuffer = gl.createBuffer(); // memory allocated in the system to hold vertices we want to draw
if (!vertexBuffer){
console.log('Cannot create buffer object');
return 1; //
}
// 2) Bind buffer object to target - gl.bindBuffer()
// (target, buffer)
// ARRAY_BUFFER - specifies that buffer object contains vertex data
// ELEMENT_ARRAY_BUFFER - specifies buffer object contains index values pointing to vertex data
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
// 3) Write data into the buffer object - gl.bufferData()
// (target, data, usage)
// allocates storage and writes the data to the buffer
// vertices gets written into gl.ARRAY_BUFFER (which is vertexBuffer) then changes internal state of webgl system
// usage gl.STATIC_DRAW - buffer obj data will be specified once and used many times to draw shapes
// usage gl.STREAM_DRAW - buffer obj data will be specified once and used a few times to draw shapes
// usage gl.DYNAMIC_DRAW - buffer obj data will be specified repeatedly and used many times to draw shapes
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
// 4) Assign buffer object to an attribute variable - gl.vertexAttribPointer()
// a_Position is attribute variable
var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
if (a_Position < 0){
console.log('Failed to get the storage location of a_Position');
return -1;
}
// a way to assign an array of vertices to attribute variable:
// assign buffer object to attribute a_Position
// (location, number of coordinates per vertex (x,y), type, normalized, stride, offset)
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
// 5) Enable the assignment - gl.enableVertexAttribArray()
// Makes it possible for us to access a buffer object in a vertex shader
// gl.disableVertexAttribArray() to disable
gl.enableVertexAttribArray(a_Position);
return n; // returns number of vertices being drawn
}
}
$('#shape').on('change', function(e){
e.preventDefault();
render($(this).val(), ANGLE);
})
render('loop', ANGLE);