-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
176 lines (157 loc) · 4.99 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
<!DOCTYPE html>
<html>
<head>
<title>WebGL crumbs</title>
</head>
<body>
<h1>Lesson 2</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;
void main(void) {
gl_Position = vec4(vertex, 0.0, 1.0);
gl_PointSize = 10.0;
}
`;
// fragment shader source code
var fragment_shader_source = `
void main(void) {
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.1);
}
`;
// Create the vertices
var vertices = new Float32Array([
-0.5, 0.5,
0.0, 0.5,
-0.25, 0.25
]);
// Create the indices
var indices = [0, 1, 2];
}
}
// 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, new Uint16Array(indices), gl.STATIC_DRAW);
// Unbind the buffer
gl.bindBuffer(gl.ELEMENT_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_location = gl.getAttribLocation(shader_program, "vertex");
gl.vertexAttribPointer(attribute_location, 2, gl.FLOAT, false, 0, 0);
// Enable the attribute
gl.enableVertexAttribArray(attribute_location);
}
}
// 5: Render
{
// 5.1: Clear
{
// Set the view port
gl.viewport(0, 0, canvas.width, canvas.height);
// Clear the canvas
gl.clearColor(0.5, 0.5, 0.5, 1);
// Clear the color buffer bit
gl.clear(gl.COLOR_BUFFER_BIT);
}
// 5.2: Draw
{
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, index_buffer);
gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT, 0);
}
}
</script>
</body>
</html>