-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
333 lines (281 loc) · 9.25 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>WebGL crumbs</title>
</head>
<body>
<h1>Lesson 5</h1>
<canvas id="canvas" width="800" height="600"></canvas>
<script>
// 1: Create the rendering context
{
// 1.1: Initialize the context
{
// Get a canvas
var canvas = document.getElementById('canvas');
// Create a 3D rendering context
var gl = canvas.getContext('webgl');
// Check errors
if (!gl) {
throw new Error('WebGL not supported!')
}
}
// 1.2: Initialize the data
{
// vertex shader source code
var vertex_shader_source = `
attribute vec2 vertex;
attribute vec3 color;
varying vec3 _color;
uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;
void main(void) {
gl_Position = projection_matrix * view_matrix * model_matrix * vec4(vertex, 0.0, 1.0);
_color = color;
}`;
// fragment shader source code
var fragment_shader_source = `
precision mediump float;
varying vec3 _color;
void main(void) {
gl_FragColor = vec4(_color, 1.0);
}
`;
// clear color
var clear_color = [0.5, 0.5, 0.5, 1];
// vertices
var vertices = new Float32Array([-0.5, 0.5, 0.0, 0.5, -0.25, 0.25]);
// indices
var indices = new Uint16Array([0, 1, 2]);
// colors
var colors = new Float32Array([1, 0, 0, 0, 1, 0, 0, 0, 1]);
// projection matrix
var projection_matrix = new Float32Array([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
]);
// view matrix
var view_matrix = new Float32Array([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
]);
// model matrix
var model_matrix = new Float32Array([
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
]);
}
}
// 2: Create the shaders and the shader program
{
// 2.1: Create the vertex shader
{
// Create a vertex shader object
var vertex_shader = gl.createShader(gl.VERTEX_SHADER);
// Attach vertex shader source code
gl.shaderSource(vertex_shader, vertex_shader_source);
// Compile the vertex shader
gl.compileShader(vertex_shader);
// Check errors
{
let compiled = gl.getShaderParameter(vertex_shader, gl.COMPILE_STATUS);
if (!compiled) {
let log = gl.getShaderInfoLog(vertex_shader)
throw new Error(log)
}
}
}
// 2.2: Create the fragment shader
{
// Create fragment shader object
var fragment_shader = gl.createShader(gl.FRAGMENT_SHADER);
// Attach fragment shader source code
gl.shaderSource(fragment_shader, fragment_shader_source);
// Compile the fragmentt shader
gl.compileShader(fragment_shader);
// Check errors
{
let compiled = gl.getShaderParameter(fragment_shader, gl.COMPILE_STATUS);
if (!compiled) {
let log = gl.getShaderInfoLog(fragment_shader)
throw new Error(log)
}
}
}
// 2.3: Create the shader program
{
// Create a shader program object
var shader_program = gl.createProgram();
// Attach the vertex shader
gl.attachShader(shader_program, vertex_shader);
// Attach the fragment shader
gl.attachShader(shader_program, fragment_shader);
// Link both programs
gl.linkProgram(shader_program);
// Use the combined shader program object
gl.useProgram(shader_program);
}
}
// 3: Create the buffer objects
{
// 3.1: Create the vertex buffer
{
// Create an empty buffer object to store the vertex buffer
var vertex_buffer = gl.createBuffer();
//Bind appropriate array buffer to it
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
// Pass the vertex data to the buffer
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
// Unbind the buffer
gl.bindBuffer(gl.ARRAY_BUFFER, null);
}
// 3.2: Create the index buffer
{
// Create an empty buffer object to store Index buffer
var index_buffer = gl.createBuffer();
// Bind appropriate array buffer to it
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, index_buffer);
// Pass the vertex data to the buffer
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
}
// 3.2: Create the color buffer
{
var color_buffer = gl.createBuffer ();
gl.bindBuffer(gl.ARRAY_BUFFER, color_buffer);
gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW);
}
}
// 4: Bind the buffer objects to the shaders
{
// 4.1: Bind the vertex buffer to the vertex attribute
{
// Bind the vertex buffer object (VBO)
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
// Point an attribute to the currently bound VBO
let vertex_attribute = gl.getAttribLocation(shader_program, 'vertex');
gl.vertexAttribPointer(vertex_attribute, 2, gl.FLOAT, false, 0, 0);
// Enable the attribute
gl.enableVertexAttribArray(vertex_attribute);
}
// 4.2: Bind the color buffer to the color attribute
{
// bind the color buffer
gl.bindBuffer(gl.ARRAY_BUFFER, color_buffer);
// get the attribute location
var color_attribute = gl.getAttribLocation(shader_program, 'color');
// point attribute to the volor buffer object
gl.vertexAttribPointer(color_attribute, 3, gl.FLOAT, false, 0, 0) ;
// enable the color attribute
gl.enableVertexAttribArray(color_attribute);
}
// 4.3: Bind the uniforms
{
projection_matrix = get_projection(45, canvas.width/canvas.height, 1, 100)
// bind the projection matrix uniform
let projection_matrix_uniform = gl.getUniformLocation(shader_program, 'projection_matrix');
gl.uniformMatrix4fv(projection_matrix_uniform, false, projection_matrix);
// bind the view matrix uniform
view_matrix[14] = view_matrix[14] - 3
view_matrix[13] = view_matrix[13] - 1
view_matrix[12] = view_matrix[12] - 1
let view_matrix_uniform = gl.getUniformLocation(shader_program, 'view_matrix');
gl.uniformMatrix4fv(view_matrix_uniform, false, view_matrix);
// bind the model matrix uniform
let model_matrix_uniform = gl.getUniformLocation(shader_program, 'model_matrix');
gl.uniformMatrix4fv(model_matrix_uniform, false, model_matrix);
}
}
var t0 = 0
var t1
var dt
// 5: Render
function render (time) {
dt = time - t0;
t0 = time
// 5.1: Clear
{
// Set the view port
gl.viewport(0, 0, canvas.width, canvas.height);
// Clear the canvas
gl.clearColor(clear_color[0], clear_color[1], clear_color[2], clear_color[3]);
// Clear the color buffer bit
gl.clear(gl.COLOR_BUFFER_BIT);
}
// 5.2: Update uniforms
{
//...
var angle = Math.PI / 2 * 0.001 * dt
rotate_x(model_matrix, angle)
let model_matrix_uniform = gl.getUniformLocation(shader_program, 'model_matrix');
gl.uniformMatrix4fv(model_matrix_uniform, false, model_matrix);
}
// 5.2: Draw
{
// Draw the triangle
gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT, 0);
}
}
// 6: Animate
function animate (time) {
render(time);
requestAnimationFrame(animate);
}
animate(0)
// A: math utilities
function get_projection (fov, aspect, near, far) {
var angle = Math.tan((fov * 0.5) * Math.PI/180);
var matrix = new Float32Array([
0.5/angle, 0 , 0, 0,
0, 0.5 * aspect/angle, 0, 0,
0, 0, -(far + near) / (far - near), -1,
0, 0, (-2 * far * near) / (far - near), 0
]);
return matrix
}
function rotate_z (m, angle) {
var c = Math.cos(angle);
var s = Math.sin(angle);
var mv0 = m[0], mv4 = m[4], mv8 = m[8];
m[0] = c*m[0]-s*m[1];
m[4] = c*m[4]-s*m[5];
m[8] = c*m[8]-s*m[9];
m[1] = c*m[1]+s*mv0;
m[5] = c*m[5]+s*mv4;
m[9] = c*m[9]+s*mv8;
}
function rotate_x(m, angle) {
var c = Math.cos(angle);
var s = Math.sin(angle);
var mv1 = m[1], mv5 = m[5], mv9 = m[9];
m[1] = m[1]*c-m[2]*s;
m[5] = m[5]*c-m[6]*s;
m[9] = m[9]*c-m[10]*s;
m[2] = m[2]*c+mv1*s;
m[6] = m[6]*c+mv5*s;
m[10] = m[10]*c+mv9*s;
}
function rotate_y(m, angle) {
var c = Math.cos(angle);
var s = Math.sin(angle);
var mv0 = m[0];
var mv4 = m[4];
var mv8 = m[8];
m[0] = c * m[0] + s * m[2];
m[4] = c * m[4] + s * m[6];
m[8] = c * m[8] + s * m[10];
m[2] = c * m[2] - s * mv0;
m[6] = c * m[6] - s * mv4;
m[10] = c * m[10] - s * mv8;
}
// CREDITS
// http://www.tutorialspoint.com/webgl
</script>
</body>
</html>