-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
196 lines (174 loc) · 5.94 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
<!DOCTYPE html>
<html>
<head>
<title>WebGL crumbs</title>
</head>
<body>
<h1>Lesson 6</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;
void main(void) {
gl_Position = vec4(vertex, 0.0, 1.0);
gl_PointSize = 10.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);
}
`;
// vertices
var vertices = [
-1.0, -1.0, 1.0, 0.0, 0.0,
1.0, -1.0, 0.0, 1.0, 0.0,
0.0, 1.0, 0.0, 0.0, 1.0
];
}
}
// 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, new Float32Array(vertices), gl.STATIC_DRAW);
// Unbind the buffer
gl.bindBuffer(gl.ARRAY_BUFFER, null);
}
}
// 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 attribute = gl.getAttribLocation(shader_program, 'vertex');
// define the offset in bytes between the beginning of consecutive vertex attributes
// 5 values (x, y, r, g, b) * 4 bytes per value (float) = 20 bytes
let stride = 5 * Float32Array.BYTES_PER_ELEMENT
// define the offset in bytes of the first component in the vertex attribute array
// (x, y) starts from 0
let offset = 0 * Float32Array.BYTES_PER_ELEMENT
gl.vertexAttribPointer(attribute, 2, gl.FLOAT, false, stride, offset);
// Enable the attribute
gl.enableVertexAttribArray(attribute);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
}
// 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 attribute = gl.getAttribLocation(shader_program, 'color');
// define the offset in bytes between the beginning of consecutive vertex attributes
// 5 values (x, y, r, g, b) * 4 bytes per value (float) = 20 bytes
let stride = 5 * Float32Array.BYTES_PER_ELEMENT
// define the offset in bytes of the first component in the vertex attribute array
// (r, g, b) starts after (x, y)
// 2 values (x, y) * 4 bytes per value (float) = 8
let offset = 2 * Float32Array.BYTES_PER_ELEMENT
gl.vertexAttribPointer(attribute, 3, gl.FLOAT, false, stride, offset);
// Enable the attribute
gl.enableVertexAttribArray(attribute);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
}
}
// 5: Draw
{
// 5.1: Clear
{
// Clear the canvas
gl.clearColor(0.5, 0.5, 0.5, 1);
// Clear the color buffer bit
gl.clear(gl.COLOR_BUFFER_BIT);
// Set the view port
gl.viewport(0, 0, canvas.width, canvas.height);
}
// 5.2: Draw
{
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
// Draw the triangle
gl.drawArrays(gl.TRIANGLES, 0, 3);
}
}
</script>
</body>
</html>